tests.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from __future__ import unicode_literals
  2. from django.core import management
  3. from django.db import transaction
  4. from django.test import TestCase, TransactionTestCase
  5. from .models import Article, Book
  6. class SampleTestCase(TestCase):
  7. fixtures = ['fixture1.json', 'fixture2.json']
  8. def testClassFixtures(self):
  9. "Test cases can load fixture objects into models defined in packages"
  10. self.assertEqual(Article.objects.count(), 3)
  11. self.assertQuerysetEqual(
  12. Article.objects.all(),[
  13. "Django conquers world!",
  14. "Copyright is fine the way it is",
  15. "Poker has no place on ESPN",
  16. ],
  17. lambda a: a.headline
  18. )
  19. class TestNoInitialDataLoading(TransactionTestCase):
  20. def test_syncdb(self):
  21. transaction.set_autocommit(False)
  22. try:
  23. Book.objects.all().delete()
  24. management.call_command(
  25. 'syncdb',
  26. verbosity=0,
  27. load_initial_data=False
  28. )
  29. self.assertQuerysetEqual(Book.objects.all(), [])
  30. transaction.rollback()
  31. finally:
  32. transaction.set_autocommit(True)
  33. def test_flush(self):
  34. # Test presence of fixture (flush called by TransactionTestCase)
  35. self.assertQuerysetEqual(
  36. Book.objects.all(), [
  37. 'Achieving self-awareness of Python programs'
  38. ],
  39. lambda a: a.name
  40. )
  41. transaction.set_autocommit(False)
  42. try:
  43. management.call_command(
  44. 'flush',
  45. verbosity=0,
  46. interactive=False,
  47. commit=False,
  48. load_initial_data=False
  49. )
  50. self.assertQuerysetEqual(Book.objects.all(), [])
  51. transaction.rollback()
  52. finally:
  53. transaction.set_autocommit(True)
  54. class FixtureTestCase(TestCase):
  55. def test_initial_data(self):
  56. "Fixtures can load initial data into models defined in packages"
  57. # syncdb introduces 1 initial data object from initial_data.json
  58. self.assertQuerysetEqual(
  59. Book.objects.all(), [
  60. 'Achieving self-awareness of Python programs'
  61. ],
  62. lambda a: a.name
  63. )
  64. def test_loaddata(self):
  65. "Fixtures can load data into models defined in packages"
  66. # Load fixture 1. Single JSON file, with two objects
  67. management.call_command("loaddata", "fixture1.json", verbosity=0, commit=False)
  68. self.assertQuerysetEqual(
  69. Article.objects.all(), [
  70. "Time to reform copyright",
  71. "Poker has no place on ESPN",
  72. ],
  73. lambda a: a.headline,
  74. )
  75. # Load fixture 2. JSON file imported by default. Overwrites some
  76. # existing objects
  77. management.call_command("loaddata", "fixture2.json", verbosity=0, commit=False)
  78. self.assertQuerysetEqual(
  79. Article.objects.all(), [
  80. "Django conquers world!",
  81. "Copyright is fine the way it is",
  82. "Poker has no place on ESPN",
  83. ],
  84. lambda a: a.headline,
  85. )
  86. # Load a fixture that doesn't exist
  87. management.call_command("loaddata", "unknown.json", verbosity=0, commit=False)
  88. self.assertQuerysetEqual(
  89. Article.objects.all(), [
  90. "Django conquers world!",
  91. "Copyright is fine the way it is",
  92. "Poker has no place on ESPN",
  93. ],
  94. lambda a: a.headline,
  95. )