-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
249 lines (187 loc) · 9.48 KB
/
tests.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import datetime
from django.test import TestCase
from django.contrib.auth.models import User
from apps.ifc_validation_models.models import ValidationRequest, ValidationTask # TODO: for now needs to be absolute!
from apps.ifc_validation_models.models import Company, AuthoringTool, Model
from apps.ifc_validation_models.decorators import requires_django_user_context
class ValidationModelsTestCase(TestCase):
@classmethod
def setUpTestData(cls):
"""
Creates a SYSTEM user in the (in-memory) database.
Runs once for the whole test case.
"""
user = User.objects.create(id=1, username='SYSTEM', is_active=True)
user.save()
@requires_django_user_context
def test_created_request_has_status_pending(self):
request = ValidationRequest.objects.create(
file_name='test.ifc',
file='test.ifc',
size=1024
)
request2 = ValidationRequest.objects.get(id=request.id)
self.assertEqual(request2.status, ValidationRequest.Status.PENDING)
@requires_django_user_context
def test_created_request_has_created_fields(self):
request = ValidationRequest.objects.create(
file_name='test2.ifc',
file='test2.ifc',
size=1024
)
request2 = ValidationRequest.objects.get(id=request.id)
self.assertEqual(request2.created.date(), datetime.date.today())
self.assertEqual(request2.created_by.username, 'SYSTEM')
self.assertEqual(request2.created_by.id, 1)
self.assertTrue(request2.updated is None)
self.assertTrue(request2.updated_by is None)
@requires_django_user_context
def test_updated_request_has_updated_fields(self):
request = ValidationRequest.objects.create(
file_name='test3.ifc',
file='test3.ifc',
size=1024
)
request2 = ValidationRequest.objects.get(id=request.id)
request2.save() # simulate update
self.assertEqual(request2.created.date(), datetime.date.today())
self.assertEqual(request2.created_by.username, 'SYSTEM')
self.assertEqual(request2.created_by.id, 1)
self.assertEqual(request2.updated.date(), datetime.date.today())
self.assertEqual(request2.updated_by.username, 'SYSTEM')
self.assertEqual(request2.updated_by.id, 1)
@requires_django_user_context
def test_created_task_has_status_pending(self):
request = ValidationRequest.objects.create(
file_name='test4.ifc',
file='test4.ifc',
size=1024
)
task = ValidationTask.objects.create(
request=request
)
task2 = ValidationTask.objects.get(id=task.id)
self.assertEqual(task2.status, ValidationTask.Status.PENDING)
@requires_django_user_context
def test_created_tasks_can_be_navigated(self):
request = ValidationRequest.objects.create(file_name='test5.ifc', file='test5.ifc', size=1024)
task1 = ValidationTask.objects.create(request=request)
task2 = ValidationTask.objects.create(request=request)
task3 = ValidationTask.objects.create(request=request)
request2 = ValidationRequest.objects.create(file_name='test6.ifc', file='test6.ifc', size=1024)
ValidationTask.objects.create(request=request2)
ValidationTask.objects.create(request=request2)
all_tasks = ValidationTask.objects.all()
tasks = ValidationTask.objects.filter(request__id=request.id)
self.assertEqual(all_tasks.count(), 5)
self.assertEqual(tasks.count(), 3)
self.assertEqual(task1.id, tasks[0].id)
self.assertEqual(task2.id, tasks[1].id)
self.assertEqual(task3.id, tasks[2].id)
@requires_django_user_context
def test_newly_created_tool_and_model_can_be_navigated(self):
user = User.objects.get(id=1)
company = Company.objects.create(name='Acme Inc.')
tool = AuthoringTool.objects.create(name='Tool XYZ', version='1.0-alpha', company=company)
model = Model.objects.create(file_name='test_123.ifc', size=2048, produced_by=tool, uploaded_by=user)
model2 = Model.objects.create(file_name='test_xyz.ifc', size=4096, produced_by=tool, uploaded_by=user)
all_tools = AuthoringTool.objects.all()
self.assertEqual(all_tools.count(), 1)
self.assertEqual(all_tools[0].id, tool.id)
self.assertEqual(tool.company.name, company.name)
self.assertEqual(all_tools.first().company.name, company.name)
self.assertEqual(model.produced_by.company.name, company.name)
self.assertEqual(model.uploaded_by.username, user.username)
self.assertEqual(user.models.count(), 2)
self.assertEqual(user.models.all()[1].file_name, model2.file_name)
@requires_django_user_context
def test_find_tool_by_full_name_should_succeed(self):
company1 = Company.objects.create(name='Acme Inc.')
tool1 = AuthoringTool.objects.create(name='Tool ABC', version='1.0', company=company1)
tool2 = AuthoringTool.objects.create(name='Tool ABC', version='2.0-alpha', company=company1)
company2 = Company.objects.create(name='PyCAD Limited')
tool3 = AuthoringTool.objects.create(name='App', version=None, company=company2)
tool4 = AuthoringTool.objects.create(name='App', version='2024', company=company2)
name_to_find = 'Acme Inc. Tool ABC - 1.0'
found_tool = AuthoringTool.find_by_full_name(name_to_find)
self.assertIsNotNone(found_tool)
self.assertIsInstance(found_tool, AuthoringTool)
self.assertEqual(found_tool.name, tool1.name)
name_to_find = 'Acme Inc. Tool ABC 1.0'
found_tool = AuthoringTool.find_by_full_name(name_to_find)
self.assertIsNotNone(found_tool)
self.assertIsInstance(found_tool, AuthoringTool)
self.assertEqual(found_tool.name, tool1.name)
name_to_find = 'PyCAD Limited'
found_tool = AuthoringTool.find_by_full_name(name_to_find)
self.assertIsNone(found_tool)
name_to_find = 'PyCAD Limited App'
found_tool = AuthoringTool.find_by_full_name(name_to_find)
self.assertIsNotNone(found_tool)
self.assertIsInstance(found_tool, AuthoringTool)
self.assertEqual(found_tool.name, tool3.name)
name_to_find = 'PyCAD Limited App 2024'
found_tool = AuthoringTool.find_by_full_name(name_to_find)
self.assertIsNotNone(found_tool)
self.assertIsInstance(found_tool, AuthoringTool)
self.assertEqual(found_tool.name, tool4.name)
name_to_find = 'PyCAD Limited App 2020'
found_tool = AuthoringTool.find_by_full_name(name_to_find)
self.assertIsNone(found_tool)
@requires_django_user_context
def test_find_tool_by_full_name_should_succeed2(self):
tool1 = AuthoringTool.objects.create(name='Test Application', version='0.10')
name_to_find = 'Test Application 0.10'
found_tool = AuthoringTool.find_by_full_name(name_to_find)
self.assertIsNotNone(found_tool)
self.assertIsInstance(found_tool, AuthoringTool)
self.assertEqual(found_tool.name, tool1.name)
tool2 = AuthoringTool.objects.create(name='Test Application', version='2023-01')
name_to_find = 'Test Application - 2023-01'
found_tool = AuthoringTool.find_by_full_name(name_to_find)
self.assertIsNotNone(found_tool)
self.assertIsInstance(found_tool, AuthoringTool)
self.assertEqual(found_tool.name, tool2.name)
@requires_django_user_context
def test_find_tool_by_full_name_should_succeed3(self):
tool1 = AuthoringTool.objects.create(name='IfcOpenShell-v0.7.0-6c9e130ca', version='v0.7.0-6c9e130ca')
name_to_find = 'IfcOpenShell-v0.7.0-6c9e130ca v0.7.0-6c9e130ca'
found_tool = AuthoringTool.find_by_full_name(name_to_find)
self.assertIsNotNone(found_tool)
self.assertIsInstance(found_tool, AuthoringTool)
self.assertEqual(found_tool.name, tool1.name)
@requires_django_user_context
def test_model_can_navigate_back_to_request(self):
request = ValidationRequest.objects.create(file_name='test.ifc', file='test.ifc', size=1024)
model, _ = Model.objects.get_or_create(
file_name = request.file_name,
file = request.file,
size = 0,
uploaded_by = request.created_by
)
request.model = model
request.save()
request2 = ValidationRequest.objects.get(id=request.id)
self.assertIsNotNone(request2.model)
self.assertEqual(request.id, model.request.id)
self.assertEqual(request2.id, model.request.id)
@requires_django_user_context
def test_task_can_navigate_back_to_model(self):
# arrange
request = ValidationRequest.objects.create(file_name='test.ifc', file='test.ifc', size=1024)
task = ValidationTask.objects.create(request=request, type=ValidationTask.Type.PARSE_INFO)
model, _ = Model.objects.get_or_create(
file_name = request.file_name,
file = request.file,
size = request.size,
uploaded_by = request.created_by
)
request.model = model
request.save()
# act
retrieved_task = ValidationTask.objects.get(id=task.id)
retrieved_model = retrieved_task.request.model
model_id = retrieved_model.id
# assert
self.assertIsNotNone(retrieved_task)
self.assertEqual(model.id, model_id)