def test_300_compare(self): greg1 = GregorianCalendar(2, 3, 4) greg2 = GregorianCalendar(2, 3, 4) assert greg1 == greg2 assert greg1 <= greg2 assert greg1 >= greg2 assert not greg1 != greg2 assert not greg1 < greg2 assert not greg1 > greg2 for year, month, day in (3, 3, 3), (2, 4, 4), (2, 3, 5): greg3 = GregorianCalendar(year, month, day) # this is larger than greg1 assert greg1 < greg3 assert greg3 > greg1 assert greg1 <= greg3 assert greg3 >= greg1 assert greg1 != greg3 assert greg3 != greg1 assert not greg1 == greg3 assert not greg3 == greg1 assert not greg1 > greg3 assert not greg3 < greg1 assert not greg1 >= greg3 assert not greg3 <= greg1
def test_420_day_of_year(self): for test_row in gregorian_test_data: year = test_row[2][0] month = test_row[2][1] day = test_row[2][2] doy = test_row[3] assert GregorianCalendar(year, month, day).day_of_year() == doy
def test_410_weekday(self): for test_row in gregorian_test_data: year = test_row[2][0] month = test_row[2][1] day = test_row[2][2] weekday = test_row[1] assert GregorianCalendar(year, month, day).weekday() == weekday
def test_400_to_rata_die(self): for test_row in gregorian_test_data: year = test_row[2][0] month = test_row[2][1] day = test_row[2][2] rd = test_row[0] assert GregorianCalendar(year, month, day).to_rata_die() == rd
def test_310_compare_invalid_types(self): class SomeClass: pass greg = GregorianCalendar(2, 3, 4) # exception with non-numeric types for par in ("1", (1, ), [1], {1: 1}, (), [], {}, None): assert not greg == par assert greg != par with pytest.raises(TypeError): greg < par with pytest.raises(TypeError): greg > par with pytest.raises(TypeError): greg <= par with pytest.raises(TypeError): greg >= 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 greg == par assert greg != par with pytest.raises(TypeError): greg < par with pytest.raises(TypeError): greg > par with pytest.raises(TypeError): greg <= par with pytest.raises(TypeError): greg >= par
def test_020_invalid_values(self): for test_row in gregorian_invalid_data: year = test_row[0] month = test_row[1] day = test_row[2] with pytest.raises(ValueError): GregorianCalendar(year, month, day)
def test_000_constructor(self): for test_row in gregorian_test_data: year = test_row[2][0] month = test_row[2][1] day = test_row[2][2] greg = GregorianCalendar(year, month, day) assert (greg.year, greg.month, greg.day) == (year, month, day)
def test_100_write_attribute(self): greg = GregorianCalendar(1, 1, 1) with pytest.raises(AttributeError): greg.year = 3 with pytest.raises(AttributeError): greg.month = 3 with pytest.raises(AttributeError): greg.day = 3
def test_436_replace_invalid_values(self): greg1 = GregorianCalendar(11, 10, 9) with pytest.raises(ValueError): greg1.replace(month=0) with pytest.raises(ValueError): greg1.replace(day=0) with pytest.raises(ValueError): greg1.replace(month=-1) with pytest.raises(ValueError): greg1.replace(day=-1) with pytest.raises(ValueError): greg1.replace(month=13) with pytest.raises(ValueError): greg1.replace(day=32) for month in (4, 6, 9, 11): greg2 = GregorianCalendar(11, month, 9) with pytest.raises(ValueError): greg2.replace(day=31) greg3 = GregorianCalendar(11, 10, 31) with pytest.raises(ValueError): greg3.replace(month=month) for day in (29, 30, 31): greg4 = GregorianCalendar(11, 3, day) with pytest.raises(ValueError): greg4.replace(month=2) greg5 = GregorianCalendar(11, 2, 4) with pytest.raises(ValueError): greg5.replace(day=day) for day in (30, 31): greg6 = GregorianCalendar(4, 3, day) # leap year with pytest.raises(ValueError): greg6.replace(month=2) greg7 = GregorianCalendar(4, 2, 4) with pytest.raises(ValueError): greg7.replace(day=day)
def test_560_cformat_percent(self): greg = GregorianCalendar(1, 2, 3) assert greg.cformat('%') == '%' assert greg.cformat('%%') == '%' assert greg.cformat('%%%') == '%%' assert greg.cformat('abcd%') == 'abcd%' assert greg.cformat('%k') == '%k' assert greg.cformat('a%k') == 'a%k' assert greg.cformat('%k%') == '%k%'
def test_320_hash_equality(self): greg1 = GregorianCalendar(2000, 12, 31) # same thing greg2 = GregorianCalendar(2000, 12, 31) assert hash(greg1) == hash(greg2) dic = {greg1: 1} dic[greg2] = 2 assert len(dic) == 1 assert dic[greg1] == 2 assert dic[greg2] == 2 greg3 = GregorianCalendar(200, 12, 31).replace(year=2000) assert hash(greg1) == hash(greg3) dic[greg3] = 2 assert len(dic) == 1 assert dic[greg3] == 2
def test_550_cformat_week_number(self): for test_row in gregorian_test_data: year = test_row[2][0] month = test_row[2][1] day = test_row[2][2] WNS = test_row[4] WNM = test_row[5] greg = GregorianCalendar(year, month, day) assert greg.cformat('%U') == WNS assert greg.cformat('%W') == WNM
def test_900_pickling(self): for test_row in gregorian_test_data: year = test_row[2][0] month = test_row[2][1] day = test_row[2][2] greg = GregorianCalendar(year, month, day) for protocol in range(pickle.HIGHEST_PROTOCOL + 1): pickled = pickle.dumps(greg, protocol) derived = pickle.loads(pickled) assert greg == derived
def test_430_replace(self): for test_row in gregorian_test_data[: 33]: # take Calendrical Calculations tests data only (other may make replace fail, as in the next tests method) year = test_row[2][0] month = test_row[2][1] day = test_row[2][2] greg = GregorianCalendar(year, month, day) assert greg.replace() == GregorianCalendar(year, month, day) assert greg.replace(year=11) == GregorianCalendar(11, month, day) assert greg.replace(month=10) == GregorianCalendar(year, 10, day) assert greg.replace(day=9) == GregorianCalendar(year, month, 9) assert greg.replace(month=10, year=11) == GregorianCalendar(11, 10, day) assert greg.replace(day=9, year=11) == GregorianCalendar(11, month, 9) assert greg.replace(day=9, month=10) == GregorianCalendar(year, 10, 9) assert greg.replace(day=9, month=10, year=11) == GregorianCalendar(11, 10, 9)
def test_500_repr(self): import datetime2 for test_row in gregorian_test_data: year = test_row[2][0] month = test_row[2][1] day = test_row[2][2] greg = GregorianCalendar(year, month, day) greg_repr = repr(greg) names, args = greg_repr.split('(') assert names.split('.') == [ 'datetime2', 'western', 'GregorianCalendar' ] args = args[:-1] # drop ')' for found, expected in zip(args.split(','), (year, month, day)): assert int(found) == expected assert greg == eval(greg_repr)
def test_530_cformat_numbers(self): for test_row in gregorian_test_data: wday = test_row[1] year = test_row[2][0] month = test_row[2][1] day = test_row[2][2] doy = test_row[3] greg = GregorianCalendar(year, month, day) assert greg.cformat('%d') == '{:02d}'.format(day) assert greg.cformat('%j') == '{:03d}'.format(doy) assert greg.cformat('%m') == '{:02d}'.format(month) assert greg.cformat('%w') == '{:d}'.format(wday) if year >= 0: assert greg.cformat('%y') == ('0' + str(year))[-2:] assert greg.cformat('%Y') == '{:04d}'.format(year) else: assert greg.cformat('%y') == ('0' + str(-year))[-2:] assert greg.cformat('%Y') == '-{:04d}'.format(-year)
def test_010_invalid_parameter_types(self): # exception with none, two or four parameters with pytest.raises(TypeError): GregorianCalendar() with pytest.raises(TypeError): GregorianCalendar(1, 2) with pytest.raises(TypeError): GregorianCalendar(1, 2, 3, 4) # exception with non-numeric types for par in ("1", (1, ), [1], {1: 1}, (), [], {}, None): with pytest.raises(TypeError): GregorianCalendar(par, 1, 1) with pytest.raises(TypeError): GregorianCalendar(1, par, 1) with pytest.raises(TypeError): GregorianCalendar(1, 1, 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): GregorianCalendar(par, 1, 1) with pytest.raises(TypeError): GregorianCalendar(1, par, 1) with pytest.raises(TypeError): GregorianCalendar(1, 1, par)
def test_920_subclass(self): class G(GregorianCalendar): theAnswer = 42 def __init__(self, *args, **kws): temp = kws.copy() self.extra = temp.pop('extra') GregorianCalendar.__init__(self, *args, **temp) def newmeth(self, start): return start + self.year + self.month greg1 = GregorianCalendar(2003, 4, 14) greg2 = G(2003, 4, 14, extra=7) assert greg2.theAnswer == 42 assert greg2.extra == 7 assert greg1.to_rata_die() == greg2.to_rata_die() assert greg2.newmeth(-7) == greg1.year + greg1.month - 7
def test_520_str(self): for test_row in gregorian_test_data: year = test_row[2][0] month = test_row[2][1] day = test_row[2][2] greg = GregorianCalendar(year, month, day) if year < 0: expected = '-' year = -year else: expected = '' ys = str(year) if len(ys) < 4: expected += ('000' + ys)[-4:] else: expected += ys expected += '-' + ('0' + str(month))[-2:] expected += '-' + ('0' + str(day))[-2:] assert str(greg) == expected
def test_433_replace_invalid_types(self): greg = GregorianCalendar(11, 10, 9) # exception for positional parameters with pytest.raises(TypeError): greg.replace(1) # exception with non-numeric types for par in ("1", (1, ), [1], {1: 1}, (), [], {}): with pytest.raises(TypeError): greg.replace(year=par) with pytest.raises(TypeError): greg.replace(month=par) with pytest.raises(TypeError): greg.replace(day=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): greg.replace(year=par) with pytest.raises(TypeError): greg.replace(month=par) with pytest.raises(TypeError): greg.replace(day=par)
def test_540_cformat_names(self): weekdays = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ] abbr_weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ] abbr_months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] for test_row in gregorian_test_data: wday = test_row[1] year = test_row[2][0] month = test_row[2][1] day = test_row[2][2] greg = GregorianCalendar(year, month, day) assert greg.cformat('%a') == abbr_weekdays[wday - 1] assert greg.cformat('%A') == weekdays[wday - 1] assert greg.cformat('%b') == abbr_months[month - 1] assert greg.cformat('%B') == months[month - 1]
def test_330_bool(self): for test_row in gregorian_test_data: year = test_row[2][0] month = test_row[2][1] day = test_row[2][2] assert GregorianCalendar(year, month, day)
def test_570_cformat_invalid_type(self): greg = GregorianCalendar(1, 2, 3) for par in (1, (1, ), [1], {1: 1}, None): with pytest.raises(TypeError): greg.cformat(par)