-
Notifications
You must be signed in to change notification settings - Fork 27
/
helpers_test.go
128 lines (106 loc) · 3.08 KB
/
helpers_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
package godb
import (
"database/sql"
"testing"
"github.com/samonzeweb/godb/adapters/sqlite"
)
func createInMemoryConnection(t *testing.T) *DB {
db, err := Open(sqlite.Adapter, ":memory:")
if err != nil {
t.Fatal(err)
}
// Enable logger if needed
//db.SetLogger(log.New(os.Stderr, "", 0))
return db
}
// Fixtures
type Dummy struct {
ID int `db:"id,key,auto"`
AText string `db:"a_text"`
AnotherText string `db:"another_text"`
AnInteger int `db:"an_integer"`
ANullableString sql.NullString `db:"a_nullable_string"`
Version int `db:"version,oplock"`
}
func (*Dummy) TableName() string {
return "dummies"
}
type RelatedToDummy struct {
ID int `db:"id,key,auto"`
DummyID int `db:"dummies_id"`
AText string `db:"a_text"`
}
func (*RelatedToDummy) TableName() string {
return "relatedtodummies"
}
type FromTwoTables struct {
Dummy `db:",rel=dummies"`
RelatedToDummy `db:",rel=relatedtodummies"`
}
type DummyAutoOplock struct {
ID int `db:"id,key,auto"`
AText string `db:"a_text"`
AnotherText string `db:"another_text"`
AnInteger int `db:"an_integer"`
ANullableString sql.NullString `db:"a_nullable_string"`
Version int `db:"version,auto,oplock"`
}
func (*DummyAutoOplock) TableName() string {
return "dummiesautooplock"
}
func fixturesSetup(t *testing.T) *DB {
db := createInMemoryConnection(t)
createTable :=
`create table dummies (
id integer not null primary key autoincrement,
a_text text not null,
another_text text not null,
an_integer integer not null,
a_nullable_string text,
version integet not null default(0));
create table relatedtodummies(
id integer not null primary key autoincrement,
dummies_id integer not null,
a_text text not null
);
create table dummiesautooplock (
id integer not null primary key autoincrement,
a_text text not null,
another_text text not null,
an_integer integer not null,
a_nullable_string text,
version integet not null default(0));
create trigger updateversion
after update
on dummiesautooplock
begin
update dummiesautooplock set version = (NEW.version + 1);
end;
`
_, err := db.sqlDB.Exec(createTable)
if err != nil {
t.Fatal(err)
}
insertRows :=
`insert into dummies
(a_text, another_text, an_integer, a_nullable_string)
values
("First", "Premier", 11, "Not empty"),
("Second", "Second", 12, ""),
("Third", "Troisième", 13, NULL);
insert into relatedtodummies
(dummies_id, a_text)
select id, "REL_" || a_text from dummies;
insert into dummiesautooplock
(a_text, another_text, an_integer, a_nullable_string)
values
("First", "Premier", 11, "Not empty"),
("Second", "Second", 12, ""),
("Third", "Troisième", 13, NULL);
`
_, err = db.sqlDB.Exec(insertRows)
if err != nil {
t.Fatal(err)
}
return db
}