-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.repo.bolt_test.go
258 lines (220 loc) · 6.81 KB
/
tests.repo.bolt_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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"context"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
// NewStore returns a new instance of Store in a temporary path.
func newTestBoltStore() (*boltBookStorage, error) {
f, err := os.CreateTemp("", "tmp.bolt.db-")
if err != nil {
return nil, err
}
f.Close()
testConfig := &Config{
BoltDB: BoltDBConfig{
FilePath: f.Name(),
Timeout: 5 * time.Second,
BucketName: "test.books",
},
}
client, err := GetBoltDBClient(testConfig)
return &boltBookStorage{
logger: zap.NewNop(),
client: client,
config: &testConfig.BoltDB,
}, err
}
// Close closes the temporary bolt store and removes the underlying data file.
func (bs *boltBookStorage) closeTestBoltStore() error {
defer os.Remove(bs.config.FilePath)
return bs.Close()
}
// Ensure concrete type boltBookStorage satisfies BookStorage interface.
func TestBoltBookStorageImplementsBookStorageInterface(t *testing.T) {
var i interface{} = new(boltBookStorage)
if _, ok := i.(BookStorage); !ok {
t.Fatalf("expected %T to implement BookStorage", i)
}
}
// Ensure bolt store can insert a new book.
func TestBoltStore_AddBook(t *testing.T) {
bs, err := newTestBoltStore()
require.NoError(t, err, "failed in creating a test bolt store")
defer func() {
err = bs.closeTestBoltStore()
assert.NoError(t, err)
}()
testBookID := "b:0"
// Create a new book.
b := Book{ID: testBookID, Title: "Bolt test book title"}
err = bs.Add(context.TODO(), testBookID, b)
require.NoError(t, err)
// Verify book can be retrieved.
book, err := bs.GetOne(context.TODO(), testBookID)
require.NoError(t, err)
assert.Equal(t, testBookID, book.ID)
assert.Equal(t, "Bolt test book title", book.Title)
}
// Ensure bolt store returns exact book details if exist.
func TestBoltStore_GetOneBook_FoundBook(t *testing.T) {
bs, err := newTestBoltStore()
require.NoError(t, err, "failed in creating a test bolt store")
defer func() {
err = bs.closeTestBoltStore()
assert.NoError(t, err)
}()
testBookID := "b:0"
// Create a new book.
b := Book{
ID: testBookID,
Title: "Bolt test book title",
Description: "Bolt test book desc",
Author: "Jerome Amon",
Price: "10$",
CreatedAt: "2023-04-26 21:42:10.7604632 +0000 UTC",
UpdatedAt: "2023-04-26 21:42:10.7604632 +0000 UTC",
}
err = bs.Add(context.TODO(), testBookID, b)
require.NoError(t, err)
// Verify book does exist.
book, err := bs.GetOne(context.TODO(), testBookID)
require.NoError(t, err)
assert.Equal(t, b, book)
}
// Ensure bolt store returns an error if book does not exist.
func TestBoltStore_GetOneBook_ErrBookNotFound(t *testing.T) {
bs, err := newTestBoltStore()
require.NoError(t, err, "failed in creating a test bolt store")
defer func() {
err = bs.closeTestBoltStore()
assert.NoError(t, err)
}()
testBookID := "b:0"
// Create a new book.
b := Book{ID: testBookID, Title: "Bolt test book title"}
err = bs.Add(context.TODO(), testBookID, b)
require.NoError(t, err)
// Verify another book does not exist.
book, err := bs.GetOne(context.TODO(), "b:1")
assert.Equal(t, ErrBookNotFound, err)
assert.Equal(t, Book{}, book)
}
// Ensure bolt store can remove a book.
func TestBoltStore_DeleteBook(t *testing.T) {
bs, err := newTestBoltStore()
require.NoError(t, err, "failed in creating a test bolt store")
defer func() {
err = bs.closeTestBoltStore()
assert.NoError(t, err)
}()
testBookID := "b:0"
// Create a new book.
b := Book{
ID: testBookID,
Title: "Bolt test book title",
Description: "Bolt test book desc",
Author: "Jerome Amon",
Price: "10$",
CreatedAt: "2023-04-26 21:42:10.7604632 +0000 UTC",
UpdatedAt: "2023-04-26 21:42:10.7604632 +0000 UTC",
}
err = bs.Add(context.TODO(), testBookID, b)
require.NoError(t, err)
// Delete the book.
err = bs.Delete(context.TODO(), testBookID)
require.NoError(t, err)
// Verify book does not exist.
book, err := bs.GetOne(context.TODO(), testBookID)
assert.Equal(t, ErrBookNotFound, err)
assert.Equal(t, Book{}, book)
}
// Ensure bolt store can retrieve multiple books.
func TestBoltStore_GetAllBooks(t *testing.T) {
bs, err := newTestBoltStore()
require.NoError(t, err, "failed in creating a test bolt store")
defer func() {
err = bs.closeTestBoltStore()
assert.NoError(t, err)
}()
testBook0ID := "b:0"
testBook1ID := "b:1"
// Create some new books.
b0 := Book{ID: testBook0ID, Title: "Bolt test book 0 title"}
err = bs.Add(context.TODO(), testBook0ID, b0)
require.NoError(t, err)
b1 := Book{ID: testBook1ID, Title: "Bolt test book 1 title"}
err = bs.Add(context.TODO(), testBook1ID, b1)
require.NoError(t, err)
// Verify books can be retrieved.
books, err := bs.GetAll(context.TODO())
require.NoError(t, err)
assert.ElementsMatch(t, books, []Book{b0, b1})
}
// Ensure bolt store can update an existing book details.
func TestBoltStore_UpdateBook_ExistingBook(t *testing.T) {
bs, err := newTestBoltStore()
require.NoError(t, err, "failed in creating a test bolt store")
defer func() {
err = bs.closeTestBoltStore()
assert.NoError(t, err)
}()
testBookID := "b:0"
// Create a new book.
b := Book{
ID: testBookID,
Title: "Bolt test book title",
Description: "Bolt test book desc",
Author: "Jerome Amon",
Price: "10$",
CreatedAt: "2023-04-26 21:42:10.7604632 +0000 UTC",
UpdatedAt: "2023-04-26 21:42:10.7604632 +0000 UTC",
}
err = bs.Add(context.TODO(), testBookID, b)
require.NoError(t, err)
// Modify existing book details and update.
newBook := b
newBook.Title = "Bolt test book new title"
newBook.Price = "20$"
newBook.UpdatedAt = time.Now().UTC().String()
book, err := bs.Update(context.TODO(), testBookID, newBook)
require.NoError(t, err)
assert.Equal(t, book, newBook)
// Check if book was updated.
book, err = bs.GetOne(context.TODO(), testBookID)
require.NoError(t, err)
assert.Equal(t, book, newBook)
}
// Ensure bolt store inserts book during update operation if book
// does not exist.
func TestBoltStore_UpdateBook_NotExistingBook(t *testing.T) {
bs, err := newTestBoltStore()
require.NoError(t, err, "failed in creating a test bolt store")
defer func() {
err = bs.closeTestBoltStore()
assert.NoError(t, err)
}()
testBookID := "b:0"
// Use update operation to add book.
b := Book{
ID: testBookID,
Title: "Bolt test book title",
Description: "Bolt test book desc",
Author: "Jerome Amon",
Price: "10$",
CreatedAt: "2023-04-26 21:42:10.7604632 +0000 UTC",
UpdatedAt: time.Now().UTC().String(),
}
// Modify existing book details and update.
book, err := bs.Update(context.TODO(), testBookID, b)
require.NoError(t, err)
assert.Equal(t, b, book)
// Check if book was added.
book, err = bs.GetOne(context.TODO(), testBookID)
require.NoError(t, err)
assert.Equal(t, b, book)
}