-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbq_validator_expect_columns_test.go
More file actions
123 lines (112 loc) · 3.22 KB
/
Copy pathdbq_validator_expect_columns_test.go
File metadata and controls
123 lines (112 loc) · 3.22 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
package dbqcore
import (
"context"
"io"
"log/slog"
"testing"
)
// MockAdapterForExpectColumns is a test adapter for expect_columns checks
type MockAdapterForExpectColumns struct {
expectedQuery string
returnValue interface{}
returnError error
}
func (m *MockAdapterForExpectColumns) InterpretDataQualityCheck(check *DataQualityCheck, dataset string, where string) (string, error) {
if check.SchemaCheck != nil && check.SchemaCheck.ExpectColumns != nil {
// Simulate SQL generation for expect_columns check
return "SELECT COUNT(*) FROM information_schema.columns WHERE column_name IN ('col1', 'col2')", nil
}
return "", nil
}
func (m *MockAdapterForExpectColumns) ExecuteQuery(ctx context.Context, query string) (interface{}, error) {
m.expectedQuery = query
return m.returnValue, m.returnError
}
func TestDbqDataValidator_ExpectColumns(t *testing.T) {
tests := []struct {
name string
check DataQualityCheck
queryResult interface{}
expectedPassed bool
expectedReason string
}{
{
name: "expect_columns all exist",
check: DataQualityCheck{
Expression: "expect_columns",
SchemaCheck: &SchemaCheckConfig{
ExpectColumns: &ExpectColumnsConfig{
Columns: []string{"user_id", "email", "created_at"},
},
},
},
queryResult: 3,
expectedPassed: true,
expectedReason: "",
},
{
name: "expect_columns some missing",
check: DataQualityCheck{
Expression: "expect_columns",
Description: "Check required columns exist",
SchemaCheck: &SchemaCheckConfig{
ExpectColumns: &ExpectColumnsConfig{
Columns: []string{"user_id", "email", "created_at"},
},
},
},
queryResult: 2,
expectedPassed: false,
expectedReason: "Check failed: expect_columns == 3 (got: 2)",
},
{
name: "expect_columns none exist",
check: DataQualityCheck{
Expression: "expect_columns",
OnFail: OnFailActionError,
SchemaCheck: &SchemaCheckConfig{
ExpectColumns: &ExpectColumnsConfig{
Columns: []string{"col1", "col2"},
},
},
},
queryResult: 0,
expectedPassed: false,
expectedReason: "Check failed: expect_columns == 2 (got: 0)",
},
{
name: "expect_columns single column",
check: DataQualityCheck{
Expression: "expect_columns",
SchemaCheck: &SchemaCheckConfig{
ExpectColumns: &ExpectColumnsConfig{
Columns: []string{"id"},
},
},
},
queryResult: 1,
expectedPassed: true,
expectedReason: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
adapter := &MockAdapterForExpectColumns{
returnValue: tt.queryResult,
}
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
validator := NewDbqDataValidator(logger)
ctx := context.Background()
result := validator.RunCheck(ctx, adapter, &tt.check, "test.table", "")
if result.Pass != tt.expectedPassed {
t.Errorf("Expected passed=%v, got %v", tt.expectedPassed, result.Pass)
}
if !tt.expectedPassed && result.Error != tt.expectedReason {
t.Errorf("Expected reason '%s', got '%s'", tt.expectedReason, result.Error)
}
if result.CheckID != tt.check.Expression {
t.Errorf("Expected expression '%s', got '%s'", tt.check.Expression, result.CheckID)
}
})
}
}