Codebase rewritten to:
tangled.org/atpasser.poxiao-labs.work/atpasser
1"""Test cases for the NSID class in atpasser.uri.nsid module."""
2
3import pytest
4from atpasser.uri.nsid import NSID
5from atpasser.uri.exceptions import InvalidNSIDError, ValidationError
6
7
8class TestNSID:
9 """Test cases for the NSID class."""
10
11 def test_valid_nsid_simple(self):
12 """Test creating an NSID with a valid simple format."""
13 nsid_str = "com.example.recordName"
14 nsid = NSID(nsid_str)
15
16 assert str(nsid) == nsid_str
17 assert nsid.nsid == nsid_str
18 assert nsid.domainAuthority == ["com", "example"]
19 assert nsid.domainAuthorityAsText == "com.example"
20 assert nsid.name == "recordName"
21 assert nsid.fragment is None
22
23 def test_valid_nsid_with_fragment(self):
24 """Test creating an NSID with a valid fragment."""
25 nsid_str = "com.example.recordName#fragment"
26 nsid = NSID(nsid_str)
27
28 assert str(nsid) == nsid_str
29 assert nsid.nsid == nsid_str
30 assert nsid.domainAuthority == ["com", "example"]
31 assert nsid.domainAuthorityAsText == "com.example"
32 assert nsid.name == "recordName"
33 assert nsid.fragment == "fragment"
34
35 def test_valid_nsid_multiple_segments(self):
36 """Test creating an NSID with multiple domain segments."""
37 nsid_str = "net.users.bob.ping"
38 nsid = NSID(nsid_str)
39
40 assert str(nsid) == nsid_str
41 assert nsid.nsid == nsid_str
42 assert nsid.domainAuthority == ["net", "users", "bob"]
43 assert nsid.domainAuthorityAsText == "net.users.bob"
44 assert nsid.name == "ping"
45 assert nsid.fragment is None
46
47 def test_valid_nsid_with_hyphens(self):
48 """Test creating an NSID with hyphens in domain segments."""
49 nsid_str = "a-0.b-1.c.recordName"
50 nsid = NSID(nsid_str)
51
52 assert str(nsid) == nsid_str
53 assert nsid.nsid == nsid_str
54 assert nsid.domainAuthority == ["a-0", "b-1", "c"]
55 assert nsid.domainAuthorityAsText == "a-0.b-1.c"
56 assert nsid.name == "recordName"
57 assert nsid.fragment is None
58
59 def test_valid_nsid_case_sensitivity(self):
60 """Test creating an NSID with case-sensitive name."""
61 nsid_str = "com.example.fooBar"
62 nsid = NSID(nsid_str)
63
64 assert str(nsid) == nsid_str
65 assert nsid.nsid == nsid_str
66 assert nsid.domainAuthority == ["com", "example"]
67 assert nsid.domainAuthorityAsText == "com.example"
68 assert nsid.name == "fooBar"
69 assert nsid.fragment is None
70
71 def test_valid_nsid_with_numbers_in_name(self):
72 """Test creating an NSID with numbers in the name."""
73 nsid_str = "com.example.record123"
74 nsid = NSID(nsid_str)
75
76 assert str(nsid) == nsid_str
77 assert nsid.nsid == nsid_str
78 assert nsid.domainAuthority == ["com", "example"]
79 assert nsid.domainAuthorityAsText == "com.example"
80 assert nsid.name == "record123"
81 assert nsid.fragment is None
82
83 def test_invalid_nsid_non_ascii_characters(self):
84 """Test that an NSID with non-ASCII characters raises InvalidNSIDError."""
85 nsid_str = "com.exa💩ple.thing"
86
87 with pytest.raises(InvalidNSIDError, match="contains invalid characters"):
88 NSID(nsid_str)
89
90 def test_invalid_nsid_too_long(self):
91 """Test that an NSID that is too long raises InvalidNSIDError."""
92 # Create an NSID that exceeds the 317 character limit
93 long_segment = "a" * 100
94 nsid_str = f"{long_segment}.{long_segment}.{long_segment}.recordName"
95
96 with pytest.raises(
97 InvalidNSIDError, match="domain authority length exceeds limit"
98 ):
99 NSID(nsid_str)
100
101 def test_invalid_nsid_starts_with_dot(self):
102 """Test that an NSID starting with a dot raises InvalidNSIDError."""
103 nsid_str = ".com.example.recordName"
104
105 with pytest.raises(InvalidNSIDError, match="invalid format"):
106 NSID(nsid_str)
107
108 def test_invalid_nsid_ends_with_dot(self):
109 """Test that an NSID ending with a dot raises InvalidNSIDError."""
110 nsid_str = "com.example.recordName."
111
112 with pytest.raises(InvalidNSIDError, match="invalid format"):
113 NSID(nsid_str)
114
115 def test_invalid_nsid_too_few_segments(self):
116 """Test that an NSID with too few segments raises InvalidNSIDError."""
117 nsid_str = "com.example"
118
119 with pytest.raises(InvalidNSIDError, match="invalid format"):
120 NSID(nsid_str)
121
122 def test_invalid_nsid_domain_authority_too_long(self):
123 """Test that an NSID with domain authority that is too long raises InvalidNSIDError."""
124 # Create a domain authority that exceeds the 253 character limit
125 long_segment = "a" * 63
126 nsid_str = (
127 f"{long_segment}.{long_segment}.{long_segment}.{long_segment}.recordName"
128 )
129
130 with pytest.raises(
131 InvalidNSIDError, match="domain authority length exceeds limit"
132 ):
133 NSID(nsid_str)
134
135 def test_invalid_nsid_domain_segment_too_long(self):
136 """Test that an NSID with a domain segment that is too long raises InvalidNSIDError."""
137 nsid_str = f"{'a' * 64}.example.recordName"
138
139 with pytest.raises(InvalidNSIDError, match="segment length error"):
140 NSID(nsid_str)
141
142 def test_invalid_nsid_domain_segment_empty(self):
143 """Test that an NSID with an empty domain segment raises InvalidNSIDError."""
144 nsid_str = "com..example.recordName"
145
146 with pytest.raises(InvalidNSIDError, match="segment length error"):
147 NSID(nsid_str)
148
149 def test_invalid_nsid_domain_invalid_characters(self):
150 """Test that an NSID with invalid characters in domain raises InvalidNSIDError."""
151 nsid_str = "com.ex@mple.recordName"
152
153 with pytest.raises(InvalidNSIDError, match="contains invalid characters"):
154 NSID(nsid_str)
155
156 def test_invalid_nsid_domain_segment_starts_with_hyphen(self):
157 """Test that an NSID with a domain segment starting with a hyphen raises InvalidNSIDError."""
158 nsid_str = "com.-example.recordName"
159
160 with pytest.raises(InvalidNSIDError, match="invalid format"):
161 NSID(nsid_str)
162
163 def test_invalid_nsid_domain_segment_ends_with_hyphen(self):
164 """Test that an NSID with a domain segment ending with a hyphen raises InvalidNSIDError."""
165 nsid_str = "com.example-.recordName"
166
167 with pytest.raises(InvalidNSIDError, match="invalid format"):
168 NSID(nsid_str)
169
170 def test_invalid_nsid_tld_starts_with_digit(self):
171 """Test that an NSID with a TLD starting with a digit raises InvalidNSIDError."""
172 nsid_str = "1com.example.recordName"
173
174 with pytest.raises(InvalidNSIDError, match="invalid format"):
175 NSID(nsid_str)
176
177 def test_invalid_nsid_name_empty(self):
178 """Test that an NSID with an empty name raises InvalidNSIDError."""
179 nsid_str = "com.example."
180
181 with pytest.raises(InvalidNSIDError, match="invalid format"):
182 NSID(nsid_str)
183
184 def test_invalid_nsid_name_too_long(self):
185 """Test that an NSID with a name that is too long raises InvalidNSIDError."""
186 nsid_str = f"com.example.{'a' * 64}"
187
188 with pytest.raises(InvalidNSIDError, match="name length error"):
189 NSID(nsid_str)
190
191 def test_invalid_nsid_name_invalid_characters(self):
192 """Test that an NSID with invalid characters in name raises InvalidNSIDError."""
193 nsid_str = "com.example.record-name"
194
195 with pytest.raises(InvalidNSIDError, match="contains invalid characters"):
196 NSID(nsid_str)
197
198 def test_invalid_nsid_name_starts_with_digit(self):
199 """Test that an NSID with a name starting with a digit raises InvalidNSIDError."""
200 nsid_str = "com.example.1record"
201
202 with pytest.raises(InvalidNSIDError, match="invalid format"):
203 NSID(nsid_str)
204
205 def test_invalid_nsid_fragment_empty(self):
206 """Test that an NSID with an empty fragment raises InvalidNSIDError."""
207 nsid_str = "com.example.recordName#"
208
209 with pytest.raises(InvalidNSIDError, match="fragment length error"):
210 NSID(nsid_str)
211
212 def test_invalid_nsid_fragment_too_long(self):
213 """Test that an NSID with a fragment that is too long raises InvalidNSIDError."""
214 nsid_str = f"com.example.recordName#{'a' * 64}"
215
216 with pytest.raises(InvalidNSIDError, match="fragment length error"):
217 NSID(nsid_str)
218
219 def test_invalid_nsid_fragment_invalid_characters(self):
220 """Test that an NSID with invalid characters in fragment raises InvalidNSIDError."""
221 nsid_str = "com.example.recordName#fragment-with-hyphen"
222
223 with pytest.raises(InvalidNSIDError, match="contains invalid characters"):
224 NSID(nsid_str)
225
226 def test_invalid_nsid_fragment_starts_with_digit(self):
227 """Test that an NSID with a fragment starting with a digit raises InvalidNSIDError."""
228 nsid_str = "com.example.recordName#1fragment"
229
230 with pytest.raises(InvalidNSIDError, match="invalid format"):
231 NSID(nsid_str)
232
233 def test_nsid_equality(self):
234 """Test NSID equality comparison."""
235 nsid_str = "com.example.recordName"
236 nsid1 = NSID(nsid_str)
237 nsid2 = NSID(nsid_str)
238
239 assert nsid1 == nsid2
240 assert nsid1 != "not an nsid object"
241
242 def test_nsid_string_representation(self):
243 """Test NSID string representation."""
244 nsid_str = "com.example.recordName"
245 nsid = NSID(nsid_str)
246
247 assert str(nsid) == nsid_str
248
249 def test_nsid_string_representation_with_fragment(self):
250 """Test NSID string representation with fragment."""
251 nsid_str = "com.example.recordName#fragment"
252 nsid = NSID(nsid_str)
253
254 assert str(nsid) == nsid_str