11from django .test import TestCase
22from django .contrib .auth .models import User
3+ from django .db .utils import IntegrityError
34
45from apps .ifc_validation_models .models import ValidationRequest , ValidationTask # TODO: for now needs to be absolute!
56from apps .ifc_validation_models .models import Company , AuthoringTool , Model
@@ -130,14 +131,15 @@ def test_newly_created_tool_and_model_can_be_navigated(self):
130131 self .assertEqual (all_tools .count (), 1 )
131132 self .assertEqual (all_tools [0 ].id , tool .id )
132133 self .assertEqual (tool .company .name , company .name )
133- self .assertEqual (all_tools . first () .company .name , company .name )
134+ self .assertEqual (all_tools [ 0 ] .company .name , company .name )
134135 self .assertEqual (model .produced_by .company .name , company .name )
135136 self .assertEqual (model .uploaded_by .username , user .username )
136137 self .assertEqual (user .models .count (), 2 )
137138 self .assertEqual (user .models .all ()[1 ].file_name , model2 .file_name )
138139
139140 def test_find_tool_by_full_name_should_succeed (self ):
140141
142+ # arrange
141143 ValidationModelsTestCase .set_user_context ()
142144 company1 = Company .objects .create (name = 'Acme Inc.' )
143145 tool1 = AuthoringTool .objects .create (name = 'Tool ABC' , version = '1.0' , company = company1 )
@@ -147,17 +149,20 @@ def test_find_tool_by_full_name_should_succeed(self):
147149 tool3 = AuthoringTool .objects .create (name = 'App' , version = None , company = company2 )
148150 tool4 = AuthoringTool .objects .create (name = 'App' , version = '2024' , company = company2 )
149151
152+ # act/assert
150153 name_to_find = 'Acme Inc. - Tool ABC - 1.0'
151154 found_tool = AuthoringTool .find_by_full_name (name_to_find )
152155 self .assertIsNotNone (found_tool )
153156 self .assertIsInstance (found_tool , AuthoringTool )
154157 self .assertEqual (found_tool .name , tool1 .name )
158+ self .assertEqual (found_tool .company .name , tool1 .company .name )
155159
156160 name_to_find = 'Acme Inc. - Tool ABC 1.0'
157161 found_tool = AuthoringTool .find_by_full_name (name_to_find )
158162 self .assertIsNotNone (found_tool )
159163 self .assertIsInstance (found_tool , AuthoringTool )
160164 self .assertEqual (found_tool .name , tool1 .name )
165+ self .assertEqual (found_tool .company .name , tool1 .company .name )
161166
162167 name_to_find = 'PyCAD Limited'
163168 found_tool = AuthoringTool .find_by_full_name (name_to_find )
@@ -168,49 +173,80 @@ def test_find_tool_by_full_name_should_succeed(self):
168173 self .assertIsNotNone (found_tool )
169174 self .assertIsInstance (found_tool , AuthoringTool )
170175 self .assertEqual (found_tool .name , tool3 .name )
176+ self .assertEqual (found_tool .company .name , tool3 .company .name )
171177
172178 name_to_find = 'PyCAD Limited - App 2024'
173179 found_tool = AuthoringTool .find_by_full_name (name_to_find )
174180 self .assertIsNotNone (found_tool )
175181 self .assertIsInstance (found_tool , AuthoringTool )
176182 self .assertEqual (found_tool .name , tool4 .name )
183+ self .assertEqual (found_tool .company .name , tool4 .company .name )
177184
178185 name_to_find = 'PyCAD Limited App 2020'
179186 found_tool = AuthoringTool .find_by_full_name (name_to_find )
180187 self .assertIsNone (found_tool )
181188
182189 def test_find_tool_by_full_name_should_succeed2 (self ):
183190
191+ # arrange
184192 ValidationModelsTestCase .set_user_context ()
185193 tool1 = AuthoringTool .objects .create (name = 'Test Application' , version = '0.10' )
194+ tool2 = AuthoringTool .objects .create (name = 'Test Application' , version = '2023-01' )
186195
187- name_to_find = 'Test Application 0.10'
188-
196+ # act/assert
197+ name_to_find = 'Test Application - 0.10'
189198 found_tool = AuthoringTool .find_by_full_name (name_to_find )
190199 self .assertIsNotNone (found_tool )
191200 self .assertIsInstance (found_tool , AuthoringTool )
192201 self .assertEqual (found_tool .name , tool1 .name )
193-
194- tool2 = AuthoringTool .objects .create (name = 'Test Application' , version = '2023-01' )
195-
202+ self .assertIsNone (found_tool .company )
203+
196204 name_to_find = 'Test Application - 2023-01'
197-
198205 found_tool = AuthoringTool .find_by_full_name (name_to_find )
199206 self .assertIsNotNone (found_tool )
200207 self .assertIsInstance (found_tool , AuthoringTool )
201208 self .assertEqual (found_tool .name , tool2 .name )
209+ self .assertIsNone (found_tool .company )
202210
203211 def test_find_tool_by_full_name_should_succeed3 (self ):
204212
213+ # arrange
205214 ValidationModelsTestCase .set_user_context ()
206215 tool1 = AuthoringTool .objects .create (name = 'IfcOpenShell-v0.7.0-6c9e130ca' , version = 'v0.7.0-6c9e130ca' )
207216
217+ # act
208218 name_to_find = 'IfcOpenShell-v0.7.0-6c9e130ca v0.7.0-6c9e130ca'
209-
210219 found_tool = AuthoringTool .find_by_full_name (name_to_find )
220+
221+ # assert
211222 self .assertIsNotNone (found_tool )
212223 self .assertIsInstance (found_tool , AuthoringTool )
213224 self .assertEqual (found_tool .name , tool1 .name )
225+ self .assertIsNone (found_tool .company )
226+
227+ def test_find_tool_by_full_name_should_return_none (self ):
228+
229+ # arrange
230+ ValidationModelsTestCase .set_user_context ()
231+ AuthoringTool .objects .create (name = 'Test Application' , version = '0.10' )
232+
233+ # act
234+ name_to_find = 'Test Application 0.12'
235+ found_tool = AuthoringTool .find_by_full_name (name_to_find )
236+
237+ # assert
238+ self .assertIsNone (found_tool )
239+
240+ def test_add_tool_twice_should_fail (self ):
241+
242+ # arrange
243+ ValidationModelsTestCase .set_user_context ()
244+
245+ # act/assert
246+ AuthoringTool .objects .create (name = 'Test Application' , version = '0.10' ) # should succeed
247+ AuthoringTool .objects .create (name = 'Test Application' , version = '0.11' ) # should succeed
248+ with self .assertRaises (IntegrityError ):
249+ AuthoringTool .objects .create (name = 'Test Application' , version = '0.11' ) # should fail
214250
215251 def test_model_can_navigate_back_to_request (self ):
216252
0 commit comments