Skip to content

Commit

Permalink
add tests for conf_from_url
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrofelder committed Jan 24, 2025
1 parent 900c3c5 commit 6dc04bc
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions tests/atlasapi/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,96 @@ def test_conf_from_url_read_only(temp_path, mocker):

# Set the permissions back to the original
temp_path.chmod(int(curr_mode, 8))


def test_conf_from_url_gin_200(temp_path, mocker):
"""Connected to the internet, GIN is available and returns 200"""
mocker.patch(
"brainglobe_atlasapi.utils.config.get_brainglobe_dir",
return_value=temp_path,
)
mock_response = mock.patch("requests.Response", autospec=True)
mock_response.status_code = 200
mock_response.text = (
"[atlases]\n"
"example_mouse_100um = 1.2\n"
"allen_mouse_10um = 1.2\n"
"allen_mouse_25um = 1.2"
)
with mock.patch(
"requests.get", autospec=True, return_value=mock_response
) as mock_request:
config = utils.conf_from_url(conf_url)
mock_request.assert_called_once_with(conf_url)
assert dict(config["atlases"]) == {
"example_mouse_100um": "1.2",
"allen_mouse_10um": "1.2",
"allen_mouse_25um": "1.2",
}


def test_conf_from_url_wrong_status_with_cache(temp_path, mocker):
"""Connected to the internet, but GIN is down (404),
so we revert to local cache of atlas versions"""
mock_response = mock.patch("requests.Response", autospec=True)
mock_response.status_code = 404

with open(temp_path / "last_versions.conf", "w") as f:
f.write(
"[atlases]\n"
"example_mouse_100um = 1.2\n"
"allen_mouse_10um = 1.2\n"
"allen_mouse_25um = 1.2"
)
mocker.patch(
"brainglobe_atlasapi.utils.config.get_brainglobe_dir",
return_value=temp_path,
)
with mock.patch(
"requests.get", autospec=True, return_value=mock_response
) as mock_request:
config = utils.conf_from_url(conf_url)
mock_request.assert_called_with(conf_url)
assert dict(config["atlases"]) == {
"example_mouse_100um": "1.2",
"allen_mouse_10um": "1.2",
"allen_mouse_25um": "1.2",
}


def test_conf_from_url_no_connection_with_cache(temp_path, mocker):
"""Not connected to the internet,
but we can revert to local cache of atlas versions"""
with open(temp_path / "last_versions.conf", "w") as f:
f.write(
"[atlases]\n"
"example_mouse_100um = 1.2\n"
"allen_mouse_10um = 1.2\n"
"allen_mouse_25um = 1.2"
)
mocker.patch(
"brainglobe_atlasapi.utils.config.get_brainglobe_dir",
return_value=temp_path,
)
with mock.patch("requests.get", autospec=True) as mock_request:
mock_request.side_effect = requests.ConnectionError()
config = utils.conf_from_url(conf_url)
assert dict(config["atlases"]) == {
"example_mouse_100um": "1.2",
"allen_mouse_10um": "1.2",
"allen_mouse_25um": "1.2",
}


def test_conf_from_url_no_connection_no_cache(temp_path, mocker):
"""Not connected to the internet
and we have no local cache of atlas version"""
mocker.patch(
"brainglobe_atlasapi.utils.config.get_brainglobe_dir",
return_value=temp_path,
)
with mock.patch("requests.get", autospec=True) as mock_request:
mock_request.side_effect = requests.ConnectionError()
with pytest.raises(FileNotFoundError) as e:
utils.conf_from_url(conf_url)
assert "Last versions cache file not found." == str(e.value)

0 comments on commit 6dc04bc

Please sign in to comment.