-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoperations_test.go
189 lines (169 loc) · 5.73 KB
/
operations_test.go
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
package flash
import "testing"
func TestIsAtomic(t *testing.T) {
tests := []struct {
name string
o Operation
expected bool
}{
{"Atomic Operation", OperationTruncate, true},
{"Composite Operation", OperationInsert | OperationUpdate, false},
{"Atomic But Invalid", 32, false},
{"Empty Operation", 0, false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.o.IsAtomic() != test.expected {
t.Errorf("IsAtomic() failed for %v: expected %v, got %v", test.o, test.expected, test.o.IsAtomic())
}
})
}
}
func TestGetAtomics(t *testing.T) {
tests := []struct {
name string
o Operation
expected []Operation
}{
{"Atomic Operation", OperationTruncate, []Operation{OperationTruncate}},
{"Composite Operation", OperationInsert | OperationUpdate, []Operation{OperationInsert, OperationUpdate}},
{"Composite All Operation", OperationAll, []Operation{OperationInsert, OperationUpdate, OperationDelete, OperationTruncate}},
{"Empty Operation", 0, []Operation{}},
{"Unknown Atomic", 32, []Operation{}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
atomics := test.o.GetAtomics()
if len(atomics) != len(test.expected) {
t.Errorf("GetAtomics() failed for %v: expected length %v, got length %v", test.o, len(test.expected), len(atomics))
} else {
for i, op := range atomics {
if op != test.expected[i] {
t.Errorf("GetAtomics() failed for %v: expected %v at index %d, got %v", test.o, test.expected[i], i, op)
}
}
}
})
}
}
func TestIncludeAll(t *testing.T) {
tests := []struct {
name string
o Operation
mask Operation
expected bool
}{
{"IncludeAll - true", OperationInsert | OperationUpdate | OperationDelete, OperationInsert | OperationUpdate, true},
{"IncludeAll - false", OperationInsert | OperationUpdate, OperationInsert | OperationUpdate | OperationDelete, false},
{"IncludeAll - empty operation", 0, OperationAll, false},
{"IncludeAll - unknown", 32, OperationAll, false},
{"IncludeAll - unknown", OperationAll, 32, false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.o.IncludeAll(test.mask) != test.expected {
t.Errorf("IncludeAll() failed for %v with mask %v: expected %v, got %v", test.o, test.mask, test.expected, test.o.IncludeAll(test.mask))
}
})
}
}
func TestIncludeOne(t *testing.T) {
tests := []struct {
name string
o Operation
mask Operation
expected bool
}{
{"IncludeOne - true", OperationInsert | OperationUpdate | OperationDelete, OperationDelete, true},
{"IncludeOne - false", OperationInsert | OperationUpdate, OperationTruncate, false},
{"IncludeOne - empty operation", 0, OperationInsert, false},
{"IncludeOne - Atomic same", OperationUpdate, OperationUpdate, true},
{"IncludeOne - Atomic different", OperationUpdate, OperationDelete, false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.o.IncludeOne(test.mask) != test.expected {
t.Errorf("IncludeOne() failed for %v with mask %v: expected %v, got %v", test.o, test.mask, test.expected, test.o.IncludeOne(test.mask))
}
})
}
}
func TestStrictName(t *testing.T) {
tests := []struct {
name string
o Operation
expectedName string
expectedErr bool
}{
{"Insert Operation", OperationInsert, "INSERT", false},
{"Update Operation", OperationUpdate, "UPDATE", false},
{"Delete Operation", OperationDelete, "DELETE", false},
{"Truncate Operation", OperationTruncate, "TRUNCATE", false},
{"Unknown Operation", Operation(32), "UNKNOWN", true},
{"Composite Operation", OperationInsert | OperationUpdate, "UNKNOWN", true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
name, err := test.o.StrictName()
// Check name correctness
if name != test.expectedName {
t.Errorf("StrictName() failed for %v: expected name %v, got %v", test.o, test.expectedName, name)
}
// Check error correctness
if (err != nil) != test.expectedErr {
t.Errorf("StrictName() failed for %v: expected error %v, got error %v", test.o, test.expectedErr, err)
}
})
}
}
func TestString(t *testing.T) {
tests := []struct {
name string
o Operation
expected string
}{
{"Single Atomic Operation", OperationInsert, "INSERT"},
{"Multiple Atomic Operations", OperationInsert | OperationUpdate | OperationTruncate, "INSERT | UPDATE | TRUNCATE"},
{"Empty Operation", 0, "UNKNOWN"},
{"Unknown Operation", 32, "UNKNOWN"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := test.o.String()
if result != test.expected {
t.Errorf("String() failed for %v: expected '%v', got '%v'", test.o, test.expected, result)
}
})
}
}
func TestOperationFromName(t *testing.T) {
tests := []struct {
name string
input string
expected Operation
expectedErr bool
}{
{"Insert", "insert", OperationInsert, false},
{"Update", "update", OperationUpdate, false},
{"Delete", "delete", OperationDelete, false},
{"Truncate", "truncate", OperationTruncate, false},
{"Truncate", "INSERT", OperationInsert, false},
{"Truncate", "UPDATE", OperationUpdate, false},
{"Truncate", "DELETE", OperationDelete, false},
{"Truncate", "TRUNCATE", OperationTruncate, false},
{"Unknown", "unknown", 0, true},
{"Empty String", "", 0, true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := OperationFromName(test.input)
if (err != nil) != test.expectedErr {
t.Errorf("OperationFromName() error = %v, expected error = %v", err, test.expectedErr)
return
}
if result != test.expected {
t.Errorf("OperationFromName() = %v, expected %v", result, test.expected)
}
})
}
}