-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlx_db_test.go
149 lines (127 loc) · 3.28 KB
/
sqlx_db_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
package goquery
import (
_ "github.com/jackc/pgx/v4/stdlib"
)
/*
func sqlxsetup(t *testing.T) DataStore {
store := getSqlxStore(t)
err := Transaction(store, func(tx Tx) {
sqltx := tx.SqlXTx()
sql := `create table fishing_spots(
id serial not null primary key,
location text)`
sqltx.MustExec(sql)
inserts := []string{
`insert into fishing_spots (location) values ('Alpine Frove')`,
`insert into fishing_spots (location) values ('Rivertown')`,
`insert into fishing_spots (location) values ('Pine Island')`,
`insert into fishing_spots (location) values (null)`,
}
for _, v := range inserts {
sqltx.MustExec(v)
}
})
if err != nil {
t.Errorf("Setup error:%s\n", err)
}
return store
}
func sqlxteardown(store DataStore, t *testing.T) {
err := Transaction(store, func(tx Tx) {
sqltx := tx.SqlXTx()
sqltx.MustExec("drop table fishing_spots")
})
if err != nil {
t.Errorf("Failed to teardown test:%s\n", err)
}
}
func getSqlxStore(t *testing.T) DataStore {
config := RdbmsConfigFromEnv()
dialect, _ := getDialect("pgx")
db, err := NewSqlxConnection(config, "pgx")
if err != nil {
t.Errorf("Failed to connect to store:%s\n", err)
}
store := SqlDataStore{&db, dialect}
return &store
}
func TestSqlxConnection(t *testing.T) {
getSqlxStore(t)
}
func TestSqlxJson(t *testing.T) {
correctResult := `[{"id":1,"location":"Alpine Frove"},{"id":2,"location":"Rivertown"},{"id":3,"location":"Pine Island"},{"id":4,"location":null}]`
store := sqlxsetup(t)
defer sqlxteardown(store, t)
json, err := store.Select(nil).
Sql("select * from fishing_spots").
OmitNull(false).
FetchJSON()
if err != nil {
t.Errorf("Failed JSON Test: %s\n", err)
}
jsonstring := string(json)
if jsonstring != correctResult {
t.Errorf("Failed JSON Test: Got %s want %s", jsonstring, correctResult)
}
}
func TestSqlxCsv(t *testing.T) {
correctResult := `"id","location"
1,"Alpine Frove"
2,"Rivertown"
3,"Pine Island"
`
store := sqlxsetup(t)
defer sqlxteardown(store, t)
csv, err := store.Select(nil).
Sql("select * from fishing_spots").
FetchCSV()
if err != nil {
t.Errorf("Failed JSON Test: %s\n", err)
}
if csv != correctResult {
t.Errorf("Failed CSV Test: Got %s want %s", csv, correctResult)
}
}
type FishingSpot struct {
ID int32 `db:"id"`
Location *string `db:"location"`
}
func TestSqlxSlice(t *testing.T) {
ap := "Alpine Frove"
rt := "Rivertown"
pi := "Pine Island"
correctResult := []FishingSpot{
{1, &ap},
{2, &rt},
{3, &pi},
}
store := sqlxsetup(t)
defer sqlxteardown(store, t)
///////////autogenerate select///////////////////
fsTbl := TableImpl{
Name: "fishing_spots",
Fields: FishingSpot{},
}
res, err := store.Select(&fsTbl).FetchSlice()
if err != nil {
t.Errorf("Failed Slice Test:%s\n", err)
}
if reflect.DeepEqual(res, correctResult) {
t.Errorf("Failed Slice Test: Got %v want %v", res, correctResult)
}
/////////////////add select statement///////////
stmts := map[string]string{
"named-select": `select * from fishing_spots`,
}
fsTbl.Statements = stmts
res, err = store.Select(&fsTbl).
StatementKey("named-select").
FetchSlice()
if err != nil {
t.Errorf("Failed Slice Test:%s\n", err)
}
if reflect.DeepEqual(res, correctResult) {
t.Errorf("Failed Slice Test: Got %v want %v", res, correctResult)
}
}
*/