Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/async mock on py38 and asyncio.run on py36 #21

Merged
merged 8 commits into from
Nov 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion activate.bat
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
"%HOME%\.virtualenvs\databay\Scripts\activate.bat"
@echo off
if "%~1"=="" (
"%HOME%\.virtualenvs\databay\Scripts\activate.bat"
)
if "%~1"=="37" (
"%HOME%\.virtualenvs\databay\Scripts\activate.bat"
)
if "%~1"=="38" (
"%HOME%\.virtualenvs\databay38\Scripts\activate.bat"
)
15 changes: 15 additions & 0 deletions databay/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,18 @@ def initialise():
(sys.stdin.encoding == 'windows-1252' or sys.stdout.encoding == 'windows-1252') and \
'windows-1252' not in IGNORE_WARNINGS:
default_logger.warning('stdin or stdout encoder is set to \'windows-1252\'. This may cause errors with data streaming. Fix by setting following environment variables: \n\nPYTHONIOENCODING=utf-8\nPYTHONLEGACYWINDOWSSTDIO=utf-8\n\nSet DATABAY_IGNORE_WARNINGS=\'windows-1252\' to ignore this warning.')

# monkey patch on asyncio.run for Python versions below 3.7.
if sys.version_info[0] >= 3 and sys.version_info[1] <= 6:
def asyncio_run_monkey_patch(task):
try:
loop = asyncio.get_event_loop()
except RuntimeError as e:
if "There is no current event loop" in str(e):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
else:
raise e
return loop.run_until_complete(task)

asyncio.run = asyncio_run_monkey_patch
5 changes: 2 additions & 3 deletions databay/misc/inlet_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import functools
import unittest
from unittest import TestCase
from unittest.mock import patch
from unittest.mock import patch, MagicMock

from asynctest import mock

