-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sqliteworks.py
81 lines (56 loc) · 2.15 KB
/
test_sqliteworks.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
import pytest
import tempfile
import os
import sqliteworks
@pytest.fixture()
def db_directory():
with tempfile.TemporaryDirectory(prefix='sqliteworks-test-') as d:
yield d
@pytest.fixture
def db_path(db_directory):
yield os.path.join(db_directory, 'test.db')
@pytest.fixture
def db_conn(db_path):
yield sqliteworks.create_connection(db_path)
@pytest.fixture
def basic_queue(db_conn):
queue = sqliteworks.SQLiteWorkQueue('basic_queue', db_conn)
queue.init()
yield queue
@pytest.fixture
def basic_kv_store(db_conn):
kv_store = sqliteworks.SQLiteKVStore('basic_kv', db_conn)
kv_store.init()
yield kv_store
def test_basic_queue_behavior(db_conn, basic_queue):
item = basic_queue.push('test-data')
assert isinstance(item, sqliteworks.WorkQueueItem)
assert item.state == sqliteworks.SQLiteWorkQueueStates.QUEUED
assert isinstance(item.item_id, int)
item_again = basic_queue.pop_queued()
assert item_again is not None
assert item_again.data == 'test-data'
assert item_again.item_id == item.item_id
assert item_again.state == sqliteworks.SQLiteWorkQueueStates.IN_PROGRESS
item = basic_queue.get_item_by_id(item.item_id)
assert item is not None
assert item_again.data == 'test-data'
assert item_again.item_id == item.item_id
assert item.state == sqliteworks.SQLiteWorkQueueStates.IN_PROGRESS
basic_queue.mark_item(item, sqliteworks.SQLiteWorkQueueStates.COMPLETED)
item = basic_queue.get_item_by_id(item.item_id)
assert item is not None
assert item_again.data == 'test-data'
assert item_again.item_id == item.item_id
assert item.state == sqliteworks.SQLiteWorkQueueStates.COMPLETED
assert basic_queue.purge_old_items(seconds_old=-1) == 1
item = basic_queue.get_item_by_id(item.item_id)
assert item is None
def test_basic_kv_store_behavior(db_conn, basic_kv_store):
assert len(basic_kv_store) == 0
assert basic_kv_store.count() == 0
basic_kv_store['foo'] = [1, 2, 3]
assert basic_kv_store['foo'] == [1, 2, 3]
assert set(basic_kv_store.keys()) == {'foo'}
del basic_kv_store['foo']
assert set(basic_kv_store.keys()) == set()