def test_SurveyInstance(self): su = Survey.objects.get(id=1) r = Respondent.objects.get(id=1) si = SurveyInstance.objects.get(id=1) rsii = RatioSurveyInstanceItem.objects.get(id=1) self.assertIsInstance(si, SurveyInstance) self.assertEqual(si.respondent, r) self.assertEqual(si.survey, su) self.assertEqual(len(si.get_items()), 1) #check_completed self.assertEqual(si.check_completed(), False) self.assertEqual(si.completed, False) self.assertEqual(si.check_completed(), False) self.assertEqual(si.completed, False) rsii.answer_item(2) survey_logic.close_survey(su) self.assertEqual(si.check_completed(), True) self.assertEqual(si.completed, True)
def test_answer_survey_view(self): survey_instance = SurveyInstance.objects.get(id=1) survey = Survey.objects.get(id=1) #test that we can reach the non-paginated page by "just clicking the link" the first time survey_instance.consent_was_given = False survey_instance.save() response = self.client.get(reverse( 'surveys-answer-survey', args=[survey_instance.get_url_token()]), follow=True, secure=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'answer_survey.html') self.assertIn("Thank you for taking the time to answer this survey", response.content.decode()) self.assertNotIn("item_1", response.content.decode()) #test that we are redirected to paginated pages when trying to go to non-paginated, if consent was given already survey_instance.consent_was_given = True survey_instance.save() response = self.client.get(reverse( 'surveys-answer-survey', args=[survey_instance.get_url_token()]), follow=True, secure=True) self.assertRedirects( response, reverse('surveys-answer-survey-pages', args=[survey_instance.get_url_token(), 1]), 302, 200) self.assertNotIn("Thank you for taking the time to answer this survey", response.content.decode()) self.assertNotIn( "Please indicate that you consent to answer the survey to continue", response.content.decode()) self.assertIn( "Welcome back! We saved your place, so you can continue where you left off.", response.content.decode()) #test that we can NOT reach the page with pagination BEFORE consent is given survey_instance.consent_was_given = False survey_instance.save() response = self.client.get(reverse( 'surveys-answer-survey-pages', args=[survey_instance.get_url_token(), 1]), follow=True, secure=True) self.assertRedirects( response, reverse('surveys-answer-survey', args=[survey_instance.get_url_token()]), 302, 200) self.assertIn("Thank you for taking the time to answer this survey", response.content.decode()) self.assertIn( "Please indicate that you consent to answer the survey to continue", response.content.decode()) #test that we can reach the page with pagination AFTER consent is given survey_instance.consent_was_given = True survey_instance.save() response = self.client.get(reverse( 'surveys-answer-survey-pages', args=[survey_instance.get_url_token(), 1]), follow=True, secure=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'answer_survey.html') self.assertNotIn("Thank you for taking the time to answer this survey", response.content.decode()) self.assertIn("item_1", response.content.decode()) self.assertIn("item_2", response.content.decode()) self.assertIn("item_3", response.content.decode()) self.assertIn("item_4", response.content.decode()) self.assertIn("item_5", response.content.decode()) #page 2 response = self.client.get(reverse( 'surveys-answer-survey-pages', args=[survey_instance.get_url_token(), 2]), follow=True, secure=True) self.assertNotIn("Thank you for taking the time to answer this survey", response.content.decode()) self.assertNotIn("item_1", response.content.decode()) self.assertIn("item_6", response.content.decode()) #test that a link with NOT exactly 1 dashes gets 404-ed response = self.client.get(reverse('surveys-answer-survey', args=["not"]), follow=True, secure=True) self.assertEqual(response.status_code, 404) self.assertEqual( response.context['exception'], "The survey you asked for does not exist. If you pasted a link, make sure you got the entire link." ) response = self.client.get(reverse( 'surveys-answer-survey', args=[survey_instance.get_url_token() + "-extra"]), follow=True, secure=True) self.assertEqual(response.status_code, 404) self.assertEqual( response.context['exception'], "The survey you asked for does not exist. If you pasted a link, make sure you got the entire link." ) #test invalid hash gets 404-ed response = self.client.get(reverse('surveys-answer-survey', args=["exactly-wrong"]), follow=True, secure=True) self.assertEqual(response.status_code, 404) self.assertEqual( response.context['exception'], "The survey you asked for does not exist. If you pasted a link, make sure you got the entire link." ) #test that we are redirected to the first page with an unanswered question if survey is started, but not completed, and we follow the link again si_items = survey_instance.get_items() self.assertEqual(len(si_items), 6) si_items[0].answer_item(3) response = self.client.get(reverse( 'surveys-answer-survey', args=[survey_instance.get_url_token()]), follow=True, secure=True) self.assertRedirects( response, reverse('surveys-answer-survey-pages', args=[survey_instance.get_url_token(), 1]), 302, 200) self.assertIn( "Welcome back! We saved your place, so you can continue where you left off.", response.content.decode()) si_items[1].answer_item(3) si_items[2].answer_item("chose_to_not_answer") si_items[3].answer_item(3) si_items[4].answer_item(3) response = self.client.get(reverse( 'surveys-answer-survey', args=[survey_instance.get_url_token()]), follow=True, secure=True) self.assertRedirects( response, reverse('surveys-answer-survey-pages', args=[survey_instance.get_url_token(), 2]), 302, 200) self.assertIn( "Welcome back! We saved your place, so you can continue where you left off.", response.content.decode()) #test that we are given the option not to answer #print(response.content.decode()) self.assertIn("I don't know, or I don't want to answer", response.content.decode()) self.assertIn("value=\"chose_to_not_answer\"", response.content.decode()) #test that we can post answers successfully valid_data = {'item_5': "chose_to_not_answer", 'item_6': 2} response = self.client.post(reverse( 'surveys-answer-survey-pages', args=[survey_instance.get_url_token(), 2]), valid_data, follow=True, secure=True) self.assertRedirects( response, reverse('surveys-answer-survey', args=[survey_instance.get_url_token()]), 302, 200) self.assertIn( "The survey has been successfully completed, and your answers saved.", response.content.decode()) si_items = survey_instance.get_items() for item in si_items: self.assertTrue(item.answered) #test closed survey is 404-ed survey = survey_logic.close_survey(survey) self.assertTrue(survey.is_closed) response = self.client.get(reverse( 'surveys-answer-survey', args=[survey_instance.get_url_token()]), follow=True, secure=True) self.assertEqual(response.status_code, 404) self.assertEqual( response.context['exception'], "The survey you asked for does not exist. If you pasted a link, make sure you got the entire link." ) response = self.client.get(reverse( 'surveys-answer-survey-pages', args=[survey_instance.get_url_token(), 1]), follow=True, secure=True) self.assertEqual(response.status_code, 404) self.assertEqual( response.context['exception'], "The survey you asked for does not exist. If you pasted a link, make sure you got the entire link." )
def test_survey_details_view(self): user = User.objects.get(id=1) instrument = Instrument.objects.get(id=1) survey = Survey.objects.get(id=1) #test that login is required response = self.client.get(reverse( 'surveys-survey-details', args=[survey.uidb64(), instrument.slug_name]), follow=True, secure=True) self.assertRedirects( response, reverse('loginc') + "?next=" + reverse('surveys-survey-details', args=[survey.uidb64(), instrument.slug_name]), 302, 200) #login self.client.force_login(user=user) #check that a faulty link triggers 404 (a faulty survey_uid) response = self.client.get(reverse( 'surveys-survey-details', args=["my_non-existant_survey", instrument.slug_name]), follow=True, secure=True) self.assertEqual(response.status_code, 404) self.assertEqual(response.context['exception'], "We couldn't find the survey you were looking for.") #check that we cannot view an open survey response = self.client.get(reverse( 'surveys-survey-details', args=[survey.uidb64(), instrument.slug_name]), follow=True, secure=True) self.assertEqual(response.status_code, 404) self.assertEqual(response.context['exception'], "We couldn't find the survey you were looking for.") #check that we CAN view a closed survey survey = survey_logic.close_survey(survey) self.assertTrue(survey.is_closed) response = self.client.get(reverse( 'surveys-survey-details', args=[survey.uidb64(), instrument.slug_name]), follow=True, secure=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'instrument_report.html') #test that we get the expected data in context self.assertIn('instrument', response.context) self.assertIn('survey_data', response.context) self.assertEqual(response.context['instrument'], instrument) self.assertIn('instrument', response.context['survey_data']) self.assertIn('survey', response.context['survey_data']) self.assertIn('dimension_results', response.context['survey_data']) self.assertIn('item_results', response.context['survey_data']) self.assertIn('previous_data', response.context['survey_data']) self.assertEqual(response.context['survey_data']['instrument'], instrument) self.assertEqual(response.context['survey_data']['survey'], survey) self.assertEqual(response.context['survey_data']['previous_data'], None) self.assertEqual( len(response.context['survey_data']['dimension_results']), 3) self.assertEqual(len(response.context['survey_data']['item_results']), 6) #check that a faulty link triggers 404 (non-existant instrument slug_name) response = self.client.get(reverse( 'surveys-survey-details', args=[survey.uidb64(), "non-existant_instrument"]), follow=True, secure=True) self.assertEqual(response.status_code, 404) self.assertEqual( response.context['exception'], "We couldn't find the instrument you were looking for.") #test that another user cannot access this survey self.client.logout() bad_user = User(username="******", email="*****@*****.**", password="******") bad_user.save() self.client.force_login(user=bad_user) response = self.client.get(reverse( 'surveys-survey-details', args=[survey.uidb64(), instrument.slug_name]), follow=True, secure=True) self.assertEqual(response.status_code, 403)
def test_dashboard_view(self): user = User.objects.get(id=1) organization = Organization.objects.get(id=1) employee = Respondent.objects.get(id=1) instrument = Instrument.objects.get(id=1) survey = Survey.objects.get(id=1) #test that login is required self.assertEqual(len(Respondent.objects.all()), 1) response = self.client.get(reverse('surveys-dashboard'), follow=True, secure=True) self.assertRedirects( response, reverse('loginc') + "?next=" + reverse('surveys-dashboard'), 302, 200) #test that we can see the dashboard self.client.force_login(user=user) response = self.client.get(reverse('surveys-dashboard'), follow=True, secure=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'dashboard.html') #test if we get the correct context self.assertEqual(response.context['todays_date'], datetime.date.today()) self.assertEqual(response.context['employee_count'], 1) self.assertEqual(len(response.context['employee_list']), 1) self.assertEqual(response.context['employee_list'][0], employee) self.assertEqual(response.context['subscription_paid'], False) self.assertEqual(response.context['inactive_instrument_list'], []) self.assertEqual( response.context['active_instrument_data'][0]['instrument'], instrument) self.assertEqual( response.context['active_instrument_data'][0]['closed_surveys'], None) self.assertEqual( response.context['active_instrument_data'][0]['open_survey'], survey) #Test if we can get some content in inactive instrument_list survey_setting = survey_logic.configure_survey_setting( organization=organization, instrument=instrument, is_active=False) response = self.client.get(reverse('surveys-dashboard'), follow=True, secure=True) self.assertEqual(response.context['inactive_instrument_list'], [instrument]) self.assertEqual(response.context['active_instrument_data'], None) #Test if we can get some data from previous surveys survey_setting = survey_logic.configure_survey_setting( organization=organization, instrument=instrument, is_active=True) #Move date_open and date_close back in time, close the survey survey.date_open = datetime.date.today() + datetime.timedelta( days=-100) survey.date_close = datetime.date.today() + datetime.timedelta( days=-93) survey.save() survey_setting.last_survey_open = datetime.date.today( ) + datetime.timedelta(days=-100) survey_setting.last_survey_close = datetime.date.today( ) + datetime.timedelta(days=-93) survey_setting.save() survey_logic.close_survey(survey) response = self.client.get(reverse('surveys-dashboard'), follow=True, secure=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'dashboard.html') #test that we get the expected data self.assertEqual( response.context['active_instrument_data'][0]['instrument'], instrument) self.assertEqual( response.context['active_instrument_data'][0]['open_survey'], None) self.assertEqual( len(response.context['active_instrument_data'][0] ['closed_surveys']), 1) self.assertIn( 'instrument', response.context['active_instrument_data'][0]['closed_surveys'][0]) self.assertIn( 'survey', response.context['active_instrument_data'][0]['closed_surveys'][0]) self.assertIn( 'dimension_results', response.context['active_instrument_data'][0]['closed_surveys'][0]) self.assertIn( 'item_results', response.context['active_instrument_data'][0]['closed_surveys'][0]) self.assertEqual( response.context['active_instrument_data'][0]['closed_surveys'][0] ['instrument'], instrument) self.assertEqual( response.context['active_instrument_data'][0]['closed_surveys'][0] ['survey'], survey) self.assertEqual( len(response.context['active_instrument_data'][0]['closed_surveys'] [0]['dimension_results']), 3) self.assertEqual( len(response.context['active_instrument_data'][0]['closed_surveys'] [0]['item_results']), 6) #test that we are stopped due to not paid subscription self.assertIn( "To get started with our employee engagement software, you first need to pick a plan", response.content.decode()) self.assertNotIn("Latest measurements", response.content.decode()) self.assertNotIn( "<a href=\"/surveys/survey-details/MQ/employee_engagement/#vigor\">Vigor</a>\n", response.content.decode()) self.assertNotIn("<div>Employee Engagement tracking:</div>", response.content.decode()) self.assertNotIn("<h3 class=\"card-title\">Co-workers at TestOrg</h3>", response.content.decode()) #test that we move past block if organization has_free_access organization.has_free_access = True organization.save() response = self.client.get(reverse('surveys-dashboard'), follow=True, secure=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'dashboard.html') self.assertNotIn( "To get started with our employee engagement software, you first need to pick a plan", response.content.decode()) self.assertIn("Latest measurements", response.content.decode()) self.assertIn( "/surveys/survey-details/MQ/employee_engagement/#vigor\">Vigor</a>\n", response.content.decode()) self.assertIn("<div>Employee Engagement tracking:</div>", response.content.decode()) self.assertIn("<h3 class=\"card-title\">Co-workers at TestOrg</h3>", response.content.decode())
def test_close_survey_and_check_if_correct_sums(self): organization = Organization.objects.get(id=1) instrument = Instrument.objects.get(id=1) #create little employees employee_target = 7 existing_employees = Respondent.objects.filter(organization=organization) if len(existing_employees) < employee_target: for e in range(employee_target-len(existing_employees)): email = "testemployee" + str(e+1+len(existing_employees)) + "@aa.aa" employee = Respondent(organization=organization, email=email) employee.save() self.assertEqual(len(Respondent.objects.all()), employee_target) #start a survey, make instances and answer # id #1 choses not to answer any questions # id #2 never opened the survey # id #3 only answered a few questions # the rest answers 3 to every question survey = survey_logic.create_survey(owner=organization, instrument_list=[instrument, ]) survey_instance_list = survey_logic.survey_instances_from_survey(survey) for survey_instance in survey_instance_list: if survey_instance.id != 2: for id, item in enumerate(survey_instance.get_items()): answer_value = 3 #random.randint(titem.survey_item.item_dimension.scale.min_value, titem.survey_item.item_dimension.scale.max_value) if survey_instance.id == 1: answer_value = "chose_to_not_answer" if survey_instance.id == 3: if id == 1 or id == 2 or id == 3: item.answer_item(answer_value) #print("answered %s to question %s in survey_instance %s."%(answer_value, id, survey_instance.id)) else: item.answer_item(answer_value) #print("answered %s to question %s in survey_instance %s."%(answer_value, id, survey_instance.id)) item = SurveyInstanceItem.objects.get(id=item.id) survey_instance.check_completed() self.assertEqual(len(Survey.objects.all()), 1) self.assertEqual(len(SurveyInstance.objects.all()), employee_target) for survey_instance in survey_instance_list: if survey_instance.id != 2 and survey_instance.id != 3: for item in survey_instance.get_items(): self.assertEqual(item.answered, True) if item.survey_instance.id == 1: self.assertEqual(item.answer, None) else: self.assertEqual(item.answer, 3) elif survey_instance.id == 2: for item in survey_instance.get_items(): self.assertEqual(item.answered, False) self.assertEqual(item.answer, None) elif survey_instance.id == 3: self.assertEqual(survey_instance.check_completed(), False) for item in survey_instance.get_items(): pass #close survey self.assertEqual(len(Survey.objects.filter(is_closed=True)), 0) survey_logic.close_survey(survey) self.assertEqual(len(Survey.objects.filter(is_closed=True)), 1) #test that the SurveyItems and DimensionResults were calculated correctly survey = Survey.objects.get(id=1) survey_items = survey.get_items() for item in survey_items: self.assertEqual(item.average, 3) for dimension_result in survey.dimensionresult_set.all(): self.assertEqual(dimension_result.average, 3) #test that n-answered is correct self.assertEqual(survey.n_invited, 7) self.assertEqual(survey.n_completed, 5) self.assertEqual(survey.n_incomplete, 1) self.assertEqual(survey.n_not_started, 1)
def test_create_survey_if_due(self): o=Organization.objects.get(id=1) i=Instrument.objects.get(id=1) ss=SurveySetting.objects.get(id=1) #test all is calm to set things off survey_list = Survey.objects.all() self.assertEqual(len(survey_list), 0) self.assertEqual(ss.last_survey_open, None) self.assertEqual(ss.last_survey_close, None) self.assertEqual(len(ss.surveys.all()), 0) #run it once, creating a survey survey = survey_logic.create_survey_if_due(organization=o) #Test that it worked, creating 1 survey survey_list = Survey.objects.all() self.assertEqual(len(survey_list), 1) self.assertIsInstance(survey, Survey) self.assertEqual(survey.date_open, datetime.date.today()) self.assertEqual(survey.date_close, datetime.date.today()+datetime.timedelta(days=ss.surveys_remain_open_days)) ss=SurveySetting.objects.get(id=1) self.assertEqual(ss.last_survey_open, datetime.date.today()) self.assertEqual(ss.last_survey_close, datetime.date.today()+datetime.timedelta(days=ss.surveys_remain_open_days)) self.assertEqual(len(ss.surveys.all()), 1) #run it again, NOT creating a survey survey2 = survey_logic.create_survey_if_due(organization=o) survey3 = survey_logic.create_survey_if_due(organization=o) survey4 = survey_logic.create_survey_if_due(organization=o) survey5 = survey_logic.create_survey_if_due(organization=o) #Test that it didnt create more surveys self.assertIsInstance(survey, Survey) self.assertEqual(survey2, None) self.assertEqual(survey3, None) self.assertEqual(survey4, None) self.assertEqual(survey5, None) survey_list = Survey.objects.all() self.assertEqual(len(survey_list), 1) #Move date_open and _close back in time so it a survey becomes due again survey.date_open = datetime.date.today()+datetime.timedelta(days=-100) survey.date_close = datetime.date.today()+datetime.timedelta(days=-93) survey.save() ss.last_survey_open = datetime.date.today()+datetime.timedelta(days=-100) ss.last_survey_close = datetime.date.today()+datetime.timedelta(days=-93) ss.save() survey_logic.close_survey(survey) #run it once, creating a new survey survey6 = survey_logic.create_survey_if_due(organization=o) #Test that it worked, creating 1 additional survey self.assertIsInstance(survey6, Survey) survey_list = Survey.objects.all() self.assertEqual(len(survey_list), 2) #add another instrument rti = create_test_data(2) rti['instrument']['name'] = "Another instrument" rti['instrument']['slug_name'] = "another_instrument" rti['instrument']['name_nb'] = "Another instrument" rti['instrument']['slug_name_nb'] = "another_instrument" rti['scales'][0]['name'] = "Another Scale" setup_instrument.setup_instrument(rti) i2=Instrument.objects.get(id=2) #run it once, ensure no new Surveys are made survey7 = survey_logic.create_survey_if_due(organization=o) survey_list = Survey.objects.all() self.assertEqual(len(survey_list), 2) #Move date_open and _close back in time so it a survey becomes due again survey6.date_open = datetime.date.today()+datetime.timedelta(days=-100) survey6.date_close = datetime.date.today()+datetime.timedelta(days=-93) survey6.save() ss.last_survey_open = datetime.date.today()+datetime.timedelta(days=-100) ss.last_survey_close = datetime.date.today()+datetime.timedelta(days=-93) ss.save() survey_logic.close_survey(survey6) #configure new instrument ss2 = survey_logic.configure_survey_setting(organization=o, instrument=i2, is_active=True) ss2.save() #run it once, 1 new survey should be made survey8 = survey_logic.create_survey_if_due(organization=o) #check that 1 was made self.assertIsInstance(survey8, Survey) survey_list = Survey.objects.all() self.assertEqual(len(survey_list), 3) #check that both instruments were included dr_list8 = survey8.dimensionresult_set.all() self.assertEqual(len(dr_list8), 6) #while in the old one, only 1 instrument dr_list6 = survey6.dimensionresult_set.all() self.assertEqual(len(dr_list6), 3) #Move date_open and _close back in time so it a survey becomes due again survey8.date_open = datetime.date.today()+datetime.timedelta(days=-100) survey8.date_close = datetime.date.today()+datetime.timedelta(days=-93) survey8.save() ss.last_survey_open = datetime.date.today()+datetime.timedelta(days=-100) ss.last_survey_close = datetime.date.today()+datetime.timedelta(days=-93) ss.save() ss2.last_survey_open = datetime.date.today()+datetime.timedelta(days=-100) ss2.last_survey_close = datetime.date.today()+datetime.timedelta(days=-93) ss2.save() survey_logic.close_survey(survey8) #deactive the first product ss = survey_logic.configure_survey_setting(organization=o, instrument=i, is_active=False) ss.save() #surveys are due again survey9 = survey_logic.create_survey_if_due(organization=o) #only one instrument should be included dr_list9 = survey9.dimensionresult_set.all() self.assertEqual(len(dr_list9), 3) #Move date_open and _close back in time so it becomes not due, but still in the timewindow to be included with anopther instrument survey9.date_open = datetime.date.today()+datetime.timedelta(days=-75) survey9.date_close = datetime.date.today()+datetime.timedelta(days=-70) survey9.save() ss2.last_survey_open = datetime.date.today()+datetime.timedelta(days=-75) ss2.last_survey_close = datetime.date.today()+datetime.timedelta(days=-70) ss2.save() survey_logic.close_survey(survey9) #Run it again, nothing happens... it not due... survey10 = survey_logic.create_survey_if_due(organization=o) self.assertEqual(survey10, None) #Activate first product again ss = survey_logic.configure_survey_setting(organization=o, instrument=i, is_active=True) ss.save() #check that everything is in order befoe the big finale self.assertEqual(ss.last_survey_open, datetime.date.today()+datetime.timedelta(days=-100)) #is due self.assertEqual(ss.last_survey_close, datetime.date.today()+datetime.timedelta(days=-93)) self.assertEqual(ss2.last_survey_open, datetime.date.today()+datetime.timedelta(days=-75)) #is not due, but close enough self.assertEqual(ss2.last_survey_close, datetime.date.today()+datetime.timedelta(days=-70)) #Run it again, both should be included, product 1 because it's so lojng ago, and product 2 it get's dragged in because it's due within 60 days and 1/3 of 90 days survey11 = survey_logic.create_survey_if_due(organization=o) #Both should be included dr_list11 = survey11.dimensionresult_set.all() self.assertEqual(len(dr_list11), 6) #and dates are now aligned so that in the future, they fire at the same time ss = survey_logic.configure_survey_setting(organization=o, instrument=i) self.assertEqual(ss.last_survey_open, datetime.date.today()) self.assertEqual(ss.last_survey_close, datetime.date.today()+datetime.timedelta(days=ss.surveys_remain_open_days)) ss2 = survey_logic.configure_survey_setting(organization=o, instrument=i2) self.assertEqual(ss2.last_survey_open, datetime.date.today()) self.assertEqual(ss2.last_survey_close, datetime.date.today()+datetime.timedelta(days=ss.surveys_remain_open_days)) #Move date_open and _close back in time so it a survey becomes due again survey11.date_open = datetime.date.today()+datetime.timedelta(days=-100) survey11.date_close = datetime.date.today()+datetime.timedelta(days=-93) survey11.save() ss.last_survey_open = datetime.date.today()+datetime.timedelta(days=-100) ss.last_survey_close = datetime.date.today()+datetime.timedelta(days=-93) ss.save() ss2.last_survey_open = datetime.date.today()+datetime.timedelta(days=-100) ss2.last_survey_close = datetime.date.today()+datetime.timedelta(days=-93) ss2.save() survey_logic.close_survey(survey11)
def test_close_survey_w_all_filled(self): o=Organization.objects.get(id=1) o.save() i=Instrument.objects.get(id=1) i.save() survey_settings = survey_logic.configure_survey_setting(o, i, is_active=True) survey = survey_logic.create_survey(o, [i, ]) r2 = Respondent( organization=o, first_name=None, last_name=None, email="*****@*****.**", receives_surveys=True ) r2.save() #answer survey_logic.survey_instances_from_survey(survey) survey_instances = SurveyInstance.objects.all() for si in survey_instances: self.assertEqual(si.started, False) self.assertEqual(si.completed, False) items = si.get_items() for i in items: self.assertEqual(i.answer, None) self.assertEqual(i.answered, False) i.answer_item(3) #check that it worked survey_instances = SurveyInstance.objects.all() for si in survey_instances: #self.assertEqual(si.started, True) # -> removed the code that marks survey as started while answering questions, this is now handled by view self.assertEqual(si.completed, False) items = si.get_items() for i in items: self.assertEqual(i.answer, 3) self.assertEqual(i.answered, True) #close survey survey = survey_logic.close_survey(survey) #check that the survey was closed self.assertEqual(survey.date_open, datetime.date.today()) self.assertEqual(survey.date_close, datetime.date.today()+datetime.timedelta(days=-1)) self.assertEqual(survey.n_invited, 2) self.assertEqual(survey.n_completed, 2) self.assertEqual(survey.n_incomplete, 0) self.assertEqual(survey.n_not_started, 0) self.assertEqual(survey.is_closed, True) #check thaty the dimensions were closed i = Instrument.objects.get(id=1) dimension_list=[d for d in i.dimension_set.all()] dr_list = survey.dimensionresult_set.all() for dr in dr_list: self.assertIsInstance(dr, RatioScaleDimensionResult) self.assertEqual(dr.survey, survey) self.assertIn(dr.dimension, dimension_list) self.assertEqual(dr.n_completed, 2) self.assertEqual(dr.average, 3) #check that the items were closed si_list = survey.surveyitem_set.all() for si in si_list: self.assertEqual(si.n_answered, 2) self.assertEqual(si.average, 3)
def handle(*args, **kwargs): logger.warning("\nCreating someone to test on...") #create a test organization (after running this command, you can log in with these credentials) tusername = "******" tuser = None try: tuser = User.objects.get(username=tusername) except User.DoesNotExist as err: tuser = User.objects.create_user( username=tusername, email=tusername, password="******", ) tuser.save() logger.warning("...created user: %s." % (tuser)) assert tuser is not None, "tuser was None" torganization = None try: torganization = Organization.objects.get(owner=tuser) except Organization.DoesNotExist as err: torganization = Organization(owner=tuser, name="Test Organization #4", phone=None, address_line_1="Test street", address_line_2="", zip_code="9999", city="Testcity", country="NO", accepted_terms_and_conditions=True, has_free_access=True) torganization.save() logger.warning("...created organization: %s." % (torganization)) assert torganization is not None, "torganization was None" #create little employees employee_target = 7 existing_employees = Respondent.objects.filter( organization=torganization) if len(existing_employees) < employee_target: for e in range(employee_target - len(existing_employees)): email = "testemployee" + str( e + 1 + len(existing_employees)) + "@aa.aa" temployee = Respondent(organization=torganization, email=email) temployee.save() logger.warning("...created employee: %s." % (temployee)) #create an instrument logger.warning("Ensuring there is an instrument in place...") if len(Instrument.objects.all()) > 0: tinstrument = Instrument.objects.get(id=1) logger.warning("...there is already one: %s" % (tinstrument)) else: ri = employee_engagement_instrument.employee_engagement_instrument setup_instrument.setup_instrument(ri) tinstrument = Instrument.objects.get(id=1) logger.warning("...there wasn't already one, so I made one: %s" % (tinstrument)) #configure surveys tsurvey_setting = survey_logic.configure_survey_setting( organization=torganization, instrument=tinstrument, is_active=True) logger.warning("...created survey settings: %s" % (tsurvey_setting)) #create surveys logger.warning("Closing existing surveys....") open_surveys = Survey.objects.filter(owner=torganization, is_closed=False) for survey in open_surveys: survey_logic.close_survey(survey) ("... closed a survey with id %s!" % (survey.id)) logger.warning("... done!") surveys_target = 5 survey_list = [] current_date = date.today() for i in range(surveys_target): logger.warning( "Creating a survey (number %s) for the test-organization..." % (i)) tsurvey = survey_logic.create_survey(owner=torganization, instrument_list=[ tinstrument, ]) survey_list.append(tsurvey) logger.warning("...created a survey: %s" % (tsurvey)) tsurvey_instance_list = survey_logic.survey_instances_from_survey( tsurvey) logger.warning("...created %s survey instances." % (len(tsurvey_instance_list))) #send emails: #for survey_instance in tsurvey_instance_list: # survey_logic.send_email_for_survey_instance(survey_instance) # logger.warning("... sent an email to %s"%(survey_instance.respondent)) #answer survey logger.warning("... answering this survey...") for tsinstance in tsurvey_instance_list: logger.warning("... ... %s is answering..." % (tsinstance.respondent)) for titem in tsinstance.get_items(): answer_value = random.randint( titem.survey_item.item_dimension.scale.min_value, titem.survey_item.item_dimension.scale.max_value) titem.answer_item(answer_value) tsinstance.check_completed() #close survey logger.warning("...moving survey back in time") current_date += timedelta(days=-90) tsurvey.date_close = current_date tsurvey.date_open = current_date + timedelta(days=-10) tsurvey.save() logger.warning("...done.") logger.warning("...closing survey with id %s" % (tsurvey.id)) survey_logic.close_survey_if_date_close_has_passed(tsurvey) logger.warning("...done.") #update survey setting tsurvey_setting.check_last_survey_dates() logger.warning("All done!")