Codebase rewritten to:
tangled.org/atpasser.poxiao-labs.work/atpasser
1"""Test cases for the RKey and TID classes in atpasser.uri.rkey module."""
2
3import pytest
4import datetime
5from atpasser.uri.rkey import RKey, TID, importTIDfromInteger, importTIDfromBase32
6from atpasser.uri.exceptions import InvalidRKeyError
7
8
9class TestRKey:
10 """Test cases for the RKey class."""
11
12 def test_valid_rkey_simple(self):
13 """Test creating an RKey with a valid simple format."""
14 rkey_str = "3jui7kd54zh2y"
15 rkey = RKey(rkey_str)
16
17 assert str(rkey) == rkey_str
18 assert rkey.recordKey == rkey_str
19
20 def test_valid_rkey_with_various_characters(self):
21 """Test creating an RKey with various valid characters."""
22 rkey_str = "example.com"
23 rkey = RKey(rkey_str)
24
25 assert str(rkey) == rkey_str
26 assert rkey.recordKey == rkey_str
27
28 def test_valid_rkey_with_special_characters(self):
29 """Test creating an RKey with valid special characters."""
30 rkey_str = "~1.2-3_"
31 rkey = RKey(rkey_str)
32
33 assert str(rkey) == rkey_str
34 assert rkey.recordKey == rkey_str
35
36 def test_valid_rkey_with_colon(self):
37 """Test creating an RKey with a colon."""
38 rkey_str = "pre:fix"
39 rkey = RKey(rkey_str)
40
41 assert str(rkey) == rkey_str
42 assert rkey.recordKey == rkey_str
43
44 def test_valid_rkey_underscore(self):
45 """Test creating an RKey with just an underscore."""
46 rkey_str = "_"
47 rkey = RKey(rkey_str)
48
49 assert str(rkey) == rkey_str
50 assert rkey.recordKey == rkey_str
51
52 def test_invalid_rkey_empty(self):
53 """Test that an empty RKey raises InvalidRKeyError."""
54 rkey_str = ""
55
56 with pytest.raises(InvalidRKeyError, match="record key is empty"):
57 RKey(rkey_str)
58
59 def test_invalid_rkey_too_long(self):
60 """Test that an RKey that is too long raises InvalidRKeyError."""
61 # Create an RKey that exceeds the 512 character limit
62 rkey_str = "a" * 513
63
64 with pytest.raises(InvalidRKeyError, match="exceeds maximum length"):
65 RKey(rkey_str)
66
67 def test_invalid_rkey_reserved_double_dot(self):
68 """Test that an RKey with '..' raises InvalidRKeyError."""
69 rkey_str = ".."
70
71 with pytest.raises(InvalidRKeyError, match="reserved value"):
72 RKey(rkey_str)
73
74 def test_invalid_rkey_reserved_single_dot(self):
75 """Test that an RKey with '.' raises InvalidRKeyError."""
76 rkey_str = "."
77
78 with pytest.raises(InvalidRKeyError, match="reserved value"):
79 RKey(rkey_str)
80
81 def test_invalid_rkey_invalid_characters(self):
82 """Test that an RKey with invalid characters raises InvalidRKeyError."""
83 rkey_str = "alpha/beta"
84
85 with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
86 RKey(rkey_str)
87
88 def test_invalid_rkey_hash_character(self):
89 """Test that an RKey with a hash character raises InvalidRKeyError."""
90 rkey_str = "#extra"
91
92 with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
93 RKey(rkey_str)
94
95 def test_invalid_rkey_at_character(self):
96 """Test that an RKey with an at character raises InvalidRKeyError."""
97 rkey_str = "@handle"
98
99 with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
100 RKey(rkey_str)
101
102 def test_invalid_rkey_space(self):
103 """Test that an RKey with a space raises InvalidRKeyError."""
104 rkey_str = "any space"
105
106 with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
107 RKey(rkey_str)
108
109 def test_invalid_rkey_plus_character(self):
110 """Test that an RKey with a plus character raises InvalidRKeyError."""
111 rkey_str = "any+space"
112
113 with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
114 RKey(rkey_str)
115
116 def test_invalid_rkey_brackets(self):
117 """Test that an RKey with brackets raises InvalidRKeyError."""
118 rkey_str = "number[3]"
119
120 with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
121 RKey(rkey_str)
122
123 def test_invalid_rkey_parentheses(self):
124 """Test that an RKey with parentheses raises InvalidRKeyError."""
125 rkey_str = "number(3)"
126
127 with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
128 RKey(rkey_str)
129
130 def test_invalid_rkey_quotes(self):
131 """Test that an RKey with quotes raises InvalidRKeyError."""
132 rkey_str = '"quote"'
133
134 with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
135 RKey(rkey_str)
136
137 def test_invalid_rkey_base64_padding(self):
138 """Test that an RKey with base64 padding raises InvalidRKeyError."""
139 rkey_str = "dHJ1ZQ=="
140
141 with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
142 RKey(rkey_str)
143
144 def test_rkey_equality(self):
145 """Test RKey equality comparison."""
146 rkey_str = "3jui7kd54zh2y"
147 rkey1 = RKey(rkey_str)
148 rkey2 = RKey(rkey_str)
149
150 assert rkey1 == rkey2
151 assert rkey1 != "not an rkey object"
152
153 def test_rkey_string_representation(self):
154 """Test RKey string representation."""
155 rkey_str = "3jui7kd54zh2y"
156 rkey = RKey(rkey_str)
157
158 assert str(rkey) == rkey_str
159
160
161class TestTID:
162 """Test cases for the TID class."""
163
164 def test_tid_creation_default(self):
165 """Test creating a TID with default parameters."""
166 tid = TID()
167
168 assert isinstance(tid, TID)
169 assert isinstance(tid, RKey)
170 assert isinstance(tid.timestamp, datetime.datetime)
171 assert isinstance(tid.clockIdentifier, int)
172 assert 0 <= tid.clockIdentifier < 1024
173 assert len(str(tid)) == 13 # TID string is always 13 characters
174
175 def test_tid_creation_with_timestamp(self):
176 """Test creating a TID with a specific timestamp."""
177 timestamp = datetime.datetime(2023, 1, 1, 12, 0, 0)
178 tid = TID(time=timestamp)
179
180 assert tid.timestamp == timestamp
181 assert isinstance(tid.clockIdentifier, int)
182 assert 0 <= tid.clockIdentifier < 1024
183
184 def test_tid_creation_with_clock_identifier(self):
185 """Test creating a TID with a specific clock identifier."""
186 clock_id = 42
187 tid = TID(clockIdentifier=clock_id)
188
189 assert tid.clockIdentifier == clock_id
190 assert isinstance(tid.timestamp, datetime.datetime)
191
192 def test_tid_creation_with_both_parameters(self):
193 """Test creating a TID with both timestamp and clock identifier."""
194 timestamp = datetime.datetime(2023, 1, 1, 12, 0, 0)
195 clock_id = 42
196 tid = TID(time=timestamp, clockIdentifier=clock_id)
197
198 assert tid.timestamp == timestamp
199 assert tid.clockIdentifier == clock_id
200
201 def test_tid_integer_representation(self):
202 """Test TID integer representation."""
203 timestamp = datetime.datetime(2023, 1, 1, 12, 0, 0)
204 clock_id = 42
205 tid = TID(time=timestamp, clockIdentifier=clock_id)
206
207 int_value = int(tid)
208 expected_value = int(timestamp.timestamp() * 1000000) * 1024 + clock_id
209
210 assert int_value == expected_value
211
212 def test_tid_string_representation(self):
213 """Test TID string representation."""
214 tid = TID()
215
216 str_value = str(tid)
217 assert len(str_value) == 13
218 assert all(c in "234567abcdefghijklmnopqrstuvwxyz" for c in str_value)
219
220 def test_tid_equality_with_tid(self):
221 """Test TID equality comparison with another TID."""
222 timestamp = datetime.datetime(2023, 1, 1, 12, 0, 0)
223 clock_id = 42
224 tid1 = TID(time=timestamp, clockIdentifier=clock_id)
225 tid2 = TID(time=timestamp, clockIdentifier=clock_id)
226
227 assert tid1 == tid2
228
229 def test_tid_equality_with_rkey(self):
230 """Test TID equality comparison with an RKey."""
231 timestamp = datetime.datetime(2023, 1, 1, 12, 0, 0)
232 clock_id = 42
233 tid = TID(time=timestamp, clockIdentifier=clock_id)
234 rkey = RKey(str(tid))
235
236 assert tid == rkey
237
238 def test_tid_inequality_with_different_object(self):
239 """Test TID inequality comparison with a different object type."""
240 tid = TID()
241
242 assert tid != "not a tid object"
243
244 def test_tid_inequality_with_different_timestamp(self):
245 """Test TID inequality comparison with different timestamp."""
246 timestamp1 = datetime.datetime(2023, 1, 1, 12, 0, 0)
247 timestamp2 = datetime.datetime(2023, 1, 1, 12, 0, 1)
248 clock_id = 42
249 tid1 = TID(time=timestamp1, clockIdentifier=clock_id)
250 tid2 = TID(time=timestamp2, clockIdentifier=clock_id)
251
252 assert tid1 != tid2
253
254 def test_tid_inequality_with_different_clock_id(self):
255 """Test TID inequality comparison with different clock identifier."""
256 timestamp = datetime.datetime(2023, 1, 1, 12, 0, 0)
257 clock_id1 = 42
258 clock_id2 = 43
259 tid1 = TID(time=timestamp, clockIdentifier=clock_id1)
260 tid2 = TID(time=timestamp, clockIdentifier=clock_id2)
261
262 assert tid1 != tid2
263
264
265class TestTIDImportFunctions:
266 """Test cases for TID import functions."""
267
268 def test_import_tid_from_integer_default(self):
269 """Test importing a TID from integer with default value."""
270 tid = importTIDfromInteger()
271
272 assert isinstance(tid, TID)
273 assert isinstance(tid.timestamp, datetime.datetime)
274 assert isinstance(tid.clockIdentifier, int)
275 assert 0 <= tid.clockIdentifier < 1024
276
277 def test_import_tid_from_integer_with_value(self):
278 """Test importing a TID from integer with a specific value."""
279 timestamp = datetime.datetime(2023, 1, 1, 12, 0, 0)
280 clock_id = 42
281 original_tid = TID(time=timestamp, clockIdentifier=clock_id)
282 int_value = int(original_tid)
283
284 imported_tid = importTIDfromInteger(int_value)
285
286 assert imported_tid.timestamp == timestamp
287 assert imported_tid.clockIdentifier == clock_id
288
289 def test_import_tid_from_base32_default(self):
290 """Test importing a TID from base32 with default value."""
291 tid = importTIDfromBase32()
292
293 assert isinstance(tid, TID)
294 assert isinstance(tid.timestamp, datetime.datetime)
295 assert isinstance(tid.clockIdentifier, int)
296 assert 0 <= tid.clockIdentifier < 1024
297
298 def test_import_tid_from_base32_with_value(self):
299 """Test importing a TID from base32 with a specific value."""
300 original_tid = TID()
301 str_value = str(original_tid)
302
303 imported_tid = importTIDfromBase32(str_value)
304
305 assert int(imported_tid) == int(original_tid)