Skip to content

Commit

Permalink
Update tests and factories after moving additional data to WantToComp…
Browse files Browse the repository at this point in the history
…uteTask and adding it to TaskToCompute
  • Loading branch information
rwrzesien committed Sep 24, 2018
1 parent ab2db94 commit 8bd4a6d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 31 deletions.
19 changes: 8 additions & 11 deletions golem_messages/factories/tasks.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# pylint: disable=too-few-public-methods,unnecessary-lambda
import calendar
import datetime
import os
import time
import typing

from eth_utils import encode_hex
import factory.fuzzy
import faker

Expand All @@ -27,6 +25,11 @@ class Meta:

node_name = factory.Faker('name')
task_id = factory.Faker('uuid4')
provider_public_key = factory.LazyFunction(
lambda: encode_key_id(cryptography.ECCx(None).raw_pubkey))
provider_ethereum_public_key = factory.SelfAttribute(
'provider_public_key'
)


class CTDBlenderExtraDataFactory(factory.DictFactory):
Expand Down Expand Up @@ -63,19 +66,14 @@ class Meta:

requestor_id = factory.SelfAttribute(
'requestor_public_key')
provider_id = factory.SelfAttribute(
'provider_public_key'
provider_id = factory.LazyAttribute(
lambda task_to_compute: task_to_compute.want_to_compute_task.provider_public_key
)
compute_task_def = factory.SubFactory(ComputeTaskDefFactory)
provider_public_key = factory.LazyFunction(
lambda: encode_key_id(cryptography.ECCx(None).raw_pubkey))
provider_ethereum_public_key = factory.SelfAttribute(
'provider_public_key'
)
requestor_public_key = factory.LazyFunction(
lambda: encode_key_id(cryptography.ECCx(None).raw_pubkey))

compute_task_def = factory.SubFactory(ComputeTaskDefFactory)
want_to_compute_task = factory.SubFactory(WantToComputeTaskFactory)
package_hash = factory.LazyFunction(lambda: 'sha1:' + faker.Faker().sha1())
size = factory.Faker('random_int', min=1 << 20, max=10 << 20)
price = factory.Faker('random_int', min=1 << 20, max=10 << 20)
Expand Down Expand Up @@ -186,7 +184,6 @@ class Meta:
node_name = factory.Faker('name')
address = factory.Faker('ipv4')
port = factory.Faker('pyint')
eth_account = factory.LazyFunction(lambda: encode_hex(os.urandom(20)))
key_id = factory.Faker('binary', length=64)
task_to_compute = factory.SubFactory(TaskToComputeFactory)
package_hash = factory.LazyFunction(lambda: 'sha1:' + faker.Faker().sha1())
Expand Down
12 changes: 11 additions & 1 deletion tests/message/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import unittest.mock as mock
import uuid

from golem_messages import cryptography
from golem_messages import datastructures
from golem_messages import factories
from golem_messages import message
from golem_messages import shortcuts
from golem_messages.utils import encode_hex


class InitializationTestCase(unittest.TestCase):
def test_default_slots(self):
Expand Down Expand Up @@ -51,6 +54,8 @@ def test_message_want_to_compute_task(self):
max_memory_size = random.randint(1, 2**10)
num_cores = random.randint(1, 2**5)
concent_enabled = random.random() > 0.5
provider_public_key = provider_ethereum_public_key = (
encode_hex(cryptography.ECCx(None).raw_pubkey))
msg = message.WantToComputeTask(
node_name=node_id,
task_id=task_id,
Expand All @@ -59,7 +64,10 @@ def test_message_want_to_compute_task(self):
max_resource_size=max_resource_size,
max_memory_size=max_memory_size,
num_cores=num_cores,
concent_enabled=concent_enabled)
concent_enabled=concent_enabled,
provider_public_key=provider_public_key,
provider_ethereum_public_key=provider_ethereum_public_key,
)
expected = [
['node_name', node_id],
['task_id', task_id],
Expand All @@ -70,6 +78,8 @@ def test_message_want_to_compute_task(self):
['price', price],
['concent_enabled', concent_enabled],
['extra_data', None],
['provider_public_key', provider_public_key],
['provider_ethereum_public_key', provider_ethereum_public_key],
]
self.assertEqual(expected, msg.slots())

Expand Down
57 changes: 38 additions & 19 deletions tests/message/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,33 @@ def test_concent_enabled_true(self):
wtct = message.tasks.WantToComputeTask(concent_enabled=True)
self.assertTrue(wtct.concent_enabled)


