-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ dependencies: | |
- python-dateutil | ||
- python=3.12 | ||
- requests | ||
- requests-mock | ||
- rich | ||
- ruff | ||
- ruyaml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ | |
"pytest-cov", | ||
"pytest-xdist", # parallel pytest | ||
"pytest", | ||
"requests-mock", | ||
] | ||
), | ||
"dev": test_extras | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,100 @@ | ||
from typing import Type | ||
|
||
import pytest | ||
import requests.exceptions | ||
from pydantic import ValidationError | ||
|
||
from bioimageio.spec._internal.validation_context import ValidationContext | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"url", | ||
[ | ||
"https://example.com", | ||
"https://colab.research.google.com/github/bioimage-io/spec-bioimage-io/blob/main/example/load_model_and_create_your_own.ipynb", | ||
], | ||
) | ||
def test_httpurl_valid(url: str): | ||
from bioimageio.spec._internal.url import HttpUrl | ||
|
||
with ValidationContext(perform_io_checks=True): | ||
assert HttpUrl(url).exists(), url | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"text,status_code", | ||
[ | ||
("OK", 200), | ||
("found", 302), | ||
("redirected", 301)("redirected", 303)("redirected", 308), | ||
("let's ignore this I guess??", 405), | ||
], | ||
) | ||
def test_httpurl_mock_valid(text: str, status_code: int, requests_mock): | ||
from bioimageio.spec._internal.url import HttpUrl | ||
|
||
url = "https://example.com" | ||
requests_mock.get(url, text=text, status_code=status_code) | ||
assert HttpUrl(url).exists() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"text,status_code", | ||
[ | ||
("forbidden", 403), | ||
("Not found", 404), | ||
("just wrong", 199), | ||
], | ||
) | ||
def test_httpurl_mock_invalid(text: str, status_code: int, requests_mock): | ||
from bioimageio.spec._internal.url import HttpUrl | ||
|
||
url = "https://example.com" | ||
requests_mock.get(url, text=text, status_code=status_code) | ||
assert HttpUrl(url).exists() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"text,exc", | ||
[ | ||
("invalid url", requests.exceptions.InvalidURL), | ||
], | ||
) | ||
def test_httpurl_mock_exc(text: str, exc: Type[Exception], requests_mock): | ||
from bioimageio.spec._internal.url import HttpUrl | ||
|
||
url = "https://example.com" | ||
requests_mock.get(url, text=text, exc=exc) | ||
with ValidationContext(perform_io_checks=True): | ||
with pytest.raises(ValidationError): | ||
_ = HttpUrl(url) | ||
|
||
with ValidationContext(perform_io_checks=False): | ||
assert not HttpUrl(url).exists() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"url,io_check", | ||
[ | ||
("https://example.invalid", True), | ||
("https://example.invalid", False), | ||
], | ||
) | ||
def test_httpurl_nonexisting(url: str, io_check: bool): | ||
from bioimageio.spec._internal.url import HttpUrl | ||
|
||
with ValidationContext(perform_io_checks=io_check): | ||
assert HttpUrl(url).exists(), url | ||
|
||
|
||
@pytest.mark.parametrize("url", ["https://github.com"]) | ||
def test_httpurl(url: str): | ||
@pytest.mark.parametrize( | ||
"url", | ||
[ | ||
"invalid-url", | ||
], | ||
) | ||
def test_httpurl_invalid(url: str): | ||
from bioimageio.spec._internal.url import HttpUrl | ||
|
||
assert HttpUrl(url).exists(), url | ||
with pytest.raises(ValidationError), ValidationContext(perform_io_checks=False): | ||
_ = HttpUrl(url) |