Skip to content

Commit

Permalink
add tests and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ytausch committed Feb 2, 2024
1 parent e33b1b5 commit 45b713e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion conda_forge_tick/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def invoke(self, ctx: click.Context):
@click.option(
"--online/--offline",
default=False,
help="Online: Use the GitHub API for accessing the dependency graph. This is useful for local testing. Note "
help="online: Use the GitHub API for accessing the dependency graph. This is useful for local testing. Note "
"however that any write operations will not be performed. Important: The current working directory will be "
"used to cache JSON files. Local files will be used if they exist.",
)
Expand Down
2 changes: 2 additions & 0 deletions conda_forge_tick/lazy_json_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ def hget(self, name: str, key: str) -> str:
sharded_path = get_sharded_path(f"{name}/{key}.json")
url = urllib.parse.urljoin(self.base_url, sharded_path)
r = requests.get(url)
if r.status_code == 404:
raise KeyError(f"Key {key} not found in hashmap {name}")
r.raise_for_status()
return r.text

Expand Down
32 changes: 32 additions & 0 deletions tests/test_lazy_json_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,35 @@ def test_hexists_failure(
mock_head.assert_called_once_with(
f"{TestGithubLazyJsonBackend.base_url}/name/key.json",
)

def test_hkeys(self, backend: GithubLazyJsonBackend) -> None:
assert backend.hkeys("name") == []

def test_hgetall(self, backend: GithubLazyJsonBackend) -> None:
assert backend.hgetall("name") == {}

@mock.patch("requests.get")
def test_hget_success(
self,
mock_get: MagicMock,
backend: GithubLazyJsonBackend,
) -> None:
mock_get.return_value.status_code = 200
mock_get.return_value.text = "{'key': 'value'}"
assert backend.hget("name", "key") == "{'key': 'value'}"
mock_get.assert_called_once_with(
f"{TestGithubLazyJsonBackend.base_url}/name/4/4/0/9/d/key.json",
)

@mock.patch("requests.get")
def test_hget_not_found(
self,
mock_get: MagicMock,
backend: GithubLazyJsonBackend,
) -> None:
mock_get.return_value.status_code = 404
with pytest.raises(KeyError):
backend.hget("name", "key")
mock_get.assert_called_once_with(
f"{TestGithubLazyJsonBackend.base_url}/name/4/4/0/9/d/key.json",
)

0 comments on commit 45b713e

Please sign in to comment.