def test_custom_encoder(self, json_data_set, filepath_json, json_data): """Test using a custom JSONEncoder when saving the data.""" pattern = (r"Failed while saving data to data set " r"JSONLocalDataSet\(.+\).*\n.*Decimal.* is not " r"JSON serializable") with pytest.raises(DataSetError, match=pattern): json_data_set.save(json_data) json_data_set_decimal_enc = JSONLocalDataSet( filepath=filepath_json, save_args=dict(cls=DecimalEncoder)) json_data_set_decimal_enc.save(json_data) reloaded = json_data_set_decimal_enc.load() assert reloaded["float"] == reloaded["dec_float"] assert reloaded["int"] == reloaded["dec_int"]
def test_allow_nan(self, json_data_set, filepath_json): """Strict JSON specification does not allow out of range float values, however the python implementation accepts them by default. Test both those cases.""" # default python json_data_set.save([inf]) assert json_data_set.load() == [inf] # strict JSON json_data_set_strict = JSONLocalDataSet( filepath=filepath_json, save_args=dict(allow_nan=False)) pattern = "Out of range float values are not JSON compliant" with pytest.raises(DataSetError, match=pattern): json_data_set_strict.save([inf])
def test_version_str_repr(self, load_version, save_version): """Test that version is in string representation of the class instance when applicable.""" filepath = "test.json" ds = JSONLocalDataSet(filepath=filepath) ds_versioned = JSONLocalDataSet(filepath=filepath, version=Version( load_version, save_version)) assert filepath in str(ds) assert "version" not in str(ds) assert filepath in str(ds_versioned) ver_str = "version=Version(load={}, save='{}')".format( load_version, save_version) assert ver_str in str(ds_versioned)
def versioned_json_data_set(filepath_json, load_version, save_version): return JSONLocalDataSet( filepath=filepath_json, version=Version(load_version, save_version) )
def json_data_set_with_load_args(filepath_json): return JSONLocalDataSet(filepath=filepath_json, load_args={"parse_float": Decimal})
def json_data_set(filepath_json): return JSONLocalDataSet(filepath=filepath_json)