-
Notifications
You must be signed in to change notification settings - Fork 27
/
sqlite_test.go
107 lines (90 loc) · 2.23 KB
/
sqlite_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
package godb_test
import (
"os"
"testing"
"github.com/samonzeweb/godb"
"github.com/samonzeweb/godb/adapters/sqlite"
"github.com/samonzeweb/godb/dbtests/common"
. "github.com/smartystreets/goconvey/convey"
)
const sqlite3testdb = "sqlite3-test.db"
func fixturesSetupSQLite(t *testing.T) (*godb.DB, func()) {
removeDBIfExists(t)
db, err := godb.Open(sqlite.Adapter, sqlite3testdb)
if err != nil {
t.Fatal(err)
}
// Enable logger if needed
//db.SetLogger(log.New(os.Stderr, "", 0))
createTable :=
`create table books (
id integer not null primary key autoincrement,
title text not null,
author text not null,
published date not null,
version int not null default 0);
create table inventories (
id integer not null primary key autoincrement,
book_id int not null,
last_inventory date not null,
counting int not null default 0);
`
_, err = db.CurrentDB().Exec(createTable)
if err != nil {
t.Fatal(err)
}
fixturesTeardown := func() {
dropTable := "drop table if exists books; drop table if exists inventories;"
_, err := db.CurrentDB().Exec(dropTable)
if err != nil {
t.Fatal(err)
}
err = db.Close()
if err != nil {
t.Fatal(err)
}
removeDBIfExists(t)
}
return db, fixturesTeardown
}
func removeDBIfExists(t *testing.T) {
_, err := os.Stat(sqlite3testdb)
if err != nil {
if !os.IsNotExist(err) {
t.Fatal(err)
}
// no db file
return
}
err = os.Remove(sqlite3testdb)
if err != nil {
t.Fatal(err)
}
}
func TestStatementsSQLite(t *testing.T) {
Convey("A DB for a SQLite database", t, func() {
db, teardown := fixturesSetupSQLite(t)
defer teardown()
Convey("The common statements tests must pass", func() {
common.StatementsTests(db, t)
})
})
}
func TestStructsSQLite(t *testing.T) {
Convey("A DB for a SQLite database", t, func() {
db, teardown := fixturesSetupSQLite(t)
defer teardown()
Convey("The common structs tests must pass", func() {
common.StructsTests(db, t)
})
})
}
func TestRawSQLite(t *testing.T) {
Convey("A DB for a SQLite database", t, func() {
db, teardown := fixturesSetupSQLite(t)
defer teardown()
Convey("The common raw tests must pass", func() {
common.RawSQLTests(db, t)
})
})
}