-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_test.go
221 lines (209 loc) · 5.27 KB
/
helper_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
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
package QueryHelper
import (
"reflect"
"testing"
)
func TestAddPrefix(t *testing.T) {
tests := []struct {
name string
prefix string
input map[string]interface{}
expected map[string]interface{}
}{
{
name: "Add prefix to each key",
prefix: "pre_",
input: map[string]interface{}{
"key1": "value1",
"key2": 2,
"key3": true,
},
expected: map[string]interface{}{
"pre_key1": "value1",
"pre_key2": 2,
"pre_key3": true,
},
},
{
name: "Empty map input",
prefix: "pre_",
input: map[string]interface{}{},
expected: map[string]interface{}{},
},
{
name: "No prefix added",
prefix: "",
input: map[string]interface{}{
"key1": "value1",
"key2": "value2",
},
expected: map[string]interface{}{
"key1": "value1",
"key2": "value2",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output := AddPrefix(tt.prefix, tt.input)
if !reflect.DeepEqual(output, tt.expected) {
t.Errorf("AddPrefix() = %v, expected %v", output, tt.expected)
}
})
}
}
func TestGetNonEmptyColumns(t *testing.T) {
// Setup: Initialize the table with some columns
table := &Table[any]{
Columns: map[string]Column{
"name": {Name: "Name"},
"age": {Name: "Age"},
"email": {Name: "Email"},
},
}
// Input data containing keys that match some of the table's columns
data := map[string]interface{}{
"name": "Alice",
"age": 30,
"address": "123 Main St", // 'address' is not in Columns
}
// Expected output: Only columns that are present in both data and table.Columns
expectedColumns := []Column{
{Name: "Name"},
{Name: "Age"},
}
// Execution: Call the function under test
resultColumns := table.GetNonEmptyColumns(data)
// Verification: Check if the result matches the expected output
if len(resultColumns) != len(expectedColumns) {
t.Fatalf("Expected %d columns, got %d", len(expectedColumns), len(resultColumns))
}
// Convert slices to maps for easier comparison
resultMap := make(map[string]Column)
for _, col := range resultColumns {
resultMap[col.Name] = col
}
expectedMap := make(map[string]Column)
for _, col := range expectedColumns {
expectedMap[col.Name] = col
}
if !reflect.DeepEqual(resultMap, expectedMap) {
t.Errorf("Expected columns %v, got %v", expectedMap, resultMap)
}
}
//func TestUpdateStatement(t *testing.T) {
// // Scenario 1: Standard update with default updatable columns
// table := &Table[any]{
// Name: "my_schema.my_table",
// Columns: map[string]Column{
// "id": {
// Name: "id",
// Primary: true,
// AutoGenerateID: false,
// Update: false,
// },
// "username": {
// Name: "username",
// Primary: false,
// AutoGenerateID: false,
// Update: true,
// },
// "email": {
// Name: "email",
// Primary: false,
// AutoGenerateID: false,
// Update: true,
// },
// "created_at": {
// Name: "created_at",
// Primary: false,
// AutoGenerateID: false,
// Update: false,
// },
// "updated_at": {
// Name: "updated_at",
// Primary: false,
// AutoGenerateID: false,
// Update: false,
// },
// },
// }
//
// expectedSQL := "UPDATE .my_schema.my_table SET username = :username ,email = :email WHERE id = :old_id"
//
// // Call UpdateStatement without specifying updateColumns
// sql := table.UpdateStatement()
//
// if sql != expectedSQL {
// t.Errorf("Scenario 1 Failed:\nExpected SQL:\n%s\nGot:\n%s", expectedSQL, sql)
// }
//
// // Scenario 2: Update with specific columns provided
// updateColumns := []Column{
// table.Columns["email"],
// }
//
// expectedSQL = "UPDATE .my_schema.my_table SET email = :email WHERE id = :old_id"
//
// sql = table.UpdateStatement(updateColumns...)
//
// if sql != expectedSQL {
// t.Errorf("Scenario 2 Failed:\nExpected SQL:\n%s\nGot:\n%s", expectedSQL, sql)
// }
//
// // Scenario 3: No updatable columns
// tableNoUpdate := &Table[any]{
// Name: "my_schema.my_table2",
// Columns: map[string]Column{
// "id": {
// Name: "id",
// Primary: true,
// AutoGenerateID: false,
// Update: false,
// },
// "created_at": {
// Name: "created_at",
// Primary: false,
// AutoGenerateID: true,
// Update: false,
// },
// "updated_at": {
// Name: "updated_at",
// Primary: false,
// AutoGenerateID: false,
// Update: false,
// },
// },
// }
//
// sql = tableNoUpdate.UpdateStatement()
//
// if sql != "" {
// t.Errorf("Scenario 3 Failed:\nExpected empty SQL, got:\n%s", sql)
// }
//
// // Scenario 4: No primary key or AutoGenerateID columns
// tableNoWhere := &Table[any]{
// Name: "my_schema.my_table3",
// Columns: map[string]Column{
// "username": {
// Name: "username",
// Primary: false,
// AutoGenerateID: false,
// Update: true,
// },
// "email": {
// Name: "email",
// Primary: false,
// AutoGenerateID: false,
// Update: true,
// },
// },
// }
//
// sql = tableNoWhere.UpdateStatement()
//
// if sql != "" {
// t.Errorf("Scenario 4 Failed:\nExpected empty SQL when no primary key or AutoGenerateID columns, got:\n%s", sql)
// }
//}