Skip to content

Commit 27c2eb2

Browse files
committed
attempt to recreate issue 237
1 parent bdae0c3 commit 27c2eb2

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

sqlx_test.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ CREATE TABLE person (
116116
first_name text,
117117
last_name text,
118118
email text,
119-
added_at timestamp default now()
119+
added_at timestamp default now() NOT NULL
120120
);
121121
122122
CREATE TABLE place (
@@ -287,6 +287,45 @@ func TestMissingNames(t *testing.T) {
287287
t.Error("Expected missing name Get to fail, but it did not.")
288288
}
289289

290+
// test Get with an invalid insertion
291+
switch db.DriverName() {
292+
case "postgres":
293+
stmt, err := db.PrepareNamed(`INSERT INTO person (first_name, last_name, added_at)
294+
VALUES (:first, :last, :added) RETURNING added_at;`)
295+
if err != nil {
296+
t.Fatal(err)
297+
}
298+
var addedAt time.Time
299+
var invalidData = map[string]interface{}{
300+
"first": "first",
301+
"last": "last",
302+
"added": nil,
303+
}
304+
var ppl int
305+
db.Get(&ppl, "SELECT count(*) FROM person;")
306+
if ppl != 2 {
307+
t.Errorf("expected 2 people but got %d", ppl)
308+
}
309+
_, err = stmt.Exec(invalidData)
310+
if err == nil {
311+
t.Error("expected an error but got nil on Exec")
312+
}
313+
fmt.Println(err)
314+
db.Get(&ppl, "SELECT count(*) FROM person;")
315+
if ppl != 2 {
316+
t.Errorf("expected 2 people but got %d", ppl)
317+
}
318+
err = stmt.Get(&addedAt, invalidData)
319+
if err == nil {
320+
t.Fatal("expected an error but got nil")
321+
}
322+
fmt.Println(err)
323+
db.Get(&ppl, "SELECT count(*) FROM person;")
324+
if ppl != 2 {
325+
t.Errorf("expected 2 people but got %d", ppl)
326+
}
327+
}
328+
290329
// test naked StructScan
291330
pps = []PersonPlus{}
292331
rows, err := db.Query("SELECT * FROM person LIMIT 1")

0 commit comments

Comments
 (0)