/tests/modeltests/many_to_one_null/tests.py

https://code.google.com/p/mango-py/ · Python · 84 lines · 57 code · 8 blank · 19 comment · 0 complexity · 59f1a63240508cdfcb1c6dec2508ab22 MD5 · raw file

  1. from django.test import TestCase
  2. from models import Reporter, Article
  3. class ManyToOneNullTests(TestCase):
  4. def setUp(self):
  5. # Create a Reporter.
  6. self.r = Reporter(name='John Smith')
  7. self.r.save()
  8. # Create an Article.
  9. self.a = Article(headline="First", reporter=self.r)
  10. self.a.save()
  11. # Create an Article via the Reporter object.
  12. self.a2 = self.r.article_set.create(headline="Second")
  13. # Create an Article with no Reporter by passing "reporter=None".
  14. self.a3 = Article(headline="Third", reporter=None)
  15. self.a3.save()
  16. # Create another article and reporter
  17. self.r2 = Reporter(name='Paul Jones')
  18. self.r2.save()
  19. self.a4 = self.r2.article_set.create(headline='Fourth')
  20. def test_get_related(self):
  21. self.assertEqual(self.a.reporter.id, self.r.id)
  22. # Article objects have access to their related Reporter objects.
  23. r = self.a.reporter
  24. self.assertEqual(r.id, self.r.id)
  25. def test_created_via_related_set(self):
  26. self.assertEqual(self.a2.reporter.id, self.r.id)
  27. def test_related_set(self):
  28. # Reporter objects have access to their related Article objects.
  29. self.assertQuerysetEqual(self.r.article_set.all(),
  30. ['<Article: First>', '<Article: Second>'])
  31. self.assertQuerysetEqual(self.r.article_set.filter(headline__startswith='Fir'),
  32. ['<Article: First>'])
  33. self.assertEqual(self.r.article_set.count(), 2)
  34. def test_created_without_related(self):
  35. self.assertEqual(self.a3.reporter, None)
  36. # Need to reget a3 to refresh the cache
  37. a3 = Article.objects.get(pk=self.a3.pk)
  38. self.assertRaises(AttributeError, getattr, a3.reporter, 'id')
  39. # Accessing an article's 'reporter' attribute returns None
  40. # if the reporter is set to None.
  41. self.assertEqual(a3.reporter, None)
  42. # To retrieve the articles with no reporters set, use "reporter__isnull=True".
  43. self.assertQuerysetEqual(Article.objects.filter(reporter__isnull=True),
  44. ['<Article: Third>'])
  45. # We can achieve the same thing by filtering for the case where the
  46. # reporter is None.
  47. self.assertQuerysetEqual(Article.objects.filter(reporter=None),
  48. ['<Article: Third>'])
  49. # Set the reporter for the Third article
  50. self.assertQuerysetEqual(self.r.article_set.all(),
  51. ['<Article: First>', '<Article: Second>'])
  52. self.r.article_set.add(a3)
  53. self.assertQuerysetEqual(self.r.article_set.all(),
  54. ['<Article: First>', '<Article: Second>', '<Article: Third>'])
  55. # Remove an article from the set, and check that it was removed.
  56. self.r.article_set.remove(a3)
  57. self.assertQuerysetEqual(self.r.article_set.all(),
  58. ['<Article: First>', '<Article: Second>'])
  59. self.assertQuerysetEqual(Article.objects.filter(reporter__isnull=True),
  60. ['<Article: Third>'])
  61. def test_remove_from_wrong_set(self):
  62. self.assertQuerysetEqual(self.r2.article_set.all(), ['<Article: Fourth>'])
  63. # Try to remove a4 from a set it does not belong to
  64. self.assertRaises(Reporter.DoesNotExist, self.r.article_set.remove, self.a4)
  65. self.assertQuerysetEqual(self.r2.article_set.all(), ['<Article: Fourth>'])
  66. def test_assign_clear_related_set(self):
  67. # Use descriptor assignment to allocate ForeignKey. Null is legal, so
  68. # existing members of set that are not in the assignment set are set null
  69. self.r2.article_set = [self.a2, self.a3]
  70. self.assertQuerysetEqual(self.r2.article_set.all(),
  71. ['<Article: Second>', '<Article: Third>'])
  72. # Clear the rest of the set
  73. self.r.article_set.clear()
  74. self.assertQuerysetEqual(self.r.article_set.all(), [])
  75. self.assertQuerysetEqual(Article.objects.filter(reporter__isnull=True),
  76. ['<Article: First>', '<Article: Fourth>'])