Skip to content

Commit

Permalink
improve url tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FynnBe committed Dec 3, 2024
1 parent f8fe382 commit 662390b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
1 change: 1 addition & 0 deletions dev/env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies:
- python-dateutil
- python=3.12
- requests
- requests-mock
- rich
- ruff
- ruyaml
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"pytest-cov",
"pytest-xdist", # parallel pytest
"pytest",
"requests-mock",
]
),
"dev": test_extras
Expand Down
98 changes: 95 additions & 3 deletions tests/test_internal/test_url.py
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)

0 comments on commit 662390b

Please sign in to comment.