tests.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from django.core.exceptions import FieldError
  2. from django.test import TestCase
  3. from models import Author, Article
  4. class CustomColumnsTests(TestCase):
  5. def test_db_column(self):
  6. a1 = Author.objects.create(first_name="John", last_name="Smith")
  7. a2 = Author.objects.create(first_name="Peter", last_name="Jones")
  8. art = Article.objects.create(headline="Django lets you build Web apps easily")
  9. art.authors = [a1, a2]
  10. # Although the table and column names on Author have been set to custom
  11. # values, nothing about using the Author model has changed...
  12. # Query the available authors
  13. self.assertQuerysetEqual(
  14. Author.objects.all(), [
  15. "Peter Jones", "John Smith",
  16. ],
  17. unicode
  18. )
  19. self.assertQuerysetEqual(
  20. Author.objects.filter(first_name__exact="John"), [
  21. "John Smith",
  22. ],
  23. unicode
  24. )
  25. self.assertEqual(
  26. Author.objects.get(first_name__exact="John"),
  27. a1,
  28. )
  29. self.assertRaises(FieldError,
  30. lambda: Author.objects.filter(firstname__exact="John")
  31. )
  32. a = Author.objects.get(last_name__exact="Smith")
  33. a.first_name = "John"
  34. a.last_name = "Smith"
  35. self.assertRaises(AttributeError, lambda: a.firstname)
  36. self.assertRaises(AttributeError, lambda: a.last)
  37. # Although the Article table uses a custom m2m table,
  38. # nothing about using the m2m relationship has changed...
  39. # Get all the authors for an article
  40. self.assertQuerysetEqual(
  41. art.authors.all(), [
  42. "Peter Jones",
  43. "John Smith",
  44. ],
  45. unicode
  46. )
  47. # Get the articles for an author
  48. self.assertQuerysetEqual(
  49. a.article_set.all(), [
  50. "Django lets you build Web apps easily",
  51. ],
  52. lambda a: a.headline
  53. )
  54. # Query the authors across the m2m relation
  55. self.assertQuerysetEqual(
  56. art.authors.filter(last_name='Jones'), [
  57. "Peter Jones"
  58. ],
  59. unicode
  60. )