-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_test.go
More file actions
151 lines (125 loc) · 4.97 KB
/
Copy pathschema_test.go
File metadata and controls
151 lines (125 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2025 - 2026 Zigflow authors <https://github.com/zigflow/schema/graphs/contributors>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package schema
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const testCronExpr = "0 0 * * *"
// minimalWorkflow returns a minimal structurally valid workflow document.
// It is used as a baseline for validation tests so that each test only
// introduces one deviation from a known-good state.
func minimalWorkflow() map[string]any {
return map[string]any{
propDocument: map[string]any{
propDSL: "1.0.0",
propTaskQueue: "default",
propWorkflowType: "test",
propVersion: "1.0.0",
},
propDo: []any{
map[string]any{
"step1": map[string]any{
propSet: map[string]any{"x": "y"},
},
},
},
}
}
// TestSchema_DocumentFields verifies that the document schema accepts the
// Zigflow-aligned field names and rejects the old Serverless Workflow names.
func TestSchema_DocumentFields(t *testing.T) {
s, err := BuildSchema("1.0.0", "json")
require.NoError(t, err)
resolved, err := s.Resolve(nil)
require.NoError(t, err)
t.Run("workflowType and taskQueue are accepted", func(t *testing.T) {
err := resolved.Validate(minimalWorkflow())
assert.NoError(t, err)
})
t.Run("old name field is rejected", func(t *testing.T) {
doc := minimalWorkflow()
document := doc["document"].(map[string]any)
delete(document, propWorkflowType)
document["name"] = "test"
err := resolved.Validate(doc)
assert.Error(t, err, "old 'name' field must be rejected; use 'workflowType' instead")
})
t.Run("old namespace field is rejected", func(t *testing.T) {
doc := minimalWorkflow()
document := doc["document"].(map[string]any)
delete(document, propTaskQueue)
document["namespace"] = "default"
err := resolved.Validate(doc)
assert.Error(t, err, "old 'namespace' field must be rejected; use 'taskQueue' instead")
})
t.Run("missing workflowType is rejected", func(t *testing.T) {
doc := minimalWorkflow()
delete(doc["document"].(map[string]any), propWorkflowType)
err := resolved.Validate(doc)
assert.Error(t, err, "document without workflowType must fail validation")
})
t.Run("missing taskQueue is rejected", func(t *testing.T) {
doc := minimalWorkflow()
delete(doc["document"].(map[string]any), propTaskQueue)
err := resolved.Validate(doc)
assert.Error(t, err, "document without taskQueue must fail validation")
})
}
// TestSchema_ScheduleRequiresScheduleWorkflowName verifies the conditional
// if/then rule: when top-level schedule is present, document.metadata must
// exist and contain scheduleWorkflowName.
func TestSchema_ScheduleRequiresScheduleWorkflowName(t *testing.T) {
resolved := resolvedTestSchema(t)
t.Run("schedule present without document.metadata is rejected", func(t *testing.T) {
doc := minimalWorkflow()
doc["schedule"] = map[string]any{propCron: testCronExpr}
err := resolved.Validate(doc)
assert.Error(t, err, "schedule without document.metadata must fail validation")
})
t.Run("schedule present with document.metadata but missing scheduleWorkflowName is rejected", func(t *testing.T) {
doc := minimalWorkflow()
doc["schedule"] = map[string]any{propCron: testCronExpr}
doc["document"].(map[string]any)["metadata"] = map[string]any{
propScheduleID: "my-schedule",
}
err := resolved.Validate(doc)
assert.Error(t, err, "schedule with document.metadata but no scheduleWorkflowName must fail validation")
})
t.Run("schedule present with valid document.metadata.scheduleWorkflowName is accepted", func(t *testing.T) {
doc := minimalWorkflow()
doc["schedule"] = map[string]any{propCron: testCronExpr}
doc["document"].(map[string]any)["metadata"] = map[string]any{
propScheduleWorkflowName: "my-workflow",
}
err := resolved.Validate(doc)
assert.NoError(t, err, "schedule with valid scheduleWorkflowName must pass validation")
})
}
// TestSchema_RejectsUnknownTopLevelProperties verifies that the root schema
// enforces UnevaluatedProperties: false by rejecting any top-level key that
// is not explicitly defined in the schema.
func TestSchema_RejectsUnknownTopLevelProperties(t *testing.T) {
s, err := BuildSchema("1.0.0", "json")
require.NoError(t, err)
resolved, err := s.Resolve(nil)
require.NoError(t, err)
t.Run("valid document passes", func(t *testing.T) {
err := resolved.Validate(minimalWorkflow())
assert.NoError(t, err, "document with only known top-level properties should pass validation")
})
}