from databay import Record, Update
from databay.inlets import NullInlet
Expand Down Expand Up @@ -80,7 +79,7 @@ def test_dont_read_metadata(self, update):
Test creating new records and overriding global metadata.
"""

meta = mock.MagicMock()
meta = MagicMock()
meta.keys.return_value = self.gmetadata.keys()
meta.__getitem__.side_effect = lambda x: self.gmetadata.__getitem__(x)

Expand Down
11 changes: 6 additions & 5 deletions test/test/integration/test_link_i.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import logging
import time
from datetime import timedelta, datetime
from unittest import TestCase, mock
from unittest import TestCase
from unittest.mock import MagicMock

from databay.errors import ImplementationError, InvalidNodeError
from databay.inlet import Inlet
Expand Down Expand Up @@ -160,8 +161,8 @@ def test_on_start(self):
def test_on_start_already_active(self):
inlet1 = DummyInlet()
outlet1 = DummyOutlet()
inlet1.on_start = mock.Mock()
outlet1.on_start = mock.Mock()
inlet1.on_start = MagicMock()()
outlet1.on_start = MagicMock()()
inlet1._active = True
outlet1._active = True
link = Link([inlet1], [outlet1], timedelta(seconds=1))
Expand All @@ -188,8 +189,8 @@ def test_on_shutdown(self):
def test_on_shutdown_already_inactive(self):
inlet1 = DummyInlet()
outlet1 = DummyOutlet()
inlet1.on_shutdown = mock.Mock()
outlet1.on_shutdown = mock.Mock()
inlet1.on_shutdown = MagicMock()()
outlet1.on_shutdown = MagicMock()()
link = Link([inlet1], [outlet1], timedelta(seconds=1))

link.on_shutdown()
Expand Down
10 changes: 5 additions & 5 deletions test/test/unit/test_base_planner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from datetime import timedelta
from unittest import TestCase, mock
from unittest.mock import patch
from unittest.mock import patch, MagicMock

import databay
from databay import BasePlanner, Link
Expand All @@ -18,10 +18,10 @@ def __init__(self, *args, **kwargs):
@patch.multiple(BasePlanner, __abstractmethods__=set())
def setUp(self):
self.planner = BasePlanner()
self.planner._schedule = mock.Mock(side_effect=lambda link: link.set_job(object()))
self.planner._unschedule = mock.Mock(side_effect=lambda link: link.set_job(None))
self.planner._start_planner = mock.Mock()
self.planner._shutdown_planner = mock.Mock()
self.planner._schedule = MagicMock(side_effect=lambda link: link.set_job(object()))
self.planner._unschedule = MagicMock(side_effect=lambda link: link.set_job(None))
self.planner._start_planner = MagicMock()
self.planner._shutdown_planner = MagicMock()

@patch(fqname(Link), spec=Link)
def test_add_links(self, link):
Expand Down
74 changes: 72 additions & 2 deletions test/test/unit/test_config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import logging
import os
import sys
import warnings
from unittest import TestCase, mock

from asynctest import patch, MagicMock
from unittest.mock import patch, MagicMock
import asyncio
from importlib import reload

from databay import config
from test_utils import DummyException

_sys_version_info = sys.version_info


async def test_coroutine():
return 123

def no_loop_error():
raise RuntimeError('There is no current event loop')

def generic_error():
raise RuntimeError('Dummy runtime error.')

test_loop = MagicMock()

class TestConfig(TestCase):

Expand All @@ -19,7 +34,10 @@ class TestConfig(TestCase):
@patch('sys.version_info')
def test_event_loop_policy_3_8(self, version_info):
if sys.platform != 'win32':
self.skipTest('Only testable on Windows')
self.skipTest('Only testable on Windows.')
if _sys_version_info[0] >= 3 and _sys_version_info[1] <= 6:
self.skipTest('Only testable on Python 3.7+')

version_info.__getitem__.side_effect = lambda x: [3, 8][x]

# fake a 3.8 default setup
Expand All @@ -33,6 +51,10 @@ def test_event_loop_policy_3_8(self, version_info):
def test_event_loop_policy_3_7(self, version_info):
if sys.platform != 'win32':
self.skipTest('Only testable on Windows')

if _sys_version_info[0] >= 3 and _sys_version_info[1] <= 6:
self.skipTest('Only testable on Python 3.7+')

version_info.__getitem__.side_effect = lambda x: [3, 7][x]

# fake a manual 3.8 default setup
Expand Down Expand Up @@ -87,3 +109,51 @@ def test_windows_1252_incorrect_ignore(self):
config.initialise()
self.assertTrue('stdin or stdout encoder is set to \'windows-1252\'' in ';'.join(cm.output))


@patch('sys.version_info')
@patch('asyncio.get_event_loop', MagicMock(return_value=test_loop))
def test_asyncio_monkey_patch(self, version_info):
version_info.__getitem__.side_effect = lambda x: [3, 6][x]

config.initialise()

with warnings.catch_warnings():
warnings.simplefilter("ignore", category=RuntimeWarning)
coro = test_coroutine()
asyncio.run(coro)

asyncio.get_event_loop.assert_called()
test_loop.run_until_complete.assert_called_with(coro)

@patch('sys.version_info')
@patch('asyncio.get_event_loop', MagicMock(side_effect=no_loop_error))
@patch('asyncio.new_event_loop', MagicMock(return_value=test_loop))
@patch('asyncio.set_event_loop',MagicMock())
def test_asyncio_monkey_patch_no_loop(self, version_info):
version_info.__getitem__.side_effect = lambda x: [3, 6][x]

config.initialise()

with warnings.catch_warnings():
warnings.simplefilter("ignore", category=RuntimeWarning)
coro = test_coroutine()
asyncio.run(coro)

asyncio.get_event_loop.assert_called()
asyncio.new_event_loop.assert_called()
asyncio.set_event_loop.assert_called_with(test_loop)
test_loop.run_until_complete.assert_called_with(coro)

@patch('sys.version_info')
@patch('asyncio.get_event_loop', MagicMock(side_effect=generic_error))
def test_asyncio_monkey_patch_other_error(self, version_info):
version_info.__getitem__.side_effect = lambda x: [3, 6][x]

async def coroutine():
return 123

config.initialise()

with warnings.catch_warnings():
warnings.simplefilter("ignore", category=RuntimeWarning)
self.assertRaises(RuntimeError, asyncio.run, coroutine())
6 changes: 3 additions & 3 deletions test/test/unit/test_inlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import threading
from unittest import TestCase

from asynctest import mock
from unittest.mock import MagicMock

from databay import Inlet, Record

Expand Down Expand Up @@ -163,7 +163,7 @@ def test_try_shutdown_multiple(self):
should indeed prevent it."""
def test_try_start_race_condition(self):
inlet = DummyStartShutdownInlet()
inlet.on_start = mock.MagicMock()
inlet.on_start = MagicMock()

