I am trying to test a function and dynamically mock out the database call to the RecordType table, as the test database does not contains all the instances that I need for the function to run. This is the part of the function in question:
permanent = []
temp = []
for category in enums.Category:
category_name = str(category.name)
if category_name in labels:
if not category.is_temporary_code:
permanent.append(category)
else:
temp.append(category)
for category in permanent:
category_name = str(category.name)
internal_code = models.RecordType.objects.get(
industry_code=category.value
).internal_code
field_label = labels[category_name]
self.fields[internal_code] = forms.BooleanField(label=field_label, required=False)
I have a dict of values (record_type_dict) that I would like to be returned in place of calling the mocked function, depending on the passed in 'category.value'. In my test file I have this code:
@mock.patch.object(forms, 'models')
def test_records(mock_models, factory, support_client):
....
mock_models.RecordType.objects.get().internal_code.side_effect = side_effect
...
def side_effect(*args, **kwargs):
if args[0] in record_type_dict:
return record_type_dict[args[0]]
else:
return ""
However this does not work as the function is being called with 'industry_code=category.value'.
Is there a way of dynamically mocking this call depending only on the value of category.value?
Aucun commentaire:
Enregistrer un commentaire