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