tests.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import datetime
  2. from django.db import models
  3. from django.test import TestCase
  4. from django.test.utils import isolate_apps
  5. from .models import InternationalArticle
  6. class SimpleTests(TestCase):
  7. def test_international(self):
  8. a = InternationalArticle.objects.create(
  9. headline='Girl wins €12.500 in lottery',
  10. pub_date=datetime.datetime(2005, 7, 28)
  11. )
  12. self.assertEqual(str(a), 'Girl wins €12.500 in lottery')
  13. @isolate_apps('str')
  14. def test_defaults(self):
  15. """
  16. The default implementation of __str__ and __repr__ should return
  17. instances of str.
  18. """
  19. class Default(models.Model):
  20. pass
  21. obj = Default()
  22. # Explicit call to __str__/__repr__ to make sure str()/repr() don't
  23. # coerce the returned value.
  24. self.assertIsInstance(obj.__str__(), str)
  25. self.assertIsInstance(obj.__repr__(), str)
  26. self.assertEqual(str(obj), 'Default object (None)')
  27. self.assertEqual(repr(obj), '<Default: Default object (None)>')
  28. obj2 = Default(pk=100)
  29. self.assertEqual(str(obj2), 'Default object (100)')
  30. self.assertEqual(repr(obj2), '<Default: Default object (100)>')