Skip to content

Commit

Permalink
added tests for parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mateo-ivc committed Apr 8, 2024
1 parent 12fb140 commit 5418330
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 21 deletions.
10 changes: 0 additions & 10 deletions server/src/services/scheduler/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ func (t DeleteBoards) GetParams() []any {
return []any{t.TimeToExpire, t.Interactions}
}

type Test struct {
Param1 string
Param2 int
Param3 bool
}

func (t Test) GetParams() []any {
return []any{t.Param1, t.Param2, t.Param3}
}

/*
deleteUnusedBoards
Expand Down
16 changes: 10 additions & 6 deletions server/src/services/scheduler/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@ package scheduler

import (
"context"
"errors"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v3"
"os"
"scrumlr.io/server/logger"
)

func parse(path string, ctx context.Context) Config {
log := logger.FromContext(ctx)
func parseFile(path string) (Config, error) {
file, err := os.ReadFile(path)
if err != nil {
log.Errorw("Failed to reading file", "error", err)
return Config{}, errors.New("failed reading file")
}
return parseToObject(file)
}

func parseToObject(data []byte) (Config, error) {
config := &Config{}
err = yaml.Unmarshal(file, config)
err := yaml.Unmarshal(data, config)
if err != nil {
log.Errorw("Failed to parse file")
return Config{}, errors.New("failed to parse file")
}
return *config
return *config, nil
}

func parseTaskParameters(ctx context.Context, parameters map[string]interface{}, task Task) []any {
Expand Down
116 changes: 116 additions & 0 deletions server/src/services/scheduler/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package scheduler

import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"testing"
)

type ParserTestSuite struct {
suite.Suite
}

func TestParserSuite(t *testing.T) {
suite.Run(t, new(ParserTestSuite))
}

type Test struct {
Val1 string
Val2 int
Val3 bool
}

func (t Test) GetParams() []any {
return []any{t.Val1, t.Val2, t.Val3}
}

func (suite *ParserTestSuite) TestExistingJob() {
configs := []struct {
name string
yaml []byte
want interface{}
wantParams []any
}{
{name: "Parse valid Job",
yaml: []byte(`jobs:
- name: "Delete Unused Boards"
schedule: "*/20 * * * * *"
withSeconds: true
task:
timeToExpiry: "720h"
interactions: 4`),
want: Config{
Jobs: []Job{
{
Name: "Delete Unused Boards",
Schedule: "*/20 * * * * *",
WithSeconds: true,
Task: map[string]interface{}{
"timeToExpiry": "720h",
"interactions": 4,
},
},
},
},
wantParams: []any{[]any{"720h", 4}},
},
{name: "Parse multiple valid Jobs",
yaml: []byte(` jobs:
- name: "Delete Unused Boards"
schedule: "*/20 * * * * *"
withSeconds: true
task:
timeToExpiry: "720h"
interactions: 4
- name: "Test"
schedule: "*/5 * * * *"
withSeconds: false
task:
val1: "val1"
val2: 2
val3: true
`),
want: Config{
Jobs: []Job{
{
Name: "Delete Unused Boards",
Schedule: "*/20 * * * * *",
WithSeconds: true,
Task: map[string]interface{}{
"timeToExpiry": "720h",
"interactions": 4,
},
},
{
Name: "Test",
Schedule: "*/5 * * * *",
WithSeconds: false,
Task: map[string]interface{}{
"val1": "val1",
"val2": 2,
"val3": true,
},
},
},
},
wantParams: []any{[]any{"720h", 4}, []any{"val1", 2, true}},
},
}

for _, tt := range configs {
got, _ := parseToObject(tt.yaml)
for i, job := range got.Jobs {
var gotParams []any
switch job.Name {
case "Delete Unused Boards":
gotParams = parseTaskParameters(context.Background(), job.Task, new(DeleteBoards))
case "Test":
gotParams = parseTaskParameters(context.Background(), job.Task, new(Test))
}
assert.Equal(suite.T(), tt.want, got)
assert.Equal(suite.T(), tt.wantParams[i], gotParams)

}
}
}
9 changes: 4 additions & 5 deletions server/src/services/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ When adding a new job to the scheduler, extend the switch case with the job's me
func (s *SchedulerService) StartScheduler(path string) {
ctx := context.Background()
log := logger.FromContext(ctx)
conf := parse(path, ctx)
conf, err := parseFile(path)
if err != nil {
log.Error("Failed to parse file", "error", err)
}

for _, job := range conf.Jobs {
var task interface{}
Expand All @@ -47,10 +50,6 @@ func (s *SchedulerService) StartScheduler(path string) {
params = append(params, ctx)
task = s.deleteUnusedBoards
params = append(params, parseTaskParameters(ctx, job.Task, new(DeleteBoards))...)
case "Test":
task = s.tmpTask
params = append(params, parseTaskParameters(ctx, job.Task, new(Test))...)

}
_, err := s.scheduler.NewJob(gocron.CronJob(job.Schedule, job.WithSeconds), gocron.NewTask(task, params...))
if err != nil {
Expand Down

0 comments on commit 5418330

Please sign in to comment.