MCPcopy Index your code
hub / github.com/Masterminds/structable / TestStructWithPointerUpdate

Function TestStructWithPointerUpdate

sqlite_ptr_test.go:157–197  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

155}
156
157func TestStructWithPointerUpdate(t *testing.T) {
158
159 db := getMoviesDb()
160
161 var lastId int64
162 if res, err := db.Exec("INSERT INTO movies (title, genre, budget) VALUES ('2001: A Space Odyssey', 'Science-Fiction', 1500000)"); err != nil {
163 t.Fatalf("Sqlite Exec failed: %s", err)
164 } else if lastId, err = res.LastInsertId(); err != nil {
165 t.Fatalf("Sqlite LastInsertId failed: %s", err)
166 }
167
168 m := &Movie{
169 Id: lastId,
170 Title: "The Usual Suspects",
171 Genre: nil,
172 Budget: 6000000}
173
174 m.Recorder = New(squirrel.NewStmtCacheProxy(db), "mysql").Bind("movies", m)
175 if err := m.Update(); err != nil {
176 t.Fatalf("Failed Update: %s", err)
177 }
178
179 msql := new(Movie)
180 msql.loadFromSql(lastId, db)
181
182 if !CompareStringPtr(msql.Genre, stringPtr("Science-Fiction")) {
183 t.Fatal("Update should ignore nil pointers")
184 }
185
186 m.Genre = stringPtr("Crime Thriller")
187 m.Recorder = New(squirrel.NewStmtCacheProxy(db), "mysql").Bind("movies", m)
188 if err := m.Update(); err != nil {
189 t.Fatalf("Failed Update: %s", err)
190 }
191
192 msql.loadFromSql(lastId, db)
193 if !CompareStringPtr(msql.Genre, stringPtr("Crime Thriller")) {
194 t.Log("msql.Genre: %v\n", *msql.Genre)
195 t.Fatal("Update should ignore nil pointers")
196 }
197}
198
199func getMoviesDb() *sql.DB {
200

Callers

nothing calls this directly

Calls 9

getMoviesDbFunction · 0.85
NewFunction · 0.85
CompareStringPtrFunction · 0.85
stringPtrFunction · 0.85
ExecMethod · 0.80
LastInsertIdMethod · 0.80
BindMethod · 0.65
UpdateMethod · 0.65
loadFromSqlMethod · 0.45

Tested by

no test coverage detected