forked from src-d/go-kallax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_test.go
270 lines (238 loc) · 5.46 KB
/
common_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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package kallax
import (
"database/sql"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func envOrDefault(key string, def string) string {
v := os.Getenv(key)
if v == "" {
v = def
}
return v
}
func openTestDB() (*sql.DB, error) {
return sql.Open("postgres", fmt.Sprintf(
"postgres://%s:%[email protected]:5432/%s?sslmode=disable",
envOrDefault("DBUSER", "testing"),
envOrDefault("DBPASS", "testing"),
envOrDefault("DBNAME", "testing"),
))
}
func setupTables(t *testing.T, db *sql.DB) {
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS model (
id serial PRIMARY KEY,
name varchar(255) not null,
email varchar(255) not null,
age int not null
)`)
require.NoError(t, err)
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS rel (
id serial PRIMARY KEY,
model_id integer,
foo text
)`)
require.NoError(t, err)
}
func teardownTables(t *testing.T, db *sql.DB) {
_, err := db.Exec("DROP TABLE model")
require.NoError(t, err)
_, err = db.Exec("DROP TABLE rel")
require.NoError(t, err)
}
type model struct {
Model
ID int64 `pk:"autoincr"`
Name string
Email string
Age int
Rel *rel
Rels []*rel
}
func newModel(name, email string, age int) *model {
m := &model{Model: NewModel(), Name: name, Email: email, Age: age}
return m
}
func (m *model) Value(col string) (interface{}, error) {
switch col {
case "id":
return m.ID, nil
case "name":
return m.Name, nil
case "email":
return m.Email, nil
case "age":
return m.Age, nil
}
return nil, fmt.Errorf("kallax: column does not exist: %s", col)
}
func (m *model) ColumnAddress(col string) (interface{}, error) {
switch col {
case "id":
return &m.ID, nil
case "name":
return &m.Name, nil
case "email":
return &m.Email, nil
case "age":
return &m.Age, nil
}
return nil, fmt.Errorf("kallax: column does not exist: %s", col)
}
func (m *model) NewRelationshipRecord(field string) (Record, error) {
switch field {
case "rel":
return new(rel), nil
case "rels":
return new(rel), nil
}
return nil, fmt.Errorf("kallax: no relationship found for field %s", field)
}
func (m *model) SetRelationship(field string, record interface{}) error {
switch field {
case "rel":
rel, ok := record.(*rel)
if !ok {
return fmt.Errorf("kallax: can't set relationship %s with a record of type %t", field, record)
}
m.Rel = rel
return nil
case "rels":
rels, ok := record.([]Record)
if !ok {
return fmt.Errorf("kallax: can't set relationship %s with value of type %T", field, record)
}
m.Rels = make([]*rel, len(rels))
for i, r := range rels {
rel, ok := r.(*rel)
if !ok {
return fmt.Errorf("kallax: can't set element of relationship %s with element of type %T", field, r)
}
m.Rels[i] = rel
}
return nil
}
return fmt.Errorf("kallax: no relationship found for field %s", field)
}
func (m *model) GetID() Identifier {
return (*NumericID)(&m.ID)
}
type rel struct {
Model
ID int64 `pk:"autoincr"`
Foo string
}
func newRel(id Identifier, foo string) *rel {
rel := &rel{NewModel(), 0, foo}
rel.AddVirtualColumn("model_id", id)
return rel
}
func (r *rel) GetID() Identifier {
return (*NumericID)(&r.ID)
}
func (m *rel) Value(col string) (interface{}, error) {
switch col {
case "id":
return m.ID, nil
case "model_id":
return m.VirtualColumn(col), nil
case "foo":
return m.Foo, nil
}
return nil, fmt.Errorf("kallax: column does not exist: %s", col)
}
func (m *rel) ColumnAddress(col string) (interface{}, error) {
switch col {
case "id":
return &m.ID, nil
case "model_id":
return VirtualColumn(col, m, new(NumericID)), nil
case "foo":
return &m.Foo, nil
}
return nil, fmt.Errorf("kallax: column does not exist: %s", col)
}
func (m *rel) NewRelationshipRecord(field string) (Record, error) {
return nil, fmt.Errorf("kallax: no relationship found for field %s", field)
}
func (m *rel) SetRelationship(field string, record interface{}) error {
return fmt.Errorf("kallax: no relationship found for field %s", field)
}
type onlyPkModel struct {
Model
ID int64 `pk:"autoincr"`
}
func newOnlyPkModel() *onlyPkModel {
m := new(onlyPkModel)
return m
}
func (m *onlyPkModel) Value(col string) (interface{}, error) {
switch col {
case "id":
return m.ID, nil
}
return nil, fmt.Errorf("kallax: column does not exist: %s", col)
}
func (m *onlyPkModel) ColumnAddress(col string) (interface{}, error) {
switch col {
case "id":
return &m.ID, nil
}
return nil, fmt.Errorf("kallax: column does not exist: %s", col)
}
func (m *onlyPkModel) NewRelationshipRecord(field string) (Record, error) {
return nil, fmt.Errorf("kallax: no relationship found for field %s", field)
}
func (m *onlyPkModel) SetRelationship(field string, record interface{}) error {
return fmt.Errorf("kallax: no relationship found for field %s", field)
}
func (m *onlyPkModel) GetID() Identifier {
return (*NumericID)(&m.ID)
}
var ModelSchema = NewBaseSchema(
"model",
"__model",
f("id"),
ForeignKeys{
"rel": NewForeignKey("model_id", false),
"rels": NewForeignKey("model_id", false),
"rel_inv": NewForeignKey("model_id", true),
},
func() Record {
return new(model)
},
true,
f("id"),
f("name"),
f("email"),
f("age"),
)
var RelSchema = NewBaseSchema(
"rel",
"__rel",
f("id"),
ForeignKeys{},
func() Record {
return new(rel)
},
true,
f("id"),
f("model_id"),
f("foo"),
)
var onlyPkModelSchema = NewBaseSchema(
"model",
"__model",
f("id"),
nil,
func() Record {
return new(onlyPkModel)
},
true,
f("id"),
)
func f(name string) SchemaField {
return NewSchemaField(name)
}