def worker(event):
event.wait()
Expand All @@ -185,7 +185,7 @@ def worker(event):
def test_try_shutdown_race_condition(self):
inlet = DummyStartShutdownInlet()
inlet._active = True
inlet.on_shutdown = mock.MagicMock()
inlet.on_shutdown = MagicMock()

def worker(event):
event.wait()
Expand Down
2 changes: 1 addition & 1 deletion test/test/unit/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def pull_mock(rv=None):
async def pull_coro(_):
return rv

return mock.MagicMock(side_effect=pull_coro)
return MagicMock(side_effect=pull_coro)


class DummyIterable():
Expand Down
6 changes: 3 additions & 3 deletions test/test/unit/test_outlet.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import asyncio
import threading
from unittest import TestCase
from unittest.mock import MagicMock

from asynctest import mock

from databay import Outlet, Record

Expand Down Expand Up @@ -102,7 +102,7 @@ def test_try_shutdown_multiple(self):
should indeed prevent it."""
def test_try_start_race_condition(self):
outlet = DummyStartShutdownOutlet()
outlet.on_start = mock.MagicMock()
outlet.on_start = MagicMock()

def worker(event):
event.wait()
Expand All @@ -124,7 +124,7 @@ def worker(event):
def test_try_shutdown_race_condition(self):
outlet = DummyStartShutdownOutlet()
outlet._active = True
outlet.on_shutdown = mock.MagicMock()
outlet.on_shutdown = MagicMock()

def worker(event):
event.wait()
Expand Down
12 changes: 10 additions & 2 deletions test/test/unit/unit_inlets/test_http_inlet.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import asyncio
import logging
import sys

from asynctest import CoroutineMock, MagicMock, patch
from unittest.mock import patch
if sys.version_info[0] >= 3 and sys.version_info[1] >= 8:
from unittest.mock import AsyncMock, MagicMock
CoroutineMock = AsyncMock
else:
from asynctest import CoroutineMock, MagicMock

from databay import Update
from databay.inlets import HttpInlet
Expand All @@ -16,7 +22,9 @@

def set_response(payload):
response = CoroutineMock(read=CoroutineMock(return_value=payload))
client_mock.get.return_value.__aenter__.return_value = response
get_mock = MagicMock()
get_mock.return_value.__aenter__.return_value = response
client_mock.get = get_mock
client.return_value.__aenter__.return_value = client_mock

@patch('aiohttp.ClientSession', new=client)
Expand Down
7 changes: 4 additions & 3 deletions test/test/unit/unit_outlets/test_csv_outlet.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import asyncio
import copy
import io
import os
from unittest import TestCase

from unittest.mock import patch

from asynctest import TestCase, patch, asyncio

from databay import Record, Update
from databay.outlets import PrintOutlet
from databay.outlets.csv_outlet import CsvOutlet
from test_utils import fqname

Expand Down
4 changes: 3 additions & 1 deletion test/test/unit/unit_outlets/test_file_outlet.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import asyncio
import copy
import io
import os
from unittest import TestCase

from asynctest import TestCase, patch, asyncio
from unittest.mock import patch

from databay import Record, Update
from databay.outlets import PrintOutlet
Expand Down
2 changes: 1 addition & 1 deletion test/test/unit/unit_outlets/test_mongo_outlet.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest import TestCase

import mongomock
from asynctest import patch, MagicMock
from unittest.mock import patch, MagicMock
from mongomock import Collection

from databay import Record, Update
Expand Down
4 changes: 3 additions & 1 deletion test/test/unit/unit_outlets/test_print_outlet.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import asyncio
import io
import unittest
from unittest import TestCase

from asynctest import TestCase, patch, asyncio
from unittest.mock import patch

from databay import Record, Update
from databay.outlets import PrintOutlet
Expand Down