forked from zph/moresql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_test.go
119 lines (111 loc) · 3.76 KB
/
utils_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
package moresql_test
import (
"github.com/rwynn/gtm"
m "github.com/zph/moresql"
. "gopkg.in/check.v1"
"gopkg.in/mgo.v2/bson"
)
func (s *MySuite) TestSanitizeData(c *C) {
bsonId := bson.ObjectId("123")
withBson := map[string]interface{}{"_id": bsonId}
withBsonResult := map[string]interface{}{"name": interface{}(nil), "age": interface{}(nil), "location_id": interface{}(nil), "_id": "313233"}
withSymbol := map[string]interface{}{"name": bson.Symbol("Alice")}
withSymbolResult := map[string]interface{}{"age": interface{}(nil), "location_id": interface{}(nil), "_id": interface{}(nil), "name": "Alice"}
withNonPrimaryKey := map[string]interface{}{"name": "Alice", "location_id": bsonId}
withNonPrimaryKeyResult := map[string]interface{}{"_id": interface{}(nil), "name": "Alice", "age": interface{}(nil), "location_id": "313233"}
var table = []struct {
op *gtm.Op
result map[string]interface{}
}{
{>m.Op{Operation: "i", Data: withBson}, withBsonResult},
{>m.Op{Operation: "i", Data: withSymbol}, withSymbolResult},
{>m.Op{Operation: "i", Data: withNonPrimaryKey}, withNonPrimaryKeyResult},
}
for _, t := range table {
actual := m.SanitizeData(BuildFields("_id", "name", "age", "location_id"), t.op)
c.Check(actual, DeepEquals, t.result)
}
// Test nested data structures
test1Mongo := m.Mongo{}
test1Postgres := m.Postgres{}
test1Mongo.Name = "name.first"
test1Postgres.Name = "name_first"
field := m.Field{}
field.Mongo = test1Mongo
field.Postgres = test1Postgres
nameFirst := m.Fields{"name.first": field}
singleNested := map[string]interface{}{"name": map[string]interface{}{"first": "John", "last": "Doe"}}
singleNestedResult := map[string]interface{}{"name_first": "John"}
mResidential := m.Mongo{}
pResidential := m.Postgres{}
mResidential.Name = "address.home"
pResidential.Name = "address_home"
f := m.Field{}
f.Mongo = mResidential
f.Postgres = pResidential
address := m.Fields{"address.home": f}
stub := map[string]interface{}{"address": map[string]interface{}{"home": false}}
result := map[string]interface{}{"address_home": false}
var nested = []struct {
op *gtm.Op
fields m.Fields
result map[string]interface{}
}{
{>m.Op{Operation: "i", Data: singleNested}, nameFirst, singleNestedResult},
{>m.Op{Operation: "i", Data: stub}, address, result},
}
for _, t := range nested {
actual := m.SanitizeData(t.fields, t.op)
c.Check(actual, DeepEquals, t.result)
}
}
func (s *MySuite) TestEnsureOpHasAllFieldsWhenEmpty(c *C) {
op := >m.Op{Operation: "i"}
fields := []string{"_id", "name", "age"}
actual := m.EnsureOpHasAllFields(op, fields)
for _, f := range fields {
val, ok := actual.Data[f]
c.Check(ok, Equals, true)
c.Check(val, Equals, nil)
}
c.Check(actual.Data, DeepEquals, map[string]interface{}{
"_id": interface{}(nil),
"name": interface{}(nil),
"age": interface{}(nil),
})
}
func (s *MySuite) TestEnsureOpHasAllFieldsWhenMissingField(c *C) {
data := map[string]interface{}{
"_id": interface{}("123"),
"name": interface{}("Alice"),
}
op := >m.Op{Operation: "i", Data: data}
fields := []string{"_id", "name", "age"}
actual := m.EnsureOpHasAllFields(op, fields)
for _, f := range fields {
_, ok := actual.Data[f]
c.Check(ok, Equals, true)
}
c.Check(actual.Data, DeepEquals, map[string]interface{}{
"_id": interface{}("123"),
"name": interface{}("Alice"),
"age": interface{}(nil),
})
}
func (s *MySuite) TestIsInsertUpdateDelete(c *C) {
var table = []struct {
op *gtm.Op
result bool
}{
{>m.Op{Operation: "c"}, false},
{>m.Op{Operation: "i"}, true},
{>m.Op{Operation: "u"}, true},
{>m.Op{Operation: "d"}, true},
}
for _, t := range table {
actual := m.IsInsertUpdateDelete(t.op)
c.Check(actual, Equals, t.result)
}
}
// func (s *MySuite) TestCreateFanKey(c *C){
// }