"""Test cases for the RestrictedURI class in atpasser.uri module.""" import pytest from atpasser.uri import RestrictedURI from atpasser.uri.exceptions import InvalidRestrictedURIError, InvalidURIError class TestRestrictedURI: """Test cases for the RestrictedURI class.""" def test_valid_restricted_uri_with_did_collection_and_rkey(self): """Test creating a RestrictedURI with a valid DID, collection, and rkey.""" uri_str = ( "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26" ) uri = RestrictedURI(uri_str) assert str(uri) == uri_str assert uri.authorityAsText == "did:plc:z72i7hdynmk6r22z27h6tvur" assert uri.path == ["app.bsky.feed.post", "3jwdwj2ctlk26"] assert uri.pathAsText == "app.bsky.feed.post/3jwdwj2ctlk26" assert uri.collection is not None assert str(uri.collection) == "app.bsky.feed.post" assert uri.rkey is not None assert str(uri.rkey) == "3jwdwj2ctlk26" def test_valid_restricted_uri_with_handle_collection_and_rkey(self): """Test creating a RestrictedURI with a valid handle, collection, and rkey.""" uri_str = "at://bnewbold.bsky.team/app.bsky.feed.post/3jwdwj2ctlk26" uri = RestrictedURI(uri_str) assert str(uri) == uri_str assert uri.authorityAsText == "bnewbold.bsky.team" assert uri.path == ["app.bsky.feed.post", "3jwdwj2ctlk26"] assert uri.collection is not None assert str(uri.collection) == "app.bsky.feed.post" assert uri.rkey is not None assert str(uri.rkey) == "3jwdwj2ctlk26" def test_valid_restricted_uri_with_collection_only(self): """Test creating a RestrictedURI with only a collection.""" uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post" uri = RestrictedURI(uri_str) assert str(uri) == uri_str assert uri.authorityAsText == "did:plc:z72i7hdynmk6r22z27h6tvur" assert uri.path == ["app.bsky.feed.post"] assert uri.collection is not None assert str(uri.collection) == "app.bsky.feed.post" assert uri.rkey is None def test_valid_restricted_uri_with_authority_only(self): """Test creating a RestrictedURI with only an authority.""" uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur" uri = RestrictedURI(uri_str) assert str(uri) == uri_str assert uri.authorityAsText == "did:plc:z72i7hdynmk6r22z27h6tvur" assert uri.path == [] assert uri.collection is None assert uri.rkey is None def test_invalid_restricted_uri_with_query(self): """Test that a RestrictedURI with query parameters raises InvalidRestrictedURIError.""" uri_str = ( "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post?param1=value1" ) with pytest.raises( InvalidRestrictedURIError, match="query parameters not supported" ): RestrictedURI(uri_str) def test_invalid_restricted_uri_with_fragment(self): """Test that a RestrictedURI with a fragment raises InvalidRestrictedURIError.""" uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26#$.some.json.path" with pytest.raises(InvalidRestrictedURIError, match="fragments not supported"): RestrictedURI(uri_str) def test_invalid_restricted_uri_with_invalid_authority(self): """Test that a RestrictedURI with invalid authority raises InvalidRestrictedURIError.""" uri_str = "at://invalid_authority/app.bsky.feed.post/3jwdwj2ctlk26" with pytest.raises(InvalidRestrictedURIError, match="invalid authority"): RestrictedURI(uri_str) def test_invalid_restricted_uri_too_many_path_segments(self): """Test that a RestrictedURI with too many path segments raises InvalidRestrictedURIError.""" uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26/extra" with pytest.raises(InvalidRestrictedURIError, match="too many path segments"): RestrictedURI(uri_str) def test_invalid_restricted_uri_base_uri_validation_failure(self): """Test that a RestrictedURI with invalid base URI raises InvalidURIError.""" uri_str = "https://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post" with pytest.raises(InvalidURIError, match="invalid format"): RestrictedURI(uri_str) def test_restricted_uri_equality(self): """Test RestrictedURI equality comparison.""" uri_str = ( "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26" ) uri1 = RestrictedURI(uri_str) uri2 = RestrictedURI(uri_str) assert uri1 == uri2 assert uri1 != "not a uri object" def test_restricted_uri_string_representation(self): """Test RestrictedURI string representation.""" uri_str = ( "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26" ) uri = RestrictedURI(uri_str) assert str(uri) == uri_str