Permalink
Browse files

Merge branch 'workshop' of github.com:hjwp/Test-Driven-Django-Tutoria…

…l into workshop

Conflicts:
	functional_tests.py
	mysite/fts/test_admin.py
  • Loading branch information...
2 parents 157728a + aa1efce commit b50089838a8e30ec44503c75a47c76dd70a31516 @hjwp committed Nov 30, 2011
File renamed without changes.
Deleted file not rendered
View
Deleted file not rendered
View
Deleted file not rendered
View
Deleted file not rendered
Deleted file not rendered
Deleted file not rendered
Deleted file not rendered
Deleted file not rendered
Deleted file not rendered
View
View
No changes.
No changes.
@@ -1,73 +0,0 @@
-from functional_tests import FunctionalTest, ROOT
-from selenium.webdriver.common.keys import Keys
-
-class TestPollsAdmin(FunctionalTest):
-
- def test_can_create_new_poll_via_admin_site(self):
-
- # Gertrude opens her web browser, and goes to the admin page
- self.browser.get(ROOT + '/admin/')
-
- # She sees the familiar 'Django administration' heading
- body = self.browser.find_element_by_tag_name('body')
- self.assertIn('Django administration', body.text)
-
- # She types in her username and passwords and hits return
- username_field = self.browser.find_element_by_name('username')
- username_field.send_keys('admin')
-
- password_field = self.browser.find_element_by_name('password')
- password_field.send_keys('adm1n')
- password_field.send_keys(Keys.RETURN)
-
- # She now sees a couple of hyperlink that says "Polls"
- polls_links = self.browser.find_elements_by_link_text('Polls')
- self.assertEquals(len(polls_links), 2)
-
- # The second one looks more exciting, so she clicks it
- polls_links[1].click()
-
- # She is taken to the polls listing page, which shows she has
- # no polls yet
- body = self.browser.find_element_by_tag_name('body')
- self.assertIn('0 polls', body.text)
-
- # She sees a link to 'add' a new poll, so she clicks it
- new_poll_link = self.browser.find_element_by_link_text('Add poll')
- new_poll_link.click()
-
- # She sees some input fields for "Question" and "Date published"
- body = self.browser.find_element_by_tag_name('body')
- self.assertIn('Question:', body.text)
- self.assertIn('Date published:', body.text)
-
- # She types in an interesting question for the Poll
- question_field = self.browser.find_element_by_name('question')
- question_field.send_keys("How awesome is Test-Driven Development?")
-
- # She sets the date and time of publication - it'll be a new year's
- # poll!
- date_field = self.browser.find_element_by_name('pub_date_0')
- date_field.send_keys('01/01/12')
- time_field = self.browser.find_element_by_name('pub_date_1')
- time_field.send_keys('00:00')
-
- # She sees she can enter choices for the Poll. She adds three
- choice_1 = self.browser.find_element_by_name('choice_set-0-choice')
- choice_1.send_keys('Very awesome')
- choice_2 = self.browser.find_element_by_name('choice_set-1-choice')
- choice_2.send_keys('Quite awesome')
- choice_3 = self.browser.find_element_by_name('choice_set-2-choice')
- choice_3.send_keys('Moderately awesome')
-
- save_button = self.browser.find_element_by_css_selector("input[value='Save']")
- save_button.click()
-
- # She is returned to the "Polls" listing, where she can see her
- # new poll, listed as a clickable link
- new_poll_links = self.browser.find_elements_by_link_text(
- "How awesome is Test-Driven Development?"
- )
- self.assertEquals(len(new_poll_links), 1)
-
-
@@ -1,108 +0,0 @@
-from functional_tests import FunctionalTest, ROOT
-from selenium.webdriver.common.keys import Keys
-
-class TestPolls(FunctionalTest):
-
- def _setup_polls_via_admin(self):
- # Gertrude logs into the admin site
- self.browser.get(ROOT + '/admin/')
- username_field = self.browser.find_element_by_name('username')
- username_field.send_keys('admin')
- password_field = self.browser.find_element_by_name('password')
- password_field.send_keys('adm1n')
- password_field.send_keys(Keys.RETURN)
-
- # She follows the link to the Polls app, and adds a new Poll
- self.browser.find_elements_by_link_text('Polls')[1].click()
- self.browser.find_element_by_link_text('Add poll').click()
-
- # She enters its name, and uses the 'today' and 'now' buttons to set
- # the publish date
- question_field = self.browser.find_element_by_name('question')
- question_field.send_keys("How awesome is Test-Driven Development?")
- self.browser.find_element_by_link_text('Today').click()
- self.browser.find_element_by_link_text('Now').click()
-
- # She sees she can enter choices for the Poll. She adds three
- choice_1 = self.browser.find_element_by_name('choice_set-0-choice')
- choice_1.send_keys('Very awesome')
- choice_2 = self.browser.find_element_by_name('choice_set-1-choice')
- choice_2.send_keys('Quite awesome')
- choice_3 = self.browser.find_element_by_name('choice_set-2-choice')
- choice_3.send_keys('Moderately awesome')
-
- # She saves her new poll
- save_button = self.browser.find_element_by_css_selector("input[value='Save']")
- save_button.click()
-
- # She is returned to the "Polls" listing, where she can see her
- # new poll, listed as a clickable link
- new_poll_links = self.browser.find_elements_by_link_text(
- "How awesome is Test-Driven Development?"
- )
- self.assertEquals(len(new_poll_links), 1)
-
- # She logs out of the admin site
- self.browser.find_element_by_link_text('Log out').click()
-
-
- def test_voting_on_a_new_poll(self):
- # First, Gertrude the administrator logs into the admin site and
- # creates a couple of new Polls, and their response choices
- self._setup_polls_via_admin()
-
- # Now, Herbert the regular user goes to the homepage of the site. He
- # sees a list of polls.
- self.browser.get(ROOT)
- heading = self.browser.find_element_by_tag_name('h1')
- self.assertEquals(heading.text, 'Polls')
-
- # He clicks on the link to the first Poll, which is called
- # 'How awesome is test-driven development?'
- first_poll_title = 'How awesome is Test-Driven Development?'
- self.browser.find_element_by_link_text(first_poll_title).click()
-
- # He is taken to a poll 'results' page, which says
- # "no-one has voted on this poll yet"
- main_heading = self.browser.find_element_by_tag_name('h1')
- self.assertEquals(main_heading.text, 'Poll Results')
- sub_heading = self.browser.find_element_by_tag_name('h2')
- self.assertEquals(sub_heading.text, first_poll_title)
- body = self.browser.find_element_by_tag_name('body')
- self.assertIn('No-one has voted on this poll yet', body.text)
-
- # He also sees a form, which offers him several choices.
- # There are three options with radio buttons
- choice_inputs = self.browser.find_elements_by_css_selector(
- "input[type='radio']"
- )
- self.assertEquals(len(choice_inputs), 3)
-
- # The buttons have labels to explain them
- choice_labels = choice_inputs = self.browser.find_elements_by_tag_name('label')
- choices_text = [c.text for c in choice_labels]
- self.assertEquals(choices_text, [
- 'Vote:', # this label is auto-generated for the whole form
- 'Very awesome',
- 'Quite awesome',
- 'Moderately awesome',
- ])
- # He decided to select "very awesome", which is answer #1
- chosen = self.browser.find_element_by_css_selector(
- "input[value='1']"
- )
- chosen.click()
-
- # Herbert clicks 'submit'
- self.browser.find_element_by_css_selector(
- "input[type='submit']"
- ).click()
-
- # The page refreshes, and he sees that his choice
- # has updated the results. they now say
- # "100 %: very awesome".
-
- # The page also says "1 votes"
-
- # Satisfied, he goes back to sleep
-
View
@@ -1,11 +0,0 @@
-#!/usr/bin/python
-from django.core.management import execute_manager
-try:
- import settings # Assumed to be in the same directory.
-except ImportError:
- import sys
- sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
- sys.exit(1)
-
-if __name__ == "__main__":
- execute_manager(settings)
No changes.
View
@@ -1,11 +0,0 @@
-from django.contrib import admin
-from polls.models import Choice, Poll
-
-class ChoiceInline(admin.StackedInline):
- model = Choice
- extra = 3
-
-class PollAdmin(admin.ModelAdmin):
- inlines = [ChoiceInline]
-
-admin.site.register(Poll, PollAdmin)
View
@@ -1,22 +0,0 @@
-from django import forms
-
-class PollVoteForm(forms.Form):
- vote = forms.ChoiceField(widget=forms.RadioSelect())
-
- def __init__(self, poll):
- super(self.__class__, self).__init__()
- self.fields['vote'].choices = [(c.id, c.choice) for c in poll.choice_set.all()]
-
-
-'''
-(saving this for use later...)
-from polls.models import Choice
-class PollVoteForm2(Form):
- vote = forms.ModelChoiceField(queryset=[])
- def __init__(self, poll):
- super(self.__class__, self).__init__()
- self.fields['vote'].queryset = poll.choice_set.all()
-'''
-
-
-
@@ -1,14 +0,0 @@
-from django.db import models
-
-class Poll(models.Model):
- question = models.CharField(max_length=200)
- pub_date = models.DateTimeField(verbose_name='Date published')
-
- def __unicode__(self):
- return self.question
-
-
-class Choice(models.Model):
- poll = models.ForeignKey(Poll)
- choice = models.CharField(max_length=200)
- votes = models.IntegerField(default=0)
@@ -1,15 +0,0 @@
-<html>
- <body>
- <h1>Poll Results</h1>
-
- <h2>{{poll.question}}</h2>
-
- <p>No-one has voted on this poll yet</p>
-
- <h3>Add your vote</h3>
- {{form.as_p}}
- <input type="submit" />
-
-
- </body>
-</html>
@@ -1,8 +0,0 @@
-<html>
- <body>
- <h1>Polls</h1>
- {% for poll in polls %}
- <a href="{% url mysite.polls.views.poll poll.id %}">{{ poll.question }}</a>
- {% endfor %}
- </body>
-</html>
Oops, something went wrong. Retry.

0 comments on commit b500898

Please sign in to comment.