Skip to content

Commit 67921bf

Browse files
committed
refactored in common identifiers: Cache_Id, Graph_Id, Node_Id and Edge_Id
1 parent 73645d0 commit 67921bf

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from osbot_utils.type_safe.primitives.domains.identifiers.Random_Guid import Random_Guid
2+
3+
4+
class Cache_Id(Random_Guid): # helper class so that we don't use Random_Guid to represent the cache_id class
5+
def __new__(cls, value=None):
6+
if value is None or value == '':
7+
return str.__new__(cls, '')
8+
else:
9+
return super(Cache_Id, cls).__new__(cls, value)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from osbot_utils.type_safe.primitives.domains.identifiers.Obj_Id import Obj_Id
2+
3+
4+
class Edge_Id(Obj_Id): # helper class so that we don't use Obj_Id to represent the graph_id class
5+
def __new__(cls, value=None):
6+
if value is None or value == '':
7+
return str.__new__(cls, '')
8+
else:
9+
return super(Obj_Id, cls).__new__(cls, value)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from osbot_utils.type_safe.primitives.domains.identifiers.Obj_Id import Obj_Id
2+
3+
4+
class Graph_Id(Obj_Id): # helper class so that we don't use Obj_Id to represent the graph_id class
5+
def __new__(cls, value=None):
6+
if value is None or value == '':
7+
return str.__new__(cls, '')
8+
else:
9+
return super(Obj_Id, cls).__new__(cls, value)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from osbot_utils.type_safe.primitives.domains.identifiers.Obj_Id import Obj_Id
2+
3+
4+
class Node_Id(Obj_Id): # helper class so that we don't use Obj_Id to represent the graph_id class
5+
def __new__(cls, value=None):
6+
if value is None or value == '':
7+
return str.__new__(cls, '')
8+
else:
9+
return super(Obj_Id, cls).__new__(cls, value)
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
from unittest import TestCase
2+
from osbot_utils.type_safe.Type_Safe__Primitive import Type_Safe__Primitive
3+
from osbot_utils.type_safe.primitives.domains.identifiers.Graph_Id import Graph_Id
4+
from osbot_utils.type_safe.primitives.domains.identifiers.Obj_Id import Obj_Id
5+
from osbot_utils.utils.Objects import base_classes
6+
7+
8+
class test_Graph_Id(TestCase):
9+
10+
# ═══════════════════════════════════════════════════════════════════════════════
11+
# Initialization Tests
12+
# ═══════════════════════════════════════════════════════════════════════════════
13+
14+
def test__init__(self): # Test auto-initialization generates UUID
15+
graph_id = Graph_Id()
16+
17+
assert type(graph_id) is Graph_Id
18+
assert len(graph_id) == 0
19+
assert graph_id == '' # Empty when no value provided
20+
21+
def test__init__inheritance(self): # Test class inheritance
22+
assert base_classes(Graph_Id) == [Obj_Id, Type_Safe__Primitive, str, object, object]
23+
24+
def test__init__with_none(self): # Test with None value returns empty string
25+
graph_id = Graph_Id(None)
26+
27+
assert type(graph_id) is Graph_Id
28+
assert graph_id == ''
29+
assert len(graph_id) == 0
30+
31+
def test__init__with_empty_string(self): # Test with empty string returns empty string
32+
graph_id = Graph_Id('')
33+
34+
assert type(graph_id) is Graph_Id
35+
assert graph_id == ''
36+
assert len(graph_id) == 0
37+
38+
def test__init__with_value(self): # Test with existing UUID value
39+
existing_id = '12345678-1234-1234-1234-123456789abc'
40+
graph_id = Graph_Id(existing_id)
41+
42+
assert type(graph_id) is Graph_Id
43+
assert graph_id == existing_id
44+
assert len(graph_id) == 36
45+
46+
# ═══════════════════════════════════════════════════════════════════════════════
47+
# Type Safety Tests
48+
# ═══════════════════════════════════════════════════════════════════════════════
49+
50+
def test__is_string_subclass(self): # Test that Graph_Id is a string
51+
graph_id = Graph_Id()
52+
53+
assert isinstance(graph_id, str)
54+
assert isinstance(graph_id, Obj_Id)
55+
assert isinstance(graph_id, Graph_Id)
56+
57+
def test__can_be_used_as_string(self): # Test string operations work
58+
graph_id = Graph_Id()
59+
60+
assert graph_id.upper() == graph_id.upper() # String methods work
61+
assert graph_id.lower() == graph_id.lower()
62+
assert str(graph_id) == graph_id # str() conversion
63+
64+
def test__empty_is_falsy(self): # Test empty Graph_Id is falsy
65+
empty_id = Graph_Id('')
66+
67+
assert not empty_id # Falsy
68+
assert bool(empty_id) is False
69+
70+
def test__non_empty_is_truthy(self): # Test non-empty Graph_Id is truthy
71+
graph_id = Graph_Id(Obj_Id())
72+
73+
assert graph_id # Truthy
74+
assert bool(graph_id) is True
75+
76+
# ═══════════════════════════════════════════════════════════════════════════════
77+
# Comparison Tests
78+
# ═══════════════════════════════════════════════════════════════════════════════
79+
80+
def test__equality__same_value(self): # Test equality with same value
81+
value = '12345678-1234-1234-1234-123456789abc'
82+
graph_id1 = Graph_Id(value)
83+
graph_id2 = Graph_Id(value)
84+
85+
assert graph_id1 == graph_id2
86+
assert graph_id1 == value # Compare with string
87+
88+
def test__equality__empty_values(self): # Test equality of empty values
89+
empty1 = Graph_Id('')
90+
empty2 = Graph_Id(None)
91+
92+
assert empty1 == empty2
93+
assert empty1 == ''
94+
95+
def test__inequality__different_values(self): # Test inequality
96+
graph_id1 = Graph_Id(Obj_Id())
97+
graph_id2 = Graph_Id(Obj_Id())
98+
99+
assert graph_id1 != graph_id2 # Different UUIDs
100+
101+
102+
# ═══════════════════════════════════════════════════════════════════════════════
103+
# Edge Cases
104+
# ═══════════════════════════════════════════════════════════════════════════════
105+
106+
def test__whitespace_handling(self): # Test whitespace in value
107+
# Obj_Id should handle whitespace
108+
value = ' 12345678-1234-1234-1234-123456789abc '
109+
graph_id = Graph_Id(value.strip())
110+
111+
assert len(graph_id) == 36
112+
113+
def test__multiple_empty_instances(self): # Test multiple empty instances are equal
114+
empties = [Graph_Id(''), Graph_Id(None), Graph_Id('')]
115+
116+
for empty in empties:
117+
assert empty == ''
118+
assert type(empty) is Graph_Id
119+
120+
def test__use_in_dict_key(self): # Test Graph_Id can be used as dict key
121+
graph_id = Graph_Id()
122+
data = {graph_id: 'test_value'}
123+
124+
assert data[graph_id] == 'test_value'
125+
assert graph_id in data
126+
127+
def test__use_in_set(self): # Test Graph_Id can be used in set
128+
graph_id1 = Graph_Id(Obj_Id())
129+
graph_id2 = Graph_Id(Obj_Id())
130+
id_set = {graph_id1, graph_id2}
131+
132+
assert len(id_set) == 2 # Two different IDs
133+
assert graph_id1 in id_set
134+
assert graph_id2 in id_set
135+
136+
def test__from_obj_id(self): # Test conversion from Obj_Id
137+
obj_id = Obj_Id()
138+
graph_id = Graph_Id(obj_id)
139+
140+
assert graph_id != obj_id # different types # todo: see if this should work (would required change to OSBOt-Utils or direct overwrite of __eq__ method in Graph_Id)
141+
assert type(graph_id) is Graph_Id

0 commit comments

Comments
 (0)