Skip to content

Commit 45f45eb

Browse files
authored
chore: Test scheduler strategy JSON schema (#1276)
1 parent 9f31cfb commit 45f45eb

File tree

3 files changed

+57
-23
lines changed

3 files changed

+57
-23
lines changed

scheduler/strategy.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ func (s *Strategy) Validate() error {
5757
return fmt.Errorf("unknown scheduler strategy: %d", s)
5858
}
5959

60-
// JSONSchema uses value receiver because of https://github.com/invopop/jsonschema/issues/102
6160
func (Strategy) JSONSchema() *jsonschema.Schema {
6261
enum := make([]any, len(AllStrategyNames))
6362
for i, s := range AllStrategyNames {

scheduler/strategy.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

scheduler/strategy_test.go

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,69 @@ import (
66
"reflect"
77
"testing"
88

9+
"github.com/cloudquery/plugin-sdk/v4/plugin"
910
"github.com/cloudquery/plugin-sdk/v4/scheduler"
1011
"github.com/invopop/jsonschema"
1112
"github.com/stretchr/testify/require"
1213
)
1314

14-
//go:embed strategy.json
15-
var jsonSchema string
16-
1715
func TestStrategy_JSONSchema(t *testing.T) {
1816
sc := (&jsonschema.Reflector{RequiredFromJSONSchemaTags: true}).ReflectFromType(reflect.TypeOf(scheduler.StrategyDFS))
19-
data, err := json.MarshalIndent(sc, "", " ")
17+
schema, err := json.MarshalIndent(sc, "", " ")
18+
require.NoError(t, err)
19+
20+
validator, err := plugin.JSONSchemaValidator(string(schema))
2021
require.NoError(t, err)
21-
require.JSONEq(t, string(data)+"\n", jsonSchema)
22+
23+
type testCase struct {
24+
Name string
25+
Spec string
26+
Err bool
27+
}
28+
29+
for _, tc := range []testCase{
30+
{
31+
Name: "dfs scheduler",
32+
Spec: `"dfs"`,
33+
},
34+
{
35+
Name: "round-robin scheduler",
36+
Spec: `"round-robin"`,
37+
},
38+
{
39+
Name: "shuffle scheduler",
40+
Spec: `"shuffle"`,
41+
},
42+
{
43+
Name: "empty scheduler",
44+
Err: true,
45+
Spec: `""`,
46+
},
47+
{
48+
Name: "bad scheduler",
49+
Err: true,
50+
Spec: `"bad"`,
51+
},
52+
{
53+
Name: "bad scheduler type",
54+
Err: true,
55+
Spec: `123`,
56+
},
57+
{
58+
Name: "null scheduler type",
59+
Err: true,
60+
Spec: `null`,
61+
},
62+
} {
63+
t.Run(tc.Name, func(t *testing.T) {
64+
var val any
65+
err := json.Unmarshal([]byte(tc.Spec), &val)
66+
require.NoError(t, err)
67+
if tc.Err {
68+
require.Error(t, validator.Validate(val))
69+
} else {
70+
require.NoError(t, validator.Validate(val))
71+
}
72+
})
73+
}
2274
}

0 commit comments

Comments
 (0)