def test_shift_od(self, *patches): patch_set_hms = patches[0] patch_shift_hms = patches[1] patch_od_to_hms = patches[2] patch_ast_ymd_to_od = patches[3] patch_shift_ast_ymd = patches[4] patch_od_to_ast_ymd = patches[5] fake_ordinal_decimal1 = FAKE.pyfloat() fake_ordinal_decimal2 = FAKE.pyfloat() fake_day_delta = FAKE.random_int(min=1) fake_ordinal_decimal3 = FAKE.pyfloat() fake_ast_ymd1 = FAKE.random_int(), FAKE.random_int(), FAKE.random_int() fake_ast_ymd2 = FAKE.random_int(), FAKE.random_int(), FAKE.random_int() fake_hms1 = FAKE.random_int(), FAKE.random_int(), FAKE.random_int() fake_hms2 = FAKE.random_int(), FAKE.random_int(), FAKE.random_int() patch_od_to_ast_ymd.return_value = fake_ast_ymd1 patch_shift_ast_ymd.return_value = fake_ast_ymd2 patch_ast_ymd_to_od.return_value = fake_ordinal_decimal2 patch_od_to_hms.return_value = fake_hms1 patch_shift_hms.return_value = fake_hms2, fake_day_delta patch_set_hms.return_value = fake_ordinal_decimal3 date_intervals = [] for i in range(FAKE.random_int(min=1, max=3)): date_intervals.append([i, FAKE.random_element(elements=DateUnit)]) time_intervals = [] for i in range(FAKE.random_int(min=1, max=3)): time_intervals.append([i, FAKE.random_element(elements=TimeUnit)]) intervals = copy.deepcopy(date_intervals) intervals.extend(time_intervals) cd = ConvertibleDate(calendar=self.calendar_factory.build()) cdt = ConvertibleDateTime(date=cd, time=self.time_factory.build()) assert cdt.shift_od(fake_ordinal_decimal1, []) == fake_ordinal_decimal1 assert ( cdt.shift_od(fake_ordinal_decimal1, intervals) == fake_ordinal_decimal3 ) patch_od_to_ast_ymd.assert_called_once_with(fake_ordinal_decimal1) patch_shift_ast_ymd.assert_called_once_with( fake_ast_ymd1, date_intervals ) patch_ast_ymd_to_od.assert_called_once_with(fake_ast_ymd2) patch_od_to_hms.assert_called_once_with(fake_ordinal_decimal2) patch_shift_hms.assert_called_once_with(fake_hms1, time_intervals) patch_set_hms.assert_called_once_with( fake_ordinal_decimal2, fake_hms2, fake_day_delta )
def test_extend_od_for_day_interval(self, patch_shift_od): delta = FAKE.random_int(min=-9999) interval = [delta, DateUnit.DAY] cd = ConvertibleDate(calendar=self.calendar_factory.build()) cdt = ConvertibleDateTime(date=cd, time=self.time_factory.build()) od = FAKE.pyfloat() assert cdt.extend_od(od, interval) == od + delta patch_shift_od.assert_not_called()
def test_extend_od_for_non_day_interval(self, patch_shift_od): units = [DateUnit.YEAR, DateUnit.MONTH] units.extend(TimeUnit) interval = [FAKE.random_int(), FAKE.random_element(elements=units)] cd = ConvertibleDate(calendar=self.calendar_factory.build()) cdt = ConvertibleDateTime(date=cd, time=self.time_factory.build()) ordinal_decimal = FAKE.pyfloat() cdt.extend_od(ordinal_decimal, interval) patch_shift_od.assert_called_once_with(ordinal_decimal, [interval])
def test_extend_od_raises(self): units = list(DateUnit) units.extend(TimeUnit) interval = [FAKE.random_int(), FAKE.random_element(elements=units)] bad_factor = FAKE.random_int(min=-9999, max=0) cd = ConvertibleDate(calendar=self.calendar_factory.build()) cdt = ConvertibleDateTime(date=cd, time=self.time_factory.build()) ordinal_decimal = FAKE.pyfloat() with pytest.raises(ValueError): cdt.extend_od(ordinal_decimal, interval, bad_factor)
def test_next_hms_raises_other_errors(self, _): fake_timeunit = FAKE.random_int() fake_interval = [FAKE.random_int(), fake_timeunit] timeunit = FAKE.random_element(elements=TimeUnit) big_frequency = FAKE.random_int(min=61) fake_float = FAKE.pyfloat(max_value=-1) earth_ct = self.earth_ct with pytest.raises(ValueError): assert earth_ct.next_hms(self.hms, fake_interval) with pytest.raises(ValueError): assert earth_ct.next_hms(self.hms, [big_frequency, timeunit]) with pytest.raises(ValueError): assert earth_ct.next_hms(self.hms, [fake_float, timeunit])
def test_next_od_for_date_unit(self, *patches): patch_ast_ymd_to_od = patches[0] patch_next_ast_ymd = patches[1] patch_od_to_ast_ymd = patches[2] od1 = FAKE.pyfloat() od2 = FAKE.pyfloat() ast_ymd1 = FAKE.random_int(), FAKE.random_int(), FAKE.random_int() ast_ymd2 = FAKE.random_int(), FAKE.random_int(), FAKE.random_int() interval = [FAKE.random_int(), FAKE.random_element(elements=DateUnit)] forward = FAKE.pybool() patch_od_to_ast_ymd.return_value = ast_ymd1 patch_next_ast_ymd.return_value = ast_ymd2 patch_ast_ymd_to_od.return_value = od2 cd = ConvertibleDate(calendar=self.calendar_factory.build()) cdt = ConvertibleDateTime(date=cd, time=self.time_factory.build()) assert cdt.next_od(od1, interval, forward) == od2 patch_od_to_ast_ymd.assert_called_once_with(od1) patch_next_ast_ymd.assert_called_once_with(ast_ymd1, interval, forward) patch_ast_ymd_to_od.assert_called_once_with(ast_ymd2)
def test_od_to_ast_ymd(self, *patches): patch_ordinal_date_to_ast_ymd = patches[0] patch_ordinal_to_ordinal_date = patches[1] fake_ordinal_decimal = FAKE.pyfloat() fake_ordinal = int(fake_ordinal_decimal) fake_ord_date = FAKE.random_int(), FAKE.random_int() patch_ordinal_to_ordinal_date.return_value = fake_ord_date cd = ConvertibleDate(calendar=self.calendar_factory.build()) cdt = ConvertibleDateTime(date=cd, time=self.time_factory.build()) cdt.od_to_ast_ymd(fake_ordinal_decimal) patch_ordinal_to_ordinal_date.assert_called_once_with(fake_ordinal) patch_ordinal_date_to_ast_ymd.assert_called_once_with(fake_ord_date)
def test_extend_od_with_reverse(self, patch_shift_od): non_day_units = [DateUnit.YEAR, DateUnit.MONTH] non_day_units.extend(TimeUnit) non_day_unit = FAKE.random_element(elements=non_day_units) delta = FAKE.random_int(min=-9999) non_day_interval = [delta, non_day_unit] day_interval = [delta, DateUnit.DAY] cd = ConvertibleDate(calendar=self.calendar_factory.build()) cdt = ConvertibleDateTime(date=cd, time=self.time_factory.build()) od = FAKE.pyfloat() assert cdt.extend_od(od, day_interval, reverse=True) == od - delta patch_shift_od.assert_not_called() cdt.extend_od(od, non_day_interval, reverse=True) patch_shift_od.assert_called_with(od, [[-delta, non_day_unit]])
def test_od_to_hr_date(self, *patches): patch_format_hr_date = patches[1] patch_hms_to_hr_time = patches[3] ordinal_decimal = FAKE.pyfloat() dateunit = FAKE.random_element(elements=DateUnit) timeunit = FAKE.random_element(elements=TimeUnit) cd = ConvertibleDate(calendar=self.calendar_factory.build()) cdt = ConvertibleDateTime(date=cd, time=self.time_factory.build()) with self.session: self.session.add_all([cd.calendar, cdt.time.clock]) self.session.flush() cdt.od_to_hr_date(ordinal_decimal, dateunit) patch_format_hr_date.assert_called_once() cdt.od_to_hr_date(ordinal_decimal, timeunit) patch_hms_to_hr_time.assert_called_once() with pytest.raises(ValueError): # noinspection PyTypeChecker cdt.od_to_hr_date(ordinal_decimal, DumEnum.DUM)
def test_next_od_raises(self): cd = ConvertibleDate(calendar=self.calendar_factory.build()) cdt = ConvertibleDateTime(date=cd, time=self.time_factory.build()) with pytest.raises(ValueError): cdt.next_od(FAKE.pyfloat(), [FAKE.random_int(), DumEnum.DUM])