···
1
+
"""Test cases for the RKey and TID classes in atpasser.uri.rkey module."""
5
+
from atpasser.uri.rkey import RKey, TID, importTIDfromInteger, importTIDfromBase32
6
+
from atpasser.uri.exceptions import InvalidRKeyError
10
+
"""Test cases for the RKey class."""
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)
17
+
assert str(rkey) == rkey_str
18
+
assert rkey.recordKey == rkey_str
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)
25
+
assert str(rkey) == rkey_str
26
+
assert rkey.recordKey == rkey_str
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)
33
+
assert str(rkey) == rkey_str
34
+
assert rkey.recordKey == rkey_str
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)
41
+
assert str(rkey) == rkey_str
42
+
assert rkey.recordKey == rkey_str
44
+
def test_valid_rkey_underscore(self):
45
+
"""Test creating an RKey with just an underscore."""
47
+
rkey = RKey(rkey_str)
49
+
assert str(rkey) == rkey_str
50
+
assert rkey.recordKey == rkey_str
52
+
def test_invalid_rkey_empty(self):
53
+
"""Test that an empty RKey raises InvalidRKeyError."""
56
+
with pytest.raises(InvalidRKeyError, match="record key is empty"):
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
64
+
with pytest.raises(InvalidRKeyError, match="exceeds maximum length"):
67
+
def test_invalid_rkey_reserved_double_dot(self):
68
+
"""Test that an RKey with '..' raises InvalidRKeyError."""
71
+
with pytest.raises(InvalidRKeyError, match="reserved value"):
74
+
def test_invalid_rkey_reserved_single_dot(self):
75
+
"""Test that an RKey with '.' raises InvalidRKeyError."""
78
+
with pytest.raises(InvalidRKeyError, match="reserved value"):
81
+
def test_invalid_rkey_invalid_characters(self):
82
+
"""Test that an RKey with invalid characters raises InvalidRKeyError."""
83
+
rkey_str = "alpha/beta"
85
+
with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
88
+
def test_invalid_rkey_hash_character(self):
89
+
"""Test that an RKey with a hash character raises InvalidRKeyError."""
92
+
with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
95
+
def test_invalid_rkey_at_character(self):
96
+
"""Test that an RKey with an at character raises InvalidRKeyError."""
97
+
rkey_str = "@handle"
99
+
with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
102
+
def test_invalid_rkey_space(self):
103
+
"""Test that an RKey with a space raises InvalidRKeyError."""
104
+
rkey_str = "any space"
106
+
with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
109
+
def test_invalid_rkey_plus_character(self):
110
+
"""Test that an RKey with a plus character raises InvalidRKeyError."""
111
+
rkey_str = "any+space"
113
+
with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
116
+
def test_invalid_rkey_brackets(self):
117
+
"""Test that an RKey with brackets raises InvalidRKeyError."""
118
+
rkey_str = "number[3]"
120
+
with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
123
+
def test_invalid_rkey_parentheses(self):
124
+
"""Test that an RKey with parentheses raises InvalidRKeyError."""
125
+
rkey_str = "number(3)"
127
+
with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
130
+
def test_invalid_rkey_quotes(self):
131
+
"""Test that an RKey with quotes raises InvalidRKeyError."""
132
+
rkey_str = '"quote"'
134
+
with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
137
+
def test_invalid_rkey_base64_padding(self):
138
+
"""Test that an RKey with base64 padding raises InvalidRKeyError."""
139
+
rkey_str = "dHJ1ZQ=="
141
+
with pytest.raises(InvalidRKeyError, match="contains invalid characters"):
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)
150
+
assert rkey1 == rkey2
151
+
assert rkey1 != "not an rkey object"
153
+
def test_rkey_string_representation(self):
154
+
"""Test RKey string representation."""
155
+
rkey_str = "3jui7kd54zh2y"
156
+
rkey = RKey(rkey_str)
158
+
assert str(rkey) == rkey_str
162
+
"""Test cases for the TID class."""
164
+
def test_tid_creation_default(self):
165
+
"""Test creating a TID with default parameters."""
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
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)
180
+
assert tid.timestamp == timestamp
181
+
assert isinstance(tid.clockIdentifier, int)
182
+
assert 0 <= tid.clockIdentifier < 1024
184
+
def test_tid_creation_with_clock_identifier(self):
185
+
"""Test creating a TID with a specific clock identifier."""
187
+
tid = TID(clockIdentifier=clock_id)
189
+
assert tid.clockIdentifier == clock_id
190
+
assert isinstance(tid.timestamp, datetime.datetime)
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)
196
+
tid = TID(time=timestamp, clockIdentifier=clock_id)
198
+
assert tid.timestamp == timestamp
199
+
assert tid.clockIdentifier == clock_id
201
+
def test_tid_integer_representation(self):
202
+
"""Test TID integer representation."""
203
+
timestamp = datetime.datetime(2023, 1, 1, 12, 0, 0)
205
+
tid = TID(time=timestamp, clockIdentifier=clock_id)
207
+
int_value = int(tid)
208
+
expected_value = int(timestamp.timestamp() * 1000000) * 1024 + clock_id
210
+
assert int_value == expected_value
212
+
def test_tid_string_representation(self):
213
+
"""Test TID string representation."""
216
+
str_value = str(tid)
217
+
assert len(str_value) == 13
218
+
assert all(c in "234567abcdefghijklmnopqrstuvwxyz" for c in str_value)
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)
224
+
tid1 = TID(time=timestamp, clockIdentifier=clock_id)
225
+
tid2 = TID(time=timestamp, clockIdentifier=clock_id)
227
+
assert tid1 == tid2
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)
233
+
tid = TID(time=timestamp, clockIdentifier=clock_id)
234
+
rkey = RKey(str(tid))
238
+
def test_tid_inequality_with_different_object(self):
239
+
"""Test TID inequality comparison with a different object type."""
242
+
assert tid != "not a tid object"
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)
249
+
tid1 = TID(time=timestamp1, clockIdentifier=clock_id)
250
+
tid2 = TID(time=timestamp2, clockIdentifier=clock_id)
252
+
assert tid1 != tid2
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)
259
+
tid1 = TID(time=timestamp, clockIdentifier=clock_id1)
260
+
tid2 = TID(time=timestamp, clockIdentifier=clock_id2)
262
+
assert tid1 != tid2
265
+
class TestTIDImportFunctions:
266
+
"""Test cases for TID import functions."""
268
+
def test_import_tid_from_integer_default(self):
269
+
"""Test importing a TID from integer with default value."""
270
+
tid = importTIDfromInteger()
272
+
assert isinstance(tid, TID)
273
+
assert isinstance(tid.timestamp, datetime.datetime)
274
+
assert isinstance(tid.clockIdentifier, int)
275
+
assert 0 <= tid.clockIdentifier < 1024
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)
281
+
original_tid = TID(time=timestamp, clockIdentifier=clock_id)
282
+
int_value = int(original_tid)
284
+
imported_tid = importTIDfromInteger(int_value)
286
+
assert imported_tid.timestamp == timestamp
287
+
assert imported_tid.clockIdentifier == clock_id
289
+
def test_import_tid_from_base32_default(self):
290
+
"""Test importing a TID from base32 with default value."""
291
+
tid = importTIDfromBase32()
293
+
assert isinstance(tid, TID)
294
+
assert isinstance(tid.timestamp, datetime.datetime)
295
+
assert isinstance(tid.clockIdentifier, int)
296
+
assert 0 <= tid.clockIdentifier < 1024
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)
303
+
imported_tid = importTIDfromBase32(str_value)
305
+
assert int(imported_tid) == int(original_tid)