Skip to content

Commit 8b0113e

Browse files
committed
Test that json encoding doesn't mutate
1 parent 41ec4e8 commit 8b0113e

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

plotly/tests/test_optional/test_utils/test_utils.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import numpy as np
77
import json
88
import pandas as pd
9+
from pandas.util.testing import assert_series_equal
910

1011
from plotly import utils
1112
from plotly.grid_objs import Column
@@ -57,24 +58,43 @@ def test_figure_json_encoding():
5758
'"z": [1, "A", "2014-01-05", '
5859
'"2014-01-05 01:01:01", "2014-01-05 01:01:01.000001"]}')
5960
assert(js2 == '{"type": "scatter", "x": [1, 2, 3]}')
61+
62+
# Test JSON encoding works
6063
json.dumps(data, cls=utils._plotlyJSONEncoder, sort_keys=True)
6164
json.dumps(figure, cls=utils._plotlyJSONEncoder, sort_keys=True)
6265

66+
# Test data wasn't mutated
67+
assert(bool(np.asarray(np_list ==
68+
np.array([1, 2, 3, np.NaN,
69+
np.NAN, np.Inf, dt(2014, 1, 5)])).all()))
70+
assert(set(data[0]['z']) ==
71+
set([1, 'A', dt(2014, 1, 5), dt(2014, 1, 5, 1, 1, 1),
72+
dt(2014, 1, 5, 1, 1, 1, 1)]))
73+
6374

6475
def test_pandas_json_encoding():
6576
j1 = json.dumps(df['col 1'], cls=utils._plotlyJSONEncoder)
6677
assert(j1 == '[1, 2, 3, "2014-01-05", null, NaN, Infinity]')
78+
79+
# Test that data wasn't mutated
80+
assert_series_equal(df['col 1'],
81+
pd.Series([1, 2, 3, dt(2014, 1, 5),
82+
pd.NaT, np.NaN, np.Inf]))
83+
6784
j2 = json.dumps(df.index, cls=utils._plotlyJSONEncoder)
6885
assert(j2 == '[0, 1, 2, 3, 4, 5, 6]')
6986

70-
j3 = json.dumps([pd.NaT], cls=utils._plotlyJSONEncoder)
87+
nat = [pd.NaT]
88+
j3 = json.dumps(nat, cls=utils._plotlyJSONEncoder)
7189
assert(j3 == '[null]')
90+
assert(nat[0] is pd.NaT)
7291

7392
j4 = json.dumps(rng, cls=utils._plotlyJSONEncoder)
7493
assert(j4 == '["2011-01-01", "2011-01-01 01:00:00"]')
7594

7695
j5 = json.dumps(ts, cls=utils._plotlyJSONEncoder)
7796
assert(j5 == '[1.5, 2.5]')
97+
assert_series_equal(ts, pd.Series([1.5, 2.5], index=rng))
7898

7999
j6 = json.dumps(ts.index, cls=utils._plotlyJSONEncoder)
80100
assert(j6 == '["2011-01-01", "2011-01-01 01:00:00"]')

0 commit comments

Comments
 (0)