def test_extra_data(self):
extra_data_content = {'some': 'content'}
wtct = message.tasks.WantToComputeTask(extra_data=extra_data_content)
wtct2 = helpers.dump_and_load(wtct)
self.assertEqual(wtct2.extra_data, extra_data_content)

def test_provider_ethereum_address_checksum(self):
msg = factories.tasks.WantToComputeTaskFactory()
self.assertTrue(msg.provider_ethereum_public_key)
self.assertTrue(is_checksum_address(msg.provider_ethereum_address))

def test_ethereum_address_provider(self):
msg = factories.tasks.WantToComputeTaskFactory()
provider_public_key = decode_hex(msg.provider_ethereum_public_key)

self.assertEqual(msg.provider_ethereum_address,
to_checksum_address(
'0x' + sha3(provider_public_key)[12:].hex()))

def test_ethereum_address(self):
msg = factories.tasks.WantToComputeTaskFactory()
serialized = shortcuts.dump(msg, None, None)
msg_l = shortcuts.load(serialized, None, None)
self.assertEqual(len(msg_l.provider_ethereum_address), 2 + (20*2))


class ComputeTaskDefTestCase(unittest.TestCase):
@mock.patch('golem_messages.message.tasks.ComputeTaskDef.validate_task_id')
def test_task_id_validator(self, v_mock):
Expand Down Expand Up @@ -165,31 +186,32 @@ def test_concent_enabled_none_false(self):
ttc = message.tasks.TaskToCompute(concent_enabled=None)
self.assertFalse(ttc.concent_enabled)

def test_ethereum_address(self):
def test_ethereum_address_requestor(self):
msg = factories.tasks.TaskToComputeFactory()
requestor_public_key = decode_hex(msg.requestor_ethereum_public_key)
serialized = shortcuts.dump(msg, None, None)
msg_l = shortcuts.load(serialized, None, None)
for addr_slot in (
'requestor_ethereum_address',
'provider_ethereum_address'):
address = getattr(msg_l, addr_slot)
self.assertEqual(len(address), 2 + (20*2))
self.assertEqual(len(msg_l.requestor_ethereum_address), 2 + (20*2))
self.assertEqual(msg.requestor_ethereum_address,
to_checksum_address(
'0x' + sha3(requestor_public_key)[12:].hex()))

def test_ethereum_address_provider(self):
msg = factories.tasks.TaskToComputeFactory()
provider_public_key = decode_hex(msg.provider_ethereum_public_key)

serialized = shortcuts.dump(msg, None, None)
msg_l = shortcuts.load(serialized, None, None)
self.assertEqual(len(msg_l.provider_ethereum_address), 2 + (20*2))
self.assertEqual(msg_l.provider_ethereum_address,
msg_l.want_to_compute_task.provider_ethereum_address)
self.assertEqual(msg.provider_ethereum_address,
to_checksum_address(
'0x' + sha3(provider_public_key)[12:].hex()))

def test_ethereum_address_requestor(self):
def test_public_key_provider(self):
msg = factories.tasks.TaskToComputeFactory()
requestor_public_key = decode_hex(msg.requestor_ethereum_public_key)

self.assertEqual(msg.requestor_ethereum_address,
to_checksum_address(
'0x' + sha3(requestor_public_key)[12:].hex()))
self.assertEqual(msg.provider_ethereum_public_key,
msg.want_to_compute_task.provider_ethereum_public_key)

def test_task_id(self):
self.assertEqual(self.msg.task_id,
Expand Down Expand Up @@ -256,11 +278,6 @@ def test_requestor_ethereum_address_checksum(self):
self.assertTrue(ttc.requestor_ethereum_public_key)
self.assertTrue(is_checksum_address(ttc.requestor_ethereum_address))

def test_provider_ethereum_address_checksum(self):
ttc = factories.tasks.TaskToComputeFactory()
self.assertTrue(ttc.provider_ethereum_public_key)
self.assertTrue(is_checksum_address(ttc.provider_ethereum_address))


# pylint:disable=protected-access

Expand Down Expand Up @@ -554,8 +571,10 @@ def setUp(self):

def get_ttc(self, **kwargs):
return factories.tasks.TaskToComputeFactory(
provider_public_key=encode_hex(self.provider_keys.raw_pubkey),
requestor_public_key=encode_hex(self.requestor_keys.raw_pubkey),
want_to_compute_task=factories.tasks.WantToComputeTaskFactory(
provider_public_key=encode_hex(self.provider_keys.raw_pubkey),
),
**kwargs,
)

Expand Down

0 comments on commit 8bd4a6d

Please sign in to comment.