-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthor_test.go
More file actions
623 lines (525 loc) · 18.1 KB
/
author_test.go
File metadata and controls
623 lines (525 loc) · 18.1 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package models
import "os"
import "io"
import "fmt"
import "time"
import "bytes"
import "strings"
import "testing"
import _ "github.com/mattn/go-sqlite3"
import "database/sql"
import "github.com/franela/goblin"
import "github.com/marlow/marlow"
func addAuthorRow(db *sql.DB, values ...[]string) error {
for _, rowValues := range values {
valueString := strings.Join(rowValues, ",")
statement := fmt.Sprintf("insert into authors (system_id,name,birthday) values(%s);", valueString)
r, e := db.Exec(statement)
if e != nil {
return e
}
count, e := r.RowsAffected()
if e != nil {
return e
}
if count != 1 {
return fmt.Errorf("no-rows-created")
}
}
return nil
}
func Test_Author(t *testing.T) {
g := goblin.Goblin(t)
var db *sql.DB
var store AuthorStore
var queryLog io.Writer
dbFile := "author-testing.db"
generatedAuthorCount := 150
g.Describe("AuthorBlueprint test suite", func() {
g.It("results in empty string w/o values", func() {
r := fmt.Sprintf("%s", &AuthorBlueprint{})
g.Assert(r).Equal("")
})
g.It("supports int values on sql.NullInt64 fields", func() {
r := fmt.Sprintf("%s", &AuthorBlueprint{
UniversityID: []sql.NullInt64{
{Int64: 10, Valid: true},
},
})
g.Assert(r).Equal("WHERE authors.university_id IN (?)")
})
g.It("supports NOT NULL selection if present but empty on sql.NullInt64 fields", func() {
r := fmt.Sprintf("%s", &AuthorBlueprint{
UniversityID: []sql.NullInt64{},
})
g.Assert(r).Equal("WHERE authors.university_id NOT NULL")
})
g.It("supports null values on sql.NullInt64 fields", func() {
r := fmt.Sprintf("%s", &AuthorBlueprint{
UniversityID: []sql.NullInt64{
{Valid: false},
},
})
g.Assert(r).Equal("WHERE authors.university_id IS NULL")
})
g.It("supports range on ID column querying", func() {
r := fmt.Sprintf("%s", &AuthorBlueprint{
IDRange: []int{1, 2},
})
g.Assert(r).Equal("WHERE (authors.system_id > ? AND authors.system_id < ?)")
})
g.It("supports in on uint column querying", func() {
r := fmt.Sprintf("%s", &AuthorBlueprint{AuthorFlags: []uint8{1, 2}})
g.Assert(r).Equal("WHERE authors.flags IN (?,?)")
})
g.It("supports range on uint column querying", func() {
r := fmt.Sprintf("%s", &AuthorBlueprint{AuthorFlagsRange: []uint8{1, 2}})
g.Assert(r).Equal("WHERE (authors.flags > ? AND authors.flags < ?)")
})
g.It("supports range on float64 column querying", func() {
r := fmt.Sprintf("%s", &AuthorBlueprint{ReaderRatingRange: []float64{1, 2}})
g.Assert(r).Equal("WHERE (authors.rating > ? AND authors.rating < ?)")
})
g.It("supports 'IN' on float64 column querying", func() {
r := fmt.Sprintf("%s", &AuthorBlueprint{ReaderRating: []float64{1, 2, 3}})
g.Assert(r).Equal("WHERE authors.rating IN (?,?,?)")
})
g.It("supports 'IN' on ID column querying", func() {
r := fmt.Sprintf("%s", &AuthorBlueprint{ID: []int{1, 2, 3}})
g.Assert(r).Equal("WHERE authors.system_id IN (?,?,?)")
})
g.It("supports a combination of range and 'IN' on ID column querying", func() {
r := fmt.Sprintf("%s", &AuthorBlueprint{
ID: []int{1, 2, 3},
IDRange: []int{1, 4},
})
g.Assert(r).Equal("WHERE authors.system_id IN (?,?,?) AND (authors.system_id > ? AND authors.system_id < ?)")
})
g.It("supports making a blueprint inclusive", func() {
r := fmt.Sprintf("%s", &AuthorBlueprint{
NameLike: []string{"%rodger%"},
IDRange: []int{1, 4},
Inclusive: true,
})
g.Assert(r).Equal("WHERE (authors.system_id > ? AND authors.system_id < ?) OR authors.name LIKE ?")
})
})
g.Describe("Author model & generated store test suite", func() {
g.Before(func() {
var e error
db, e = loadDB(dbFile)
g.Assert(e).Equal(nil)
authors := [][]string{}
for i := 1; i < generatedAuthorCount; i++ {
id, name := fmt.Sprintf("%d", i), fmt.Sprintf("'author-%d'", (i*10)+1)
authors = append(authors, []string{id, name, "date()"})
}
g.Assert(addAuthorRow(db, authors...)).Equal(nil)
_, e = db.Exec(
"insert into authors (system_id,name,university_id,birthday) values(1337,'learned author',10,date());",
)
g.Assert(e).Equal(nil)
_, e = db.Exec(
"insert into authors (system_id,name,university_id,birthday) values(1338,'other author',null,date());",
)
g.Assert(e).Equal(nil)
})
g.BeforeEach(func() {
queryLog = new(bytes.Buffer)
store = NewAuthorStore(db, queryLog)
})
g.After(func() {
e := db.Close()
g.Assert(e).Equal(nil)
os.Remove(dbFile)
})
g.It("allows the consumer to search for authors w/o (default limit)", func() {
authors, e := store.FindAuthors(nil)
g.Assert(e).Equal(nil)
g.Assert(len(authors)).Equal(marlow.DefaultBlueprintLimit)
})
g.It("allows the consumer to search for authors w/ blueprint (explicit limit)", func() {
authors, e := store.FindAuthors(&AuthorBlueprint{Limit: 20})
g.Assert(e).Equal(nil)
g.Assert(len(authors)).Equal(20)
})
g.It("allows the consumer to search for authors w/ blueprint (explicit offset)", func() {
authors, e := store.FindAuthors(&AuthorBlueprint{Offset: 1, Limit: 1})
g.Assert(e).Equal(nil)
g.Assert(len(authors)).Equal(1)
g.Assert(authors[0].ID).Equal(2)
})
g.It("allows the consumer to search for authors by explicit Name", func() {
authors, e := store.FindAuthors(&AuthorBlueprint{
Name: []string{"author-11", "author-21", "not-exists"},
})
g.Assert(e).Equal(nil)
g.Assert(len(authors)).Equal(2)
})
g.It("allows the consumer to search by 'NameLike'", func() {
authors, e := store.FindAuthors(&AuthorBlueprint{
NameLike: []string{"%-100%"},
})
g.Assert(e).Equal(nil)
g.Assert(len(authors)).Equal(1)
})
g.It("allows the consumer to search for authors by explicit ID", func() {
authors, e := store.FindAuthors(&AuthorBlueprint{
ID: []int{1, 2},
})
g.Assert(e).Equal(nil)
g.Assert(len(authors)).Equal(2)
})
g.It("allows the consumer to search for authors by ID range", func() {
authors, e := store.FindAuthors(&AuthorBlueprint{
IDRange: []int{1, 4},
})
g.Assert(e).Equal(nil)
g.Assert(len(authors)).Equal(2)
})
g.It("correctly serializes null/not null values into a sql.NullInt64 field", func() {
authors, e := store.FindAuthors(&AuthorBlueprint{
ID: []int{1337, 1338},
})
g.Assert(e).Equal(nil)
g.Assert(authors[0].Name).Equal("learned author")
g.Assert(authors[0].UniversityID.Valid).Equal(true)
g.Assert(authors[0].UniversityID.Int64).Equal(int64(10))
g.Assert(authors[1].Name).Equal("other author")
g.Assert(authors[1].UniversityID.Valid).Equal(false)
})
g.It("allows consumer to count w/ nil blueprint", func() {
count, e := store.CountAuthors(nil)
g.Assert(e).Equal(nil)
g.Assert(count).Equal(generatedAuthorCount + 1)
})
g.It("allows consumer to search by authors with null UniversityID", func() {
count, e := store.CountAuthors(&AuthorBlueprint{
UniversityID: []sql.NullInt64{
{Valid: false},
},
})
g.Assert(e).Equal(nil)
g.Assert(count).Equal(generatedAuthorCount)
})
g.It("allows consumers to select individual university ids with limit", func() {
var uniID sql.NullInt64
uniID.Scan(10)
_, e := store.CreateAuthors([]Author{
{Name: "limited author 001"},
{Name: "limited author 002", UniversityID: uniID},
{Name: "limited author 003"},
}...)
results, e := store.SelectAuthorUniversityIDs(&AuthorBlueprint{
NameLike: []string{"%limited%"},
Limit: 1,
Offset: 1,
})
g.Assert(e).Equal(nil)
g.Assert(len(results)).Equal(1)
g.Assert(results[0].Int64).Equal(int64(10))
store.DeleteAuthors(&AuthorBlueprint{
NameLike: []string{"%limited%"},
})
})
g.It("allows consumers to select individual university ids (result being null)", func() {
results, e := store.SelectAuthorUniversityIDs(&AuthorBlueprint{
ID: []int{10},
})
g.Assert(e).Equal(nil)
g.Assert(len(results)).Equal(1)
g.Assert(results[0].Valid).Equal(false)
})
g.It("allows consumers to select individual university ids (result being valid)", func() {
results, e := store.SelectAuthorUniversityIDs(&AuthorBlueprint{
ID: []int{1337},
})
g.Assert(e).Equal(nil)
g.Assert(len(results)).Equal(1)
g.Assert(results[0].Valid).Equal(true)
g.Assert(results[0].Int64).Equal(int64(10))
})
g.It("allows consumers to select individual author names with limit", func() {
_, e := store.CreateAuthors([]Author{
{Name: "limited name 001"},
{Name: "limited name 002"},
{Name: "limited name 003"},
{Name: "limited name 004"},
}...)
results, e := store.SelectAuthorNames(&AuthorBlueprint{
NameLike: []string{"%limited name%"},
Limit: 1,
Offset: 1,
})
g.Assert(e).Equal(nil)
g.Assert(len(results)).Equal(1)
g.Assert(results[0]).Equal("limited name 002")
store.DeleteAuthors(&AuthorBlueprint{
NameLike: []string{"%limited name%"},
})
})
g.It("allows consumers to select individual author names", func() {
results, e := store.SelectAuthorNames(&AuthorBlueprint{
ID: []int{10},
})
g.Assert(e).Equal(nil)
g.Assert(len(results)).Equal(1)
g.Assert(results[0]).Equal("author-101")
})
g.It("allows consumers to select individual author ids", func() {
ids, e := store.SelectAuthorIDs(&AuthorBlueprint{
ID: []int{10},
})
g.Assert(e).Equal(nil)
g.Assert(len(ids)).Equal(1)
})
g.It("allows consumer to search by authors where not null if empty", func() {
count, e := store.CountAuthors(&AuthorBlueprint{
UniversityID: []sql.NullInt64{},
})
g.Assert(e).Equal(nil)
g.Assert(count).Equal(1)
})
g.It("allows consumer to search by authors with explicit UniversityID", func() {
count, e := store.CountAuthors(&AuthorBlueprint{
UniversityID: []sql.NullInt64{
{Int64: 10, Valid: true},
},
})
g.Assert(e).Equal(nil)
g.Assert(count).Equal(1)
})
g.It("allows the consumer to update the author id", func() {
c, e := store.UpdateAuthorID(1991, &AuthorBlueprint{ID: []int{8}})
g.Assert(e).Equal(nil)
g.Assert(c).Equal(int64(1))
})
g.It("allows the consumer to update the author university id using nil", func() {
c, e := store.UpdateAuthorUniversityID(nil, &AuthorBlueprint{ID: []int{1}})
g.Assert(e).Equal(nil)
g.Assert(c).Equal(int64(1))
})
g.It("allows the consumer to update the author university id using valid: false", func() {
c, e := store.UpdateAuthorUniversityID(&sql.NullInt64{Valid: false}, &AuthorBlueprint{ID: []int{2}})
g.Assert(e).Equal(nil)
g.Assert(c).Equal(int64(1))
})
g.It("allows the consumer to update the author university id using valid: true", func() {
c, e := store.UpdateAuthorUniversityID(&sql.NullInt64{
Valid: true,
Int64: 101,
}, &AuthorBlueprint{ID: []int{2}})
g.Assert(e).Equal(nil)
g.Assert(c).Equal(int64(1))
})
g.It("allows the consumer to update the author name", func() {
c, e := store.CountAuthors(&AuthorBlueprint{Name: []string{"danny"}})
g.Assert(e).Equal(nil)
g.Assert(c).Equal(0)
updatedCount, e := store.UpdateAuthorName("danny", &AuthorBlueprint{ID: []int{1}})
g.Assert(e).Equal(nil)
g.Assert(updatedCount).Equal(int64(1))
a, e := store.FindAuthors(&AuthorBlueprint{ID: []int{1}})
g.Assert(e).Equal(nil)
g.Assert(a[0].Name).Equal("danny")
})
g.It("allows the consumer to count authors by blueprint", func() {
count, e := store.CountAuthors(&AuthorBlueprint{
ID: []int{1, 2},
})
g.Assert(e).Equal(nil)
g.Assert(count).Equal(2)
})
g.Describe("CreateAuthors", func() {
g.It("returns immediately with 0 if no authors", func() {
s, e := store.CreateAuthors()
g.Assert(e).Equal(nil)
g.Assert(s).Equal(int64(0))
})
g.It("returns the id of the latest author created", func() {
s, e := store.CreateAuthors([]Author{
{Name: "Danny", ReaderRating: 25.00},
{Name: "Amelia", ReaderRating: 99.99},
}...)
g.Assert(e).Equal(nil)
found, e := store.FindAuthors(&AuthorBlueprint{ID: []int{int(s)}})
g.Assert(e).Equal(nil)
g.Assert(len(found)).Equal(1)
g.Assert(found[0].Name).Equal("Amelia")
badReaderCount, e := store.CountAuthors(&AuthorBlueprint{
ReaderRating: []float64{25.00},
})
g.Assert(e).Equal(nil)
g.Assert(badReaderCount).Equal(1)
goodReaderCount, e := store.CountAuthors(&AuthorBlueprint{
ReaderRating: []float64{99.99},
})
g.Assert(e).Equal(nil)
g.Assert(goodReaderCount).Equal(1)
})
})
g.Describe("uint8 field interactions", func() {
g.It("allows the consumer to create records w/ uint8 fields", func() {
_, e := store.CreateAuthors(Author{
Name: "flaot64 testing",
AuthorFlags: 3,
})
g.Assert(e).Equal(nil)
_, e = store.DeleteAuthors(&AuthorBlueprint{
AuthorFlagsRange: []uint8{1, 4},
})
g.Assert(e).Equal(nil)
})
g.It("allows users to select uint8 fields", func() {
ratings, e := store.SelectAuthorAuthorFlags(&AuthorBlueprint{ID: []int{20}})
g.Assert(e).Equal(nil)
g.Assert(ratings[0]).Equal(uint8(0))
})
g.It("allows users to update uint8 fields", func() {
blueprint := &AuthorBlueprint{ID: []int{20}}
_, e := store.UpdateAuthorAuthorFlags(5, blueprint)
g.Assert(e).Equal(nil)
authors, e := store.FindAuthors(blueprint)
g.Assert(e).Equal(nil)
g.Assert(authors[0].AuthorFlags).Equal(uint8(5))
})
})
g.Describe("time.Time field interactions", func() {
fyodor := "fyodor dostoevsky"
g.Before(func() {
var uni sql.NullInt64
uni.Scan(100)
birthday, e := time.Parse(time.RFC3339, "1821-11-11T15:04:05Z")
g.Assert(e).Equal(nil)
store.CreateAuthors(Author{
Name: fyodor,
UniversityID: uni,
ReaderRating: 100.00,
Birthday: birthday,
})
})
g.It("allows users to select birthdays", func() {
b, e := store.SelectAuthorBirthdays(&AuthorBlueprint{Name: []string{fyodor}})
g.Assert(e).Equal(nil)
g.Assert(len(b)).Equal(1)
g.Assert(b[0].Year()).Equal(1821)
})
g.It("allows users to update birthdays", func() {
birthday, e := time.Parse(time.RFC3339, "1991-05-26T00:00:00Z")
g.Assert(e).Equal(nil)
_, e = store.UpdateAuthorBirthday(birthday, &AuthorBlueprint{
ID: []int{1},
})
g.Assert(e).Equal(nil)
a, e := store.SelectAuthorBirthdays(&AuthorBlueprint{ID: []int{1}})
g.Assert(e).Equal(nil)
g.Assert(len(a)).Equal(1)
g.Assert(a[0].Year()).Equal(1991)
})
g.It("allows users to search by birthday range", func() {
start, e := time.Parse(time.RFC3339, "1821-01-01T15:04:05Z")
g.Assert(e).Equal(nil)
end, e := time.Parse(time.RFC3339, "1850-01-01T15:04:05Z")
a, e := store.CountAuthors(&AuthorBlueprint{BirthdayRange: []time.Time{start, end}})
g.Assert(e).Equal(nil)
g.Assert(a).Equal(1)
})
g.It("allows users to search by birthday", func() {
birthday, e := time.Parse(time.RFC3339, "1821-11-11T15:04:05Z")
g.Assert(e).Equal(nil)
b, e := store.FindAuthors(&AuthorBlueprint{Birthday: []time.Time{birthday}})
g.Assert(e).Equal(nil)
g.Assert(len(b)).Equal(1)
})
})
g.Describe("float64 field interactions", func() {
g.It("allows the consumer to create records w/ float64 fields", func() {
_, e := store.CreateAuthors(Author{
Name: "flaot64 testing",
ReaderRating: 89.99,
})
g.Assert(e).Equal(nil)
_, e = store.DeleteAuthors(&AuthorBlueprint{
ReaderRatingRange: []float64{89, 90},
})
g.Assert(e).Equal(nil)
})
g.It("allows users to select float64 fields", func() {
ratings, e := store.SelectAuthorReaderRatings(&AuthorBlueprint{ID: []int{20}})
g.Assert(e).Equal(nil)
g.Assert(ratings[0]).Equal(100.00)
})
g.It("allows users to update float64 fields", func() {
blueprint := &AuthorBlueprint{ID: []int{20}}
_, e := store.UpdateAuthorReaderRating(50.00, blueprint)
g.Assert(e).Equal(nil)
authors, e := store.FindAuthors(blueprint)
g.Assert(e).Equal(nil)
g.Assert(authors[0].ReaderRating).Equal(50.00)
})
})
g.Describe("bitwise operations on bitmask", func() {
var blueprint *AuthorBlueprint
g.BeforeEach(func() {
birthday, e := time.Parse(time.RFC3339, "1821-11-11T15:04:05Z")
g.Assert(e).Equal(nil)
created, e := store.CreateAuthors(Author{
Name: "Bit Author",
Birthday: birthday,
})
g.Assert(e).Equal(nil)
blueprint = &AuthorBlueprint{
ID: []int{int(created)},
}
})
g.AfterEach(func() {
_, e := store.DeleteAuthors(blueprint)
g.Assert(e).Equal(nil)
})
g.It("allows the user to add to the bitmask", func() {
_, e := store.UpdateAuthorAuthorFlags(1, blueprint)
g.Assert(e).Equal(nil)
flags, e := store.SelectAuthorAuthorFlags(blueprint)
g.Assert(e).Equal(nil)
g.Assert(len(flags)).Equal(1)
g.Assert(fmt.Sprintf("%b", flags[0])).Equal("1")
_, e = store.AddAuthorAuthorFlags(2, blueprint)
g.Assert(e).Equal(nil)
flags, e = store.SelectAuthorAuthorFlags(blueprint)
g.Assert(e).Equal(nil)
g.Assert(len(flags)).Equal(1)
g.Assert(fmt.Sprintf("%b", flags[0])).Equal("11")
})
g.It("allows the user to remove from the bitmask", func() {
_, e := store.UpdateAuthorAuthorFlags(7, blueprint)
g.Assert(e).Equal(nil)
_, e = store.DropAuthorAuthorFlags(2, blueprint)
g.Assert(e).Equal(nil)
flags, e := store.SelectAuthorAuthorFlags(blueprint)
g.Assert(e).Equal(nil)
g.Assert(len(flags)).Equal(1)
g.Assert(fmt.Sprintf("%b", flags[0])).Equal("101")
})
})
g.Describe("DeleteAuthors", func() {
g.It("returns an error and a negative number with an empty blueprint", func() {
c, e := store.DeleteAuthors(&AuthorBlueprint{})
g.Assert(e == nil).Equal(false)
g.Assert(c).Equal(int64(-1))
})
g.It("returns an error and a negative number without a blueprint", func() {
c, e := store.DeleteAuthors(nil)
g.Assert(e == nil).Equal(false)
g.Assert(c).Equal(int64(-1))
})
g.It("successfully deletes the records found by the blueprint", func() {
deleted, e := store.DeleteAuthors(&AuthorBlueprint{ID: []int{13}})
g.Assert(e).Equal(nil)
g.Assert(deleted).Equal(int64(1))
found, e := store.CountAuthors(&AuthorBlueprint{ID: []int{13}})
g.Assert(e).Equal(nil)
g.Assert(found).Equal(0)
})
})
})
}