This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
config_test.go
165 lines (154 loc) · 3.89 KB
/
config_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
package moresql_test
import (
m "github.com/zph/moresql"
. "gopkg.in/check.v1"
)
func BuildFieldFromId(str string) m.Field {
field := m.Field{}
field.Mongo.Name = str
field.Mongo.Type = "id"
field.Postgres.Name = str
field.Postgres.Type = "text"
return field
}
func BuildTextField(str string) m.Field {
field := m.Field{}
field.Mongo.Name = str
field.Mongo.Type = "text"
field.Postgres.Name = str
field.Postgres.Type = "text"
return field
}
func (s *MySuite) TestConfigParsingForFields(c *C) {
// Fields struct
ex1 := `
{"_id": {
"mongo": {
"name": "_id",
"type": "id"
},
"postgres": {
"name": "_id",
"type": "text"
}
}}
`
field := BuildFieldFromId("_id")
expected1 := m.Fields{"_id": field}
ex2 := `
{"name": {
"mongo": {
"name": "name",
"type": "text"
},
"postgres": {
"name": "name",
"type": "text"
}
}}
`
expected2 := m.Fields{"name": BuildTextField("name")}
ex3 := `{"name": "text"}`
expected3 := m.Fields{"name": BuildTextField("name")}
ex4 := `{"age": "integer", "name": "text"}`
exField4 := BuildTextField("age")
exField4.Mongo.Type = "integer"
exField4.Postgres.Type = "integer"
expected4 := m.Fields{"name": BuildTextField("name"), "age": exField4}
var table = []struct {
js string
expected m.Fields
}{
{ex1, expected1},
{ex2, expected2},
{ex3, expected3},
{ex4, expected4},
}
for _, t := range table {
f, err := m.JsonToFields(t.js)
c.Check(err, Equals, nil)
c.Check(f, DeepEquals, t.expected)
}
}
func (s *MySuite) TestConfigParsingFull(c *C) {
// Fields struct
ex1 := `
{
"company-production": {
"collections": {
"accounts": {
"name": "users",
"pg_table": "users",
"fields": {
"_id": {
"mongo": {
"name": "_id",
"type": "id"
},
"postgres": {
"name": "_id",
"type": "text"
}
},
"bio": {
"mongo": {
"name": "bio",
"type": "text"
},
"postgres": {
"name": "bio",
"type": "text"
}
}
}
},
"campaigns": {
"name": "campaigns",
"pg_table": "campaigns",
"fields": {
"_id": "id",
"created_at": "text"
}
}
}
}
}
`
expected1 := m.Config{"company-production": m.DB{Collections: m.Collections{"accounts": m.Collection{Name: "users", PgTable: "users", Fields: m.Fields{"_id": m.Field{Mongo: m.Mongo{Name: "_id", Type: "id"}, Postgres: m.Postgres{Name: "_id", Type: "text"}}, "bio": m.Field{Mongo: m.Mongo{Name: "bio", Type: "text"}, Postgres: m.Postgres{Name: "bio", Type: "text"}}}}, "campaigns": m.Collection{Name: "campaigns", PgTable: "campaigns", Fields: m.Fields{"_id": m.Field{Mongo: m.Mongo{Name: "_id", Type: "id"}, Postgres: m.Postgres{Name: "_id", Type: "text"}}, "created_at": m.Field{Mongo: m.Mongo{Name: "created_at", Type: "text"}, Postgres: m.Postgres{Name: "created_at", Type: "text"}}}}}}}
shorthand := `
{
"company-production": {
"collections": {
"accounts": {
"name": "users",
"pg_table": "users",
"fields": {
"_id": "id",
"bio": "text"
}
},
"campaigns": {
"name": "campaigns",
"pg_table": "campaigns",
"fields": {
"_id": "id",
"created_at": "text"
}
}
}
}
}
`
var table = []struct {
js string
expected m.Config
}{
{ex1, expected1},
{shorthand, expected1},
}
for _, t := range table {
f, err := m.LoadConfigString(t.js)
c.Check(f, DeepEquals, t.expected)
c.Check(err, Equals, nil)
}
}