2
0

tests.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from __future__ import unicode_literals
  2. from datetime import datetime
  3. from django.test import TestCase
  4. from django.utils import six
  5. from .models import Article, Reporter, Writer
  6. class M2MIntermediaryTests(TestCase):
  7. def test_intermeiary(self):
  8. r1 = Reporter.objects.create(first_name="John", last_name="Smith")
  9. r2 = Reporter.objects.create(first_name="Jane", last_name="Doe")
  10. a = Article.objects.create(
  11. headline="This is a test", pub_date=datetime(2005, 7, 27)
  12. )
  13. w1 = Writer.objects.create(reporter=r1, article=a, position="Main writer")
  14. w2 = Writer.objects.create(reporter=r2, article=a, position="Contributor")
  15. self.assertQuerysetEqual(
  16. a.writer_set.select_related().order_by("-position"), [
  17. ("John Smith", "Main writer"),
  18. ("Jane Doe", "Contributor"),
  19. ],
  20. lambda w: (six.text_type(w.reporter), w.position)
  21. )
  22. self.assertEqual(w1.reporter, r1)
  23. self.assertEqual(w2.reporter, r2)
  24. self.assertEqual(w1.article, a)
  25. self.assertEqual(w2.article, a)
  26. self.assertQuerysetEqual(
  27. r1.writer_set.all(), [
  28. ("John Smith", "Main writer")
  29. ],
  30. lambda w: (six.text_type(w.reporter), w.position)
  31. )