This repository has been archived on 2023-02-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
flamenco-worker/tests/test_coro_mock.py
Sybren A. Stüvel 05408dc8c1 Cleanup: reformat with Black
Reformat the entire project with Black.

No functional changes.
2021-07-09 14:26:13 +02:00

41 lines
1.2 KiB
Python

"""Unit test for our CoroMock implementation."""
import asyncio
import unittest
class CoroMockTest(unittest.TestCase):
def setUp(self):
from flamenco_worker.cli import construct_asyncio_loop
self.loop = construct_asyncio_loop()
def test_setting_return_value(self):
from tests.mock_responses import CoroMock
cm = CoroMock()
cm.coro.return_value = "123"
result = self.loop.run_until_complete(cm(3, 4))
cm.assert_called_once_with(3, 4)
self.assertEqual("123", result)
def test_setting_side_effect(self):
from tests.mock_responses import CoroMock
cm = CoroMock()
cm.coro.side_effect = ["123", "456", IOError("oops")]
self.assertEqual("123", self.loop.run_until_complete(cm(3, 4)))
self.assertEqual("456", self.loop.run_until_complete(cm(3, 4)))
with self.assertRaises(IOError):
self.loop.run_until_complete(cm(3, 4))
# A generator is not allowed to raise StopIteration by itself,
# so the StopIteration caused by side_effect being exhausted
# results in a RuntimeError.
with self.assertRaises(RuntimeError):
self.loop.run_until_complete(cm(3, 4))