-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_json_storage.py
More file actions
91 lines (75 loc) · 2.83 KB
/
test_json_storage.py
File metadata and controls
91 lines (75 loc) · 2.83 KB
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
import pytest
from jsonschema import ValidationError
from json_storage import JSONStorage
from json_storage.model import BaseModel
class Product(BaseModel):
filename = 'product.json'
id_field = 'UUID'
schema = {
'type': 'object',
'required': [
'UUID',
'name',
'price'
],
'properties': {
'UUID': {'type': 'string'},
'name': {'type': 'string'},
'price': {"type": 'number'},
},
'additionalProperties': True,
}
def test_json_update(tmpdir):
test_data = {'UUID': '111-111-111', 'name': 'Product 1', 'price': 220.99}
storage = JSONStorage(tmpdir)
manager = storage.get_manager(Product)
manager.update(test_data)
item = manager.get([test_data['UUID']])[0]
assert item['UUID'] == test_data['UUID']
assert item['name'] == test_data['name']
assert item['price'] == test_data['price']
def test_json_validate():
product = Product({'UUID': '111-111-111', 'name': 'Product 1', 'price': 220.99})
product.validate()
product = Product({'UUID': 222, 'name': 'Product 1', 'price': 220.99})
with pytest.raises(ValidationError):
product.validate()
product = Product({'UUID': 222, 'name': 'Product 1', 'price': '220.99'})
with pytest.raises(ValidationError):
product.validate()
product = Product({'UUID': '111-111-111', 'name': 'Product 1'})
with pytest.raises(ValidationError):
product.validate()
product = Product({'UUID': '111-111-111', 'name': 'Product 1', 'price': 220.99, 'other': 'test'})
product.validate()
def test_json_get(tmpdir):
test_data = [
{'UUID': '111-111-111', 'name': 'Product 1', 'price': 220.99},
{'UUID': '222-222-111', 'name': 'Product 2', 'price': 220.99},
{'UUID': '111-222-345', 'name': 'Product 3', 'price': 220.99},
]
storage = JSONStorage(tmpdir)
manager = storage.get_manager(Product)
for item in test_data:
manager.update(item)
assert manager.get(['111-111-111'])[0] == test_data[0]
assert manager.get(['111']) == []
results = manager.get(['111-111-111', '222-222-111'])
assert len(results) == 2
def test_json_all(tmpdir):
test_data = [
{'UUID': '111-111-111', 'name': 'Product 1', 'price': 220.99},
{'UUID': '222-222-111', 'name': 'Product 2', 'price': 220.99},
{'UUID': '111-222-345', 'name': 'Product 3', 'price': 220.99},
]
storage = JSONStorage(tmpdir)
manager = storage.get_manager(Product)
for item in test_data:
manager.update(item)
assert test_data == manager.all()
def test_json_clear(tmpdir):
storage = JSONStorage(tmpdir)
manager = storage.get_manager(Product)
manager.update({'UUID': '111-111-111', 'name': 'Product 1', 'price': 220.99})
manager.clear_all()
assert manager.all() == []