def test_001_constructor_types_for_seconds(self): for integer_second in (3, '3'): western = WesternTime(5, 4, integer_second) assert western.as_seconds() == Fraction(18243, 1) for fractional_second in (1.25, Fraction(5, 4), '1.25', Decimal('1.25'), '5/4'): western = WesternTime(5, 4, fractional_second) assert western.as_seconds() == Fraction(72965, 4)
def test_500_compare(self): western1 = WesternTime(2, 3, 4) western2 = WesternTime(2, 3, 4) assert western1 == western2 assert western1 <= western2 assert western1 >= western2 assert not western1 != western2 assert not western1 < western2 assert not western1 > western2 for hour, minute, second in (3, 3, 3), (2, 4, 4), (2, 3, 5): western3 = WesternTime(hour, minute, second) # this is larger than western1 assert western1 < western3 assert western3 > western1 assert western1 <= western3 assert western3 >= western1 assert western1 != western3 assert western3 != western1 assert not western1 == western3 assert not western3 == western1 assert not western1 > western3 assert not western3 < western1 assert not western1 >= western3 assert not western3 <= western1
def test_300_write_attribute(self): western = WesternTime(10, 10, 10) with pytest.raises(AttributeError): western.hour = 3 with pytest.raises(AttributeError): western.minute = 3 with pytest.raises(AttributeError): western.second = 3
def test_100_write_attribute(self): western = WesternTime(10, 10, 10) with pytest.raises(AttributeError): western.hour = 3 with pytest.raises(AttributeError): western.minute = 3 with pytest.raises(AttributeError): western.second = 3
def test_070_timezone_valid(self): for test_to_utc in to_utc_test_data: western1 = WesternTime(1, 2, 3, to_utc=test_to_utc[0]) assert western1.to_utc == test_to_utc[1] western2 = WesternTime.in_hours("789/123", to_utc=test_to_utc[0]) assert western2.to_utc == test_to_utc[1] western2 = WesternTime.in_minutes("78901/123", to_utc=test_to_utc[0]) assert western2.to_utc == test_to_utc[1] western3 = WesternTime.in_seconds("789012/123", to_utc=test_to_utc[0]) assert western3.to_utc == test_to_utc[1]
def test_200_invalid_values(self): for test_row in western_time_out_of_range_data: hour = test_row[0] minute = test_row[1] second = test_row[2] with pytest.raises(ValueError): WesternTime(hour, minute, second)
def test_620_to_seconds(self): for test_row in western_time_test_data: hour = test_row[1][0] minute = test_row[1][1] second = Fraction(test_row[1][2]) to_seconds = Fraction(test_row[4]) assert WesternTime(hour, minute, second).to_seconds() == to_seconds
def test_630_to_day_frac(self): for test_row in western_time_test_data: day_frac = Fraction(test_row[0]) hour = test_row[1][0] minute = test_row[1][1] second = Fraction(test_row[1][2]) assert WesternTime(hour, minute, second).to_day_frac() == day_frac
def test_750_cformat_percent(self): western = WesternTime(1, 2, 3) assert western.cformat("%") == "%" assert western.cformat("%%") == "%" assert western.cformat("%%%") == "%%" assert western.cformat("abcd%") == "abcd%" assert western.cformat("%k") == "%k" assert western.cformat("a%k") == "a%k" assert western.cformat("%k%") == "%k%"
def test_008_constructor_day_frac(self): for test_row in western_time_test_data: day_frac = Fraction(test_row[0]) hour = test_row[1][0] minute = test_row[1][1] second = Fraction(test_row[1][2]) western = WesternTime.from_day_frac(day_frac) assert (western.hour, western.minute, western.second) == (hour, minute, second)
def test_006_constructor_in_seconds(self): for test_row in western_time_test_data: in_seconds = Fraction(test_row[4]) hour = test_row[1][0] minute = test_row[1][1] second = Fraction(test_row[1][2]) western = WesternTime.in_seconds(in_seconds) assert (western.hour, western.minute, western.second) == (hour, minute, second)
def test_450_replace(self): for test_row in western_time_test_data: hour = test_row[1][0] minute = test_row[1][1] second = Fraction(test_row[1][2]) western = WesternTime(hour, minute, second) assert western.replace() == WesternTime(hour, minute, second) assert western.replace(hour=11) == WesternTime(11, minute, second) assert western.replace(minute=10) == WesternTime(hour, 10, second) assert western.replace(second=9) == WesternTime(hour, minute, 9) assert western.replace(minute=10, hour=11) == WesternTime(11, 10, second) assert western.replace(second=9, hour=11) == WesternTime(11, minute, 9) assert western.replace(second=9, minute=10) == WesternTime(hour, 10, 9) assert western.replace(second=9, minute=10, hour=11) == WesternTime(11, 10, 9)
def test_720_str(self): for test_row in western_time_test_data: hour = test_row[1][0] minute = test_row[1][1] second = Fraction(test_row[1][2]) western = WesternTime(hour, minute, second) expected = "{:02d}:{:02d}:{:02d}".format(hour, minute, floor(second)) assert str(western) == expected
def test_520_hash_equality(self): western1 = WesternTime(11, 12, 13) # same thing western2 = WesternTime(11, 12, 13) assert hash(western1) == hash(western2) dic = {western1: 1} dic[western2] = 2 assert len(dic) == 1 assert dic[western1] == 2 assert dic[western2] == 2 western3 = WesternTime(1, 12, 13).replace(hour=11) assert hash(western1) == hash(western3) dic[western3] = 2 assert len(dic) == 1 assert dic[western3] == 2
def test_900_pickling(self): for test_row in western_time_test_data: hour = test_row[1][0] minute = test_row[1][1] second = Fraction(test_row[1][2]) western = WesternTime(hour, minute, second) for protocol in range(pickle.HIGHEST_PROTOCOL + 1): pickled = pickle.dumps(western, protocol) derived = pickle.loads(pickled) assert western == derived
def test_920_subclass(self): class W(WesternTime): theAnswer = 42 def __init__(self, *args, **kws): temp = kws.copy() self.extra = temp.pop("extra") WesternTime.__init__(self, *args, **temp) def newmeth(self, start): return start + self.hour + self.second western1 = WesternTime(11, 12, 13) western2 = W(11, 12, 13, extra=7) assert western2.theAnswer == 42 assert western2.extra == 7 assert western1.to_day_frac() == western2.to_day_frac() assert western2.newmeth(-7) == western1.hour + western1.second - 7
def test_550_cformat_percent(self): western = WesternTime(1, 2, 3) assert western.cformat('%') == '%' assert western.cformat('%%') == '%' assert western.cformat('%%%') == '%%' assert western.cformat('abcd%') == 'abcd%' assert western.cformat('%k') == '%k' assert western.cformat('a%k') == 'a%k' assert western.cformat('%k%') == '%k%'
def test_000_constructor(self): for test_row in western_time_test_data: hour = test_row[1][0] minute = test_row[1][1] second = Fraction(test_row[1][2]) western = WesternTime(hour, minute, second) assert (western.hour, western.minute, western.second) == ( hour, minute, second, )
def test_920_subclass(self): class W(WesternTime): theAnswer = 42 def __init__(self, *args, **kws): temp = kws.copy() self.extra = temp.pop('extra') WesternTime.__init__(self, *args, **temp) def newmeth(self, start): return start + self.hour + self.second western1 = WesternTime(11, 12, 13) western2 = W(11, 12, 13, extra=7) assert western2.theAnswer == 42 assert western2.extra == 7 assert western1.to_day_frac() == western2.to_day_frac() assert western2.newmeth(-7) == western1.hour + western1.second - 7
def test_260_timezone_invalid_values(self): for invalid_value in (-25, -24.000001, 24.000001, 25): with pytest.raises(ValueError): WesternTime(1, 2, 3, to_utc=invalid_value) with pytest.raises(ValueError): WesternTime.in_hours("789/123", invalid_value) with pytest.raises(ValueError): WesternTime.in_minutes("78901/123", invalid_value) with pytest.raises(ValueError): WesternTime.in_seconds("789012/123", invalid_value)
def test_030_constructor_in_seconds(self): for test_row in western_time_test_data: in_seconds = Fraction(test_row[4]) hour = test_row[1][0] minute = test_row[1][1] second = Fraction(test_row[1][2]) western = WesternTime.in_seconds(in_seconds) assert (western.hour, western.minute, western.second) == ( hour, minute, second, )
def test_090_constructor_day_frac(self): for test_row in western_time_test_data: day_frac = Fraction(test_row[0]) hour = test_row[1][0] minute = test_row[1][1] second = Fraction(test_row[1][2]) western = WesternTime.from_day_frac(day_frac) assert (western.hour, western.minute, western.second) == ( hour, minute, second, )
def test_120_invalid_parameter_types_in_minutes(self): # exception with none, two or four parameters with pytest.raises(TypeError): WesternTime.in_minutes() with pytest.raises(TypeError): WesternTime.in_minutes(1, 2) # exception with non-numeric types for invalid_minutes in ((1, ), [1], {1: 1}, (), [], {}, None): with pytest.raises(TypeError): WesternTime.in_minutes(invalid_minutes) # exception with invalid numeric types for invalid_minutes in (1j, 1 + 1j, INF, NAN): with pytest.raises(TypeError): WesternTime.in_minutes(invalid_minutes)
def test_016_invalid_parameter_types_in_seconds(self): # exception with none, two or four parameters with pytest.raises(TypeError): WesternTime.in_seconds() with pytest.raises(TypeError): WesternTime.in_seconds(1, 2) # exception with non-numeric types for invalid_seconds in ((1,), [1], {1: 1}, (), [], {}, None): with pytest.raises(TypeError): WesternTime.in_seconds(invalid_seconds) # exception with invalid numeric types for invalid_seconds in (1j, 1 + 1j, INF): with pytest.raises(TypeError): WesternTime.in_seconds(invalid_seconds)
def test_018_invalid_parameter_types_day_frac(self): # exception with none, two or four parameters with pytest.raises(TypeError): WesternTime.from_day_frac() with pytest.raises(TypeError): WesternTime.from_day_frac(1, 2) # exception with non-numeric types for invalid_day_frac in ("1", (1,), [1], {1: 1}, (), [], {}, None): with pytest.raises(TypeError): WesternTime.from_day_frac(invalid_day_frac) # exception with invalid numeric types for invalid_day_frac in (1.0, Decimal(1), 1j, 1 + 1j, INF, NAN): with pytest.raises(TypeError): WesternTime.from_day_frac(invalid_day_frac)
def test_001_constructor_types_for_seconds(self): for integer_second in (3, "3"): western = WesternTime(5, 4, integer_second) assert western.to_seconds() == Fraction(18243, 1) for fractional_second in (1.25, Fraction(5, 4), "1.25", Decimal("1.25"), "5/4"): western = WesternTime(5, 4, fractional_second) assert western.to_seconds() == Fraction(72965, 4)
def test_190_invalid_parameter_types_day_frac(self): # exception with none, two or four parameters with pytest.raises(TypeError): WesternTime.from_day_frac() with pytest.raises(TypeError): WesternTime.from_day_frac(1, 2) # exception with non-numeric types for invalid_day_frac in ("1", (1, ), [1], {1: 1}, (), [], {}, None): with pytest.raises(TypeError): WesternTime.from_day_frac(invalid_day_frac) # exception with invalid numeric types for invalid_day_frac in (1.0, Decimal(1), 1j, 1 + 1j, INF, NAN): with pytest.raises(TypeError): WesternTime.from_day_frac(invalid_day_frac)
def test_700_repr(self): import datetime2 for test_row in western_time_test_data: hour = test_row[1][0] minute = test_row[1][1] second = Fraction(test_row[1][2]) western = WesternTime(hour, minute, second) western_repr = repr(western) assert western_repr.startswith("datetime2.western.WesternTime(" ) and western_repr.endswith(")") args = western_repr[30:-1] found_hour, found_minute, found_second = args.split(",", 2) assert western == eval(western_repr) assert int(found_hour.strip()) == hour assert int(found_minute.strip()) == minute assert Fraction(eval(found_second)) == second
def test_100_invalid_parameter_types(self): # exception with none, two or four parameters with pytest.raises(TypeError): WesternTime() with pytest.raises(TypeError): WesternTime(1, 2) with pytest.raises(TypeError): WesternTime(1, 2, 3, 4) # exception with non-numeric types for invalid_par in ("1", (1, ), [1], {1: 1}, (), [], {}, None): with pytest.raises(TypeError): WesternTime(invalid_par, 1, 1) with pytest.raises(TypeError): WesternTime(1, invalid_par, 1) for invalid_par in ( (1, ), [1], { 1: 1 }, (), [], {}, None, ): # "1" is acceptable for seconds, since it is a valid Fraction argument with pytest.raises(TypeError): WesternTime(1, 1, invalid_par) # exception with invalid numeric types for invalid_par in (1.0, Fraction(1, 1), Decimal(1), 1j, 1 + 1j, INF, NAN): with pytest.raises(TypeError): WesternTime(invalid_par, 1, 1) with pytest.raises(TypeError): WesternTime(1, invalid_par, 1) for invalid_par in (1j, 1 + 1j, INF, NAN): with pytest.raises(TypeError): WesternTime(1, 1, invalid_par)
def test_510_compare_invalid_types(self): class SomeClass: pass western = WesternTime(2, 3, 4) # exception with non-numeric types for par in ("1", (1, ), [1], {1: 1}, (), [], {}, None): assert not western == par assert western != par with pytest.raises(TypeError): western < par with pytest.raises(TypeError): western > par with pytest.raises(TypeError): western <= par with pytest.raises(TypeError): western >= par # exception with numeric types (all invalid) and other objects for par in ( 1, 1.0, Fraction(1, 1), Decimal(1), 1j, 1 + 1j, INF, NAN, SomeClass(), ): assert not western == par assert western != par with pytest.raises(TypeError): western < par with pytest.raises(TypeError): western > par with pytest.raises(TypeError): western <= par with pytest.raises(TypeError): western >= par
def test_730_cformat_numbers(self): for test_row in western_time_test_data: hour = test_row[1][0] minute = test_row[1][1] second = Fraction(test_row[1][2]) western = WesternTime(hour, minute, second) # hours assert western.cformat("%H") == "{:02d}".format(hour) if hour == 0: assert western.cformat("%I") == "12" assert western.cformat("%p") == "AM" elif hour <= 11: assert western.cformat("%I") == "{:02d}".format(hour) assert western.cformat("%p") == "AM" elif hour == 12: assert western.cformat("%I") == "{:02d}".format(hour) assert western.cformat("%p") == "PM" else: assert western.cformat("%I") == "{:02d}".format(hour - 12) assert western.cformat("%p") == "PM" # minutes and seconds assert western.cformat("%M") == "{:02d}".format(minute) assert western.cformat("%S") == "{:02d}".format(floor(second))
def test_530_cformat_numbers(self): for test_row in western_time_test_data: hour = test_row[1][0] minute = test_row[1][1] second = Fraction(test_row[1][2]) western = WesternTime(hour, minute, second) # hours assert western.cformat('%H') == '{:02d}'.format(hour) if hour == 0: assert western.cformat('%I') == '12' assert western.cformat('%p') == 'AM' elif hour <= 11: assert western.cformat('%I') == '{:02d}'.format(hour) assert western.cformat('%p') == 'AM' elif hour == 12: assert western.cformat('%I') == '{:02d}'.format(hour) assert western.cformat('%p') == 'PM' else: assert western.cformat('%I') == '{:02d}'.format(hour - 12) assert western.cformat('%p') == 'PM' # minutes and seconds assert western.cformat('%M') == '{:02d}'.format(minute) assert western.cformat('%S') == '{:02d}'.format(floor(second))
def test_560_cformat_invalid_type(self): western = WesternTime(1, 2, 3) for par in (1, (1,), [1], {1: 1}, None): with pytest.raises(TypeError): western.cformat(par)
def test_310_write_attribute_to_utc(self): western = WesternTime(10, 10, 10, to_utc=10) with pytest.raises(AttributeError): western.to_utc = 3
def test_540_cformat_microseconds(self): for fraction, microseconds in western_time_microseconds: western = WesternTime.in_seconds(Fraction(fraction)) assert western.cformat('%f') == microseconds
def test_436_replace_invalid_values(self): western1 = WesternTime(11, 10, 9) with pytest.raises(ValueError): western1.replace(hour=-1) with pytest.raises(ValueError): western1.replace(minute=-1) with pytest.raises(ValueError): western1.replace(second=-1) with pytest.raises(ValueError): western1.replace(hour=24) with pytest.raises(ValueError): western1.replace(minute=60) with pytest.raises(ValueError): western1.replace(second=60) with pytest.raises(ValueError): western1.replace(second=NAN)
def test_530_bool(self): for test_row in western_time_test_data: hour = test_row[1][0] minute = test_row[1][1] second = Fraction(test_row[1][2]) assert WesternTime(hour, minute, second)
def test_433_replace_invalid_types(self): western = WesternTime(11, 10, 9) # exception for positional parameters with pytest.raises(TypeError): western.replace(1) # exception with non-numeric types for par in ("1", (1,), [1], {1: 1}, (), [], {}): with pytest.raises(TypeError): western.replace(hour=par) with pytest.raises(TypeError): western.replace(minute=par) for par in ((1,), [1], {1: 1}, (), [], {}): with pytest.raises(TypeError): western.replace(second=par) # exception with invalid numeric types for par in (1.0, Fraction(1, 1), Decimal(1), 1j, 1 + 1j, INF, NAN): with pytest.raises(TypeError): western.replace(hour=par) with pytest.raises(TypeError): western.replace(minute=par) for par in (1j, 1 + 1j, INF): with pytest.raises(TypeError): western.replace(second=par)
def test_650_replace(self): for test_row in western_time_test_data: hour = test_row[1][0] minute = test_row[1][1] second = Fraction(test_row[1][2]) western = WesternTime(hour, minute, second) assert western.replace() == WesternTime(hour, minute, second) assert western.replace(hour=11) == WesternTime(11, minute, second) assert western.replace(minute=10) == WesternTime(hour, 10, second) assert western.replace(second=9) == WesternTime(hour, minute, 9) assert western.replace(minute=10, hour=11) == WesternTime(11, 10, second) assert western.replace(second=9, hour=11) == WesternTime(11, minute, 9) assert western.replace(second=9, minute=10) == WesternTime(hour, 10, 9) assert western.replace(second=9, minute=10, hour=11) == WesternTime(11, 10, 9)
def test_110_write_attribute_timezone(self): western = WesternTime(10, 10, 10, tz=10) with pytest.raises(AttributeError): western.tz = 3
def test_026_invalid_values_in_seconds(self): for num, denum in ((86400, 1), (1, -1), (86400000001, 1000000), (-1, 1000000)): with pytest.raises(ValueError): WesternTime.in_seconds(Fraction(num, denum))
def test_028_invalid_values_day_frac(self): for num, denum in ((1, 1), (1, -1), (1000001, 1000000), (-1, 1000000)): with pytest.raises(ValueError): WesternTime.from_day_frac(Fraction(num, denum))
def test_024_invalid_values_in_minutes(self): for num, denum in ((1440, 1), (1, -1), (1440000001, 1000000), (-1, 1000000)): with pytest.raises(ValueError): WesternTime.in_minutes(Fraction(num, denum))
def test_653_replace_invalid_types(self): western = WesternTime(11, 10, 9) # exception for positional parameters with pytest.raises(TypeError): western.replace(1) # exception with non-numeric types for par in ("1", (1, ), [1], {1: 1}, (), [], {}): with pytest.raises(TypeError): western.replace(hour=par) with pytest.raises(TypeError): western.replace(minute=par) for par in ((1, ), [1], {1: 1}, (), [], {}): with pytest.raises(TypeError): western.replace(second=par) # exception with invalid numeric types for par in (1.0, Fraction(1, 1), Decimal(1), 1j, 1 + 1j, INF, NAN): with pytest.raises(TypeError): western.replace(hour=par) with pytest.raises(TypeError): western.replace(minute=par) for par in (1j, 1 + 1j, INF): with pytest.raises(TypeError): western.replace(second=par)
def test_656_replace_invalid_values(self): western1 = WesternTime(11, 10, 9) with pytest.raises(ValueError): western1.replace(hour=-1) with pytest.raises(ValueError): western1.replace(minute=-1) with pytest.raises(ValueError): western1.replace(second=-1) with pytest.raises(ValueError): western1.replace(hour=24) with pytest.raises(ValueError): western1.replace(minute=60) with pytest.raises(ValueError): western1.replace(second=60) with pytest.raises(TypeError): western1.replace(second=NAN)
def test_760_cformat_invalid_type(self): western = WesternTime(1, 2, 3) for par in (1, (1, ), [1], {1: 1}, None): with pytest.raises(TypeError): western.cformat(par)
def __init__(self, *args, **kws): temp = kws.copy() self.extra = temp.pop("extra") WesternTime.__init__(self, *args, **temp)
def __init__(self, *args, **kws): temp = kws.copy() self.extra = temp.pop('extra') WesternTime.__init__(self, *args, **temp)
def test_740_cformat_microseconds(self): for fraction, microseconds in western_time_microseconds: western = WesternTime.in_seconds(Fraction(fraction)) assert western.cformat("%f") == microseconds