Skip to content

Commit 49cff45

Browse files
committed
added tests for Type_Safe__Fast_Create
1 parent 08f79cf commit 49cff45

File tree

5 files changed

+944
-87
lines changed

5 files changed

+944
-87
lines changed

osbot_utils/type_safe/type_safe_core/fast_create/Type_Safe__Fast_Create.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#
1818
# ═══════════════════════════════════════════════════════════════════════════════
1919

20-
from typing import Any
21-
from Type_Safe__Fast_Create__Cache import type_safe_fast_create_cache
20+
from typing import Any
21+
from osbot_utils.type_safe.type_safe_core.fast_create.Type_Safe__Fast_Create__Cache import type_safe_fast_create_cache
2222

2323

2424
# ═══════════════════════════════════════════════════════════════════════════════

osbot_utils/type_safe/type_safe_core/fast_create/Type_Safe__Fast_Create__Cache.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,19 @@
3232

3333
class Type_Safe__Fast_Create__Cache: # Schema generation and caching
3434

35-
schema_cache : Dict[Type, Schema__Type_Safe__Fast_Create__Class] # Class -> Schema mapping
35+
schema_cache : Dict[Type, Schema__Type_Safe__Fast_Create__Class] # Class -> Schema mapping
3636
generating : Set[Type] # Guards against recursion
3737

3838
def __init__(self):
3939
self.schema_cache = {} # Regular dict - classes persist
4040
self.generating = set() # Recursion guard
4141

42+
def __enter__(self):
43+
return self
44+
45+
def __exit__(self, exc_type, exc_val, exc_tb):
46+
pass
47+
4248
# ═══════════════════════════════════════════════════════════════════════════
4349
# Public API
4450
# ═══════════════════════════════════════════════════════════════════════════
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# ═══════════════════════════════════════════════════════════════════════════════
2-
# Tests: Type_Safe__Fast_Create__Cache - Schema Generation and Caching
3-
# Verify schema generation, field classification, and cache behavior
2+
# Tests: Type_Safe__Hypothesis_G - Functional Tests for Fast Object Creation
3+
# Verify fast_create produces correct, fully functional objects
44
# ═══════════════════════════════════════════════════════════════════════════════
55

66
from typing import Dict, List
77
from unittest import TestCase
8+
from Type_Safe__Config import Type_Safe__Config
89
from Type_Safe__Hypothesis_G import Type_Safe__Hypothesis_G
910
from Type_Safe__Fast_Create__Cache import type_safe_fast_create_cache
10-
from schemas.Field__Schema import FIELD_MODE__STATIC
11-
from schemas.Field__Schema import FIELD_MODE__NESTED
1211

1312

1413
# ═══════════════════════════════════════════════════════════════════════════════
@@ -52,135 +51,178 @@ class TS__Deep(Type_Safe__Hypothesis_G):
5251

5352

5453
# ═══════════════════════════════════════════════════════════════════════════════
55-
# Immutability Tests
54+
# Normal Mode Tests (fast_create=False)
5655
# ═══════════════════════════════════════════════════════════════════════════════
5756

58-
class test_is_immutable(TestCase):
57+
class test_normal_mode(TestCase):
5958

60-
def test__immutable_types(self):
61-
cache = type_safe_fast_create_cache
59+
def setUp(self):
60+
type_safe_fast_create_cache.clear_cache()
61+
62+
def test__creates_object_with_defaults(self):
63+
obj = TS__Simple()
64+
65+
assert type(obj) is TS__Simple
66+
assert obj.name == ''
67+
assert obj.count == 0
68+
assert obj.active == False
69+
70+
def test__creates_object_with_kwargs(self):
71+
obj = TS__Simple(name='test', count=42)
6272

63-
assert cache.is_immutable('') is True
64-
assert cache.is_immutable('hello') is True
65-
assert cache.is_immutable(0) is True
66-
assert cache.is_immutable(42) is True
67-
assert cache.is_immutable(3.14) is True
68-
assert cache.is_immutable(True) is True
69-
assert cache.is_immutable(False) is True
70-
assert cache.is_immutable(None) is True
71-
assert cache.is_immutable(b'bytes') is True
72-
assert cache.is_immutable((1, 2, 3)) is True
73+
assert obj.name == 'test'
74+
assert obj.count == 42
75+
assert obj.active == False
7376

74-
def test__mutable_types(self):
75-
cache = type_safe_fast_create_cache
77+
def test__creates_nested_objects(self):
78+
obj = TS__With_Nested()
7679

