tests.py 922 B

1234567891011121314151617181920212223242526272829
  1. from __future__ import unicode_literals
  2. from django.test import TestCase
  3. from .models import Person
  4. class PropertyTests(TestCase):
  5. def setUp(self):
  6. self.a = Person(first_name='John', last_name='Lennon')
  7. self.a.save()
  8. def test_getter(self):
  9. self.assertEqual(self.a.full_name, 'John Lennon')
  10. def test_setter(self):
  11. # The "full_name" property hasn't provided a "set" method.
  12. with self.assertRaises(AttributeError):
  13. setattr(self.a, 'full_name', 'Paul McCartney')
  14. # And cannot be used to initialize the class.
  15. with self.assertRaisesMessage(TypeError, "'full_name' is an invalid keyword argument"):
  16. Person(full_name='Paul McCartney')
  17. # But "full_name_2" has, and it can be used to initialize the class.
  18. a2 = Person(full_name_2='Paul McCartney')
  19. a2.save()
  20. self.assertEqual(a2.first_name, 'Paul')