forked from gocraft/dbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbr_test.go
169 lines (145 loc) · 3.86 KB
/
dbr_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
package dbr
import (
"log"
"os"
"testing"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"github.com/stretchr/testify/assert"
)
//
// Test helpers
//
var (
currID int64 = 256
)
// create id
func nextID() int64 {
currID++
return currID
}
const (
mysqlDSN = "root@unix(/tmp/mysql.sock)/uservoice_test?charset=utf8"
postgresDSN = "postgres://postgres@localhost:5432/uservoice_test?sslmode=disable"
)
func createSession(driver, dsn string) *Session {
var testDSN string
switch driver {
case "mysql":
testDSN = os.Getenv("DBR_TEST_MYSQL_DSN")
case "postgres":
testDSN = os.Getenv("DBR_TEST_POSTGRES_DSN")
}
if testDSN != "" {
dsn = testDSN
}
conn, err := Open(driver, dsn, nil)
if err != nil {
log.Fatal(err)
}
reset(conn)
sess := conn.NewSession(nil)
return sess
}
var (
mysqlSession = createSession("mysql", mysqlDSN)
postgresSession = createSession("postgres", postgresDSN)
// all test sessions should be here
testSession = []*Session{mysqlSession, postgresSession}
)
type dbrPerson struct {
Id int64
Name string
Email string
}
type nullTypedRecord struct {
Id int64
StringVal NullString
Int64Val NullInt64
Float64Val NullFloat64
TimeVal NullTime
BoolVal NullBool
}
func reset(conn *Connection) {
// serial = BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE
// the following sql should work for both mysql and postgres
for _, v := range []string{
`DROP TABLE IF EXISTS dbr_people`,
`CREATE TABLE dbr_people (
id serial PRIMARY KEY,
name varchar(255) NOT NULL,
email varchar(255)
)`,
`DROP TABLE IF EXISTS null_types`,
`CREATE TABLE null_types (
id serial PRIMARY KEY,
string_val varchar(255) NULL,
int64_val integer NULL,
float64_val float NULL,
time_val timestamp NULL ,
bool_val bool NULL
)`,
} {
_, err := conn.Exec(v)
if err != nil {
log.Fatalf("Failed to execute statement: %s, Got error: %s", v, err)
}
}
}
func TestBasicCRUD(t *testing.T) {
jonathan := dbrPerson{
Name: "jonathan",
Email: "[email protected]",
}
dmitri := dbrPerson{
Name: "dmitri",
Email: "[email protected]",
}
for _, sess := range testSession {
if sess == postgresSession {
jonathan.Id = nextID()
}
// insert
result, err := sess.InsertInto("dbr_people").Columns("id", "name", "email").Record(&jonathan).Record(dmitri).Exec()
assert.NoError(t, err)
rowsAffected, err := result.RowsAffected()
assert.NoError(t, err)
assert.EqualValues(t, 2, rowsAffected)
assert.True(t, jonathan.Id > 0)
// select
var people []dbrPerson
count, err := sess.Select("*").From("dbr_people").Where(Eq("id", jonathan.Id)).LoadStructs(&people)
assert.NoError(t, err)
assert.Equal(t, 1, count)
assert.Equal(t, jonathan.Id, people[0].Id)
assert.Equal(t, jonathan.Name, people[0].Name)
assert.Equal(t, jonathan.Email, people[0].Email)
// select id
ids, err := sess.Select("id").From("dbr_people").ReturnInt64s()
assert.NoError(t, err)
assert.Equal(t, 2, len(ids))
// select id limit
ids, err = sess.Select("id").From("dbr_people").Limit(1).ReturnInt64s()
assert.NoError(t, err)
assert.Equal(t, 1, len(ids))
// update
result, err = sess.Update("dbr_people").Where(Eq("id", jonathan.Id)).Set("name", "jonathan1").Exec()
assert.NoError(t, err)
rowsAffected, err = result.RowsAffected()
assert.NoError(t, err)
assert.EqualValues(t, 1, rowsAffected)
var n NullInt64
sess.Select("count(*)").From("dbr_people").Where("name = ?", "jonathan1").LoadValue(&n)
assert.EqualValues(t, 1, n.Int64)
// delete
result, err = sess.DeleteFrom("dbr_people").Where(Eq("id", jonathan.Id)).Exec()
assert.NoError(t, err)
rowsAffected, err = result.RowsAffected()
assert.NoError(t, err)
assert.EqualValues(t, 1, rowsAffected)
// select id
ids, err = sess.Select("id").From("dbr_people").ReturnInt64s()
assert.NoError(t, err)
assert.Equal(t, 1, len(ids))
}
}