-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_model.py
217 lines (188 loc) · 6.16 KB
/
test_model.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
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
"""Tests for the database model."""
import datetime
import os
import sqlite3
from pathlib import Path
from tracemalloc import stop
import pytest
from loguru import logger
from pydantic import EmailStr, ValidationError
from sqlmodel import Session, SQLModel, create_engine, select
from tuttle import model, time
from tuttle.model import (
Address,
Client,
Contact,
Contract,
Project,
User,
TimeUnit,
Cycle,
)
def store_and_retrieve(model_object):
# in-memory sqlite db
db_engine = create_engine("sqlite:///")
SQLModel.metadata.create_all(db_engine)
with Session(db_engine) as session:
session.add(model_object)
session.commit()
with Session(db_engine) as session:
retrieved = session.exec((select(type(model_object)))).first()
return True
def test_model_creation():
"""Test whether the entire data model can be materialized as DB tables."""
try:
test_home = Path("tuttle_tests/data/tmp")
db_path = test_home / "tuttle_test.db"
db_url = f"sqlite:///{db_path}"
db_engine = create_engine(db_url, echo=True)
SQLModel.metadata.create_all(db_engine)
# test if database intact
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute(
"""
SELECT name FROM sqlite_master
WHERE type='table'
ORDER BY name;
"""
)
tables = cursor.fetchall()
conn.close()
finally:
try:
os.remove(db_path)
except OSError:
pass
class TestUser:
"""Tests for the User model."""
def test_valid_instantiation(self):
user = User.validate(
dict(
name="Harry Tuttle",
subtitle="Heating Engineer",
email="[email protected]",
)
)
class TestContact:
def test_valid_instantiation(self):
contact = Contact.validate(
dict(
first_name="Sam",
last_name="Lowry",
email="[email protected]",
company="Ministry of Information",
)
)
assert store_and_retrieve(contact)
def test_invalid_email(self):
with pytest.raises(ValidationError):
Contact.validate(
dict(
first_name="Sam",
last_name="Lowry",
email="27B-",
company="Ministry of Information",
)
)
class TestClient:
"""Tests for the Client model."""
def test_valid_instantiation(self):
invoicing_contact = Contact(
first_name="Sam",
last_name="Lowry",
email="[email protected]",
company="Ministry of Information",
)
client = Client.validate(
dict(
name="Ministry of Information",
invoicing_contact=invoicing_contact,
)
)
assert store_and_retrieve(client)
def test_missing_name(self):
"""Test that a ValidationError is raised when the name is missing."""
with pytest.raises(ValidationError):
Client.validate(dict())
try:
client = Client.validate(dict())
except ValidationError as ve:
for error in ve.errors():
field_name = error.get("loc")[0]
error_message = error.get("msg")
assert field_name == "name"
def test_missing_fields_instantiation(self):
with pytest.raises(ValidationError):
Client.validate(dict())
class TestContract:
"""Tests for the Contract model."""
def test_valid_instantiation(self):
client = Client(name="Ministry of Information")
contract = Contract.validate(
dict(
title="Project X Contract",
client=client,
signature_date=datetime.date(2022, 10, 1),
start_date=datetime.date(2022, 10, 2),
end_date=datetime.date(2022, 12, 31),
rate=100,
is_completed=False,
currency="USD",
VAT_rate=0.19,
unit=TimeUnit.hour,
units_per_workday=8,
volume=100,
term_of_payment=31,
billing_cycle=Cycle.monthly,
)
)
assert store_and_retrieve(contract)
def test_missing_fields_instantiation(self):
with pytest.raises(ValidationError):
Contract.validate(dict())
class TestProject:
"""Tests for the Project model."""
def test_valid_instantiation(self):
client = Client(name="Ministry of Information")
contract = Contract(
title="Project X Contract",
client=client,
signature_date=datetime.date(2022, 10, 1),
start_date=datetime.date(2022, 10, 2),
end_date=datetime.date(2022, 12, 31),
rate=100,
is_completed=False,
currency="USD",
VAT_rate=0.19,
unit=TimeUnit.hour,
units_per_workday=8,
volume=100,
term_of_payment=31,
billing_cycle=Cycle.monthly,
)
project = Project.validate(
dict(
title="Project X",
description="The description of Project X",
tag="#project_x",
start_date=datetime.date(2022, 10, 2),
end_date=datetime.date(2022, 12, 31),
contract=contract,
)
)
assert store_and_retrieve(project)
def test_missing_fields_instantiation(self):
with pytest.raises(ValidationError):
Project.validate(dict())
def test_invalid_tag_instantiation(self):
with pytest.raises(ValidationError):
Project.validate(
dict(
title="Project X",
description="The description of Project X",
tag="project_x",
start_date=datetime.date(2022, 10, 2),
end_date=datetime.date(2022, 12, 31),
)
)