Skip to content

Commit

Permalink
chore(deps): bump pytest from 7.1.2 to 8.3.1 (#368)
Browse files Browse the repository at this point in the history
Signed-off-by: dependabot[bot] <[email protected]>
Signed-off-by: Guilhem Barthés <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Guilhem Barthés <[email protected]>
  • Loading branch information
dependabot[bot] and guilhem-barthes authored Jul 23, 2024
1 parent bd29367 commit 1fd4e1b
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 20 deletions.
1 change: 1 addition & 0 deletions changes/1509.removed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dependency `pytest-lazy-fixture`
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
docker==7.1.0
pytest==7.1.2
pytest==8.3.1
dataclasses==0.7;python_version<"3.7"
pydantic>=2.3.0, <3.0.0
pytest-xdist==3.6.1
pytest-lazy-fixture==0.6.3
flake8==7.1.0
flake8-bugbear==24.4.26
isort==5.13.2
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from substratest.factory import AugmentedDataset
from substratest.fl_interface import FunctionCategory

from .lazy_fixture import pytest_fixture_setup # noqa: F401, needed to set-up fixture

TESTS_RUN_UUID = uuid.uuid4().hex # unique uuid identifying the tests run

pytest_plugins = ["pytest_skipuntil"]
Expand Down
89 changes: 89 additions & 0 deletions tests/lazy_fixture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import dataclasses
import typing

import pytest


@dataclasses.dataclass
class LazyFixture:
"""Lazy fixture dataclass."""

name: str


def lazy_fixture(name: str) -> LazyFixture:
"""Mark a fixture as lazy."""
return LazyFixture(name)


def is_lazy_fixture(value: object) -> bool:
"""Check whether a value is a lazy fixture."""
return isinstance(value, LazyFixture)


def pytest_make_parametrize_id(
config: pytest.Config,
val: object,
argname: str,
) -> typing.Optional[str]:
"""Inject lazy fixture parametrized id.
Reference:
- https://docs.pytest.org/en/latest/reference/reference.html#pytest.hookspec.pytest_make_parametrize_id
Args:
config (pytest.Config): pytest configuration.
value (object): fixture value.
argname (str): automatic parameter name.
Returns:
typing.Optional[str]: new parameter id.
"""
if is_lazy_fixture(val):
return typing.cast(LazyFixture, val).name
return None


@pytest.hookimpl(tryfirst=True)
def pytest_fixture_setup(
fixturedef: pytest.FixtureDef,
request: pytest.FixtureRequest,
) -> typing.Optional[object]:
"""Lazy fixture setup hook.
This hook will never take over a fixture setup but just simply will
try to resolve recursively any lazy fixture found in request.param.
Reference:
- https://docs.pytest.org/en/latest/reference/reference.html#pytest.hookspec.pytest_fixture_setup
Args:
fixturedef (pytest.FixtureDef): fixture definition object.
request (pytest.FixtureRequest): fixture request object.
Returns:
typing.Optional[object]: fixture value or None otherwise.
"""
if hasattr(request, "param") and request.param:
request.param = _resolve_lazy_fixture(request.param, request)
return None


def _resolve_lazy_fixture(__val: object, request: pytest.FixtureRequest) -> object:
"""Lazy fixture resolver.
Args:
__val (object): fixture value object.
request (pytest.FixtureRequest): pytest fixture request object.
Returns:
object: resolved fixture value.
"""
if isinstance(__val, (list, tuple)):
return tuple(_resolve_lazy_fixture(v, request) for v in __val)
if isinstance(__val, typing.Mapping):
return {k: _resolve_lazy_fixture(v, request) for k, v in __val.items()}
if not is_lazy_fixture(__val):
return __val
lazy_obj = typing.cast(LazyFixture, __val)
return request.getfixturevalue(lazy_obj.name)
38 changes: 20 additions & 18 deletions tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from substratest.fl_interface import FLTaskOutputGenerator
from substratest.fl_interface import OutputIdentifiers

from .lazy_fixture import lazy_fixture


@pytest.fixture
def public():
Expand Down Expand Up @@ -56,11 +58,11 @@ def test_permission_creation(is_public, factory, client):
@pytest.mark.parametrize(
"permissions",
[
pytest.lazy_fixture("public"),
pytest.lazy_fixture("private"),
pytest.lazy_fixture("all_organizations"),
pytest.lazy_fixture("organization_1_only"),
pytest.lazy_fixture("organization_2_only"),
lazy_fixture("public"),
lazy_fixture("private"),
lazy_fixture("all_organizations"),
lazy_fixture("organization_1_only"),
lazy_fixture("organization_2_only"),
],
)
def test_get_metadata(permissions, factory, clients, channel):
Expand Down Expand Up @@ -99,8 +101,8 @@ def test_permission_invalid_organization_id(factory, client):
@pytest.mark.parametrize(
"permissions",
[
pytest.lazy_fixture("public"),
pytest.lazy_fixture("organization_2_only"),
lazy_fixture("public"),
lazy_fixture("organization_2_only"),
],
)
def test_download_asset_access_granted(permissions, factory, client_1, client_2, channel):
Expand Down Expand Up @@ -136,14 +138,14 @@ def test_download_asset_access_restricted(factory, client_1, client_2, channel):
"permissions_1,permissions_2,expected_permissions",
[
(
pytest.lazy_fixture("organization_2_only"),
pytest.lazy_fixture("organization_1_only"),
pytest.lazy_fixture("organizations_1_and_2_only"),
lazy_fixture("organization_2_only"),
lazy_fixture("organization_1_only"),
lazy_fixture("organizations_1_and_2_only"),
),
(
pytest.lazy_fixture("public"),
pytest.lazy_fixture("organization_1_only"),
pytest.lazy_fixture("organizations_1_and_2_only"),
lazy_fixture("public"),
lazy_fixture("organization_1_only"),
lazy_fixture("organizations_1_and_2_only"),
),
],
)
Expand Down Expand Up @@ -227,11 +229,11 @@ def test_permissions_denied_process(factory, client_1, client_2, channel, worker
"client_1_permissions,client_2_permissions,expectation",
[
(
pytest.lazy_fixture("private"),
pytest.lazy_fixture("private"),
lazy_fixture("private"),
lazy_fixture("private"),
pytest.raises(substra.exceptions.AuthorizationError),
),
(pytest.lazy_fixture("organization_2_only"), pytest.lazy_fixture("private"), does_not_raise()),
(lazy_fixture("organization_2_only"), lazy_fixture("private"), does_not_raise()),
],
)
def test_permissions_model_process(
Expand Down Expand Up @@ -414,8 +416,8 @@ def test_permissions_denied_head_model_process(factory, client_1, client_2, chan
@pytest.mark.parametrize(
"permission_train_output, expectation",
[
(pytest.lazy_fixture("organization_1_only"), pytest.raises(substra.exceptions.AuthorizationError)),
(pytest.lazy_fixture("organization_2_only"), does_not_raise()),
(lazy_fixture("organization_1_only"), pytest.raises(substra.exceptions.AuthorizationError)),
(lazy_fixture("organization_2_only"), does_not_raise()),
],
)
def test_permission_to_test_on_org_without_training(
Expand Down

0 comments on commit 1fd4e1b

Please sign in to comment.