-
Notifications
You must be signed in to change notification settings - Fork 2
/
testutil_test.go
318 lines (272 loc) · 7.3 KB
/
testutil_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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package worksheets
import (
"database/sql"
"fmt"
"strings"
"testing"
runner "github.com/homelight/dat/sqlx-runner"
_ "github.com/lib/pq"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
// some useful values
var (
alice = NewText("Alice")
bob = NewText("Bob")
carol = NewText("Carol")
)
// definitions
var defs = `
type simple worksheet {
83:name text
91:age number[0]
}
type all_types worksheet {
1:text text
2:bool bool
3:num_0 number[0]
4:num_2 number[2]
5:undefined undefined
6:ws all_types
7:slice_t []text
11:slice_b []bool
12:slice_bu []bool
9:slice_n0 []number[0]
10:slice_n2 []number[2]
13:slice_nu []number[0]
8:slice_ws []all_types
}
type with_slice worksheet {
42:names []text
}
type with_slice_of_refs worksheet {
42:many_simples []simple
}
type with_refs worksheet {
46:some_flag bool
87:simple simple
}
type with_refs_and_cycles worksheet {
404:point_to_me with_refs_and_cycles
500:point_to_my_friends []with_refs_and_cycles
}
type with_repeat_refs worksheet {
111:point_to_something simple
112:point_to_the_same_thing simple
113:and_again []simple
}
type Ping worksheet {
123:point_to_pong pong
124:slice_of_Ping []Ping
}
type pong worksheet {
321:point_to_Ping Ping
}
type DefaultMappingsTest worksheet {
83:Name text
91:Age number[0]
99:Child DefaultMappingsTest
}`
func forciblySetId(ws *Worksheet, id string) {
ws.data[indexId] = NewText(id)
}
type allDefs struct {
defs *Definitions
cloneDefs *Definitions
defsForSelectors *Definitions
defsCrossWs *Definitions
defsCrossWsThroughSlice *Definitions
enumsDefs *Definitions
}
func newAllDefs() allDefs {
var s allDefs
// When initializing, we purposefully ignore errors to make it easier to work
// on specific parts of the parser by running single tests:
// - If we're running a single test which does not depend on these
// definitions, we shouldn't fail early, so as to provide feedback to the
// programmer on the test being ran (rather than whether full parsing works).
// - And since the suite itself will fail if any of these are nil, we are not
// changing the test suite outcome by ignoring errors, simply shifting where
// and how these errors are reported.
s.defs, _ = NewDefinitions(strings.NewReader(defs))
s.cloneDefs, _ = NewDefinitions(strings.NewReader(cloneDefs))
s.defsForSelectors, _ = NewDefinitions(strings.NewReader(defsForSelectors))
s.defsCrossWs, _ = NewDefinitions(strings.NewReader(defsCrossWs))
s.defsCrossWsThroughSlice, _ = NewDefinitions(strings.NewReader(defsCrossWsThroughSlice), defsCrossWsThroughSliceOptions)
s.enumsDefs, _ = NewDefinitions(strings.NewReader(enumsDefs))
return s
}
type Zuite struct {
suite.Suite
allDefs
db *runner.DB
store *DbStore
}
func (s *Zuite) SetupSuite() {
// init
s.allDefs = newAllDefs()
// db
dbUrl := "postgres://ws_user:@localhost/ws_test?sslmode=disable"
db, err := sql.Open("postgres", dbUrl)
if err != nil {
panic(err)
}
s.db = runner.NewDB(db, "postgres")
// store
s.store = NewStore(s.defs)
}
func (s *Zuite) SetupTest() {
for table := range tableToEntities {
_, err := s.db.Exec(fmt.Sprintf("truncate %s", table))
if err != nil {
panic(err)
}
}
}
func (s *Zuite) TearDownSuite() {
err := s.db.DB.Close()
if err != nil {
panic(err)
}
}
func TestRunAllTheTests(t *testing.T) {
suite.Run(t, new(Zuite))
}
func RunTransaction(db *runner.DB, fn func(tx *runner.Tx) error) error {
tx, err := db.Begin()
if err != nil {
return err
}
defer tx.AutoRollback()
err = fn(tx)
if err != nil {
return err
}
return tx.Commit()
}
func (s *Zuite) RunTransaction(fn func(tx *runner.Tx) error) error {
return RunTransaction(s.db, fn)
}
func (s *Zuite) MustRunTransaction(fn func(tx *runner.Tx) error) {
err := s.RunTransaction(fn)
require.NoError(s.T(), err)
}
type fakeClock struct {
now int64
}
// Assert that fakeClock implements the clock interface.
var _ clock = &fakeClock{}
func (fc *fakeClock) nowAsUnixNano() int64 {
return fc.now
}
type rValueForTesting struct {
WorksheetId string
Index int
FromVersion int
ToVersion int
Value string
IsUndefined bool
}
type rSliceElementForTesting struct {
SliceId string
Rank int
FromVersion int
ToVersion int
Value string
IsUndefined bool
}
type dbState struct {
wsRecs []rWorksheet
editRecs []rEdit
valuesRecs []rValueForTesting
parentsRecs []rParent
sliceElementsRecs []rSliceElementForTesting
}
func (s *Zuite) snapshotDbState() *dbState {
var (
err error
wsRecs []rWorksheet
editRecs []rEdit
dbValuesRecs []rValue
parentsRecs []rParent
dbSliceElementsRecs []rSliceElement
)
err = s.db.
Select("*").
From("worksheets").
OrderBy("id").
QueryStructs(&wsRecs)
require.NoError(s.T(), err)
err = s.db.
Select("*").
From("worksheet_edits").
OrderBy("worksheet_id, to_version").
QueryStructs(&editRecs)
require.NoError(s.T(), err)
err = s.db.
Select("*").
From("worksheet_values").
OrderBy("worksheet_id, index, from_version").
QueryStructs(&dbValuesRecs)
require.NoError(s.T(), err)
err = s.db.
Select("*").
From("worksheet_parents").
OrderBy("child_id, parent_id, parent_field_index").
QueryStructs(&parentsRecs)
require.NoError(s.T(), err)
err = s.db.
Select("*").
From("worksheet_slice_elements").
OrderBy("slice_id, rank, from_version").
QueryStructs(&dbSliceElementsRecs)
require.NoError(s.T(), err)
// rValue to rValueForTesting
valuesRecs := make([]rValueForTesting, len(dbValuesRecs))
for i, dbValueRec := range dbValuesRecs {
valuesRecs[i] = rValueForTesting{
WorksheetId: dbValueRec.WorksheetId,
Index: dbValueRec.Index,
FromVersion: dbValueRec.FromVersion,
ToVersion: dbValueRec.ToVersion,
}
if dbValueRec.Value != nil {
valuesRecs[i].Value = *dbValueRec.Value
} else {
valuesRecs[i].IsUndefined = true
}
}
// rSliceElement to rSliceElementForTesting
sliceElementsRecs := make([]rSliceElementForTesting, len(dbSliceElementsRecs))
for i, dbSliceElementRec := range dbSliceElementsRecs {
sliceElementsRecs[i] = rSliceElementForTesting{
SliceId: dbSliceElementRec.SliceId,
Rank: dbSliceElementRec.Rank,
FromVersion: dbSliceElementRec.FromVersion,
ToVersion: dbSliceElementRec.ToVersion,
}
if dbSliceElementRec.Value != nil {
sliceElementsRecs[i].Value = *dbSliceElementRec.Value
} else {
sliceElementsRecs[i].IsUndefined = true
}
}
return &dbState{
wsRecs: wsRecs,
editRecs: editRecs,
valuesRecs: valuesRecs,
parentsRecs: parentsRecs,
sliceElementsRecs: sliceElementsRecs,
}
}