77-
assert cache.is_immutable([]) is False
78-
assert cache.is_immutable({}) is False
79-
assert cache.is_immutable(set()) is False
80-
assert cache.is_immutable([1, 2, 3]) is False
80+
assert obj.inner is not None
81+
assert type(obj.inner) is TS__Inner
82+
assert obj.inner.value == ''
83+
assert obj.inner.count == 0
8184

8285

8386
# ═══════════════════════════════════════════════════════════════════════════════
84-
# Schema Generation Tests
87+
# Fast Create Mode Tests (fast_create=True)
8588
# ═══════════════════════════════════════════════════════════════════════════════
8689

87-
class test_generate_schema(TestCase):
90+
class test_fast_create_mode(TestCase):
8891

8992
def setUp(self):
9093
type_safe_fast_create_cache.clear_cache()
9194

92-
def test__simple_class__all_static(self):
93-
schema = type_safe_fast_create_cache.generate_schema(TS__Simple)
95+
def test__creates_object_with_defaults(self):
96+
with Type_Safe__Config(fast_create=True):
97+
obj = TS__Simple()
98+
99+
assert type(obj) is TS__Simple
100+
assert obj.name == ''
101+
assert obj.count == 0
102+
assert obj.active == False
103+
104+
def test__creates_object_with_kwargs(self):
105+
with Type_Safe__Config(fast_create=True):
106+
obj = TS__Simple(name='fast', count=99)
107+
108+
assert obj.name == 'fast'
109+
assert obj.count == 99
110+
assert obj.active == False
111+
112+
def test__collections_not_shared(self):
113+
with Type_Safe__Config(fast_create=True):
114+
obj1 = TS__With_Collections()
115+
obj2 = TS__With_Collections()
94116

95-
assert schema.target_class is TS__Simple
96-
assert len(schema.fields) == 3
117+
obj1.items.append('item1')
97118

98-
for field in schema.fields:
99-
assert field.mode == FIELD_MODE__STATIC
119+
assert len(obj1.items) == 1
120+
assert len(obj2.items) == 0 # Not shared!
100121

101-
assert schema.static_dict['name'] == ''
102-
assert schema.static_dict['count'] == 0
103-
assert schema.static_dict['active'] == False
122+
def test__nested_objects_created(self):
123+
with Type_Safe__Config(fast_create=True):
124+
obj = TS__With_Nested()
104125

105-
def test__class_with_collections__factory_fields(self):
106-
schema = type_safe_fast_create_cache.generate_schema(TS__With_Collections)
126+
assert obj.inner is not None
127+
assert type(obj.inner) is TS__Inner
128+
assert obj.inner.value == ''
129+
assert obj.inner.count == 0
107130

108-
assert 'name' in schema.static_dict # Static field
131+
def test__nested_objects_not_shared(self):
132+
with Type_Safe__Config(fast_create=True):
133+
obj1 = TS__With_Nested()
134+
obj2 = TS__With_Nested()
109135

110-
assert len(schema.factory_fields) == 2 # items and data
111-
factory_names = {f.name for f in schema.factory_fields}
112-
assert factory_names == {'items', 'data'}
136+
obj1.inner.value = 'modified'
113137

114-
def test__class_with_nested__nested_fields(self):
115-
schema = type_safe_fast_create_cache.generate_schema(TS__With_Nested)
138+
assert obj1.inner.value == 'modified'
139+
assert obj2.inner.value == '' # Not shared!
116140

117-
assert len(schema.nested_fields) == 1
118-
assert schema.nested_fields[0].name == 'inner'
119-
assert schema.nested_fields[0].mode == FIELD_MODE__NESTED
120-
assert schema.nested_fields[0].nested_class is TS__Inner
141+
def test__deep_nested_created(self):
142+
with Type_Safe__Config(fast_create=True):
143+
obj = TS__Deep()
121144

122-
def test__deep_nested_class(self):
123-
schema = type_safe_fast_create_cache.generate_schema(TS__Deep)
145+
assert obj.level1 is not None
146+
assert obj.level1.level2 is not None
147+
assert obj.level1.level2.data == ''
124148

125-
assert len(schema.nested_fields) == 1
126-
assert schema.nested_fields[0].name == 'level1'
127-
assert schema.nested_fields[0].nested_class is TS__Deep_Level1
149+
def test__json_works(self):
150+
with Type_Safe__Config(fast_create=True):
151+
obj = TS__Simple(name='json_test', count=42)
152+
153+
json_data = obj.json()
154+
155+
assert json_data['name'] == 'json_test'
156+
assert json_data['count'] == 42
157+
assert json_data['active'] == False
158+
159+
def test__isinstance_works(self):
160+
with Type_Safe__Config(fast_create=True):
161+
obj = TS__Simple()
162+
163+
assert isinstance(obj, TS__Simple)
164+
assert isinstance(obj, Type_Safe__Hypothesis_G)
128165

