~amirouche/python-okvs

python-okvs/tests.py -rw-r--r-- 6.0 KiB
c1f701a8 — Amirouche rename size to bytes_count, and count to key_count. 11 months ago
                                                                                
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
import os
from tempfile import mkstemp
from contextlib import contextmanager

import okdb as db
from okdb import nstore2


@contextmanager
def tempfile():
    fd, filename = mkstemp(".sqlite3", "lm4k-tests-", "/tmp/")
    os.close(fd)
    yield filename
    os.remove(filename)


def test_db_open():
    with tempfile() as filepath:
        cnx = db.open(filepath)
        db.close(cnx)


def test_db_get():
    with tempfile() as filepath:
        cnx = db.open(filepath)
        assert db.get(cnx, b'\x42') is None
        db.close(cnx)


def test_db_setget():
    with tempfile() as filepath:
        cnx = db.open(filepath)
        assert db.set(cnx, b'\x42', b'\x42') is None
        assert db.get(cnx, b'\x42') == b'\x42'
        db.close(cnx)


def test_db_query():
    with tempfile() as filepath:
        cnx = db.open(filepath)
        assert len(list(db.query(cnx, b'\x00', b'\xFF'))) == 0
        db.close(cnx)


def test_db_set_and_query():
    with tempfile() as filepath:
        cnx = db.open(filepath)
        expected = [
            (b'\x01', b''),
            (b'\x02', b''),
            (b'\x03', b''),
        ]
        for k, v in expected:
            db.set(cnx, k, v)
        assert list(db.query(cnx, b'\x00', b'\xFF')) == expected
        db.close(cnx)


def test_db_set_and_query_desc():
    with tempfile() as filepath:
        cnx = db.open(filepath)
        expected = [
            (b'\x01', b''),
            (b'\x02', b''),
            (b'\x03', b''),
        ]
        for k, v in expected:
            db.set(cnx, k, v)
        assert list(db.query(cnx, b'\xFF', b'\x00')) == list(reversed(expected))
        db.close(cnx)


def test_db_query_limit_offset_desc():
    with tempfile() as filepath:
        cnx = db.open(filepath)
        expected = [
            (b'\x01', b''),
            (b'\x02', b''),
            (b'\x03', b''),
        ]
        for k, v in expected:
            db.set(cnx, k, v)
        out = db.query(cnx, b'\x03', b'\x00', limit=1, offset=1)
        assert list(out) == [(b'\x01', b'')]
        db.close(cnx)


def test_db_key_count():
    with tempfile() as filepath:
        cnx = db.open(filepath)
        expected = [
            (b'\x01', b''),
            (b'\x02', b''),
            (b'\x03', b''),
        ]
        for k, v in expected:
            db.set(cnx, k, v)
        assert db.key_count(cnx, b'\x00', b'\xFF') == 3
        db.close(cnx)


def test_db_bytes_count():
    with tempfile() as filepath:
        cnx = db.open(filepath)
        expected = [
            (b'\x01', b'\x01'),
            (b'\x02', b'\x01'),
            (b'\x03', b'\x01'),
        ]
        for k, v in expected:
            db.set(cnx, k, v)
        assert db.bytes_count(cnx, b'\x00', b'\xFF') == 6
        db.close(cnx)


def test_db_key_count2():
    with tempfile() as filepath:
        cnx = db.open(filepath)
        expected = [
            (b'\x00', b''),
            (b'\x01', b''),
            (b'\x02', b''),
            (b'\x03', b''),
            (b'\xFF', b''),
        ]
        for k, v in expected:
            db.set(cnx, k, v)
        assert db.key_count(cnx, b'\x01', b'\xFF') == 3
        db.close(cnx)


def test_db_bytes_count2():
    with tempfile() as filepath:
        cnx = db.open(filepath)
        expected = [
            (b'\x00', b'\x01'),
            (b'\x01', b'\x01'),
            (b'\x02', b'\x01'),
            (b'\x03', b'\x01'),
            (b'\xFF', b'\x01'),
        ]
        for k, v in expected:
            db.set(cnx, k, v)
        assert db.bytes_count(cnx, b'\x01', b'\xFF') == 6
        db.close(cnx)


def test_db_delete():
    with tempfile() as filepath:
        # WHEN
        cnx = db.open(filepath)
        kvs = [
            (b'\x00', b'\x01'),
            (b'\x01', b'\x01'),
            (b'\x02', b'\x01'),
            (b'\x03', b'\x01'),
        ]
        for k, v in kvs:
            db.set(cnx, k, v)
        assert db.key_count(cnx, b'\x00', b'\xFF') == 4
        # GIVEN
        db.delete(cnx, b'\x00')
        # THEN
        assert db.key_count(cnx, b'\x00', b'\xFF') == 3
        # GIVEN
        db.delete(cnx, b'\x00', b'\xFF')
        # THEN
        assert db.key_count(cnx, b'\x00', b'\xFF') == 0
        db.close(cnx)


def test_db_pack_unpack():
    expected = (None, True, False, 42, 0, -42, "abcdef", b'\xc0ff33', (1337, None))
    assert db.unpack(db.pack(expected)) == expected


def test_db_sorted_pack():
    given = (
        ((-100,), (0,)),
        ((0,), (100,)),
        ((42,), (43,)),
        ((-42,), (42,)),
        ((-52,), (-42,)),
        (("car",), ("care",)),
        (("abc",), ("xyz",)),
        ((b'\x00',), (b'\xFF',)),
        ((None, (b'\x00',)), (None, (b'\xFF',))),
    )
    for item, other in given:
        assert item < other
        assert db.pack(item) < db.pack(other)


def test_nstore2_get():
    ns = nstore2.init("test", [b'\x01'], 3)
    with tempfile() as filepath:
        cnx = db.open(filepath)
        out = nstore2.get(cnx, ns, 1, 2, 3)
        assert out is None


def test_nstore2_add_get():
    ns = nstore2.init("test", [b'\x01'], 3)
    with tempfile() as filepath:
        cnx = db.open(filepath)
        expected = b'\x42'
        nstore2.add(cnx, ns, 1, 2, 3, value=expected)
        out = nstore2.get(cnx, ns, 1, 2, 3)
        assert out == expected

def test_nstore2_add_delete_get():
    ns = nstore2.init("test", [b'\x01'], 3)
    with tempfile() as filepath:
        cnx = db.open(filepath)
        nstore2.add(cnx, ns, 1, 2, 3, b'\x42')
        nstore2.delete(cnx, ns, 1, 2, 3)
        out = nstore2.get(cnx, ns, 1, 2, 3)
        assert out is None


def test_nstore2_query():
    ns = nstore2.init("test", [b'\x01'], 3)
    with tempfile() as filepath:
        cnx = db.open(filepath)
        for i in range(10):
            nstore2.add(cnx, ns, 0, 0, i, b'\x42')
        for i in range(10):
            nstore2.add(cnx, ns, 1, 1, i, b'\x42')
        for i in range(10):
            nstore2.add(cnx, ns, 2, 2, i, b'\x42')
        out = nstore2.query(cnx, ns, (1, 1, nstore2.var('i')))
        out = [x.get('i') for x in out]
        expected = list(range(10))
        assert out == expected