-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecks_cfg_columns_not_present_test.go
More file actions
326 lines (302 loc) · 7.98 KB
/
checks_cfg_columns_not_present_test.go
File metadata and controls
326 lines (302 loc) · 7.98 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package dbqcore
import (
"testing"
"gopkg.in/yaml.v3"
)
func TestColumnsNotPresentConfig(t *testing.T) {
tests := []struct {
name string
yamlContent string
expectedConfig ColumnsNotPresentConfig
expectError bool
}{
{
name: "columns_not_present with column list only",
yamlContent: `
version: 1
rules:
- dataset: test_table
checks:
- schema_check:
columns_not_present:
columns: [credit_card_number, ssn, password]
desc: "Ensure no sensitive columns exist"
on_fail: error
`,
expectedConfig: ColumnsNotPresentConfig{
Columns: []string{"credit_card_number", "ssn", "password"},
Pattern: "",
},
expectError: false,
},
{
name: "columns_not_present with pattern only",
yamlContent: `
version: 1
rules:
- dataset: test_table
checks:
- schema_check:
columns_not_present:
pattern: "pii_*"
desc: "Ensure no PII columns exist"
on_fail: warn
`,
expectedConfig: ColumnsNotPresentConfig{
Columns: nil,
Pattern: "pii_*",
},
expectError: false,
},
{
name: "columns_not_present with both columns and pattern",
yamlContent: `
version: 1
rules:
- dataset: test_table
checks:
- schema_check:
columns_not_present:
columns: [credit_card, cvv]
pattern: "sensitive_*"
desc: "Ensure no sensitive data columns"
on_fail: error
`,
expectedConfig: ColumnsNotPresentConfig{
Columns: []string{"credit_card", "cvv"},
Pattern: "sensitive_*",
},
expectError: false,
},
{
name: "columns_not_present with single column",
yamlContent: `
version: 1
rules:
- dataset: test_table
checks:
- schema_check:
columns_not_present:
columns: [password]
desc: "Ensure password column doesn't exist"
`,
expectedConfig: ColumnsNotPresentConfig{
Columns: []string{"password"},
Pattern: "",
},
expectError: false,
},
{
name: "columns_not_present with wildcard pattern",
yamlContent: `
version: 1
rules:
- dataset: test_table
checks:
- schema_check:
columns_not_present:
pattern: "*_temp"
desc: "Ensure no temporary columns"
`,
expectedConfig: ColumnsNotPresentConfig{
Columns: nil,
Pattern: "*_temp",
},
expectError: false,
},
{
name: "columns_not_present with complex pattern",
yamlContent: `
version: 1
rules:
- dataset: test_table
checks:
- schema_check:
columns_not_present:
pattern: "test_*_backup"
desc: "Ensure no backup columns"
`,
expectedConfig: ColumnsNotPresentConfig{
Columns: nil,
Pattern: "test_*_backup",
},
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var config ChecksFileConfig
err := yaml.Unmarshal([]byte(tt.yamlContent), &config)
if tt.expectError {
if err == nil {
t.Errorf("Expected error but got none")
}
return
}
if err != nil {
t.Fatalf("Failed to unmarshal config: %v", err)
}
if len(config.Rules) != 1 {
t.Fatalf("Expected 1 rule, got %d", len(config.Rules))
}
if len(config.Rules[0].Checks) != 1 {
t.Fatalf("Expected 1 check, got %d", len(config.Rules[0].Checks))
}
check := config.Rules[0].Checks[0]
if check.SchemaCheck == nil {
t.Fatal("Expected SchemaCheck to be set")
}
if check.SchemaCheck.ColumnsNotPresent == nil {
t.Fatal("Expected ColumnsNotPresent to be set")
}
// Verify Expression is set correctly
if check.Expression != "columns_not_present" {
t.Errorf("Expected Expression to be 'columns_not_present', got %s", check.Expression)
}
// Verify ParsedCheck is nil for schema checks
if check.ParsedCheck != nil {
t.Error("Expected ParsedCheck to be nil for schema checks")
}
// Compare columns
actualConfig := check.SchemaCheck.ColumnsNotPresent
if len(actualConfig.Columns) != len(tt.expectedConfig.Columns) {
t.Errorf("Expected %d columns, got %d", len(tt.expectedConfig.Columns), len(actualConfig.Columns))
} else {
for i, col := range actualConfig.Columns {
if col != tt.expectedConfig.Columns[i] {
t.Errorf("Column[%d] mismatch: expected %s, got %s", i, tt.expectedConfig.Columns[i], col)
}
}
}
// Compare pattern
if actualConfig.Pattern != tt.expectedConfig.Pattern {
t.Errorf("Pattern mismatch: expected %s, got %s", tt.expectedConfig.Pattern, actualConfig.Pattern)
}
})
}
}
func TestColumnsNotPresentWithOtherChecks(t *testing.T) {
yamlContent := `
version: 1
rules:
- dataset: test_table
checks:
- schema_check:
expect_columns:
columns: [id, name, email]
desc: "Ensure required columns exist"
- schema_check:
columns_not_present:
columns: [password, ssn]
desc: "Ensure sensitive columns don't exist"
- row_count > 100:
desc: "Ensure table has data"
`
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.Fatalf("Expected 1 rule, got %d", len(config.Rules))
}
if len(config.Rules[0].Checks) != 3 {
t.Fatalf("Expected 3 checks, got %d", len(config.Rules[0].Checks))
}
// Check the first check (expect_columns)
check1 := config.Rules[0].Checks[0]
if check1.Expression != "expect_columns" {
t.Errorf("First check: expected Expression 'expect_columns', got %s", check1.Expression)
}
if check1.SchemaCheck == nil || check1.SchemaCheck.ExpectColumns == nil {
t.Error("First check should have ExpectColumns")
}
// Check the second check (columns_not_present)
check2 := config.Rules[0].Checks[1]
if check2.Expression != "columns_not_present" {
t.Errorf("Second check: expected Expression 'columns_not_present', got %s", check2.Expression)
}
if check2.SchemaCheck == nil || check2.SchemaCheck.ColumnsNotPresent == nil {
t.Error("Second check should have ColumnsNotPresent")
}
if len(check2.SchemaCheck.ColumnsNotPresent.Columns) != 2 {
t.Errorf("Expected 2 columns to not be present, got %d", len(check2.SchemaCheck.ColumnsNotPresent.Columns))
}
// Check the third check (row_count)
check3 := config.Rules[0].Checks[2]
if check3.Expression != "row_count > 100" {
t.Errorf("Third check: expected Expression 'row_count > 100', got %s", check3.Expression)
}
if check3.ParsedCheck == nil {
t.Error("Third check should have ParsedCheck")
}
}
func TestColumnsNotPresentValidationEdgeCases(t *testing.T) {
tests := []struct {
name string
yamlContent string
expectValid bool
description string
}{
{
name: "empty columns list with pattern",
yamlContent: `
version: 1
rules:
- dataset: test_table
checks:
- schema_check:
columns_not_present:
columns: []
pattern: "temp_*"
`,
expectValid: true,
description: "Empty columns list with pattern should be valid",
},
{
name: "empty pattern with columns",
yamlContent: `
version: 1
rules:
- dataset: test_table
checks:
- schema_check:
columns_not_present:
columns: [temp_col]
pattern: ""
`,
expectValid: true,
description: "Empty pattern with columns should be valid",
},
{
name: "neither columns nor pattern",
yamlContent: `
version: 1
rules:
- dataset: test_table
checks:
- schema_check:
columns_not_present:
columns: []
pattern: ""
`,
expectValid: true, // Will be validated at runtime in adapter
description: "Neither columns nor pattern will be caught during SQL generation",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var config ChecksFileConfig
err := yaml.Unmarshal([]byte(tt.yamlContent), &config)
if !tt.expectValid {
if err == nil {
t.Errorf("%s: Expected error but got none", tt.description)
}
} else {
if err != nil {
t.Errorf("%s: Unexpected error: %v", tt.description, err)
}
}
})
}
}