129166

130167
# ═══════════════════════════════════════════════════════════════════════════════
131-
# Cache Behavior Tests
168+
# Skip Validation Tests
132169
# ═══════════════════════════════════════════════════════════════════════════════
133170

134-
class test_schema_cache(TestCase):
171+
class test_skip_validation(TestCase):
135172

136173
def setUp(self):
137174
type_safe_fast_create_cache.clear_cache()
138175

139-
def test__schema_is_cached(self):
140-
schema1 = type_safe_fast_create_cache.get_schema(TS__Simple)
141-
schema2 = type_safe_fast_create_cache.get_schema(TS__Simple)
176+
def test__setattr_works_with_skip_validation(self):
177+
with Type_Safe__Config(fast_create=True, skip_validation=True):
178+
obj = TS__Simple()
179+
obj.name = 'updated'
142180

143-
assert schema1 is schema2 # Same instance
181+
assert obj.name == 'updated'
144182

145-
def test__warm_cache__recursive(self):
146-
type_safe_fast_create_cache.clear_cache()
147-
type_safe_fast_create_cache.warm_cache(TS__Deep)
183+
def test__setattr_validates_without_skip_validation(self):
184+
with Type_Safe__Config(fast_create=True, skip_validation=False):
185+
obj = TS__Simple()
186+
obj.name = 'validated' # Should still work
148187

149-
assert TS__Deep in type_safe_fast_create_cache.schema_cache
150-
assert TS__Deep_Level1 in type_safe_fast_create_cache.schema_cache
151-
assert TS__Deep_Level2 in type_safe_fast_create_cache.schema_cache
152-
153-
def test__clear_cache(self):
154-
type_safe_fast_create_cache.get_schema(TS__Simple)
155-
assert len(type_safe_fast_create_cache.schema_cache) > 0
156-
157-
type_safe_fast_create_cache.clear_cache()
158-
assert len(type_safe_fast_create_cache.schema_cache) == 0
188+
assert obj.name == 'validated'
159189

160190

161191
# ═══════════════════════════════════════════════════════════════════════════════
162-
# Factory Function Tests
192+
# Config Context Tests
163193
# ═══════════════════════════════════════════════════════════════════════════════
164194

165-
class test_factory_functions(TestCase):
195+
class test_config_context(TestCase):
166196

167197
def setUp(self):
168198
type_safe_fast_create_cache.clear_cache()
169199

170-
def test__factory_creates_fresh_instances(self):
171-
schema = type_safe_fast_create_cache.get_schema(TS__With_Collections)
200+
def test__config_only_active_in_context(self):
201+
# Outside context - normal mode
202+
obj1 = TS__Simple()
203+
204+
with Type_Safe__Config(fast_create=True):
205+
# Inside context - fast mode
206+
obj2 = TS__Simple()
172207

173-
items_field = next(f for f in schema.factory_fields if f.name == 'items')
208+
# Outside context again - normal mode
209+
obj3 = TS__Simple()
174210

175-
instance1 = items_field.factory_func()
176-
instance2 = items_field.factory_func()
211+
assert type(obj1) is TS__Simple
212+
assert type(obj2) is TS__Simple
213+
assert type(obj3) is TS__Simple
177214

178-
assert instance1 is not instance2 # Different instances
215+
def test__nested_contexts(self):
216+
with Type_Safe__Config(fast_create=True):
217+
obj1 = TS__Simple()
179218

180-
def test__type_safe_list_preserves_expected_type(self):
181-
schema = type_safe_fast_create_cache.get_schema(TS__With_Collections)
219+
with Type_Safe__Config(fast_create=True, skip_validation=True):
220+
obj2 = TS__Simple()
221+
obj2.name = 'nested'
182222

183-
items_field = next(f for f in schema.factory_fields if f.name == 'items')
184-
items = items_field.factory_func()
223+
obj3 = TS__Simple()
185224

186-
assert items.expected_type is str
225+
assert type(obj1) is TS__Simple
226+
assert type(obj2) is TS__Simple
227+
assert type(obj3) is TS__Simple
228+
assert obj2.name == 'nested'

0 commit comments

Comments
 (0)