-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_main.py
125 lines (112 loc) · 3.52 KB
/
test_main.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
from main import TransactionsHandler
from main import InMemoryDBClient
import pytest
def test_without_rollback():
client = InMemoryDBClient()
handler = TransactionsHandler(client)
handler.set("a", 1)
handler.set("b", 2)
handler.set("c", 3)
assert client.data == {"a": 1, "b": 2, "c": 3}
handler.delete("b")
assert client.data == {"a": 1, "c": 3}
handler.set("a", 4)
assert client.data == {"a": 4, "c": 3}
assert handler.count(4) == 1
assert handler.count(6) == 0
handler.set("c", 4)
assert handler.count(4) == 2
def test_with_rollback():
client = InMemoryDBClient()
handler = TransactionsHandler(client)
handler.set("a", 1)
handler.set("b", 2)
handler.set("c", 3)
assert client.data == {"a": 1, "b": 2, "c": 3}
handler.begin()
handler.set("b", 4)
assert handler.get("b") == 4
handler.set("c", 5)
handler.rollback()
assert client.data == {"a": 1, "b": 2, "c": 3}
assert handler.count(4) == 0
assert handler.count(5) == 0
def test_with_commit():
client = InMemoryDBClient()
handler = TransactionsHandler(client)
handler.set("a", 1)
handler.set("b", 2)
handler.set("c", 3)
assert client.data == {"a": 1, "b": 2, "c": 3}
handler.begin()
handler.set("b", 4)
assert handler.get("b") == 4
handler.set("c", 5)
handler.commit()
assert client.data == {"a": 1, "b": 4, "c": 5}
assert handler.count(4) == 1
assert handler.count(5) == 1
def test_with_nested_transactions():
client = InMemoryDBClient()
handler = TransactionsHandler(client)
handler.set("a", 1)
handler.set("b", 2)
handler.set("c", 3)
assert client.data == {"a": 1, "b": 2, "c": 3}
handler.begin()
handler.set("b", 4)
assert handler.get("b") == 4
handler.set("c", 5)
handler.begin()
handler.set("c", 6)
handler.rollback()
assert client.data == {"a": 1, "b": 4, "c": 5}
assert handler.count(4) == 1
assert handler.count(5) == 1
assert handler.count(6) == 0
handler.commit()
assert client.data == {"a": 1, "b": 4, "c": 5}
assert handler.count(4) == 1
assert handler.count(5) == 1
assert handler.count(6) == 0
def test_delete_unknown_key_raises_exception():
client = InMemoryDBClient()
handler = TransactionsHandler(client)
with pytest.raises(Exception):
handler.delete("a")
def test_rollback_without_transaction_raises_exception():
client = InMemoryDBClient()
handler = TransactionsHandler(client)
with pytest.raises(Exception):
handler.rollback()
def test_commit_without_transaction_raises_exception():
client = InMemoryDBClient()
handler = TransactionsHandler(client)
with pytest.raises(Exception):
handler.commit()
def test_rollback_with_empty_transaction_does_nothing():
client = InMemoryDBClient()
handler = TransactionsHandler(client)
handler.begin()
handler.rollback()
assert client.data == {}
def test_commit_with_empty_transaction_does_nothing():
client = InMemoryDBClient()
handler = TransactionsHandler(client)
handler.begin()
handler.commit()
assert client.data == {}
def test_rollback_with_multiple_transactions():
client = InMemoryDBClient()
handler = TransactionsHandler(client)
handler.begin()
handler.set("a", 1)
handler.set("b", 2)
handler.set("c", 3)
handler.begin()
handler.set("b", 4)
handler.set("c", 5)
handler.rollback()
assert client.data == {"a": 1, "b": 2, "c": 3}
handler.rollback()
assert client.data == {}