Skip to content

Commit 13359a1

Browse files
committed
Add date encoder unit test.
1 parent 8d81b63 commit 13359a1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

plotly/tests/test_optional/test_utils/test_utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,27 @@ def test_encode_as_datetime(self):
123123
res = utils.PlotlyJSONEncoder.encode_as_datetime(aware_datetime)
124124
self.assertEqual(res, '2013-10-01 04:00:00')
125125

126+
def test_encode_as_date(self):
127+
128+
# should *fail* without 'utcoffset' and 'isoformat' and '__sub__' attrs
129+
non_datetimes = ['noon', 56, '00:00:00']
130+
for obj in non_datetimes:
131+
self.assertRaises(utils.NotEncodable,
132+
utils.PlotlyJSONEncoder.encode_as_date, obj)
133+
134+
# should work with a date
135+
a_date = datetime.date(2013, 10, 01)
136+
res = utils.PlotlyJSONEncoder.encode_as_date(a_date)
137+
self.assertEqual(res, '2013-10-01')
138+
139+
# should also work with a date time without a utc offset!
140+
# TODO: is this OK? We could raise errors after checking isinstance...
141+
res = utils.PlotlyJSONEncoder.encode_as_date(
142+
datetime.datetime(2013, 10, 1, microsecond=10)
143+
)
144+
self.assertEqual(res, '2013-10-01 00:00:00.000010')
145+
146+
126147
## JSON encoding
127148
numeric_list = [1, 2, 3]
128149
np_list = np.array([1, 2, 3, np.NaN, np.NAN, np.Inf, dt(2014, 1, 5)])

0 commit comments

Comments
 (0)