Skip to content

Commit 9cd3bd2

Browse files
committed
test: Unit test case for ensuing that layout.table contains valid fields
ref:
1 parent 79b0fc4 commit 9cd3bd2

19 files changed

Lines changed: 179 additions & 3 deletions

File tree

app/config_management/tests/unit/config_group_hosts/test_unit_config_group_hosts_model.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,6 @@ class ConfigGroupHostModelPyTest(
8989
def test_method_get_url_returns_str(self, mocker, model_instance):
9090

9191
pytest.xfail( reason = 'This model has no endpoint' )
92+
93+
def test_attribute_page_layout_table_fields(self):
94+
pytest.xfail( reason = 'This model has no endpoint' )

app/config_management/tests/unit/config_groups_software/test_unit_config_groups_software_model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,6 @@ class ConfigGroupSoftwareModelInheritedCases(
159159
class ConfigGroupSoftwareModelPyTest(
160160
ConfigGroupSoftwareModelTestCases,
161161
):
162-
pass
162+
163+
def test_attribute_page_layout_table_fields(self):
164+
pytest.xfail( reason = 'This model has no endpoint' )

app/core/tests/unit/centurion_abstract/test_unit_centurion_abstract_model.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,6 @@ def test_manager_tenancy_filter_tenant(self):
242242

243243
def test_manager_tenancy_select_related(self):
244244
pytest.xfail( reason = 'base model, test is n/a.' )
245+
246+
def test_attribute_page_layout_table_fields(self):
247+
pytest.xfail( reason = 'base model, test is n/a.' )

app/core/tests/unit/centurion_audit/test_unit_centurion_audit_model.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,7 @@ def test_method_get_model_history_populates_field_before(self, model_instance):
313313
del model_instance._before
314314

315315
assert model_instance.before == test_value
316+
317+
318+
def test_attribute_page_layout_table_fields(self):
319+
pytest.xfail( reason = 'base model, test is n/a.' )

app/core/tests/unit/centurion_audit_meta/test_unit_centurion_audit_meta_model.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,6 @@ def test_method_clean_fields_calls_super_centurion_mixin(self):
264264

265265
def test_method_clean_fields_calls_super_tenancy_abstract(self):
266266
pytest.xfail( reason = 'base model, test is n/a.' )
267+
268+
def test_attribute_page_layout_table_fields(self):
269+
pytest.xfail( reason = 'base model, test is n/a.' )

app/core/tests/unit/centurion_audit_meta/test_unit_meta_audit_history_model.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import importlib
2+
13
import pytest
24

35
from django.apps import apps
@@ -183,7 +185,24 @@ def model(self, request):
183185

184186
yield request.cls.model_class
185187

186-
188+
189+
@pytest.fixture( scope = 'class')
190+
def model_serializer(self, model):
191+
192+
serializer_name: str = 'centurionaudit_' + str(model._meta.model_name)[0:( len(model._meta.model_name) - len('audithistory') )]
193+
194+
serializer_module = importlib.import_module(
195+
name = model._meta.app_label + '.serializers.' + str(
196+
serializer_name
197+
)
198+
)
199+
200+
201+
yield {
202+
'view': getattr( serializer_module, 'ViewSerializer' )
203+
}
204+
205+
187206
@pytest.mark.skip( reason = 'ToDo: Figure out how to dynomagic add audit_model instance' )
188207
def test_model_creation(self, model, user):
189208
pass

app/core/tests/unit/centurion_model_note/test_unit_centurion_model_note_model.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,6 @@ def test_method_get_url_returns_str(self, model, model_instance):
164164
"""
165165

166166
pytest.xfail( reason = 'Test not required as model does not have direct endpoint.' )
167+
168+
def test_attribute_page_layout_table_fields(self):
169+
pytest.xfail( reason = 'base model, test is n/a.' )

app/core/tests/unit/centurion_model_note_meta/test_unit_centurion_model_note_meta_model.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,6 @@ def test_method_clean_fields_calls_super_centurion_mixin(self):
265265

266266
def test_method_clean_fields_calls_super_tenancy_abstract(self):
267267
pytest.xfail( reason = 'base model, test is n/a.' )
268+
269+
def test_attribute_page_layout_table_fields(self):
270+
pytest.xfail( reason = 'base model, test is n/a.' )

app/core/tests/unit/centurion_model_note_meta/test_unit_meta_model_note_model.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import importlib
2+
13
import pytest
24

35
from django.apps import apps
@@ -188,6 +190,24 @@ def model(self, request):
188190
return request.cls.model_class
189191

190192

193+
@pytest.fixture( scope = 'class')
194+
def model_serializer(self, model):
195+
196+
serializer_name: str = 'centurionmodelnote_' + str(model._meta.model_name).replace('centurionmodelnote', '')
197+
198+
serializer_module = importlib.import_module(
199+
name = model._meta.app_label + '.serializers.' + str(
200+
serializer_name
201+
)
202+
)
203+
204+
205+
yield {
206+
'view': getattr( serializer_module, 'ViewSerializer' )
207+
}
208+
209+
210+
191211
@pytest.mark.skip( reason = 'ToDo: Figure out how to dynomagic add note_model instance' )
192212
def test_model_creation(self, model, user):
193213
pass

app/core/tests/unit/mixin_centurion/test_unit_centurion_mixin.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,69 @@ def get(*args, **kwargs):
440440
# Fail-Safe to ensure test writer fills all fields
441441

442442

443+
def test_attribute_page_layout_table_fields(self, model, model_serializer):
444+
"""Test page_layout key
445+
446+
Ensure that all table columns use valid model fields.
447+
"""
448+
449+
if 'table' not in model.page_layout:
450+
pytest.xfail( reason = 'model does not use any layout.' )
451+
452+
453+
454+
allowed_unknown_fields = [
455+
'-action_delete-', # Adds delete button to UI
456+
'nbsp', # blank cell
457+
]
458+
459+
def check(field) -> list:
460+
461+
return_val = []
462+
463+
if model_serializer['view'].Meta.fields == '__all__':
464+
465+
if not hasattr(model, field):
466+
467+
return_val += [ field ]
468+
469+
470+
elif(
471+
not field in model_serializer['view'].Meta.fields
472+
and field not in allowed_unknown_fields
473+
):
474+
475+
return_val += [ field ]
476+
477+
return return_val
478+
479+
480+
481+
unknown_fields = []
482+
483+
for field in model.page_layout['table']:
484+
485+
if isinstance(field, list):
486+
487+
for sub_field in field:
488+
489+
unknown_fields += check(sub_field)
490+
491+
elif isinstance(field, dict):
492+
493+
field = field['field']
494+
495+
unknown_fields += check(field)
496+
497+
else:
498+
499+
unknown_fields += check(field)
500+
501+
502+
assert len(unknown_fields) == 0, f"The following field(s) are not a model field {unknown_fields}"
503+
504+
505+
443506

444507
@pytest.mark.module_core
445508
class CenturionMixnPyTest(

0 commit comments

Comments
 (0)