8000 Add tests for date module · matplotlib/matplotlib@90d034c · GitHub
[go: up one dir, main page]

Skip to content

Commit 90d034c

Browse files
committed
Add tests for date module
1 parent 2aa71eb commit 90d034c

File tree

2 files changed

+68
-20
lines changed

2 files changed

+68
-20
lines changed

lib/matplotlib/dates.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,7 @@ def _dt64_to_ordinalf(d):
332332

333333
NaT_int = np.datetime64('NaT').astype(np.int64)
334334
d_int = d.astype(np.int64)
335-
try:
336-
dt[d_int == NaT_int] = np.nan
337-
except TypeError:
338-
if d_int == NaT_int:
339-
dt = np.nan
335+
dt[d_int == NaT_int] = np.nan
340336
return dt
341337

342338

@@ -592,7 +588,8 @@ def drange(dstart, dend, delta):
592588

593589
# ensure, that an half open interval will be generated [dstart, dend)
594590
if dinterval_end >= dend:
595-
# if the endpoint is greater than dend, just subtract one delta
591+
# if the endpoint is greater than or equal to dend,
592+
# just subtract one delta
596593
dinterval_end -= delta
597594
num -= 1
598595

lib/matplotlib/tests/test_dates.py

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,13 @@ def test_drange():
357357
# dates from an half open interval [start, end)
358358
assert len(mdates.drange(start, end, delta)) == 24
359359

360+
# Same if interval ends slightly earlier
361+
end = end - datetime.timedelta(microseconds=1)
362+
assert len(mdates.drange(start, end, delta)) == 24
363+
360364
# if end is a little bit later, we expect the range to contain one element
361365
# more
362-
end = end + datetime.timedelta(microseconds=1)
366+
end = end + datetime.timedelta(microseconds=2)
363367
assert len(mdates.drange(start, end, delta)) == 25
364368

365369
# reset end
@@ -998,7 +1002,6 @@ def _test_rrulewrapper(attach_tz, get_tz):
9981002
dtend = attach_tz(datetime.datetime(2017, 4, 4, 0), SYD)
9991003

10001004
rule = mdates.rrulewrapper(freq=dateutil.rrule.DAILY, dtstart=dtstart)
1001-
10021005
act = rule.between(dtstart, dtend)
10031006
exp = [datetime.datetime(2017, 4, 1, 13, tzinfo=dateutil.tz.tzutc()),
10041007
datetime.datetime(2017, 4, 2, 14, tzinfo=dateutil.tz.tzutc())]
@@ -1012,6 +1015,20 @@ def attach_tz(dt, zi):
10121015

10131016
_test_rrulewrapper(attach_tz, dateutil.tz.gettz)
10141017

1018+
SYD = dateutil.tz.gettz('Australia/Sydney')
1019+
dtstart = datetime.datetime(2017, 4, 1, 0)
1020+
dtend = datetime.datetime(2017, 4, 4, 0)
1021+
rule = mdates.rrulewrapper(freq=dateutil.rrule.DAILY, dtstart=dtstart,
1022+
tzinfo=SYD, until=dtend)
1023+
assert rule.after(dtstart) == datetime.datetime(2017, 4, 2, 0, 0,
1024+
tzinfo=SYD)
1025+
assert rule.before(dtend) == datetime.datetime(2017, 4, 3, 0, 0,
1026+
tzinfo=SYD)
1027+
1028+
# Test parts of __getattr__
1029+
assert rule._base_tzinfo == SYD
1030+
assert rule._interval == 1
1031+
10151032

