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])