forked from qingyunha/boltdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bucket.py
200 lines (167 loc) · 6.51 KB
/
test_bucket.py
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
import os
import unittest
import tempfile
from boltdb import BoltDB
class TestBucket(unittest.TestCase):
def setUp(self):
self.db = BoltDB(tempfile.mktemp())
def tearDown(self):
os.unlink(self.db.filename)
def test_get_nonexistent(self):
with self.db.update() as tx:
b = tx.bucket()
v = b.get(b"foo")
self.assertIsNone(v)
def test_get_from_node(self):
with self.db.update() as tx:
b = tx.bucket()
b.put(b"foo", b"bar")
v = b.get(b"foo")
self.assertEqual(v, b"bar")
def test_get_bucket_is_none(self):
with self.db.update() as tx:
tx.create_bucket(b"widgets")
tx.bucket(b"widgets").create_bucket(b"foo")
self.assertIsNone(tx.bucket(b"widgets").get(b"foo"))
def test_put(self):
with self.db.update() as tx:
b = tx.create_bucket(b"widgets")
b.put(b"foo", b"bar")
v = tx.bucket(b"widgets").get(b"foo")
self.assertEqual(v, b"bar")
def test_put_repeat(self):
with self.db.update() as tx:
b = tx.create_bucket(b"widgets")
b.put(b"foo", b"bar")
b.put(b"foo", b"baz")
v = tx.bucket(b"widgets").get(b"foo")
self.assertEqual(v, b"baz")
def test_put_large(self):
count, factor = 100, 200
with self.db.update() as tx:
b = tx.create_bucket(b"widgets")
for i in range(1, count):
b.put(b"0"*i*factor, b"X"*(count-1)*factor)
with self.db.view() as tx:
b = tx.bucket(b"widgets")
for i in range(1, count):
v = b.get(b"0"*i*factor)
self.assertEqual(v, b"X"*(count-1)*factor)
def test_put_incompatible(self):
with self.db.update() as tx:
b = tx.create_bucket(b"widgets")
tx.bucket(b"widgets").create_bucket(b"foo")
with self.assertRaisesRegex(Exception, "cannot write sub bucket"):
b.put(b"foo", b"bar")
def test_iter(self):
orderd_tyes = b"abcdefghijklmnopqrstuvwxyz"
with self.db.update() as tx:
b = tx.create_bucket(b"widgets")
for i in range(len(orderd_tyes)):
b.put(orderd_tyes[i:], b"foo")
with self.db.view() as tx:
b = tx.bucket(b"widgets")
for i, (k, _) in enumerate(b):
self.assertEqual(k, orderd_tyes[i:])
self.assertEqual(i, len(orderd_tyes)-1)
def test_delete(self):
with self.db.update() as tx:
b = tx.create_bucket(b"widgets")
b.put(b"foo", b"bar")
b.delete(b"foo")
v = tx.bucket(b"widgets").get(b"foo")
self.assertIsNone(v)
def test_delete_large(self):
count = 100
with self.db.update() as tx:
b = tx.create_bucket(b"widgets")
for i in range(count):
b.put(str(i).encode(), b"*" * 1024)
with self.db.update() as tx:
b = tx.bucket(b"widgets")
for i in range(count):
b.delete(str(i).encode())
with self.db.view() as tx:
b = tx.bucket(b"widgets")
for i in range(count):
v = b.get(str(i).encode())
self.assertIsNone(v)
def test_delete_nonexisting(self):
with self.db.update() as tx:
b = tx.create_bucket(b"widgets")
b.create_bucket(b"nested")
with self.db.update() as tx:
b = tx.bucket(b"widgets")
b.delete(b"foo")
self.assertIsNotNone(b.bucket(b"nested"))
def test_nested(self):
with self.db.update() as tx:
b = tx.create_bucket(b"widgets")
b.create_bucket(b"foo")
b.put(b"bar", b"0000")
# Update widgets/bar.
with self.db.update() as tx:
b = tx.bucket(b"widgets")
b.put(b"bar", b"xxxx")
# Cause a split.
with self.db.update() as tx:
b = tx.bucket(b"widgets")
for i in range(10000):
k = str(i).encode()
b.put(k, k)
# Insert into widgets/foo/baz.
with self.db.update() as tx:
b = tx.bucket(b"widgets").bucket(b"foo")
b.put(b"baz", b"yyyy")
with self.db.view() as tx:
b = tx.bucket(b"widgets")
v = b.bucket(b"foo").get(b"baz")
self.assertEqual(v, b"yyyy")
v = b.get(b"bar")
self.assertEqual(v, b"xxxx")
for i in range(10000):
k = str(i).encode()
v = b.get(k)
self.assertEqual(v, k)
def test_delete_a_bucket(self):
# Ensure that deleting a bucket using delete() returns an error.
with self.db.update() as tx:
b = tx.create_bucket(b"widgets")
b.create_bucket(b"foo")
with self.assertRaises(Exception):
b.delete(b"foo")
def test_delete_bucket_nested(self):
with self.db.update() as tx:
widgets = tx.create_bucket(b"widgets")
foo = widgets.create_bucket(b"foo")
bar = foo.create_bucket(b"bar")
bar.put(b"baz", b"bat")
widgets.delete_bucket(b"foo")
self.assertIsNone(widgets.bucket(b"foo"))
with self.db.view() as tx:
widgets = tx.bucket(b"widgets")
self.assertIsNone(widgets.bucket(b"foo"))
def test_delete_bucket_large(self):
with self.db.update() as tx:
widgets = tx.create_bucket(b"widgets")
foo = widgets.create_bucket(b"foo")
for i in range(1000):
k = str(i).encode()
foo.put(k, k)
with self.db.update() as tx:
widgets = tx.delete_bucket(b"widgets")
with self.db.update() as tx:
widgets = tx.create_bucket(b"widgets")
self.assertIsNone(widgets.bucket(b"foo"))
def test_create_bucket_incompatible(self):
with self.db.update() as tx:
widgets = tx.create_bucket(b"widgets")
widgets.put(b"foo", b"bar")
with self.assertRaisesRegex(Exception, "incompatible value"):
widgets.create_bucket(b"foo")
def test_delete_bucket_incompatible(self):
with self.db.update() as tx:
widgets = tx.create_bucket(b"widgets")
widgets.put(b"foo", b"bar")
with self.assertRaisesRegex(Exception, "bucket not exists"):
widgets.delete_bucket(b"foo")