Skip to content

Commit a1856e7

Browse files
committed
test: add unit test for getters
1 parent 4bc5008 commit a1856e7

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

tests/database/test_getters.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
import json
3+
import pytest
4+
5+
from json_as_db import Database
6+
from utils import file, logger
7+
8+
9+
CUR_DIR = os.path.dirname(os.path.realpath(__file__))
10+
DB_FILENAME = 'basic.json'
11+
DB_FILEPATH = os.path.join(CUR_DIR, '..', 'samples', DB_FILENAME)
12+
ID = 'kcbPuqpfV3YSHT8YbECjvh'
13+
ID_NOT_EXIST = 'N0t3xIstKeyV41ueString'
14+
15+
16+
@pytest.fixture()
17+
def db() -> Database:
18+
return Database().load(DB_FILEPATH)
19+
20+
21+
def test_metadata(db: Database):
22+
metadata = db.metadata
23+
assert bool(metadata)
24+
assert metadata['version'] == '0.0.2b1'
25+
assert metadata['creator'] == 'json-as-db'
26+
assert metadata['created_at'] == '2022-12-23T09:17:31.814215'
27+
assert metadata['updated_at'] == '2022-12-23T15:56:04.586110'
28+
29+
30+
def test_data(db: Database):
31+
assert db.data[ID] == db.get(ID)
32+
assert db.data.get(ID) == db.get(ID)
33+
34+
35+
def test_get_item(db: Database):
36+
found = db[ID]
37+
assert found['randomInteger'] == 123
38+
39+
40+
def test_get_item_not_exist(db: Database):
41+
found = db[ID_NOT_EXIST]
42+
assert found == None
43+

tests/database/test_database.py renamed to tests/database/test_operations.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,9 @@
1414
REC_ID_NOT_EXIST = 'N0t3xIstKeyV41ueString'
1515

1616

17-
def setup_db() -> Database:
18-
logger.debug('setup: (file) '+ DB_FILEPATH)
19-
return Database().load(DB_FILEPATH)
20-
21-
2217
@pytest.fixture()
2318
def db() -> Database:
24-
yield setup_db()
25-
26-
27-
def test_db_metadata(db: Database):
28-
record = db.get(REC_ID)
29-
30-
assert isinstance(record.get('list'), list)
31-
assert record.get('booleanTrue') == True
32-
assert record.get('booleanFalse') == False
33-
assert record.get('randomInteger') == 123
34-
assert record.get('randomString') == 'keyboard-cat'
35-
assert record.get('not-exists-key') == None
19+
return Database().load(DB_FILEPATH)
3620

3721

3822
def test_db_add(db: Database):

0 commit comments

Comments
 (0)