forked from Masterminds/structable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite_ptr_test.go
224 lines (178 loc) · 5.31 KB
/
sqlite_ptr_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
// +build sqlite
package structable
import (
"database/sql"
"log"
"testing"
"github.com/Masterminds/squirrel"
_ "github.com/mattn/go-sqlite3"
)
type Movie struct {
Recorder
builder squirrel.StatementBuilderType
Id int64 `stbl:"id,PRIMARY_KEY,AUTO_INCREMENT"`
Title string `stbl:"title"`
Genre *string `stbl:"genre"`
Budget float64 `stbl:"budget"`
}
func (l *Movie) equals(other *Movie) bool {
return l.Id == other.Id &&
l.Title == other.Title &&
l.Budget == other.Budget &&
CompareStringPtr(l.Genre, other.Genre)
}
func CompareStringPtr(s1, s2 *string) bool {
switch {
case s1 == nil && s2 == nil:
return true
case s1 != nil && s2 == nil || s1 == nil && s2 != nil:
return false
default:
return *s1 == *s2
}
}
func stringPtr(s string) *string {
return &s
}
func (m *Movie) loadFromSql(Id int64, db *sql.DB) error {
if m.Genre == nil {
m.Genre = new(string)
}
err := db.QueryRow("SELECT title, genre, budget FROM movies WHERE id=?", Id).Scan(
&m.Title,
m.Genre,
&m.Budget)
if err == nil {
m.Id = Id
}
return err
}
func TestStructWithPointerInsert(t *testing.T) {
db := getMoviesDb()
m := &Movie{
Id: -1,
Title: "2001: A Space Odyssey",
Genre: stringPtr("Science-Fiction"),
Budget: 1500000}
m.Recorder = New(squirrel.NewStmtCacheProxy(db), "mysql").Bind("movies", m)
if err := m.Insert(); err != nil {
t.Fatalf("Failed Insert: %s", err)
}
msql := new(Movie)
msql.loadFromSql(m.Id, db)
if *msql.Genre != "Science-Fiction" {
t.Fatal("Insert should dereference allocated pointers")
}
m.Genre = nil
if err := m.Insert(); err != nil {
t.Fatalf("Failed Insert: %s", err)
}
msql.loadFromSql(m.Id, db)
if *msql.Genre != "unclassifiable" {
t.Fatal("Insert should ignore nil pointers")
}
}
func TestStructWithPointerLoad(t *testing.T) {
db := getMoviesDb()
msql := &Movie{}
if res, err := db.Exec("INSERT INTO movies (title, genre, budget) VALUES ('2001: A Space Odyssey', 'Science-Fiction', 1500000)"); err != nil {
t.Fatalf("Sqlite Exec failed: %s", err)
} else if msql.Id, err = res.LastInsertId(); err != nil {
t.Fatalf("Sqlite LastInsertId failed: %s", err)
}
m := &Movie{Id: msql.Id, Genre: new(string)}
m.Recorder = New(squirrel.NewStmtCacheProxy(db), "mysql").Bind("movies", m)
if err := m.Load(); err != nil {
t.Fatalf("Failed Load: %s", err)
}
if !CompareStringPtr(m.Genre, stringPtr("Science-Fiction")) {
t.Fatal("Load should load pointer fields")
}
m.Genre = nil
m.Recorder = New(squirrel.NewStmtCacheProxy(db), "mysql").Bind("movies", m)
if err := m.Load(); err != nil {
t.Fatalf("Failed Load: %s", err)
}
if !CompareStringPtr(m.Genre, stringPtr("Science-Fiction")) {
t.Fatal("Load should instantiate nil pointers")
}
}
func TestStructWithPointerLoadWhere(t *testing.T) {
db := getMoviesDb()
msql := &Movie{}
if res, err := db.Exec("INSERT INTO movies (title, genre, budget) VALUES ('2001: A Space Odyssey', 'Science-Fiction', 1500000)"); err != nil {
t.Fatalf("Sqlite Exec failed: %s", err)
} else if msql.Id, err = res.LastInsertId(); err != nil {
t.Fatalf("Sqlite LastInsertId failed: %s", err)
}
m := &Movie{}
m.Recorder = New(squirrel.NewStmtCacheProxy(db), "mysql").Bind("movies", m)
if err := m.LoadWhere("budget = ?", 1500000); err != nil {
t.Fatalf("Failed LoadWhere: %s", err)
}
if !CompareStringPtr(m.Genre, stringPtr("Science-Fiction")) {
t.Fatal("LoadWhere should load pointer fields")
}
m.Genre = nil
m.Recorder = New(squirrel.NewStmtCacheProxy(db), "mysql").Bind("movies", m)
if err := m.LoadWhere("budget = ?", 1500000); err != nil {
t.Fatalf("Failed LoadWhere: %s", err)
}
if !CompareStringPtr(m.Genre, stringPtr("Science-Fiction")) {
t.Fatal("LoadWhere should instantiate nil pointers")
}
}
func TestStructWithPointerUpdate(t *testing.T) {
db := getMoviesDb()
var lastId int64
if res, err := db.Exec("INSERT INTO movies (title, genre, budget) VALUES ('2001: A Space Odyssey', 'Science-Fiction', 1500000)"); err != nil {
t.Fatalf("Sqlite Exec failed: %s", err)
} else if lastId, err = res.LastInsertId(); err != nil {
t.Fatalf("Sqlite LastInsertId failed: %s", err)
}
m := &Movie{
Id: lastId,
Title: "The Usual Suspects",
Genre: nil,
Budget: 6000000}
m.Recorder = New(squirrel.NewStmtCacheProxy(db), "mysql").Bind("movies", m)
if err := m.Update(); err != nil {
t.Fatalf("Failed Update: %s", err)
}
msql := new(Movie)
msql.loadFromSql(lastId, db)
if !CompareStringPtr(msql.Genre, stringPtr("Science-Fiction")) {
t.Fatal("Update should ignore nil pointers")
}
m.Genre = stringPtr("Crime Thriller")
m.Recorder = New(squirrel.NewStmtCacheProxy(db), "mysql").Bind("movies", m)
if err := m.Update(); err != nil {
t.Fatalf("Failed Update: %s", err)
}
msql.loadFromSql(lastId, db)
if !CompareStringPtr(msql.Genre, stringPtr("Crime Thriller")) {
t.Log("msql.Genre: %v\n", *msql.Genre)
t.Fatal("Update should ignore nil pointers")
}
}
func getMoviesDb() *sql.DB {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
log.Fatalf("Couldn't Open database: %s\n", err)
}
stmt := `
CREATE TABLE movies (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title STRING,
genre STRING DEFAULT('unclassifiable'),
budget REAL
);
DELETE FROM movies;
`
_, err = db.Exec(stmt)
if err != nil {
log.Fatalf("Couldn't Exec query \"%q\": %s\n", err, stmt)
return nil
}
return db
}