Skip to content

Commit b8f72f0

Browse files
authored
Merge pull request #389 from ChrisBarker-NOAA/tai-chb
Add tests for tai calendar
2 parents d9fe073 + 19c1b54 commit b8f72f0

3 files changed

Lines changed: 75 additions & 4 deletions

File tree

src/cftime/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from ._cftime import CFWarning
88
# these will be removed in a future release
99
from ._cftime import (DatetimeNoLeap, DatetimeAllLeap, Datetime360Day,
10-
Datetime360Day, DatetimeJulian,
10+
Datetime360Day, DatetimeJulian, DatetimeTAI,
1111
DatetimeGregorian, DatetimeProlepticGregorian)
1212

1313

src/cftime/_cftime.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ _units = microsec_units+millisec_units+sec_units+min_units+hr_units+day_units
3030
# '366_day'=='all_leap','365_day'=='noleap')
3131
# see http://cfconventions.org/cf-conventions/cf-conventions#calendar
3232
# for definitions.
33-
_calendars = ['standard', 'gregorian', 'proleptic_gregorian','tai',
33+
_calendars = ['standard', 'gregorian', 'proleptic_gregorian', 'tai',
3434
'noleap', 'julian', 'all_leap', '365_day', '366_day', '360_day']
3535
_idealized_calendars= ['all_leap','noleap','366_day','365_day','360_day']
3636
# Following are number of days per month
@@ -83,7 +83,7 @@ def _datesplit(timestr):
8383

8484
return units.lower(), remainder
8585

86-
def _dateparse(timestr,calendar,has_year_zero=None):
86+
def _dateparse(timestr, calendar, has_year_zero=None):
8787
"""parse a string of the form time-units since yyyy-mm-dd hh:mm:ss,
8888
return a datetime instance"""
8989
# same as version in cftime, but returns a timezone naive

test/test_cftime.py

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from cftime import real_datetime
44
from cftime import (Datetime360Day, DatetimeAllLeap,
55
DatetimeGregorian, DatetimeJulian, DatetimeNoLeap,
6-
DatetimeProlepticGregorian, _parse_date,
6+
DatetimeProlepticGregorian, DatetimeTAI, _parse_date,
77
date2index, date2num, num2date, UNIT_CONVERSION_FACTORS)
88
import copy
99
import unittest
@@ -2248,6 +2248,77 @@ def test_num2date_precision():
22482248
assert np.ma.is_masked(date2[0])
22492249
assert date[1] == date2[1]
22502250

2251+
# NOTE: using pytest style tests -- these won't run without pytest
2252+
# but it looks like you're using pytest, so this is cleaner and easier
2253+
2254+
# There's really no reason to put these in a class, but it does organize things
2255+
class Test_tai:
2256+
"""
2257+
tests specific to the tai calendar
2258+
"""
2259+
def test_dateparse_valid(self):
2260+
"""
2261+
It should raise for an epoch before 1958
2262+
"""
2263+
# This is directly testing the _dateparse function
2264+
# Which is a "proper" unit test, but also testing an internal function.
2265+
timestring = "seconds since 1958-01-01T00:00:00"
2266+
basedate = cftime._dateparse(timestring, 'tai')
2267+
2268+
print(repr(basedate))
2269+
2270+
assert basedate == cftime.datetime(1958, 1, 1, 0, 0, 0, 0, calendar='tai', has_year_zero=False)
2271+
2272+
def test_dateparse_before_1958(self):
2273+
"""
2274+
It should raise for an epoch before 1958
2275+
"""
2276+
# This is directly testing the _dateparse function
2277+
# Which is a "proper" unit test, but also testing an internal function.
2278+
timestring = "seconds since 1957-01-01T00:00:00"
2279+
with pytest.raises(ValueError):
2280+
basedate = cftime._dateparse(timestring, 'tai')
2281+
# cftime._dateparse(timestring, 'tai', has_year_zero=None)
2282+
print(basedate)
2283+
2284+
def test_dateparse_with_offset(self):
2285+
"""
2286+
It should raise if there's an offset
2287+
"""
2288+
# This is directly testing the _dateparse function
2289+
# Which is a "proper" unit test, but also testing an internal function.
2290+
timestring = "seconds since 1965-01-01T00:00:00+08:00"
2291+
with pytest.raises(ValueError):
2292+
basedate = cftime._dateparse(timestring, 'tai')
2293+
# cftime._dateparse(timestring, 'tai', has_year_zero=None)
2294+
print(basedate)
2295+
2296+
def test_year_zero(self):
2297+
"""
2298+
tai does not have a year zero -- not sure this is worth testing, but for full coverage.
2299+
"""
2300+
# This is directly testing the _dateparse function
2301+
# Which is a "proper" unit test, but also testing an internal function.
2302+
timestring = "seconds since 1965-01-01T00:00:00"
2303+
with pytest.raises(ValueError):
2304+
basedate = cftime._dateparse(timestring, 'tai', has_year_zero=True)
2305+
# cftime._dateparse(timestring, 'tai', has_year_zero=None)
2306+
print(basedate)
2307+
2308+
def test_creation_valid(self):
2309+
dt = DatetimeTAI(2025, 1, 9, 14, 18)
2310+
2311+
assert dt.calendar == 'tai'
2312+
assert dt.has_year_zero is False
2313+
2314+
print(repr(dt))
2315+
2316+
def test_creation_before_1958(self):
2317+
with pytest.raises(ValueError):
2318+
dt = DatetimeTAI(1957, 1, 9, 14, 18)
2319+
print(repr(dt))
2320+
2321+
22512322

22522323
if __name__ == '__main__':
22532324
unittest.main()

0 commit comments

Comments
 (0)