forked from sdomino/scribble
-
Notifications
You must be signed in to change notification settings - Fork 29
/
scribble_test.go
205 lines (155 loc) · 3.58 KB
/
scribble_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package scribble
import (
"os"
"testing"
)
//
type Fish struct {
Type string `json:"type"`
}
//
var (
db *Driver
database = "./deep/school"
collection = "fish"
onefish = Fish{}
twofish = Fish{}
redfish = Fish{Type: "red"}
bluefish = Fish{Type: "blue"}
)
//
func TestMain(m *testing.M) {
// remove any thing for a potentially failed previous test
os.RemoveAll("./deep")
// run
code := m.Run()
// cleanup
os.RemoveAll("./deep")
// exit
os.Exit(code)
}
// Tests creating a new database, and using an existing database
func TestNew(t *testing.T) {
// database should not exist
if _, err := os.Stat(database); err == nil {
t.Error("Expected nothing, got database")
}
// create a new database
createDB()
// database should exist
if _, err := os.Stat(database); err != nil {
t.Error("Expected database, got nothing")
}
// should use existing database
createDB()
// database should exist
if _, err := os.Stat(database); err != nil {
t.Error("Expected database, got nothing")
}
}
//
func TestWriteAndRead(t *testing.T) {
createDB()
// add fish to database
if err := db.Write(collection, "redfish", redfish); err != nil {
t.Error("Create fish failed: ", err.Error())
}
// read fish from database
if err := db.Read(collection, "redfish", &onefish); err != nil {
t.Error("Failed to read: ", err.Error())
}
//
if onefish.Type != "red" {
t.Error("Expected red fish, got: ", onefish.Type)
}
destroySchool()
}
//
func TestReadall(t *testing.T) {
createDB()
createSchool()
fish, err := db.ReadAll(collection)
if err != nil {
t.Error("Failed to read: ", err.Error())
}
if len(fish) <= 0 {
t.Error("Expected some fish, have none")
}
destroySchool()
}
//
func TestWriteAndReadEmpty(t *testing.T) {
createDB()
// create a fish with no home
if err := db.Write("", "redfish", redfish); err == nil {
t.Error("Allowed write of empty resource", err.Error())
}
// create a home with no fish
if err := db.Write(collection, "", redfish); err == nil {
t.Error("Allowed write of empty resource", err.Error())
}
// no place to read
if err := db.Read("", "redfish", onefish); err == nil {
t.Error("Allowed read of empty resource", err.Error())
}
destroySchool()
}
//
func TestDelete(t *testing.T) {
createDB()
// add fish to database
if err := db.Write(collection, "redfish", redfish); err != nil {
t.Error("Create fish failed: ", err.Error())
}
// delete the fish
if err := db.Delete(collection, "redfish"); err != nil {
t.Error("Failed to delete: ", err.Error())
}
// read fish from database
if err := db.Read(collection, "redfish", &onefish); err == nil {
t.Error("Expected nothing, got fish")
}
destroySchool()
}
//
func TestDeleteall(t *testing.T) {
createDB()
createSchool()
if err := db.Delete(collection, ""); err != nil {
t.Error("Failed to delete: ", err.Error())
}
if _, err := os.Stat(collection); err == nil {
t.Error("Expected nothing, have fish")
}
destroySchool()
}
//
// create a new scribble database
func createDB() error {
var err error
if db, err = New(database, nil); err != nil {
return err
}
return nil
}
// create a fish
func createFish(fish Fish) error {
return db.Write(collection, fish.Type, fish)
}
// create many fish
func createSchool() error {
for _, f := range []Fish{{Type: "red"}, {Type: "blue"}} {
if err := db.Write(collection, f.Type, f); err != nil {
return err
}
}
return nil
}
// destroy a fish
func destroyFish(name string) error {
return db.Delete(collection, name)
}
// destroy all fish
func destroySchool() error {
return db.Delete(collection, "")
}