|
3 | 3 | from cftime import real_datetime |
4 | 4 | from cftime import (Datetime360Day, DatetimeAllLeap, |
5 | 5 | DatetimeGregorian, DatetimeJulian, DatetimeNoLeap, |
6 | | - DatetimeProlepticGregorian, _parse_date, |
| 6 | + DatetimeProlepticGregorian, DatetimeTAI, _parse_date, |
7 | 7 | date2index, date2num, num2date, UNIT_CONVERSION_FACTORS) |
8 | 8 | import copy |
9 | 9 | import unittest |
@@ -2248,6 +2248,77 @@ def test_num2date_precision(): |
2248 | 2248 | assert np.ma.is_masked(date2[0]) |
2249 | 2249 | assert date[1] == date2[1] |
2250 | 2250 |
|
| 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 | + |
2251 | 2322 |
|
2252 | 2323 | if __name__ == '__main__': |
2253 | 2324 | unittest.main() |
0 commit comments