-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_test.go
238 lines (203 loc) · 6.09 KB
/
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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
Copyright © 2023 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package api
import (
//"github.com/alecthomas/repr"
"github.com/maxatome/go-testdeep/td"
"github.com/tlinden/ephemerup/cfg"
"github.com/tlinden/ephemerup/common"
"os"
"testing"
"time"
)
func finalize(db *Db) {
if db.bolt != nil {
db.Close()
}
if _, err := os.Stat(db.cfg.DbFile); err == nil {
os.Remove(db.cfg.DbFile)
}
}
func TestNew(t *testing.T) {
var tests = []struct {
name string
dbfile string
wantfail bool
}{
{"opennew", "test.db", false},
{"openfail", "/hopefully/not/existing/directory/test.db", true},
}
for _, tt := range tests {
c := &cfg.Config{DbFile: tt.dbfile}
t.Run(tt.name, func(t *testing.T) {
db, err := NewDb(c)
defer finalize(db)
if err != nil && !tt.wantfail {
t.Errorf("expected: &Db{}, got err: " + err.Error())
}
if err == nil && tt.wantfail {
t.Errorf("expected: fail, got &Db{}")
}
})
}
}
const timeformat string = "2006-01-02T15:04:05.000Z"
var dbtests = []struct {
name string
dbfile string
wantfail bool
id string
context string
ts string
filter string
query string
upload common.Upload
form common.Form
}{
{
"upload", "test.db", false, "1", "foo",
"2023-03-10T11:45:00.000Z", "", "",
common.Upload{
Id: "1", Expire: "asap", File: "none", Context: "foo",
Created: common.Timestamp{}, Type: common.TypeUpload},
common.Form{},
},
{
"form", "test.db", false, "2", "foo",
"2023-03-10T11:45:00.000Z", "", "",
common.Upload{},
common.Form{
Id: "1", Expire: "asap", Description: "none", Context: "foo",
Created: common.Timestamp{}, Type: common.TypeForm},
},
}
/*
We need to test the whole Db operation in one run, because it
doesn't work well if using a global Db.
*/
func TestDboperation(t *testing.T) {
for _, tt := range dbtests {
c := &cfg.Config{DbFile: tt.dbfile}
t.Run(tt.name, func(t *testing.T) {
// create new bbolt db
db, err := NewDb(c)
defer finalize(db)
if err != nil {
t.Errorf("Could not open new DB: " + err.Error())
}
if tt.upload.Id != "" {
// set ts
ts, err := time.Parse(timeformat, tt.ts)
if err != nil {
t.Errorf("Could not parse time: " + err.Error())
}
tt.upload.Created = common.Timestamp{Time: ts}
// create new upload db object
err = db.Insert(tt.id, tt.upload)
if err != nil {
t.Errorf("Could not insert new upload object: " + err.Error())
}
// fetch it
response, err := db.Get(tt.context, tt.id, common.TypeUpload)
if err != nil {
t.Errorf("Could not fetch upload object: " + err.Error())
}
// is it there?
if len(response.Uploads) != 1 {
t.Errorf("db.Get() did not return an upload obj")
}
// compare times
if !tt.upload.Created.Time.Equal(response.Uploads[0].Created.Time) {
t.Errorf("Timestamps don't match!\ngot: %s\nexp: %s\n",
response.Uploads[0].Created, tt.upload.Created)
}
// equal them artificially, because otherwise td will
// fail because of time.Time.wall+ext, or TZ is missing
response.Uploads[0].Created = tt.upload.Created
// compare
td.Cmp(t, response.Uploads[0], &tt.upload, tt.name)
// fetch list
response, err = db.List(tt.context, tt.filter, tt.query, common.TypeUpload)
if err != nil {
t.Errorf("Could not fetch uploads list: " + err.Error())
}
// is it there?
if len(response.Uploads) != 1 {
t.Errorf("db.List() did not return upload obj[s]")
}
// delete
err = db.Delete(tt.context, tt.id)
if err != nil {
t.Errorf("Could not delete upload obj: " + err.Error())
}
// fetch again, shall return empty
_, err = db.Get(tt.context, tt.id, common.TypeUpload)
if err == nil {
t.Errorf("Could fetch upload object again although we deleted it")
}
}
if tt.form.Id != "" {
// set ts
ts, err := time.Parse(timeformat, tt.ts)
if err != nil {
t.Errorf("Could not parse time: " + err.Error())
}
tt.form.Created = common.Timestamp{Time: ts}
// create new form db object
err = db.Insert(tt.id, tt.form)
if err != nil {
t.Errorf("Could not insert new form object: " + err.Error())
}
// fetch it
response, err := db.Get(tt.context, tt.id, common.TypeForm)
if err != nil {
t.Errorf("Could not fetch form object: " + err.Error())
}
// is it there?
if len(response.Forms) != 1 {
t.Errorf("db.Get() did not return an form obj")
}
// compare times
if !tt.form.Created.Time.Equal(response.Forms[0].Created.Time) {
t.Errorf("Timestamps don't match!\ngot: %s\nexp: %s\n",
response.Forms[0].Created, tt.form.Created)
}
// equal them artificially, because otherwise td will
// fail because of time.Time.wall+ext, or TZ is missing
response.Forms[0].Created = tt.form.Created
// compare
td.Cmp(t, response.Forms[0], &tt.form, tt.name)
// fetch list
response, err = db.List(tt.context, tt.filter, tt.query, common.TypeForm)
if err != nil {
t.Errorf("Could not fetch forms list: " + err.Error())
}
// is it there?
if len(response.Forms) != 1 {
t.Errorf("db.FormsList() did not return form obj[s]")
}
// delete
err = db.Delete(tt.context, tt.id)
if err != nil {
t.Errorf("Could not delete form obj: " + err.Error())
}
// fetch again, shall return empty
_, err = db.Get(tt.context, tt.id, common.TypeForm)
if err == nil {
t.Errorf("Could fetch form object again although we deleted it")
}
}
})
}
}