-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres_test.go
170 lines (156 loc) · 5.04 KB
/
postgres_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
170
package testchado
import (
"bytes"
"regexp"
"testing"
)
func TestPostgresManager(t *testing.T) {
if !CheckPostgresEnv() {
t.Skip("postgres environment variable TC_DSOURCE is not set")
}
ds := GetDataSource()
dbm := NewPostgresManager(ds)
if dbm.dbsource != ds {
t.Errorf("should have %s dbsource\n", ds)
}
if dbm.Driver() != "postgres" {
t.Error("should have postgres driver")
}
if dbm.dbhandler == nil {
t.Error("should have dbhandler instance")
}
if dbm.Database() == "something" {
t.Error("should not have any database name")
}
if dbm.dbsource != dbm.DataSource() {
t.Error("should have identical datasource")
}
if _, err := regexp.MatchString(`^\w+$`, dbm.Schema); err != nil {
t.Errorf("should have matched schema %s name\n", dbm.Schema)
}
}
func TestPostgresSchemaPath(t *testing.T) {
if !CheckPostgresEnv() {
t.Skip("postgres environment variable TC_DSOURCE is not set")
}
ds := GetDataSource()
dbm := NewPostgresManager(ds)
content, err := dbm.SchemaDDL()
if err != nil {
t.Errorf("Should not throw any error: %s", err)
}
if !bytes.Contains(content.Bytes(), []byte("feature")) {
t.Error("should have contain feature")
}
}
func TestPostgresSchemaCRUD(t *testing.T) {
if !CheckPostgresEnv() {
t.Skip("postgres environment variable TC_DSOURCE is not set")
}
ds := GetDataSource()
dbm := NewPostgresManager(ds)
if err := dbm.DeploySchema(); err != nil {
t.Errorf("error %s: should have deployed the chado schema", err)
}
if !dbm.hasLoadedSchema {
t.Error("should have been set after schema deployment")
}
defer dbm.DropSchema()
sqlx := dbm.DBHandle()
type entries struct{ Counter int }
e := entries{}
err := sqlx.Get(&e, "SELECT count(table_name) counter FROM information_schema.tables WHERE table_schema = ($1)", dbm.Schema)
if err != nil {
t.Errorf("should have executed the query %s", err)
}
if e.Counter != 173 {
t.Error("should have 173 tables")
}
type tbls struct{ Tname string }
tbl := tbls{}
err = sqlx.Get(&tbl, "SELECT table_name tname FROM information_schema.tables WHERE table_schema = ($1) AND table_name = ($2)", dbm.Schema, "feature")
if err != nil {
t.Errorf("should have executed the query %s", err)
}
if tbl.Tname != "feature" {
t.Error("should have got feature table name")
}
if err = dbm.DropSchema(); err != nil {
t.Errorf("should have dropped the schema: %s", err)
}
err = sqlx.Get(&e, "SELECT count(table_name) counter FROM information_schema.tables WHERE table_schema = ($1)", dbm.Schema)
if err != nil {
t.Errorf("should have executed the query %s", err)
}
if e.Counter != 0 {
t.Error("should have 0 tables")
}
_ = dbm.DeploySchema()
if err = dbm.ResetSchema(); err != nil {
t.Errorf("should have reset the schema: %s", err)
}
if !dbm.hasLoadedSchema {
t.Error("should have been set after schema re-deployment")
}
}
func TestPostgresLoadDefaultFixture(t *testing.T) {
if !CheckPostgresEnv() {
t.Skip("postgres environment variable TC_DSOURCE is not set")
}
ds := GetDataSource()
dbm := NewPostgresManager(ds)
if err := dbm.LoadDefaultFixture(); err == nil {
t.Error("should have not loaded default fixture")
}
defer dbm.DropSchema()
_ = dbm.DeploySchema()
if err := dbm.LoadDefaultFixture(); err != nil {
t.Errorf("should have loaded fixture: %s", err)
}
type entries struct{ Counter int }
e := entries{}
sqlx := dbm.DBHandle()
err := sqlx.Get(&e, "SELECT count(*) counter FROM organism")
if err != nil {
t.Errorf("should have executed the query %s", err)
}
if e.Counter != 12 {
t.Error("should have 12 organisms")
}
query := `
SELECT count(cvterm.cvterm_id) counter from CVTERM join CV on CV.CV_ID=CVTERM.CV_ID
WHERE CV.NAME = 'sequence'
`
err = sqlx.Get(&e, query)
if err != nil {
t.Errorf("should have executed the query %s", err)
}
if e.Counter != 286 {
t.Error("should have 286 sequence ontology term")
}
}
func TestPostgresLoadPresetFixture(t *testing.T) {
if !CheckPostgresEnv() {
t.Skip("postgres environment variable TC_DSOURCE is not set")
}
ds := GetDataSource()
dbm := NewPostgresManager(ds)
if err := dbm.LoadPresetFixture("cvprop"); err == nil {
t.Error("should have not loaded preset fixture")
}
defer dbm.DropSchema()
_ = dbm.DeploySchema()
if err := dbm.LoadPresetFixture("cvprop"); err != nil {
t.Errorf("should have loaded fixture: %s", err)
}
type entries struct{ Counter int }
e := entries{}
sqlx := dbm.DBHandle()
err := sqlx.Get(&e, "SELECT count(*) counter FROM cvterm")
if err != nil {
t.Errorf("should have executed the query %s", err)
}
if e.Counter != 13 {
t.Error("should have 13 cvterms")
}
}