def test_yesterday_general(self): today = now() first_dt = today - datetime.timedelta(7) input_strings = [(first_dt + datetime.timedelta(delta_days)).strftime('%Y-%m-%d') for delta_days in range(7)] # the behavior depends on the current datetime relative to registration end, # so try it with registration end before and after the current time reg_period = RegistrationPeriodFactory(start_time=today - datetime.timedelta(days=5), end_time=today) for fake_registration_end in [today - datetime.timedelta(days=1), today + datetime.timedelta(days=1)]: reg_period.end_time = fake_registration_end reg_period.save() if registration_in_progress(as_of=today.date()): expected_str = input_strings[-2] else: expected_str = input_strings[-1] expected_date = datetime.datetime.strptime(expected_str, '%Y-%m-%d').date() date_and_string = calc_yesterday(input_strings) self.assertEqual(date_and_string, (expected_date, expected_str)) # try again, providing the date objects input_dates = [datetime.datetime.strptime(s, '%Y-%m-%d').date() for s in input_strings] date_and_string = calc_yesterday(input_strings, input_dates) self.assertEqual(date_and_string, (expected_date, expected_str))
def test_yesterday_from_single_date(self): today = now() today_str = today.strftime('%Y-%m-%d') date_and_string = calc_yesterday([today_str]) # since there's just one date provided, yesterday can only # be that date regardless of other factors self.assertEqual(date_and_string, (today.date(), today_str)) # try again, providing the date object date_and_string = calc_yesterday([today_str], [today.date()]) self.assertEqual(date_and_string, (today.date(), today_str))
def setUp(self): self.staff_user = UserFactory() self.staff_user.is_staff = True self.staff_user.save() assert self.client.login(username=self.staff_user.username, password=DEFAULT_USER_PASSWORD) self.reporting_user = test_reports.TEST_USERNAME self.reporting_password = test_reports.TEST_PASSWORD REPORT_USER_DB[self.reporting_user] = self.reporting_password # Pick a start time that represents different days in Libya vs UTC tz = timezone(settings.TIME_ZONE) polling_start_time = astz(FUTURE_DAY.replace(hour=22), tz) polling_end_time = tz.normalize(polling_start_time + timedelta(hours=16)) self.election = ElectionFactory( polling_start_time=polling_start_time, polling_end_time=polling_end_time, ) self.election_day_dt = self.election.polling_start_time # Create "decoy" election just to confirm that it doesn't break reports. decoy_start_time = tz.normalize(polling_start_time - timedelta(days=10)) decoy_end_time = tz.normalize(decoy_start_time + timedelta(hours=16)) ElectionFactory( polling_start_time=decoy_start_time, polling_end_time=decoy_end_time, ) self.all_centers = [] self.rc_1 = RegistrationCenterFactory() self.all_centers.append(self.rc_1) self.rc_2 = RegistrationCenterFactory() self.all_centers.append(self.rc_2) self.rc_3 = RegistrationCenterFactory() self.all_centers.append(self.rc_3) self.rc_4 = RegistrationCenterFactory() self.all_centers.append(self.rc_4) self.copy_of_rc_1 = RegistrationCenterFactory(copy_of=self.rc_1, office=self.rc_1.office) self.all_centers.append(self.copy_of_rc_1) # rc_5 is inactive for this election self.rc_5 = RegistrationCenterFactory(office=self.rc_1.office) self.all_centers.append(self.rc_5) inactive_on_election = CenterClosedForElection( registration_center=self.rc_5, election=self.election ) inactive_on_election.full_clean() inactive_on_election.save() self.all_office_ids = [center.office_id for center in self.all_centers] self.carrier_1 = BackendFactory() self.citizen_1 = CitizenFactory() # Create registrations on the 4 days leading up to election day # Put the registrations at different hours of the day to stress TZ handling. self.registration_dates = [] self.registration_date_strs = [] hour_of_day = 0 for delta_days in range(10, 4, -1): assert hour_of_day < 24 reg_date = astz(self.election_day_dt - timedelta(days=delta_days), tz)\ .replace(hour=hour_of_day) hour_of_day += 4 self.registration_dates.append(reg_date) self.registration_date_strs.append(reg_date.strftime('%Y-%m-%d')) self.yesterday_date, _ = calc_yesterday(self.registration_date_strs) self.yesterday_date_dm = self.yesterday_date.strftime('%d/%m') # yesterday_date is a date; get a datetime form self.yesterday_date_dt = tz.localize(datetime(self.yesterday_date.year, self.yesterday_date.month, self.yesterday_date.day, 0, 0, 0)) self.staff_phone_number = STAFF_PHONE_NUMBER_PATTERN % 12345
def setUp(self): self.staff_user = UserFactory() self.staff_user.is_staff = True self.staff_user.save() assert self.client.login(username=self.staff_user.username, password=DEFAULT_USER_PASSWORD) self.reporting_user = test_reports.TEST_USERNAME self.reporting_password = test_reports.TEST_PASSWORD REPORT_USER_DB[self.reporting_user] = self.reporting_password # Pick a start time that represents different days in Libya vs UTC tz = timezone(settings.TIME_ZONE) polling_start_time = astz(FUTURE_DAY.replace(hour=22), tz) polling_end_time = tz.normalize(polling_start_time + timedelta(hours=16)) self.election = ElectionFactory( polling_start_time=polling_start_time, polling_end_time=polling_end_time, ) self.election_day_dt = self.election.polling_start_time # Create "decoy" election just to confirm that it doesn't break reports. decoy_start_time = tz.normalize(polling_start_time - timedelta(days=10)) decoy_end_time = tz.normalize(decoy_start_time + timedelta(hours=16)) ElectionFactory( polling_start_time=decoy_start_time, polling_end_time=decoy_end_time, ) self.all_centers = [] self.rc_1 = RegistrationCenterFactory() self.all_centers.append(self.rc_1) self.rc_2 = RegistrationCenterFactory() self.all_centers.append(self.rc_2) self.rc_3 = RegistrationCenterFactory() self.all_centers.append(self.rc_3) self.rc_4 = RegistrationCenterFactory() self.all_centers.append(self.rc_4) self.copy_of_rc_1 = RegistrationCenterFactory(copy_of=self.rc_1, office=self.rc_1.office) self.all_centers.append(self.copy_of_rc_1) # rc_5 is inactive for this election self.rc_5 = RegistrationCenterFactory(office=self.rc_1.office) self.all_centers.append(self.rc_5) inactive_on_election = CenterClosedForElection( registration_center=self.rc_5, election=self.election) inactive_on_election.full_clean() inactive_on_election.save() self.all_office_ids = [center.office_id for center in self.all_centers] self.carrier_1 = BackendFactory() self.citizen_1 = CitizenFactory() # Create registrations on the 4 days leading up to election day # Put the registrations at different hours of the day to stress TZ handling. self.registration_dates = [] self.registration_date_strs = [] hour_of_day = 0 for delta_days in range(10, 4, -1): assert hour_of_day < 24 reg_date = astz(self.election_day_dt - timedelta(days=delta_days), tz)\ .replace(hour=hour_of_day) hour_of_day += 4 self.registration_dates.append(reg_date) self.registration_date_strs.append(reg_date.strftime('%Y-%m-%d')) self.yesterday_date, _ = calc_yesterday(self.registration_date_strs) self.yesterday_date_dm = self.yesterday_date.strftime('%d/%m') # yesterday_date is a date; get a datetime form self.yesterday_date_dt = tz.localize( datetime(self.yesterday_date.year, self.yesterday_date.month, self.yesterday_date.day, 0, 0, 0)) self.staff_phone_number = STAFF_PHONE_NUMBER_PATTERN % 12345
def test_yesterday_no_dates(self): string_and_date = calc_yesterday(()) self.assertEqual(string_and_date, (None, None))