Unit testing models.py

23 views
Skip to first unread message

Mark Phillips

unread,
May 13, 2018, 12:12:17 PM5/13/18
to django users
What should be unit tested in models.py? I assume the storing of data and retrieving of data from the database does not need to be tested, as I further assume django has a full set of tests for those operations.

I can see testing these parts of models.py

* All custom methods

* Labels for all fields
something like
def test_first_name_label(self):
        author=Author.objects.get(id=1)
        field_label = author._meta.get_field('first_name').verbose_name
        self.assertEquals(field_label,'first name')

* Field attributes (eg length of CharField)? Is this really necessary, as I would again assume django covers this in their unit tests? 

Anything else that I am missing?

Thanks!

Mark

Jani Tiainen

unread,
May 13, 2018, 2:09:35 PM5/13/18
to django...@googlegroups.com
Hi,

In general you don't need to test your models if you don't use anything special there. If you do have properties or methods on models that do something that is good to test that they return correct values or do correct things.



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEqej2P0_sZt2j06nf0OnOL%2BE%3DuVa1Cs0BOFmTx8vjQm6-io2Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

Anthony Flury

unread,
May 14, 2018, 2:19:12 AM5/14/18
to django...@googlegroups.com
I would agree with that - test any custom functionality -

* Custom methods (including __str__ and __repr__)
* custom managers
* Triggers (that maybe save custom fields on update)
* validations - testing to ensure that the right validation is
performed on that field - i.e. you linked the right field with the
right validation.

I would do a cursory test that you can save and retrieve an object -
just to make sure that you have executed your migrations correctly
> send an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/group/django-users>.
> <https://groups.google.com/d/msgid/django-users/CAEqej2P0_sZt2j06nf0OnOL%2BE%3DuVa1Cs0BOFmTx8vjQm6-io2Q%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
>
>
> --
> Jani Tiainen
>
> - Well planned is half done, and a half done has been sufficient before...
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAHn91oeJYPGbLaJAZbJqd3jxbDcY6m3d8e2fSE06_E_ZwXXosQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAHn91oeJYPGbLaJAZbJqd3jxbDcY6m3d8e2fSE06_E_ZwXXosQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--
--
Anthony Flury
email : *Anthon...@btinternet.com*
Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>*

Melvyn Sopacua

unread,
May 14, 2018, 6:29:20 AM5/14/18
to django...@googlegroups.com
Hi Mark,

On zondag 13 mei 2018 18:11:07 CEST Mark Phillips wrote:
> What should be unit tested in models.py? I assume the storing of data and
> retrieving of data from the database does not need to be tested, as I
> further assume django has a full set of tests for those operations.

You should test your business requirements. Even for those operations. Best
illustrated:

# models.py
class Customer(models.Model):
email = models.EmailField()
name = models.CharField(max_length=100)

# tests.py

class CustomerTestCase(TestCase):
def test_create(self):
data = { 'email': 'in...@example.com', name='John Doe' }
first = Customer.objects.create(**data)
with self.assertRaisesMessage(IntegrityError, 'duplicate key value'):
second = Customer.objects.create(**data)

This test will fail cause there's no unique constrain on email, which is
likely to be a business requirement. You should test it. Especially because if
someone refactor years down the line and accidentally removes the requirement,
you have a problem.

> * Labels for all fields
> something like
> def test_first_name_label(self):
> author=Author.objects.get(id=1)
> field_label = author._meta.get_field('first_name').verbose_name
> self.assertEquals(field_label,'first name')

I would say this is exactly the thing you wouldn't test: anything dealing with
_meta. Test these further downstream, by verifying form labels and their
translated version if it applies. And I'd say the exact wording of a form
label may not be at all important, so you could skip this.

--
Melvyn Sopacua
Reply all
Reply to author
Forward
0 new messages