-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reputation unit tests (in progress...)
- Loading branch information
1 parent
3e319e8
commit fdb26c4
Showing
9 changed files
with
128 additions
and
0 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
Empty file.
Empty file.
Empty file.
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 |
---|---|---|
@@ -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) |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from golem.resources import Demand | ||
|
||
from tests.factories.golem.resources.base import ResourceFactory | ||
|
||
|
||
class DemandFactory(ResourceFactory): | ||
class Meta: | ||
model = Demand |
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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()) |
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 |
---|---|---|
@@ -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) |