tests.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """Django model tests."""
  2. from datetime import datetime
  3. from django.test import TestCase
  4. from testapp.models import Question, Choice
  5. class NoDatabaseTestCase(TestCase):
  6. """Tests that don't read or write to the database."""
  7. def test_question_str(self):
  8. """Test Question.__str__ method."""
  9. question = Question(question_text="What is your name?")
  10. self.assertEqual("What is your name?", str(question))
  11. def test_choice_str(self):
  12. """Test Choice.__str__ method."""
  13. choice = Choice(choice_text='My name is Sir Lancelot of Camelot.')
  14. self.assertEqual('My name is Sir Lancelot of Camelot.', str(choice))
  15. class UsesDatabaseTestCase(TestCase):
  16. """Tests that read and write to the database."""
  17. def test_question(self):
  18. """Test that votes is initialized to 0."""
  19. question = Question.objects.create(
  20. question_text="What is your quest?", pub_date=datetime(1975, 4, 9))
  21. Choice.objects.create(
  22. question=question, choice_text="To seek the Holy Grail.")
  23. self.assertTrue(question.choice_set.exists())
  24. the_choice = question.choice_set.get()
  25. self.assertEqual(0, the_choice.votes)
  26. class UsesFixtureTestCase(TestCase):
  27. """Tests that use a test fixture."""
  28. fixtures = ["testdata.json"]
  29. def test_fixture_loaded(self):
  30. """Test that fixture was loaded."""
  31. question = Question.objects.get()
  32. self.assertEqual(
  33. 'What is your favorite color?', question.question_text)
  34. self.assertEqual(datetime(1975, 4, 9), question.pub_date)
  35. choice = question.choice_set.get()
  36. self.assertEqual("Blue.", choice.choice_text)
  37. self.assertEqual(3, choice.votes)