Skip to content

Commit

Permalink
Add a factory for WorkContext objects in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kmazurek committed Sep 8, 2021
1 parent fbb32b1 commit 53d23eb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
13 changes: 13 additions & 0 deletions tests/factories/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import factory
from unittest import mock

from yapapi.ctx import WorkContext


class WorkContextFactory(factory.Factory):
class Meta:
model = WorkContext

activity = mock.MagicMock()
agreement_details = mock.MagicMock()
storage = mock.AsyncMock()
28 changes: 15 additions & 13 deletions tests/script/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,29 @@
import sys
from unittest import mock

from yapapi import WorkContext
from yapapi.events import CommandExecuted
from yapapi.script import Script
from yapapi.script.command import Deploy, Start

if sys.version_info >= (3, 8):
from tests.factories.context import WorkContextFactory


@pytest.mark.skipif(sys.version_info < (3, 8), reason="AsyncMock requires python 3.8+")
class TestScript:
@pytest.fixture(autouse=True)
def setUp(self):
self._on_download_executed = False

@pytest.fixture
def work_context(self):
return WorkContext(mock.MagicMock(), mock.MagicMock(), storage=mock.AsyncMock())

@staticmethod
def _assert_dst_path(script: Script, dst_path):
batch = script._evaluate()
# transfer_cmd = {'transfer': {'from': 'some/mock/path', 'to': 'container:expected/path'}
transfer_cmd = [cmd for cmd in batch if "transfer" in cmd][0]
assert transfer_cmd["transfer"]["to"] == f"container:{dst_path}"

@staticmethod
def _assert_src_path(script: Script, src_path):
batch = script._evaluate()
# transfer_cmd = {'transfer': {'from': 'container:expected/path', 'to': 'some/mock/path'}
transfer_cmd = [cmd for cmd in batch if "transfer" in cmd][0]
assert transfer_cmd["transfer"]["from"] == f"container:{src_path}"

Expand All @@ -39,7 +35,8 @@ async def _on_download(self, expected, data: bytes):
self._on_download_executed = True

@pytest.mark.asyncio
async def test_send_json(self, work_context: WorkContext):
async def test_send_json(self):
work_context = WorkContextFactory()
storage: mock.AsyncMock = work_context._storage
dst_path = "/test/path"
data = {
Expand All @@ -54,7 +51,8 @@ async def test_send_json(self, work_context: WorkContext):
self._assert_dst_path(script, dst_path)

@pytest.mark.asyncio
async def test_send_bytes(self, work_context: WorkContext):
async def test_send_bytes(self):
work_context = WorkContextFactory()
storage: mock.AsyncMock = work_context._storage
dst_path = "/test/path"
data = b"some byte string"
Expand All @@ -67,7 +65,8 @@ async def test_send_bytes(self, work_context: WorkContext):
self._assert_dst_path(script, dst_path)

@pytest.mark.asyncio
async def test_download_bytes(self, work_context: WorkContext):
async def test_download_bytes(self):
work_context = WorkContextFactory()
expected = b"some byte string"
storage: mock.AsyncMock = work_context._storage
storage.new_destination.return_value.download_bytes.return_value = expected
Expand All @@ -82,7 +81,8 @@ async def test_download_bytes(self, work_context: WorkContext):
assert self._on_download_executed

@pytest.mark.asyncio
async def test_download_json(self, work_context: WorkContext):
async def test_download_json(self):
work_context = WorkContextFactory()
expected = {"key": "val"}
storage: mock.AsyncMock = work_context._storage
storage.new_destination.return_value.download_bytes.return_value = json.dumps(
Expand All @@ -99,7 +99,8 @@ async def test_download_json(self, work_context: WorkContext):
assert self._on_download_executed

@pytest.mark.asyncio
async def test_implicit_init(self, work_context: WorkContext):
async def test_implicit_init(self):
work_context = WorkContextFactory()
script = work_context.new_script()

# first script, should include implicit deploy and start cmds
Expand All @@ -118,7 +119,8 @@ async def test_implicit_init(self, work_context: WorkContext):
assert len(script._commands) == 1

@pytest.mark.asyncio
async def test_cmd_result(self, work_context: WorkContext):
async def test_cmd_result(self):
work_context = WorkContextFactory()
script = work_context.new_script()
future_result = script.run("/some/cmd", 1)

Expand Down
2 changes: 0 additions & 2 deletions tests/test_ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ def _get_work_context(storage=None):
@staticmethod
def _assert_dst_path(script: Script, dst_path):
batch = script._evaluate()
# transfer_cmd = {'transfer': {'from': 'some/mock/path', 'to': 'container:expected/path'}
transfer_cmd = [cmd for cmd in batch if "transfer" in cmd][0]
assert transfer_cmd["transfer"]["to"] == f"container:{dst_path}"

@staticmethod
def _assert_src_path(script: Script, src_path):
batch = script._evaluate()
# transfer_cmd = {'transfer': {'from': 'container:expected/path', 'to': 'some/mock/path'}
transfer_cmd = [cmd for cmd in batch if "transfer" in cmd][0]
assert transfer_cmd["transfer"]["from"] == f"container:{src_path}"

Expand Down

0 comments on commit 53d23eb

Please sign in to comment.