def test_jsonables(): base = BaseTypes(None, True, 1, 1.0, "string") param = ParamTypes(1, [None, True, 1, 1.0, "string"], [1, 2, 3], {"key": "value"}) obj = JSONableTypes(base, param) assert obj.to_json() == { "base_types": { "null": None, "boolean": True, "integer": 1, "float_": 1.0, "string": "string", }, "param_types": { "literal": 1, "array_fixed": [None, True, 1, 1.0, "string"], "array": [1, 2, 3], "obj": {"key": "value"}, }, } assert ph.dumps(obj) == ( "{" r'"base_types": {"null": null, "boolean": true, "integer": 1, "float_": 1.0, "string": "string"}, ' r'"param_types": {"literal": 1, "array_fixed": [null, true, 1, 1.0, "string"], "array": [1, 2, 3], "obj": {"key": "value"}}' "}" ) assert obj == JSONableTypes.Decoder.loads(ph.dumps(obj)) assert obj == JSONableTypes.from_json(ph.dumps(obj)) assert obj == JSONableTypes.from_json(json.loads(ph.dumps(obj)))
def test_jsonable_object(): obj = JsonableDict({"one": 1, "two": 2, "three": 3}) assert obj.to_json() == {"one": 1, "two": 2, "three": 3} assert ph.dumps(obj) == r'{"one": 1, "two": 2, "three": 3}' assert obj == JsonableDict.Decoder.loads(ph.dumps(obj)) assert obj == JsonableDict.from_json(ph.dumps(obj)) assert obj == JsonableDict.from_json(json.loads(ph.dumps(obj)))
def test_default_jsonables(): obj = DefaultJSONableTypes() assert obj.to_json() == {} assert obj.to_json(with_defaults=True) == { "base_types_d": { "type_": "dbt", "null_d": None, "boolean_d": False, "integer_d": 0, "float_d": 0.0, "string_d": "", }, "param_types_d": { "type_": "dpt", "literal_d": 0, "array_fixed_d": [None, False, 0, 0.0, ""], "array_d": [], "obj_d": {}, }, } assert ph.dumps(obj) == r"{}" assert obj == DefaultJSONableTypes.Decoder.loads(ph.dumps(obj)) assert obj == DefaultJSONableTypes.from_json(ph.dumps(obj)) assert obj == DefaultJSONableTypes.from_json(json.loads(ph.dumps(obj))) base = DefaultBaseTypes(None, True, 1, 1.0, "string") param = DefaultParamTypes( 1, [None, True, 1, 1.0, "string"], [1, 2, 3], {"key": "value"} ) obj = DefaultJSONableTypes(base, param) print(ph.dumps(obj, indent=2)) assert obj.to_json(with_defaults=True) == { "base_types_d": { "type_": "dbt", "null_d": None, "boolean_d": True, "integer_d": 1, "float_d": 1.0, "string_d": "string", }, "param_types_d": { "type_": "dpt", "literal_d": 1, "array_fixed_d": [None, True, 1, 1.0, "string"], "array_d": [1, 2, 3], "obj_d": {"key": "value"}, }, } assert ph.dumps(obj) == ( "{" r'"base_types_d": {"type_": "dbt", "boolean_d": true, "integer_d": 1, "float_d": 1.0, "string_d": "string"}, ' r'"param_types_d": {"type_": "dpt", "literal_d": 1, "array_fixed_d": [null, true, 1, 1.0, "string"], "array_d": [1, 2, 3], "obj_d": {"key": "value"}}' "}" ) assert obj == DefaultJSONableTypes.Decoder.loads(ph.dumps(obj)) assert obj == DefaultJSONableTypes.from_json(ph.dumps(obj)) assert obj == DefaultJSONableTypes.from_json(json.loads(ph.dumps(obj)))
def test_param_types(): obj = ParamTypes(0, [None, True, 1, 1.0, "string"], [1, 2, 3], {"key": "value"}) assert obj.to_json() == { "literal": 0, "array_fixed": [None, True, 1, 1.0, "string"], "array": [1, 2, 3], "obj": {"key": "value"}, } assert ph.dumps(obj) == ( r'{"literal": 0, "array_fixed": [null, true, 1, 1.0, "string"], "array": [1, 2, 3], "obj": {"key": "value"}}' ) assert obj == ParamTypes.Decoder.loads(ph.dumps(obj)) assert obj == ParamTypes.from_json(ph.dumps(obj)) assert obj == ParamTypes.from_json(json.loads(ph.dumps(obj)))
def test_jsonable_value(): obj = JsonableInt(1) assert obj.to_json() == 1 assert ph.dumps(obj) == "1" assert obj == JsonableInt.Decoder.loads(ph.dumps(obj)) assert obj == JsonableInt.from_json(ph.dumps(obj)) assert obj == JsonableInt.from_json(json.loads(ph.dumps(obj))) with pytest.raises(ph.TypedJSONDecodeError): JsonableInt.from_json(None) with pytest.raises(ph.TypedJSONDecodeError): JsonableInt.from_json(1.0) with pytest.raises(ph.TypedJSONDecodeError): JsonableInt.from_json(r"null") with pytest.raises(ph.TypedJSONDecodeError): JsonableInt.from_json(r'"string"')
def test_base_types(): obj = BaseTypes(None, True, 1, 1.0, "string") assert obj.to_json() == { "null": None, "boolean": True, "integer": 1, "float_": 1.0, "string": "string", } assert ph.dumps(obj) == ( r'{"null": null, "boolean": true, "integer": 1, "float_": 1.0, "string": "string"}' ) assert obj == BaseTypes.Decoder.loads(ph.dumps(obj)) assert obj == BaseTypes.from_json(ph.dumps(obj)) assert obj == BaseTypes.from_json(json.loads(ph.dumps(obj)))
def test_jsonable_array(): obj = JsonableArrayFixed(1, 2, 3) assert obj.to_json() == [1, 2, 3] assert ph.dumps(obj) == r"[1, 2, 3]" assert obj == JsonableArrayFixed.Decoder.loads(ph.dumps(obj)) assert obj == JsonableArrayFixed.from_json(ph.dumps(obj)) assert obj == JsonableArrayFixed.from_json(json.loads(ph.dumps(obj))) with pytest.raises(ph.TypedJSONDecodeError): JsonableArrayFixed.from_json([1, 2]) with pytest.raises(ph.TypedJSONDecodeError): JsonableArrayFixed.from_json([1, 2, 3, 4]) with pytest.raises(ph.TypedJSONDecodeError): JsonableArrayFixed.from_json(r"[1, 2]") with pytest.raises(ph.TypedJSONDecodeError): JsonableArrayFixed.from_json(r"[1,2,3,4]") with pytest.raises(ph.TypedJSONDecodeError): JsonableArrayFixed.from_json(r'[1, 2, "string"]')
def test_default_bases(): obj = DefaultBaseTypes() assert obj.to_json() == {"type_": "dbt"} assert obj.to_json(with_defaults=True) == { "type_": "dbt", "null_d": None, "boolean_d": False, "integer_d": 0, "float_d": 0.0, "string_d": "", } assert ph.dumps(obj) == r'{"type_": "dbt"}' assert obj == DefaultBaseTypes.Decoder.loads(ph.dumps(obj)) assert obj == DefaultBaseTypes.from_json(ph.dumps(obj)) assert obj == DefaultBaseTypes.from_json(json.loads(ph.dumps(obj))) # assert isinstance(ph.loads(ph.dumps(obj)), DefaultBaseTypes) obj = DefaultBaseTypes(None, True, 1, 1.0, "string") assert obj.to_json() == { "type_": "dbt", "boolean_d": True, "integer_d": 1, "float_d": 1.0, "string_d": "string", } assert obj.to_json(with_defaults=True) == { "type_": "dbt", "null_d": None, "boolean_d": True, "integer_d": 1, "float_d": 1.0, "string_d": "string", } assert ph.dumps(obj) == ( r'{"type_": "dbt", "boolean_d": true, "integer_d": 1, "float_d": 1.0, "string_d": "string"}' ) assert obj == DefaultBaseTypes.Decoder.loads(ph.dumps(obj)) assert obj == DefaultBaseTypes.from_json(ph.dumps(obj)) assert obj == DefaultBaseTypes.from_json(json.loads(ph.dumps(obj)))
def test_default_param(): obj = DefaultParamTypes() assert obj.to_json() == {"type_": "dpt"} assert obj.to_json(with_defaults=True) == { "type_": "dpt", "literal_d": 0, "array_fixed_d": [None, False, 0, 0.0, ""], "array_d": [], "obj_d": {}, } assert ph.dumps(obj) == r'{"type_": "dpt"}' assert obj == DefaultParamTypes.Decoder.loads(ph.dumps(obj)) assert obj == DefaultParamTypes.from_json(ph.dumps(obj)) assert obj == DefaultParamTypes.from_json(json.loads(ph.dumps(obj))) # assert isinstance(ph.loads(ph.dumps(obj)), DefaultParamTypes) obj = DefaultParamTypes( 1, [None, True, 1, 1.0, "string"], [1, 2, 3], {"key": "value"} ) assert obj.to_json() == { "type_": "dpt", "literal_d": 1, "array_fixed_d": [None, True, 1, 1.0, "string"], "array_d": [1, 2, 3], "obj_d": {"key": "value"}, } assert obj.to_json(with_defaults=True) == { "type_": "dpt", "literal_d": 1, "array_fixed_d": [None, True, 1, 1.0, "string"], "array_d": [1, 2, 3], "obj_d": {"key": "value"}, } assert ph.dumps(obj) == ( r'{"type_": "dpt", "literal_d": 1, "array_fixed_d": [null, true, 1, 1.0, "string"], "array_d": [1, 2, 3], "obj_d": {"key": "value"}}' ) assert obj == DefaultParamTypes.Decoder.loads(ph.dumps(obj)) assert obj == DefaultParamTypes.from_json(ph.dumps(obj)) assert obj == DefaultParamTypes.from_json(json.loads(ph.dumps(obj)))