10161033
@pytest.mark.pytz
10171034
def test_rrulewrapper_pytz():
@@ -1046,6 +1063,15 @@ def test_yearlocator_pytz():
10461063
'2014-01-01 00:00:00-05:00', '2015-01-01 00:00:00-05:00']
10471064
st = list(map(str, mdates.num2date(locator(), tz=tz)))
10481065
assert st == expected
1066+
assert np.allclose(locator.tick_values(x[0], x[1]), np.array(
1067+
[14610.20833333, 14610.33333333, 14610.45833333, 14610.58333333,
1068+
14610.70833333, 14610.83333333, 14610.95833333, 14611.08333333,
1069+
14611.20833333]))
1070+
assert np.allclose(locator.get_locator(x[1], x[0]).tick_values(x[0], x[1]),
1071+
np.array(
1072+
[14610.20833333, 14610.33333333, 14610.45833333, 14610.58333333,
1073+
14610.70833333, 14610.83333333, 14610.95833333, 14611.08333333,
1074+
14611.20833333]))
10491075

10501076

10511077
def test_YearLocator():
@@ -1290,18 +1316,14 @@ def test_datestr2num():
12901316
month=1, day=10)).size == 0
12911317

12921318

1293-
def test_concise_formatter_exceptions():
1319+
@pytest.mark.parametrize('kwarg',
1320+
('formats', 'zero_formats', 'offset_formats'))
1321+
def test_concise_formatter_exceptions(kwarg):
12941322
locator = mdates.AutoDateLocator()
1295-
with pytest.raises(ValueError, match="formats argument must be a list"):
1296-
mdates.ConciseDateFormatter(locator, formats=['', '%Y'])
1297-
1298-
with pytest.raises(ValueError,
1299-
match="zero_formats argument must be a list"):
1300-
mdates.ConciseDateFormatter(locator, zero_formats=['', '%Y'])
1301-
1302-
with pytest.raises(ValueError,
1303-
match="offset_formats argument must be a list"):
1304-
mdates.ConciseDateFormatter(locator, offset_formats=['', '%Y'])
1323+
kwargs = {kwarg: ['', '%Y']}
1324+
match = f"{kwarg} argument must be a list"
1325+
with pytest.raises(ValueError, match=match):
1326+
mdates.ConciseDateFormatter(locator, **kwargs)
13051327

13061328

13071329
def test_concise_formatter_call():
@@ -1312,7 +1334,10 @@ def test_concise_formatter_call():
13121334

13131335

13141336
@pytest.mark.parametrize('span, expected_locator',
1315-
((0.02, mdates.MinuteLocator),
1337+
((0, mdates.MinuteLocator),
1338+
(0.00001, mdates.MicrosecondLocator),
1339+
(0.001, mdates.SecondLocator),
1340+
(0.02, mdates.MinuteLocator),
13161341
(1, mdates.HourLocator),
13171342
(19, mdates.DayLocator),
13181343
(40, mdates.WeekdayLocator),
@@ -1327,3 +1352,29 @@ def test_usetex_newline():
13271352
fig, ax = plt.subplots()
13281353
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m\n%Y'))
13291354
fig.canvas.draw()
1355+
1356+
1357+
@pytest.mark.parametrize('val', (-1000000, 10000000))
1358+
def test_num2date_error(val):
1359+
with pytest.raises(ValueError, match=f"Date ordinal {val} converts"):
1360+
mdates.num2date(val)
1361+
1362+
1363+
def test_num2date_roundoff():
1364+
assert mdates.num2date(100000.0000578702) == datetime.datetime(
1365+
2243, 10, 17, 0, 0, 4, 999980, tzinfo=datetime.timezone.utc)
1366+
# Slightly larger, steps of 20 microseconds
1367+
assert mdates.num2date(100000.0000578703) == datetime.datetime(
1368+
2243, 10, 17, 0, 0, 5, tzinfo=datetime.timezone.utc)
1369+
1370+
1371+
def test_DateFormatter_settz():
1372+
time = mdates.date2num(datetime.datetime(2011, 1, 1, 0, 0,
1373+
tzinfo=mdates.UTC))
1374+
formatter = mdates.DateFormatter('%Y-%b-%d %H:%M')
1375+
# Default UTC
1376+
assert formatter(time) == '2011-Jan-01 00:00'
1377+
1378+
# Set tzinfo
1379+
formatter.set_tzinfo('Pacific/Kiritimati')
1380+
assert formatter(time) == '2011-Jan-01 14:00'

0 commit comments

Comments
 (0)
0