def test_encode_decode(obj, obj_type): """Test encoding/decoding of the various object types to/from JSON.""" encoded = encode4js(obj) decoded = decode4js(encoded) assert decoded == obj assert isinstance(decoded, obj_type)
def test_encode_decode_pandas(): """Test encoding/decoding of various pandas objects to/from JSON.""" pytest.importorskip('pandas') import pandas as pd obj_df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=['a', 'b', 'c']) encoded_df = encode4js(obj_df) decoded_df = decode4js(encoded_df) assert np.all(pd.DataFrame.eq(obj_df, decoded_df)) assert isinstance(decoded_df, pd.DataFrame) obj_ser = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) encoded_ser = encode4js(obj_ser) decoded_ser = decode4js(encoded_ser) assert np.all(pd.Series.eq(obj_ser, decoded_ser)) assert isinstance(decoded_ser, pd.Series)
def test_encode_decode(obj, obj_type): """Test encoding/decoding of the various object types to/from JSON.""" encoded = encode4js(obj) decoded = decode4js(encoded) if isinstance(obj, np.ndarray) and obj.dtype == 'object': assert decoded[0] == obj[0] assert np.all(decoded[1] == obj[1]) else: assert decoded == obj assert isinstance(decoded, obj_type)