"""Test cases for the NSID class in atpasser.uri.nsid module.""" import pytest from atpasser.uri.nsid import NSID from atpasser.uri.exceptions import InvalidNSIDError, ValidationError class TestNSID: """Test cases for the NSID class.""" def test_valid_nsid_simple(self): """Test creating an NSID with a valid simple format.""" nsid_str = "com.example.recordName" nsid = NSID(nsid_str) assert str(nsid) == nsid_str assert nsid.nsid == nsid_str assert nsid.domainAuthority == ["com", "example"] assert nsid.domainAuthorityAsText == "com.example" assert nsid.name == "recordName" assert nsid.fragment is None def test_valid_nsid_with_fragment(self): """Test creating an NSID with a valid fragment.""" nsid_str = "com.example.recordName#fragment" nsid = NSID(nsid_str) assert str(nsid) == nsid_str assert nsid.nsid == nsid_str assert nsid.domainAuthority == ["com", "example"] assert nsid.domainAuthorityAsText == "com.example" assert nsid.name == "recordName" assert nsid.fragment == "fragment" def test_valid_nsid_multiple_segments(self): """Test creating an NSID with multiple domain segments.""" nsid_str = "net.users.bob.ping" nsid = NSID(nsid_str) assert str(nsid) == nsid_str assert nsid.nsid == nsid_str assert nsid.domainAuthority == ["net", "users", "bob"] assert nsid.domainAuthorityAsText == "net.users.bob" assert nsid.name == "ping" assert nsid.fragment is None def test_valid_nsid_with_hyphens(self): """Test creating an NSID with hyphens in domain segments.""" nsid_str = "a-0.b-1.c.recordName" nsid = NSID(nsid_str) assert str(nsid) == nsid_str assert nsid.nsid == nsid_str assert nsid.domainAuthority == ["a-0", "b-1", "c"] assert nsid.domainAuthorityAsText == "a-0.b-1.c" assert nsid.name == "recordName" assert nsid.fragment is None def test_valid_nsid_case_sensitivity(self): """Test creating an NSID with case-sensitive name.""" nsid_str = "com.example.fooBar" nsid = NSID(nsid_str) assert str(nsid) == nsid_str assert nsid.nsid == nsid_str assert nsid.domainAuthority == ["com", "example"] assert nsid.domainAuthorityAsText == "com.example" assert nsid.name == "fooBar" assert nsid.fragment is None def test_valid_nsid_with_numbers_in_name(self): """Test creating an NSID with numbers in the name.""" nsid_str = "com.example.record123" nsid = NSID(nsid_str) assert str(nsid) == nsid_str assert nsid.nsid == nsid_str assert nsid.domainAuthority == ["com", "example"] assert nsid.domainAuthorityAsText == "com.example" assert nsid.name == "record123" assert nsid.fragment is None def test_invalid_nsid_non_ascii_characters(self): """Test that an NSID with non-ASCII characters raises InvalidNSIDError.""" nsid_str = "com.exa💩ple.thing" with pytest.raises(InvalidNSIDError, match="contains invalid characters"): NSID(nsid_str) def test_invalid_nsid_too_long(self): """Test that an NSID that is too long raises InvalidNSIDError.""" # Create an NSID that exceeds the 317 character limit long_segment = "a" * 100 nsid_str = f"{long_segment}.{long_segment}.{long_segment}.recordName" with pytest.raises( InvalidNSIDError, match="domain authority length exceeds limit" ): NSID(nsid_str) def test_invalid_nsid_starts_with_dot(self): """Test that an NSID starting with a dot raises InvalidNSIDError.""" nsid_str = ".com.example.recordName" with pytest.raises(InvalidNSIDError, match="invalid format"): NSID(nsid_str) def test_invalid_nsid_ends_with_dot(self): """Test that an NSID ending with a dot raises InvalidNSIDError.""" nsid_str = "com.example.recordName." with pytest.raises(InvalidNSIDError, match="invalid format"): NSID(nsid_str) def test_invalid_nsid_too_few_segments(self): """Test that an NSID with too few segments raises InvalidNSIDError.""" nsid_str = "com.example" with pytest.raises(InvalidNSIDError, match="invalid format"): NSID(nsid_str) def test_invalid_nsid_domain_authority_too_long(self): """Test that an NSID with domain authority that is too long raises InvalidNSIDError.""" # Create a domain authority that exceeds the 253 character limit long_segment = "a" * 63 nsid_str = ( f"{long_segment}.{long_segment}.{long_segment}.{long_segment}.recordName" ) with pytest.raises( InvalidNSIDError, match="domain authority length exceeds limit" ): NSID(nsid_str) def test_invalid_nsid_domain_segment_too_long(self): """Test that an NSID with a domain segment that is too long raises InvalidNSIDError.""" nsid_str = f"{'a' * 64}.example.recordName" with pytest.raises(InvalidNSIDError, match="segment length error"): NSID(nsid_str) def test_invalid_nsid_domain_segment_empty(self): """Test that an NSID with an empty domain segment raises InvalidNSIDError.""" nsid_str = "com..example.recordName" with pytest.raises(InvalidNSIDError, match="segment length error"): NSID(nsid_str) def test_invalid_nsid_domain_invalid_characters(self): """Test that an NSID with invalid characters in domain raises InvalidNSIDError.""" nsid_str = "com.ex@mple.recordName" with pytest.raises(InvalidNSIDError, match="contains invalid characters"): NSID(nsid_str) def test_invalid_nsid_domain_segment_starts_with_hyphen(self): """Test that an NSID with a domain segment starting with a hyphen raises InvalidNSIDError.""" nsid_str = "com.-example.recordName" with pytest.raises(InvalidNSIDError, match="invalid format"): NSID(nsid_str) def test_invalid_nsid_domain_segment_ends_with_hyphen(self): """Test that an NSID with a domain segment ending with a hyphen raises InvalidNSIDError.""" nsid_str = "com.example-.recordName" with pytest.raises(InvalidNSIDError, match="invalid format"): NSID(nsid_str) def test_invalid_nsid_tld_starts_with_digit(self): """Test that an NSID with a TLD starting with a digit raises InvalidNSIDError.""" nsid_str = "1com.example.recordName" with pytest.raises(InvalidNSIDError, match="invalid format"): NSID(nsid_str) def test_invalid_nsid_name_empty(self): """Test that an NSID with an empty name raises InvalidNSIDError.""" nsid_str = "com.example." with pytest.raises(InvalidNSIDError, match="invalid format"): NSID(nsid_str) def test_invalid_nsid_name_too_long(self): """Test that an NSID with a name that is too long raises InvalidNSIDError.""" nsid_str = f"com.example.{'a' * 64}" with pytest.raises(InvalidNSIDError, match="name length error"): NSID(nsid_str) def test_invalid_nsid_name_invalid_characters(self): """Test that an NSID with invalid characters in name raises InvalidNSIDError.""" nsid_str = "com.example.record-name" with pytest.raises(InvalidNSIDError, match="contains invalid characters"): NSID(nsid_str) def test_invalid_nsid_name_starts_with_digit(self): """Test that an NSID with a name starting with a digit raises InvalidNSIDError.""" nsid_str = "com.example.1record" with pytest.raises(InvalidNSIDError, match="invalid format"): NSID(nsid_str) def test_invalid_nsid_fragment_empty(self): """Test that an NSID with an empty fragment raises InvalidNSIDError.""" nsid_str = "com.example.recordName#" with pytest.raises(InvalidNSIDError, match="fragment length error"): NSID(nsid_str) def test_invalid_nsid_fragment_too_long(self): """Test that an NSID with a fragment that is too long raises InvalidNSIDError.""" nsid_str = f"com.example.recordName#{'a' * 64}" with pytest.raises(InvalidNSIDError, match="fragment length error"): NSID(nsid_str) def test_invalid_nsid_fragment_invalid_characters(self): """Test that an NSID with invalid characters in fragment raises InvalidNSIDError.""" nsid_str = "com.example.recordName#fragment-with-hyphen" with pytest.raises(InvalidNSIDError, match="contains invalid characters"): NSID(nsid_str) def test_invalid_nsid_fragment_starts_with_digit(self): """Test that an NSID with a fragment starting with a digit raises InvalidNSIDError.""" nsid_str = "com.example.recordName#1fragment" with pytest.raises(InvalidNSIDError, match="invalid format"): NSID(nsid_str) def test_nsid_equality(self): """Test NSID equality comparison.""" nsid_str = "com.example.recordName" nsid1 = NSID(nsid_str) nsid2 = NSID(nsid_str) assert nsid1 == nsid2 assert nsid1 != "not an nsid object" def test_nsid_string_representation(self): """Test NSID string representation.""" nsid_str = "com.example.recordName" nsid = NSID(nsid_str) assert str(nsid) == nsid_str def test_nsid_string_representation_with_fragment(self): """Test NSID string representation with fragment.""" nsid_str = "com.example.recordName#fragment" nsid = NSID(nsid_str) assert str(nsid) == nsid_str