-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
111 lines (108 loc) · 2.68 KB
/
main_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
package main
import (
"reflect"
"testing"
)
func TestGenerateInsertSQL(t *testing.T) {
tests := []struct {
name string
oplog string
want []string
wantErr bool
}{
{name: "Empty Operation",
oplog: "",
want: []string{},
wantErr: true,
},
{name: "Insert Operation", oplog: `{
"op" : "i",
"ns" : "test.student",
"o" : {
"_id" : "635b79e231d82a8ab1de863b",
"name" : "Selena Miller",
"roll_no" : 51,
"is_graduated" : false,
"date_of_birth" : "2000-01-30"
}
}`,
want: []string{
"CREATE SCHEMA test;",
"CREATE TABLE test.student (_id VARCHAR(255) PRIMARY KEY, date_of_birth VARCHAR(255), is_graduated BOOLEAN, name VARCHAR(255), roll_no FLOAT);",
"INSERT INTO test.student (_id, date_of_birth, is_graduated, name, roll_no) VALUES ('635b79e231d82a8ab1de863b', '2000-01-30', false, 'Selena Miller', 51);"},
wantErr: false},
{name: "Update Operation -set", oplog: `{
"op": "u",
"ns": "test.student",
"o": {
"$v": 2,
"diff": {
"u": {
"is_graduated": true
}
}
},
"o2": {
"_id": "635b79e231d82a8ab1de863b"
}
}`,
want: []string{"UPDATE test.student SET is_graduated = true WHERE _id = '635b79e231d82a8ab1de863b';"},
wantErr: false},
{name: "Update Operation -set with multiple option", oplog: `{
"op": "u",
"ns": "test.student",
"o": {
"$v": 2,
"diff": {
"u": {
"is_graduated": true,
"roll_no" : 51
}
}
},
"o2": {
"_id": "635b79e231d82a8ab1de863b"
}
}`,
want: []string{"UPDATE test.student SET is_graduated = true, roll_no = 51 WHERE _id = '635b79e231d82a8ab1de863b';"},
wantErr: false},
{name: "Update Operation -unset", oplog: `{
"op": "u",
"ns": "test.student",
"o": {
"$v": 2,
"diff": {
"d": {
"roll_no": false
}
}
},
"o2": {
"_id": "635b79e231d82a8ab1de863b"
}
}`,
want: []string{"UPDATE test.student SET roll_no = NULL WHERE _id = '635b79e231d82a8ab1de863b';"},
wantErr: false},
{name: "Delete Operation", oplog: `{
"op": "d",
"ns": "test.student",
"o": {
"_id": "635b79e231d82a8ab1de863b"
}
}`,
want: []string{"DELETE FROM test.student WHERE _id = '635b79e231d82a8ab1de863b';"},
wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GenerateSQL(tt.oplog)
if (err != nil) != tt.wantErr {
t.Errorf("GenerateSQL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GenerateSQL() = %v, want %v", got, tt.want)
}
})
}
}