def test_choice_saves_trial(self): user_test = User.objects.create_user('george', '', 'somethingpassword') user_test.save() first_trial = Trial() first_trial.user = user_test first_trial.save() first_choice = Choice() first_choice.trial = first_trial first_choice.save() saved_choices = Choice.objects.all() self.assertEqual(saved_choices.count(), 1) first_saved_choice = saved_choices[0] first_saved_trial = first_saved_choice.trial self.assertEqual(first_saved_trial, first_trial)
def choose_door(request): username_logged_in = request.user.username if request.method == 'POST': user_logged_in = request.user # Here I need to retrieve the latest trial # find the trials by this user trial_existing_objects = Trial.objects.filter( user=request.user ) trial_existing = trial_existing_objects.last() choice = Choice() choice.door_number = request.POST.get('door_chosen', 0) choice.trial = trial_existing choice.first_or_second_choice = 1 choice.save() result = Result() result.door_number = randint(1, 3) result.trial = trial_existing result.save() return redirect('/user/' + username_logged_in + '/door-result') else: pass
def test_saving_and_retrieving_choices(self): user_test = User.objects.create_user('george', '', 'somethingpassword') user_test.save() first_choice = Choice() first_choice.door_number = 1 first_choice.save() second_choice = Choice() second_choice.door_number = 2 second_choice.save() saved_choices = Choice.objects.all() self.assertEqual(saved_choices.count(), 2) first_saved_choice = saved_choices[0] second_saved_choice = saved_choices[1] self.assertEqual(first_saved_choice.door_number, 1) self.assertEqual(second_saved_choice.door_number, 2)
def choose_final_door(request): if request.method == 'POST': user_logged_in = request.user username_logged_in = user_logged_in.username # find the trials by this user trial_existing_objects = Trial.objects.filter( user=request.user ) trial_existing = trial_existing_objects.last() choice = Choice() choice.door_number = request.POST.get('final_door_chosen', 0) choice.trial = trial_existing choice.first_or_second_choice = 2 choice.save() return redirect('/user/' + username_logged_in + '/final-door-result')
def test_two_users_create_models_at_same_time(self): # set up the users user_test_one = User.objects.create_user('Acho', '', 'tu_ajedrez') user_test_one.save() user_test_two = User.objects.create_user('Darren', '', 'just_a_sea_between_us') user_test_two.save() # set up a trial for each user trial_user_one = Trial() trial_user_one.user = user_test_one trial_user_one.save() trial_user_two = Trial() trial_user_two.user = user_test_two trial_user_two.save() # assign a first choice for each trial of each user choice_one_user_one = Choice() choice_one_user_one.trial = trial_user_one choice_one_user_one.first_or_second_choice = 1 choice_one_user_one.door_number = 1 choice_one_user_one.save() choice_one_user_two = Choice() choice_one_user_two.trial = trial_user_two choice_one_user_two.first_or_second_choice = 1 choice_one_user_two.door_number = 2 choice_one_user_two.save() # save a result for each user result_user_one = Result() result_user_one.trial = trial_user_one result_user_one.door_number = 3 result_user_one.save() result_user_two = Result() result_user_two.trial = trial_user_two result_user_two.door_number = 1 result_user_two.save() # save a second choice for each user final_choice_user_one = Choice() final_choice_user_one.trial = trial_user_one final_choice_user_one.first_or_second_choice = 2 final_choice_user_one.door_number = 4 final_choice_user_one.save() final_choice_user_two = Choice() final_choice_user_two.trial = trial_user_two final_choice_user_two.first_or_second_choice = 2 final_choice_user_two.door_number = 5 final_choice_user_two.save() # check results are consistent # check trials saved correctly saved_trial_user_one = Trial.objects.get(user=user_test_one) self.assertEqual(saved_trial_user_one, trial_user_one) saved_trial_user_two = Trial.objects.get(user=user_test_two) self.assertEqual(saved_trial_user_two, trial_user_two) # check first choices saved correctly saved_choice_one_user_one = Choice.objects.get( trial=saved_trial_user_one, first_or_second_choice=1, ) saved_choice_one_user_two = Choice.objects.get( trial=saved_trial_user_two, first_or_second_choice=1, ) self.assertEqual(saved_choice_one_user_one, choice_one_user_one) self.assertEqual(saved_choice_one_user_two, choice_one_user_two) # check second choices saved correctly saved_final_choice_user_one = Choice.objects.get( trial=saved_trial_user_one, first_or_second_choice=2) saved_final_choice_user_two = Choice.objects.get( trial=saved_trial_user_two, first_or_second_choice=2) self.assertEqual(saved_final_choice_user_one, final_choice_user_one) self.assertEqual(saved_final_choice_user_two, final_choice_user_two) saved_result_one = Result.objects.get(trial=saved_trial_user_one) saved_result_two = Result.objects.get(trial=saved_trial_user_two) self.assertEqual(saved_result_one, result_user_one) self.assertEqual(saved_result_two, result_user_two)
def test_saving_and_retrieving_user_name_from_choices(self): user_test_one = User.objects.create_user('george', '', 'somethingpassword') user_test_one.save() first_trial = Trial() first_trial.user = user_test_one first_trial.save() first_choice = Choice() first_choice.door_number = 1 first_choice.trial = first_trial first_choice.save() user_test_two = User.objects.create_user('brian', '', 'not-the-messiah') user_test_two.save() second_trial = Trial() second_trial.user = user_test_two second_trial.save() second_choice = Choice() second_choice.door_number = 2 second_choice.trial = second_trial second_choice.save() saved_choices = Choice.objects.all() self.assertEqual(saved_choices.count(), 2) first_saved_choice = saved_choices[0] first_saved_trial = first_saved_choice.trial first_saved_user = first_saved_trial.user second_saved_choice = saved_choices[1] second_saved_trial = second_saved_choice.trial second_saved_user = second_saved_trial.user self.assertEqual(first_saved_user, user_test_one) self.assertEqual(second_saved_user, user_test_two)
def test_saving_and_retrieving_final_choices_keeps_same_door_number(self): user_test = User.objects.create_user('george', '', 'somethingpassword') user_test.save() trial = Trial() trial.user = user_test trial.save() first_choice = Choice() first_choice.door_number = 3 first_choice.first_or_second_choice = 1 first_choice.trial = trial first_choice.save() # keeps same number final_choice = Choice() final_choice.door_number = 3 final_choice.first_or_second_choice = 2 final_choice.trial = trial final_choice.save() first_saved_choice = Choice.objects.get(trial=trial, first_or_second_choice=1) final_saved_choice = Choice.objects.get(trial=trial, first_or_second_choice=2) self.assertEqual(first_saved_choice.door_number, 3) self.assertEqual(first_saved_choice.first_or_second_choice, 1) self.assertEqual(final_saved_choice.door_number, 3) self.assertEqual(final_saved_choice.first_or_second_choice, 2)
def test_two_users_at_same_time_make_second_choice(self): # set up inital saved data on db # set up the users user_test_one = User.objects.create_user( 'Acho', '', 'tu_ajedrez') user_test_one.save() user_test_two = User.objects.create_user( 'Darren', '', 'just_a_sea_between_us') user_test_two.save() # set up a trial for each user trial_user_one = Trial() trial_user_one.user = user_test_one trial_user_one.save() trial_user_two = Trial() trial_user_two.user = user_test_two trial_user_two.save() # assign a first choice for each trial of each user choice_one_user_one = Choice() choice_one_user_one.trial = trial_user_one choice_one_user_one.first_or_second_choice = 1 choice_one_user_one.door_number = 1 choice_one_user_one.save() choice_one_user_two = Choice() choice_one_user_two.trial = trial_user_two choice_one_user_two.first_or_second_choice = 1 choice_one_user_two.door_number = 2 choice_one_user_two.save() # save a result for each user result_user_one = Result() result_user_one.trial = trial_user_one result_user_one.door_number = 3 result_user_one.save() result_user_two = Result() result_user_two.trial = trial_user_two result_user_two.door_number = 1 result_user_two.save() self.client.login(username='******', password='******') response_one = self.client.post( '/user/Acho/door-result', {'door_chosen': 4} ) self.client.login(username='******', password='******' ) response_two = self.client.post( '/user/Darren/door-result', {'door_chosen': 5} ) # check has saved 2 choices for each user saved_choices = Choice.objects.all() username_list = [] for choice in saved_choices: username_list.append(choice.trial.user.username) self.assertEqual(username_list.count('Acho'), 2) self.assertEqual(username_list.count('Darren'), 2) # retrieve saved second choices '''