diff --git a/example2/polls/tests/__init__.py b/example2/polls/tests/__init__.py index f062221..b04a142 100644 --- a/example2/polls/tests/__init__.py +++ b/example2/polls/tests/__init__.py @@ -1 +1 @@ -from test_views import * +from test_models import * diff --git a/example2/polls/tests/test_models.py b/example2/polls/tests/test_models.py new file mode 100644 index 0000000..badd16d --- /dev/null +++ b/example2/polls/tests/test_models.py @@ -0,0 +1,81 @@ +from django.test import TestCase +from django.utils import timezone + +from polls.models import Poll +from polls.models import Choice + + +class PollTestCase(TestCase): + + def setUp(self): + self.poll = Poll.objects.create(question = "mine", pub_date = timezone.now()) + self.poll.save() + + def test_creation(self): + p = Poll.objects.create(question = "lo lo", pub_date = timezone.now()) + p.save() + self.assertEqual(Poll.objects.count(), 2) # Cause setup created one already + + def test_update(self): + # TODO Add code + # change self.poll.question to "yours" + self.poll.question = "yours" + # do self.poll.save() + self.poll.save() + + # TODO Add assertions + # make p = Poll.objects.get() + p = Poll.objects.get() + # add self.assertEqual(p.question, "yours") + self.assertEqual(p.question, "yours") + + def test_delete(self): + # TODO Add code + # get from the db using poll question + p = Poll.objects.get() + # delete poll from the db + p.delete() + + # TODO Add assertions + # check if d is empty + self.assertEqual(Poll.objects.count(), 0) + + +class ChoiceTestCase(TestCase): + + def setUp(self): + self.poll = Poll.objects.create(question = "mine", pub_date = timezone.now()) + self.poll.save() + self.choice = Choice.objects.create(poll=self.poll, choice_text = "first text", votes = 2) + self.choice.save() + + def test_choice_creation(self): + # code + # add another choice + p = Choice.objects.create(poll=self.poll, choice_text = "second text", votes = 5) + p.save() + + # assertion + #check that there are two choices + self.assertEqual(Choice.objects.count(), 2) + + def test_choice_update(self): + # code + # change a choice + self.choice.choice_text = "third text" + self.choice.save() + p = Choice.objects.get() + + # assertion + # check the choice is egal to the new choice + self.assertEqual(p.choice_text, "third text") + + def test_choice_delete(self): + # code + # get Choice obj and delete it + p = Choice.objects.get() + p.delete() + + # assertion + # check there are nothing in db + self.assertEqual(Choice.objects.count(), 0) \ No newline at end of file diff --git a/runtests.py b/runtests.py index 6e2cf3f..6e53d07 100755 --- a/runtests.py +++ b/runtests.py @@ -2,15 +2,15 @@ import os import sys -os.environ['DJANGO_SETTINGS_MODULE'] = 'example.settings' -exampleproject_dir = os.path.join(os.path.dirname(__file__), 'example') +os.environ['DJANGO_SETTINGS_MODULE'] = 'example2.settings' +exampleproject_dir = os.path.join(os.path.dirname(__file__), 'example2') sys.path.insert(0, exampleproject_dir) from django.test.utils import get_runner from django.conf import settings -def runtests(tests=('blog', 'djadmin2',)): +def runtests(tests=('polls', 'djadmin2',)): ''' Takes a list as first argument, enumerating the apps and specific testcases that should be executed. The syntax is the same as for what you would pass