-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecks_cfg_schema_test.go
More file actions
199 lines (180 loc) · 5.33 KB
/
checks_cfg_schema_test.go
File metadata and controls
199 lines (180 loc) · 5.33 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package dbqcore
import (
"testing"
"gopkg.in/yaml.v3"
)
func TestSchemaCheckConfigUnmarshal(t *testing.T) {
tests := []struct {
name string
yamlContent string
expectedCheck DataQualityCheck
expectError bool
}{
{
name: "schema_check with expect_columns_ordered",
yamlContent: `
schema_check:
expect_columns_ordered:
columns_order: [id, name, email, created_at]
desc: "Validate column order"
on_fail: error
`,
expectedCheck: DataQualityCheck{
Expression: "expect_columns_ordered",
Description: "Validate column order",
OnFail: OnFailActionError,
SchemaCheck: &SchemaCheckConfig{
ExpectColumnsOrdered: &ExpectColumnsOrderedConfig{
ColumnsOrder: []string{"id", "name", "email", "created_at"},
},
},
ParsedCheck: nil,
},
},
{
name: "schema_check with single column",
yamlContent: `
schema_check:
expect_columns_ordered:
columns_order: [id]
`,
expectedCheck: DataQualityCheck{
Expression: "expect_columns_ordered",
SchemaCheck: &SchemaCheckConfig{
ExpectColumnsOrdered: &ExpectColumnsOrderedConfig{
ColumnsOrder: []string{"id"},
},
},
ParsedCheck: nil,
},
},
{
name: "schema_check with empty columns should parse",
yamlContent: `
schema_check:
expect_columns_ordered:
columns_order: []
`,
expectedCheck: DataQualityCheck{
Expression: "expect_columns_ordered",
SchemaCheck: &SchemaCheckConfig{
ExpectColumnsOrdered: &ExpectColumnsOrderedConfig{
ColumnsOrder: []string{},
},
},
ParsedCheck: nil,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var check DataQualityCheck
err := yaml.Unmarshal([]byte(tt.yamlContent), &check)
if tt.expectError {
if err == nil {
t.Errorf("Expected error but got none")
}
return
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
// Validate Expression
if check.Expression != tt.expectedCheck.Expression {
t.Errorf("Expression mismatch: got %s, want %s",
check.Expression, tt.expectedCheck.Expression)
}
// Validate Description
if check.Description != tt.expectedCheck.Description {
t.Errorf("Description mismatch: got %s, want %s",
check.Description, tt.expectedCheck.Description)
}
// Validate OnFail
if check.OnFail != tt.expectedCheck.OnFail {
t.Errorf("OnFail mismatch: got %s, want %s",
check.OnFail, tt.expectedCheck.OnFail)
}
// Validate SchemaCheck
if tt.expectedCheck.SchemaCheck != nil {
if check.SchemaCheck == nil {
t.Errorf("SchemaCheck is nil, expected non-nil")
} else if check.SchemaCheck.ExpectColumnsOrdered != nil {
if len(check.SchemaCheck.ExpectColumnsOrdered.ColumnsOrder) !=
len(tt.expectedCheck.SchemaCheck.ExpectColumnsOrdered.ColumnsOrder) {
t.Errorf("ColumnsOrder length mismatch: got %d, want %d",
len(check.SchemaCheck.ExpectColumnsOrdered.ColumnsOrder),
len(tt.expectedCheck.SchemaCheck.ExpectColumnsOrdered.ColumnsOrder))
}
for i, col := range check.SchemaCheck.ExpectColumnsOrdered.ColumnsOrder {
if col != tt.expectedCheck.SchemaCheck.ExpectColumnsOrdered.ColumnsOrder[i] {
t.Errorf("Column[%d] mismatch: got %s, want %s",
i, col, tt.expectedCheck.SchemaCheck.ExpectColumnsOrdered.ColumnsOrder[i])
}
}
}
}
// Validate ParsedCheck
if tt.expectedCheck.ParsedCheck != nil {
t.Errorf("ParsedCheck is non-nil, expected nil for schema checks")
}
})
}
}
func TestSchemaCheckIntegrationWithConfig(t *testing.T) {
yamlContent := `
version: 1
rules:
- dataset: pg@[public.users]
checks:
- schema_check:
expect_columns_ordered:
columns_order: [id, name, email]
desc: "Ensure columns are ordered correctly"
on_fail: warn
- row_count > 100
- not_null(id)
`
var config ChecksFileConfig
err := yaml.Unmarshal([]byte(yamlContent), &config)
if err != nil {
t.Fatalf("Failed to unmarshal config: %v", err)
}
if len(config.Rules) != 1 {
t.Errorf("Expected 1 rule, got %d", len(config.Rules))
}
rule := config.Rules[0]
if len(rule.Checks) != 3 {
t.Errorf("Expected 3 checks, got %d", len(rule.Checks))
}
// Check the schema_check
schemaCheck := rule.Checks[0]
if schemaCheck.SchemaCheck == nil {
t.Errorf("First check should be a schema_check")
} else if schemaCheck.SchemaCheck.ExpectColumnsOrdered == nil {
t.Errorf("Schema check should have ExpectColumnsOrdered")
} else {
expectedColumns := []string{"id", "name", "email"}
if len(schemaCheck.SchemaCheck.ExpectColumnsOrdered.ColumnsOrder) != len(expectedColumns) {
t.Errorf("Expected %d columns, got %d",
len(expectedColumns),
len(schemaCheck.SchemaCheck.ExpectColumnsOrdered.ColumnsOrder))
}
for i, col := range schemaCheck.SchemaCheck.ExpectColumnsOrdered.ColumnsOrder {
if col != expectedColumns[i] {
t.Errorf("Column[%d] mismatch: got %s, want %s",
i, col, expectedColumns[i])
}
}
}
if schemaCheck.Description != "Ensure columns are ordered correctly" {
t.Errorf("Description mismatch: got %s", schemaCheck.Description)
}
if schemaCheck.OnFail != OnFailActionWarn {
t.Errorf("OnFail mismatch: got %s, want warn", schemaCheck.OnFail)
}
// verify ParsedCheck is nil
if schemaCheck.ParsedCheck != nil {
t.Errorf("ParsedCheck should not be present")
}
}