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
|
Testing with Django
===================
Django's ORM has an unfortunate implementation choice of considering
:class:`~django.db.models.Model` instances to be identical as long as their
primary keys are the same:
>>> from testfixtures.tests.test_django.models import SampleModel
>>> SampleModel(id=1, value=1) == SampleModel(id=1, value=2)
True
To work around this, :mod:`testfixtures.django` :ref:`registers <comparer-register>`
a :func:`comparer <testfixtures.django.compare_model>` for the django
:class:`~django.db.models.Model` class. However, for this to work,
``ignore_eq=True`` must be passed:
>>> from testfixtures import compare
>>> import testfixtures.django # to register the comparer...
>>> compare(SampleModel(id=1, value=1), SampleModel(id=1, value=2),
... ignore_eq=True)
Traceback (most recent call last):
...
AssertionError: SampleModel not as expected:
<BLANKLINE>
same:
['id']
<BLANKLINE>
values differ:
'value': 1 != 2
Since the above can quickly become cumbersome, a django-specific version
of :func:`~testfixtures.compare` that ignores ``__eq__`` by default is provided:
>>> from testfixtures.django import compare as django_compare
>>> django_compare(SampleModel(id=1, value=1), SampleModel(id=1, value=2))
Traceback (most recent call last):
...
AssertionError: SampleModel not as expected:
<BLANKLINE>
same:
['id']
<BLANKLINE>
values differ:
'value': 1 != 2
Ignoring fields
---------------
It may also be that you want to ignore fields over which you have no control
and cannot easily mock, such as created or modified times. For this, you
can use the `ignore_fields` option:
>>> compare(SampleModel(id=1, value=1), SampleModel(id=1, value=2),
... ignore_eq=True, ignore_fields=['value'])
Comparing non-editable fields
-----------------------------
By default, non-editable fields are ignored:
>>> django_compare(SampleModel(not_editable=1), SampleModel(not_editable=2))
If you wish to include these fields in the comparison, pass the
``non_editable_fields`` option:
>>> django_compare(SampleModel(not_editable=1), SampleModel(not_editable=2),
... non_editable_fields=True)
Traceback (most recent call last):
...
AssertionError: SampleModel not as expected:
<BLANKLINE>
same:
['created', 'id', 'value']
<BLANKLINE>
values differ:
'not_editable': 1 != 2
.. note::
The registered comparer currently ignores
:class:`many to many <django.db.models.ManyToManyField>` fields.
Patches to fix this deficiency are welcome!
|