-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockdb_test.go
185 lines (163 loc) · 4.34 KB
/
mockdb_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package testutils
import (
"reflect"
"testing"
)
type TestModel struct {
ID int `pg:"id" json:"id"`
Name string `pg:"name" json:"name"`
}
func (tm *TestModel) GetID() string {
return string((*tm).ID)
}
func (tm *TestModel) Equals(i interface{}) bool {
b, ok := i.(*TestModel)
if !ok {
return false
}
return tm.ID == b.ID && tm.Name == b.Name
}
func TestMockDB(t *testing.T) {
t.Run("Implements DB interface", func(t *testing.T) {
var db interface{}
db = NewMockDB()
if _, ok := db.(DB); !ok {
t.Fatal("MockDB does not of interface type DB")
}
})
t.Run("QueueResponses", func(t *testing.T) {
db := &MockDB{}
tm := &TestModel{ID: 1, Name: "Test Model"}
db.QueueResponses(tm)
n := len(db.responses)
if n != 1 {
t.Fatal("expected 1 response in queue; found ", n)
}
eT := reflect.TypeOf(tm)
rT := reflect.TypeOf(db.responses[0])
if rT != eT {
t.Fatal("expected response in queue of type ", eT.String(), "; found ", rT.String())
}
if db.responses[0] != tm {
t.Fatalf("response in queue (%v) does not match expected (%v)", db.responses[0], tm)
}
})
t.Run("QueueModels", func(t *testing.T) {
db := &MockDB{}
tm := &TestModel{ID: 1, Name: "Test Model"}
db.QueueModels(tm)
n := len(db.models)
if n != 1 {
t.Fatal("expected 1 model in queue; found ", n)
}
eT := reflect.TypeOf(tm)
rT := reflect.TypeOf(db.models[0])
if rT != eT {
t.Fatal("expected model in queue of type ", eT.String(), "; found ", rT.String())
}
if db.models[0].GetID() != tm.GetID() {
t.Fatalf("model in queue (%v) does not match expected (%v)", db.models[0], tm)
}
})
t.Run("Query", func(t *testing.T) {
tm := &TestModel{ID: 1, Name: "Test Model"}
emptyModel := &TestModel{}
db := &MockDB{responses: []interface{}{*tm}}
response := &TestModel{}
_, err := db.Query(response, "SELECT whatever FROM fake_table")
if err != nil {
t.Fatal(err)
}
if response.Equals(emptyModel) {
t.Fatal("Returned TestModel is empty")
}
if !response.Equals(tm) {
t.Fatal("response struct doesn't match queued response")
}
})
t.Run("QueryOne", func(t *testing.T) {
tm := &TestModel{ID: 1, Name: "Test Model"}
emptyModel := &TestModel{}
db := &MockDB{responses: []interface{}{*tm}}
response := &TestModel{}
_, err := db.Query(response, "SELECT whatever FROM fake_table")
if err != nil {
t.Fatal(err)
}
if response.Equals(emptyModel) {
t.Fatal("Returned TestModel is empty")
}
if !response.Equals(tm) {
t.Fatal("response struct doesn't match queued response")
}
})
t.Run("Select", func(t *testing.T) {
tm := &TestModel{ID: 1, Name: "Test Model"}
emptyModel := &TestModel{}
db := &MockDB{responses: []interface{}{*tm}}
response := &TestModel{}
err := db.Select(response)
if err != nil {
t.Fatal(err)
}
if response.Equals(emptyModel) {
t.Fatal("Returned TestModel is empty")
}
if !response.Equals(tm) {
t.Fatal("response struct doesn't match queued response")
}
})
t.Run("Insert", func(t *testing.T) {
tm := &TestModel{ID: 1, Name: "Test Model"}
db := &MockDB{}
if err := db.Insert(tm); err != nil {
t.Fatal(err)
}
n := len(db.models)
if n != 1 {
t.Fatal("expected 1 model in queue; found ", n)
}
eT := reflect.TypeOf(tm)
rT := reflect.TypeOf(db.models[0])
if rT != eT {
t.Fatal("expected model in queue of type ", eT.String(), "; found ", rT.String())
}
iM := db.models[0]
if !iM.Equals(tm) {
t.Fatalf("MockDB model (%v) does not match expected (%v)", iM, tm)
}
})
t.Run("Update", func(t *testing.T) {
tm := &TestModel{ID: 1, Name: "Test Model"}
db := &MockDB{models: []Model{tm}}
tm2 := &TestModel{ID: 1, Name: "Updated Model"}
if err := db.Update(tm2); err != nil {
t.Fatal(err)
}
um := db.models[0]
if !um.Equals(tm2) {
t.Fatalf("MockDB model (%v) does not match expected (%v)", *um.(*TestModel), *tm)
}
})
t.Run("Delete", func(t *testing.T) {
tm := &TestModel{ID: 1, Name: "Test Model"}
db := &MockDB{models: []Model{tm}}
if err := db.Delete(tm); err != nil {
t.Fatal(err)
}
if len(db.models) != 0 {
t.Fatal("MockDB.models should be empty")
}
})
t.Run("Find", func(t *testing.T) {
tm := &TestModel{ID: 1, Name: "Test Model"}
db := &MockDB{models: []Model{tm}}
fm, err := db.Find(tm)
if err != nil {
t.Fatal(err)
}
if !fm.Equals(tm) {
t.Fatal("Did not find test model")
}
})
}