|
1 | 1 | import pytest
|
2 | 2 | from unittest import TestCase
|
3 |
| -from typing import Dict, Type, Set |
| 3 | +from typing import Dict, Type, Set, Any |
| 4 | +from osbot_utils.type_safe.Type_Safe__List import Type_Safe__List |
| 5 | +from osbot_utils.utils.Objects import base_classes |
| 6 | + |
| 7 | +from osbot_utils.helpers.Safe_Id import Safe_Id |
| 8 | + |
4 | 9 | from osbot_utils.type_safe.Type_Safe import Type_Safe
|
5 | 10 | from osbot_utils.type_safe.Type_Safe__Dict import Type_Safe__Dict
|
6 | 11 |
|
7 | 12 |
|
8 | 13 | class test_Type_Safe__Dict__regression(TestCase):
|
9 | 14 |
|
| 15 | + def test__regression__str_to_safe_id__conversion_not_supports(self): |
| 16 | + class An_Class(Type_Safe): |
| 17 | + an_dict: Dict[Safe_Id, Safe_Id] |
| 18 | + an_class = An_Class() |
| 19 | + an_class.an_dict[Safe_Id("aaa")] = Safe_Id("bbbb") # strongly typed |
| 20 | + assert isinstance(Safe_Id, str) is False # confirm Safe_Id is not a direct string |
| 21 | + assert issubclass(Safe_Id, str) is True # but has str as a base class |
| 22 | + assert base_classes(Safe_Id) == [str, object] # also confirmed by the base class list |
| 23 | + #with pytest.raises(TypeError, match="Expected 'Safe_Id', but got 'str'") : |
| 24 | + # an_class.an_dict[Safe_Id("aaa")] = "bbbb" # FIXED: BUG: this should be supported since we can convert the str into Safe_Id |
| 25 | + #with pytest.raises(TypeError, match="Expected 'Safe_Id', but got 'str'") : |
| 26 | + # an_class.an_dict["ccc"] = Safe_Id("dddd") # FIXED: BUG: this should be supported since we can convert the str into Safe_Id |
| 27 | + |
| 28 | + # confirm direct assigment now works |
| 29 | + an_class.an_dict[Safe_Id("aaa")] = "bbbb" |
| 30 | + an_class.an_dict["ccc" ] = Safe_Id("dddd") |
| 31 | + assert type(an_class.an_dict["aaa"]) is Safe_Id |
| 32 | + assert type(an_class.an_dict["ccc"]) is Safe_Id |
| 33 | + for key, value in an_class.an_dict.items(): |
| 34 | + assert type(key) is Safe_Id |
| 35 | + assert type(value) is Safe_Id |
| 36 | + |
| 37 | + # confirm kwargs assigment now works |
| 38 | + kwargs = dict(an_dict={ "an_key_1" :"an_value_1" , |
| 39 | + Safe_Id("an_key_2"): "an_value_2" , |
| 40 | + "an_key_3" : Safe_Id("an_value_3" )}) |
| 41 | + an_class_2 = An_Class(**kwargs) |
| 42 | + |
| 43 | + with an_class_2.an_dict as _: |
| 44 | + for key, value in _.items(): |
| 45 | + assert type(key) is Safe_Id |
| 46 | + assert type(value) is Safe_Id |
| 47 | + |
| 48 | + with an_class_2.an_dict.keys() as _: |
| 49 | + assert type(_) is Type_Safe__List |
| 50 | + assert _.expected_type is Safe_Id |
| 51 | + assert _ == ['an_key_1', 'an_key_2', 'an_key_3'] |
| 52 | + assert _ == [Safe_Id('an_key_1'), 'an_key_2', 'an_key_3'] |
| 53 | + assert _ == [Safe_Id('an_key_1'), Safe_Id('an_key_2'), Safe_Id('an_key_3')] |
| 54 | + |
| 55 | + with an_class_2.an_dict.values() as _: |
| 56 | + assert type(_) is Type_Safe__List |
| 57 | + assert _.expected_type is Safe_Id |
| 58 | + assert _ == [ 'an_value_1', 'an_value_2', 'an_value_3'] |
| 59 | + assert _ == [Safe_Id('an_value_1'), 'an_value_2', 'an_value_3'] |
| 60 | + assert _ == [Safe_Id('an_value_1'), Safe_Id('an_value_2'), Safe_Id('an_value_3')] |
| 61 | + |
| 62 | + def test__bug__doesnt_support__nested__json__with_mixed_content(self): |
| 63 | + class TestTypeSafe(Type_Safe): |
| 64 | + value: str |
| 65 | + |
| 66 | + safe_dict = Type_Safe__Dict(str, Any) |
| 67 | + safe_dict["number"] = 42 |
| 68 | + safe_dict["string"] = "text" |
| 69 | + safe_dict["type_safe"] = TestTypeSafe(value="safe") |
| 70 | + safe_dict["list"] = [1, TestTypeSafe(value="in_list"), {"nested": TestTypeSafe(value="in_dict")}] |
| 71 | + safe_dict["dict"] = { |
| 72 | + "normal": "value", |
| 73 | + "safe_obj": TestTypeSafe(value="in_nested_dict") |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + expected = { |
| 78 | + "number": 42, |
| 79 | + "string": "text", |
| 80 | + "type_safe": {"value": "safe"}, |
| 81 | + "list": [1, {"value": "in_list"}, {"nested": {"value": "in_dict"}}], |
| 82 | + "dict": { |
| 83 | + "normal": "value", |
| 84 | + "safe_obj": {"value": "in_nested_dict"} |
| 85 | + } |
| 86 | + } |
| 87 | + assert safe_dict.json() != expected # BUG should be equal |
| 88 | + assert safe_dict.json()['list'][2]['nested'] != {"value": "in_dict"} |
| 89 | + assert safe_dict.json()['list'][2]['nested'].value == 'in_dict' |
| 90 | + assert type(safe_dict.json()['list'][2]['nested']) is TestTypeSafe |
| 91 | + |
| 92 | + |
10 | 93 | def test__regression__type_keys_in_json(self):
|
11 | 94 |
|
12 | 95 | class Bug_Type_Keys: # Simple class for testing
|
|
0 commit comments