test_default_related_name.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from django.test import TestCase
  2. from .models.default_related_name import Author, Book, Editor
  3. class DefaultRelatedNameTests(TestCase):
  4. def setUp(self):
  5. self.author = Author.objects.create(first_name="Dave", last_name="Loper")
  6. self.editor = Editor.objects.create(name="Test Editions",
  7. bestselling_author=self.author)
  8. self.book = Book.objects.create(title="Test Book", editor=self.editor)
  9. self.book.authors.add(self.author)
  10. self.book.save()
  11. def test_no_default_related_name(self):
  12. try:
  13. self.author.editor_set
  14. except AttributeError:
  15. self.fail("Author should have an editor_set relation.")
  16. def test_default_related_name(self):
  17. try:
  18. self.author.books
  19. except AttributeError:
  20. self.fail("Author should have a books relation.")
  21. def test_related_name_overrides_default_related_name(self):
  22. try:
  23. self.editor.edited_books
  24. except AttributeError:
  25. self.fail("Editor should have a edited_books relation.")
  26. def test_inheritance(self):
  27. try:
  28. # Here model_options corresponds to the name of the application used
  29. # in this test
  30. self.book.model_options_bookstores
  31. except AttributeError:
  32. self.fail("Book should have a model_options_bookstores relation.")
  33. def test_inheritance_with_overridden_default_related_name(self):
  34. try:
  35. self.book.editor_stores
  36. except AttributeError:
  37. self.fail("Book should have a editor_stores relation.")