Skip to content

Commit

Permalink
Added GolemFactory factor_boy instead of pytest fixure golem_factory
Browse files Browse the repository at this point in the history
  • Loading branch information
lucekdudek committed Dec 8, 2022
1 parent 37ec82b commit 1f4833e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 41 deletions.
20 changes: 0 additions & 20 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,6 @@ async def _gftp_aexit(self, *args):
monkeypatch.setattr(GftpProvider, "__aexit__", _gftp_aexit)


@pytest.fixture
def api_config_factory():
def _api_config_factory(**kwargs) -> ApiConfig:
if "app_key" not in kwargs:
kwargs["app_key"] = "yagna-app-key"
return ApiConfig(**kwargs)

return _api_config_factory


@pytest.fixture
def golem_factory(api_config_factory) -> Golem:
def _golem_factory(**kwargs) -> Golem:
if "api_config" not in kwargs:
kwargs["api_config"] = api_config_factory()
return Golem(**kwargs)

return _golem_factory


@pytest.fixture
def purge_yagna_os_env() -> None:
for key in [
Expand Down
7 changes: 4 additions & 3 deletions tests/engine/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest
from unittest.mock import Mock

from tests.factories.golem import GolemFactory
from yapapi import Golem
from yapapi.config import ApiConfig
import yapapi.engine
Expand All @@ -24,15 +25,15 @@ def mock_rest_configuration(monkeypatch):
("my-little-subnet", "whole-golem", "whole-golem"),
],
)
def test_set_subnet_tag(default_subnet, subnet_arg, expected_subnet, monkeypatch, golem_factory):
def test_set_subnet_tag(default_subnet, subnet_arg, expected_subnet, monkeypatch):
"""Check that `subnet_tag` argument takes precedence over `yapapi.engine.DEFAULT_SUBNET`."""

monkeypatch.setattr(yapapi.engine, "DEFAULT_SUBNET", default_subnet)

if subnet_arg is not None:
golem = golem_factory(budget=1.0, subnet_tag=subnet_arg)
golem = GolemFactory(budget=1.0, subnet_tag=subnet_arg)
else:
golem = golem_factory(budget=1.0)
golem = GolemFactory(budget=1.0)
assert golem.subnet_tag == expected_subnet


Expand Down
8 changes: 8 additions & 0 deletions tests/factories/golem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import factory

import yapapi.golem


class GolemFactory(factory.Factory):
class Meta:
model = yapapi.golem.Golem
5 changes: 3 additions & 2 deletions tests/services/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from unittest import mock
from unittest.mock import Mock, patch

from tests.factories.golem import GolemFactory
from yapapi import Golem
from yapapi.services import Service, ServiceRunner

Expand Down Expand Up @@ -81,14 +82,14 @@ async def start(self):
)
@pytest.mark.asyncio
@pytest.mark.skipif(sys.version_info < (3, 8), reason="AsyncMock requires python 3.8+")
async def test_spawn_instances(kwargs, args, error, monkeypatch, golem_factory):
async def test_spawn_instances(kwargs, args, error, monkeypatch):
def _get_new_engine(self):
return mock.AsyncMock()

monkeypatch.setattr(Golem, "_get_new_engine", _get_new_engine)

