Skip to content

Commit

Permalink
reputation unit tests (in progress...)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadeofblue committed Mar 19, 2024
1 parent 3e319e8 commit fdb26c4
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ gvmkit-build = "^0.3.13"
dpath = "^2.1.6"
yamlpath = "^3.8.1"
pytest = "^7.4.3"
pytest-asyncio = "^0.23.5.post1"
factory-boy = "^3.3.0"

[build-system]
requires = ["poetry-core"]
Expand Down
Empty file added tests/factories/__init__.py
Empty file.
Empty file.
Empty file.
18 changes: 18 additions & 0 deletions tests/factories/golem/resources/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from unittest import mock

import factory


class ResourceFactory(factory.Factory):
node = factory.LazyFunction(mock.AsyncMock)
id_ = factory.Faker("pystr")

@classmethod
def _create(cls, model_class, *args, **kwargs):
def _mock_call(cls, *args, **_kwargs):
obj = cls.__new__(cls)
obj.__init__(*args, **kwargs)
return obj

with mock.patch("golem.resources.base.ResourceMeta.__call__", new=_mock_call):
return model_class(*args, **kwargs)
8 changes: 8 additions & 0 deletions tests/factories/golem/resources/demand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from golem.resources import Demand

from tests.factories.golem.resources.base import ResourceFactory


class DemandFactory(ResourceFactory):
class Meta:
model = Demand
12 changes: 12 additions & 0 deletions tests/factories/golem/resources/payload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import factory
from golem.payload import Constraints, Properties


class PropertiesFactory(factory.Factory):
class Meta:
model = Properties


class ConstraintsFactory(factory.Factory):
class Meta:
model = Constraints
48 changes: 48 additions & 0 deletions tests/factories/golem/resources/proposal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from datetime import datetime, timezone
from functools import partial
from typing import Union

import factory
from golem.resources import Demand, Proposal, ProposalData

from tests.factories.golem.resources.base import ResourceFactory
from tests.factories.golem.resources.demand import DemandFactory
from tests.factories.golem.resources.payload import ConstraintsFactory, PropertiesFactory


class ProposalDataFactory(factory.Factory):
class Meta:
model = ProposalData

properties = factory.SubFactory(PropertiesFactory)
constraints = factory.SubFactory(ConstraintsFactory)
proposal_id = factory.Faker("pystr")
issuer_id = factory.Faker("pystr")
state = "Initial"
timestamp = factory.LazyFunction(partial(datetime.now, timezone.utc))
prev_proposal_id = None


class ProposalFactory(ResourceFactory):
class Meta:
model = Proposal

data = factory.SubFactory(ProposalDataFactory)

@factory.post_generation
def demand(obj: Proposal, create, extracted: Demand, **kwargs): # noqa
"""Set a demand on the proposal."""
if extracted:
obj.demand = extracted
elif kwargs:
obj.demand = DemandFactory(**kwargs)

@factory.post_generation
def parent(obj: Proposal, create, extracted: Union[Demand, Proposal], **kwargs): # noqa
"""Set a parent on the proposal."""
if extracted:
extracted.add_child(obj)

@classmethod
def initial(cls, *args, **kwargs):
return cls(*args, **kwargs, parent=DemandFactory())
40 changes: 40 additions & 0 deletions tests/unit/reputation/test_plugins_blacklist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from unittest import mock

import pytest

from ray_on_golem.reputation.plugins import ProviderBlacklistPlugin
from tests.factories.golem.resources.proposal import ProposalFactory


class _ProposalGeneratorException(Exception):
...


class _ProposalGenerator:
def __init__(self, count=1):
self.total_count = count
self.count = 0
self.proposals = list()

async def _get_proposal(self):
if self.count < self.total_count:
self.count += 1
proposal = ProposalFactory.initial()
self.proposals.append(proposal)
return proposal
raise _ProposalGeneratorException()


@pytest.mark.asyncio
async def test_blacklist():
with mock.patch(
"ray_on_golem.reputation.models.NodeReputation.check_blacklisted",
mock.AsyncMock(return_value=False),
):
with mock.patch("golem.resources.Proposal.reject") as reject_mock:
blacklist = ProviderBlacklistPlugin("some-network")
blacklist.set_proposal_callback(_ProposalGenerator()._get_proposal)

proposal = await blacklist.get_proposal()

print(proposal)

0 comments on commit fdb26c4

Please sign in to comment.