clan-core/pkgs/clan-cli/tests/test_clan_uri.py

63 lines
1.8 KiB
Python
Raw Normal View History

2023-12-05 17:16:51 +00:00
from pathlib import Path
2023-12-05 15:17:15 +00:00
import pytest
2023-12-05 17:16:51 +00:00
from clan_cli.clan_uri import ClanScheme, ClanURI
2023-12-05 15:17:15 +00:00
from clan_cli.errors import ClanError
2023-12-05 17:16:51 +00:00
2023-12-05 15:17:15 +00:00
def test_local_uri() -> None:
# Create a ClanURI object from a local URI
uri = ClanURI("clan://file:///home/user/Downloads")
2023-12-05 17:08:27 +00:00
match uri.scheme:
case ClanScheme.FILE.value(path):
2023-12-05 17:16:51 +00:00
assert path == Path("/home/user/Downloads") # type: ignore
2023-12-05 17:08:27 +00:00
case _:
assert False
2023-12-05 15:17:15 +00:00
2023-12-05 17:16:51 +00:00
2023-12-05 15:17:15 +00:00
def test_unsupported_schema() -> None:
with pytest.raises(ClanError, match="Unsupported scheme: ftp"):
# Create a ClanURI object from an unsupported URI
ClanURI("clan://ftp://ftp.example.com")
def test_is_remote() -> None:
# Create a ClanURI object from a remote URI
uri = ClanURI("clan://https://example.com")
2023-12-05 17:08:27 +00:00
match uri.scheme:
case ClanScheme.HTTPS.value(url):
2023-12-05 17:16:51 +00:00
assert url == "https://example.com" # type: ignore
2023-12-05 17:08:27 +00:00
case _:
assert False
2023-12-05 17:16:51 +00:00
2023-12-05 17:08:27 +00:00
def remote_with_clanparams() -> None:
# Create a ClanURI object from a remote URI with parameters
uri = ClanURI("clan://https://example.com?flake_attr=defaultVM")
assert uri.params.flake_attr == "defaultVM"
match uri.scheme:
case ClanScheme.HTTPS.value(url):
2023-12-05 17:16:51 +00:00
assert url == "https://example.com" # type: ignore
2023-12-05 17:08:27 +00:00
case _:
assert False
2023-12-05 17:16:51 +00:00
2023-12-05 17:08:27 +00:00
def remote_with_all_params() -> None:
# Create a ClanURI object from a remote URI with parameters
2023-12-05 17:16:51 +00:00
uri = ClanURI(
"clan://https://example.com?flake_attr=defaultVM&machine=vm1&password=1234"
)
2023-12-05 17:08:27 +00:00
assert uri.params.flake_attr == "defaultVM"
assert uri.params.machine == "vm1"
match uri.scheme:
case ClanScheme.HTTPS.value(url):
2023-12-05 17:16:51 +00:00
assert url == "https://example.com&password=1234" # type: ignore
2023-12-05 17:08:27 +00:00
case _:
2023-12-05 17:16:51 +00:00
assert False