with patch("yapapi.services.ServiceRunner.spawn_instance") as spawn_instance:
golem = golem_factory(budget=1)
golem = GolemFactory(budget=1)
try:
await golem.run_service(
service_class=_TestService, payload=Mock(), network=Mock(), **kwargs
Expand Down
9 changes: 5 additions & 4 deletions tests/strategy/test_default_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from unittest.mock import Mock

from tests.factories.golem import GolemFactory
from tests.factories.rest.market import OfferProposalFactory
from yapapi import Golem
from yapapi.props.com import Counter
Expand Down Expand Up @@ -114,25 +115,25 @@ async def _test_strategy(strategy, cpu_price_cap, time_price_cap, fixed_price_ca


@pytest.mark.asyncio
async def test_default_strategy_type(monkeypatch, golem_factory):
async def test_default_strategy_type(monkeypatch):
"""Test if the default strategy is composed of appropriate `MarketStrategy` subclasses."""

monkeypatch.setattr(yapapi.rest, "Configuration", Mock)

golem = golem_factory(budget=1.0)
golem = GolemFactory(budget=1.0)
default_strategy = golem.strategy
assert isinstance(default_strategy, DecreaseScoreForUnconfirmedAgreement)
assert isinstance(default_strategy.base_strategy, LeastExpensiveLinearPayuMS)


@pytest.mark.asyncio
async def test_user_strategy_not_modified(monkeypatch, golem_factory):
async def test_user_strategy_not_modified(monkeypatch):
"""Test that a user strategy is not wrapped in `DecreaseScoreForUnconfirmedAgreement`."""

monkeypatch.setattr(yapapi.rest, "Configuration", Mock)

user_strategy = Mock()
golem = golem_factory(budget=1.0, strategy=user_strategy)
golem = GolemFactory(budget=1.0, strategy=user_strategy)
assert golem.strategy == user_strategy


Expand Down
23 changes: 11 additions & 12 deletions tests/test_payment_platforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from ya_payment import RequestorApi

from tests.factories.golem import GolemFactory
from yapapi import NoPaymentAccountError
from yapapi.config import ApiConfig
from yapapi.engine import DEFAULT_DRIVER, DEFAULT_NETWORK
Expand Down Expand Up @@ -67,18 +68,18 @@ def _mock_create_allocation(monkeypatch):


@pytest.mark.asyncio
async def test_no_accounts_raises(monkeypatch, golem_factory):
async def test_no_accounts_raises(monkeypatch):
"""Test that exception is raised if `Payment.accounts()` returns empty list."""

monkeypatch.setattr(Payment, "accounts", _mock_accounts_iterator())

with pytest.raises(NoPaymentAccountError):
async with golem_factory(budget=10.0):
async with GolemFactory(budget=10.0):
pass


@pytest.mark.asyncio
async def test_no_matching_account_raises(monkeypatch, golem_factory):
async def test_no_matching_account_raises(monkeypatch):
"""Test that exception is raised if `Payment.accounts()` returns no matching accounts."""

monkeypatch.setattr(
Expand All @@ -92,7 +93,7 @@ async def test_no_matching_account_raises(monkeypatch, golem_factory):
)

with pytest.raises(NoPaymentAccountError) as exc_info:
async with golem_factory(
async with GolemFactory(
budget=10.0,
payment_driver="matching-driver",
payment_network="matching-network",
Expand All @@ -105,9 +106,7 @@ async def test_no_matching_account_raises(monkeypatch, golem_factory):


@pytest.mark.asyncio
async def test_matching_account_creates_allocation(
monkeypatch, golem_factory, _mock_decorate_demand
):
async def test_matching_account_creates_allocation(monkeypatch, _mock_decorate_demand):
"""Test that matching accounts are correctly selected and allocations are created for them."""

monkeypatch.setattr(
Expand Down Expand Up @@ -135,7 +134,7 @@ async def mock_release_allocation(*args, **kwargs):
monkeypatch.setattr(RequestorApi, "release_allocation", mock_release_allocation)

with pytest.raises(_StopExecutor):
async with golem_factory(
async with GolemFactory(
budget=10.0, payment_driver="matching-driver", payment_network="matching-network"
):
pass
Expand All @@ -146,13 +145,13 @@ async def mock_release_allocation(*args, **kwargs):


@pytest.mark.asyncio
async def test_driver_network_case_insensitive(monkeypatch, golem_factory, _mock_create_allocation):
async def test_driver_network_case_insensitive(monkeypatch, _mock_create_allocation):
"""Test that matching driver and network names is not case sensitive."""

monkeypatch.setattr(Payment, "accounts", _mock_accounts_iterator(("dRIVER", "NetWORK")))

with pytest.raises(_StopExecutor):
async with golem_factory(
async with GolemFactory(
budget=10.0,
payment_driver="dRiVeR",
payment_network="NeTwOrK",
Expand All @@ -161,13 +160,13 @@ async def test_driver_network_case_insensitive(monkeypatch, golem_factory, _mock


@pytest.mark.asyncio
async def test_default_driver_network(monkeypatch, golem_factory, _mock_create_allocation):
async def test_default_driver_network(monkeypatch, _mock_create_allocation):
"""Test that defaults are used if driver and network are not specified."""

monkeypatch.setattr(
Payment, "accounts", _mock_accounts_iterator((DEFAULT_DRIVER, DEFAULT_NETWORK))
)

with pytest.raises(_StopExecutor):
async with golem_factory(budget=10.0):
async with GolemFactory(budget=10.0):
pass

0 comments on commit 1f4833e

Please sign in to comment.