def test_creating_some_hobbies_for_a_signup(self): # start by creating a new Poll object signup = SignUp() signup.email="*****@*****.**" signup.full_name = "Amerigo" signup.save() # now create a Choice object hobby = Hobby() # link it with our Poll hobby.signup = signup # give it some text hobby.hobby = "Reading" # save it hobby.save() # try retrieving it from the database, using the signup object's reverse # lookup signup_hobbies = signup.hobby_set.all() self.assertEquals(signup_hobbies.count(), 1) # finally, check its attributes have been saved hobby_from_db = signup_hobbies[0] self.assertEquals(hobby_from_db, hobby) self.assertEquals(hobby_from_db.hobby, "Reading")
def test_creating_some_hobbies_for_a_signup(self): # start by creating a new Poll object signup = SignUp() signup.email = "*****@*****.**" signup.full_name = "Amerigo" signup.save() # now create a Choice object hobby = Hobby() # link it with our Poll hobby.signup = signup # give it some text hobby.hobby = "Reading" # save it hobby.save() # try retrieving it from the database, using the signup object's reverse # lookup signup_hobbies = signup.hobby_set.all() self.assertEquals(signup_hobbies.count(), 1) # finally, check its attributes have been saved hobby_from_db = signup_hobbies[0] self.assertEquals(hobby_from_db, hobby) self.assertEquals(hobby_from_db.hobby, "Reading")
def test_page_shows_hobbies_using_form(self): # set up a signup with hobbies signup1 = SignUp(email='*****@*****.**', full_name='Filippo Insaghi') signup1.save() hobby1 = Hobby(signup=signup1, hobby='Fishing') hobby1.save() hobby2 = Hobby(signup=signup1, hobby='Swimming') hobby2.save() response = self.client.get('/signup/%d/' % (signup1.id, )) # check we've passed in a form of the right type self.assertTrue(isinstance(response.context['form'], SignUpVoteForm)) # and check the form is being used in the template, # by checking for the hobby text form = SignUpVoteForm(signup=signup1) print form.as_p() self.assertIn(hobby1.hobby, response.content) self.assertIn(hobby2.hobby, response.content)
def test_form_renders_poll_choices_as_radio_inputs(self): # set up a signup with a couple of hobbies signup1 = SignUp(email='*****@*****.**', full_name='Filippo Insaghi') signup1.save() hobby1 = Hobby(signup=signup1, hobby='Fishing') hobby1.save() hobby2 = Hobby(signup=signup1, hobby='Swimming') hobby2.save() # set up another poll to make sure we only see the right choices signup2 = SignUp(email='*****@*****.**', full_name="Cezaro Prandelli") signup2.save() hobby = Hobby(signup=signup2, hobby='Movies') hobby.save() # build a voting form for signup1 form = SignUpVoteForm(signup=signup1) # check it has a single field called 'vote', which has right choices: self.assertEquals(form.fields.keys(), ['vote']) # choices are tuples in the format (choice_number, choice_text): self.assertEquals(form.fields['vote'].hobbies, [ (hobby1.id, hobby1.hobby), (hobby2.id, hobby2.hobby), ]) # check it uses radio inputs to render print form.as_p()