11import re
22import sys
3+ import json
34import pytest
45from enum import Enum
56from decimal import Decimal
67from typing import Optional , Union , List , Dict , get_origin , Type , ForwardRef , Any , Set , Tuple
78from unittest import TestCase
89from unittest .mock import patch
10+ from osbot_utils .type_safe .primitives .domains .identifiers .Safe_Id import Safe_Id
11+ from osbot_utils .type_safe .primitives .domains .identifiers .Edge_Id import Edge_Id
912from osbot_utils .type_safe .primitives .core .Safe_Float import Safe_Float
1013from osbot_utils .type_safe .primitives .domains .identifiers .safe_str .Safe_Str__Id import Safe_Str__Id
1114from osbot_utils .type_safe .primitives .domains .identifiers .Obj_Id import Obj_Id
@@ -2022,4 +2025,34 @@ class An_Class(Type_Safe):
20222025 # assert With_Base ().obj () == __(an_str=None) # BUG
20232026
20242027 assert With_Base ().json () == {'an_str' : '' } # FIXED
2025- assert With_Base ().obj () == __ (an_str = '' ) # FIXED
2028+ assert With_Base ().obj () == __ (an_str = '' ) # FIXED
2029+
2030+ def test__regression__set__json__serialisation_issue (self ):
2031+
2032+ class An_Class (Type_Safe ):
2033+ an_dict : Dict [Safe_Id , Set [Edge_Id ]]
2034+ safe_id = Safe_Id ('safe-id_jlqsh' )
2035+ edge_id = Edge_Id ('6106b8e7' )
2036+ an_class = An_Class ()
2037+ an_class .an_dict [safe_id ] = {edge_id }
2038+
2039+ #assert an_class.obj () == __(an_dict=__(safe_id_jlqsh={'6106b8e7'})) # BUG, this should be list, right? i.e. ['6106b8e7']
2040+ #assert an_class.json() == {'an_dict': {'safe-id_jlqsh': {'6106b8e7'}}} # BUG, this should be list, right? i.e. ['6106b8e7']
2041+
2042+ assert an_class .obj () == __ (an_dict = __ (safe_id_jlqsh = ['6106b8e7' ])) # FIXED
2043+ assert an_class .json () == {'an_dict' : {'safe-id_jlqsh' : ['6106b8e7' ]}} # FIXED
2044+
2045+ # error_message = "Object of type set is not JSON serializable"
2046+ # with pytest.raises(TypeError, match=error_message): # BUG
2047+ # json.dumps(an_class.json())
2048+ assert json .dumps (an_class .json ()) == '{"an_dict": {"safe-id_jlqsh": ["6106b8e7"]}}'
2049+
2050+ assert type (an_class .json ().get ('an_dict' ) ) is dict
2051+ #assert type(an_class.json().get('an_dict').get('safe-id_jlqsh')) is set # BUG
2052+ assert type (an_class .json ().get ('an_dict' ).get ('safe-id_jlqsh' )) is list # FIXED
2053+ assert json .loads (json .dumps (an_class .json ())) == an_class .json ()
2054+ error_message = "Type Set cannot be instantiated; use set() instead"
2055+ # with pytest.raises(TypeError, match=re.escape(error_message)):
2056+ # An_Class.from_json(an_class.json()) # BUG
2057+ assert An_Class .from_json (an_class .json ()).obj () == an_class .obj () # FIXED
2058+ assert An_Class .from_json (an_class .json ()).json () == an_class .json () # FIXED
0 commit comments