-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_test.go
56 lines (46 loc) · 1.38 KB
/
insert_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
package azamat
import (
"testing"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInsertRun(t *testing.T) {
db, _ := sqlx.Open("sqlite3", ":memory:")
type Todo struct {
ID int
Title string
}
db.MustExec(`CREATE TABLE todos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL
)`)
// When creating a single entry...
todo1 := "buy bear food"
insert := Insert("todos").Columns("title").Values(todo1)
result, err := insert.Run(db)
todoID, _ := result.LastInsertId()
require.NoError(t, err)
require.NotZero(t, todoID)
// Make sure entry was actually inserted
var rows []Todo
err = db.Select(&rows, "SELECT id, title FROM todos WHERE id = ?", todoID)
require.NoError(t, err)
assert.Len(t, rows, 1)
// When creating multiple entries...
todo2, todo3 := "buy Korky Buchek album", "fuel van"
insert = Insert("todos").Columns("title").Values(todo2).Values(todo3)
result, err = insert.Run(db)
todoID, _ = result.LastInsertId()
require.NoError(t, err)
require.NotZero(t, todoID)
// Make sure entries were actually inserted
rows = nil
err = db.Select(&rows, "SELECT id, title FROM todos WHERE id = ?", todoID)
require.NoError(t, err)
assert.Len(t, rows, 1)
rows = nil
err = db.Select(&rows, "SELECT id, title FROM todos WHERE id = ?", todoID-1)
require.NoError(t, err)
assert.Len(t, rows, 1)
}