Skip to content
Snippets Groups Projects
Commit 1bb5d61f authored by Hannes Feldt's avatar Hannes Feldt
Browse files

Resolve L2SS-1408: "Fix async tests"

parent b9a56a61
No related branches found
No related tags found
1 merge request!780Resolve L2SS-1408: "Fix async tests"
......@@ -5,7 +5,7 @@
#
# Url to the jumppad download location
jumppad_download="https://git.astron.nl/lofar2.0/tango/-/package_files/37347/download"
jumppad_download="https://git.astron.nl/lofar2.0/tango/-/package_files/40604/download"
if [ -z "$LOFAR20_DIR" ]; then
# We assume we aren't in the PATH, so we can derive our path.
......
......@@ -3,7 +3,6 @@
import unittest
import asynctest
import testscenarios
from tangostationcontrol.common.lofar_logging import configure_logger
......@@ -25,7 +24,9 @@ class IntegrationTestCase(BaseIntegrationTestCase):
super().setUp()
class IntegrationAsyncTestCase(testscenarios.WithScenarios, asynctest.TestCase):
class IntegrationAsyncTestCase(
testscenarios.WithScenarios, unittest.IsolatedAsyncioTestCase
):
"""Integration test case base class for all asyncio unit tests."""
def setUp(self):
......
......@@ -90,7 +90,6 @@ class TestClientServer(base.IntegrationAsyncTestCase):
self.namespace,
5,
self.fault_func,
self.loop,
OPCUAConnectionStatus(),
)
try:
......@@ -106,7 +105,6 @@ class TestClientServer(base.IntegrationAsyncTestCase):
self.namespace,
5,
self.fault_func,
self.loop,
OPCUAConnectionStatus(),
)
try:
......@@ -135,7 +133,6 @@ class TestClientServer(base.IntegrationAsyncTestCase):
self.namespace,
5,
self.fault_func,
self.loop,
OPCUAConnectionStatus(),
)
try:
......@@ -166,7 +163,6 @@ class TestClientServer(base.IntegrationAsyncTestCase):
self.namespace,
5,
self.fault_func,
self.loop,
OPCUAConnectionStatus(),
)
......@@ -185,7 +181,6 @@ class TestClientServer(base.IntegrationAsyncTestCase):
self.namespace,
5,
self.fault_func,
self.loop,
OPCUAConnectionStatus(),
)
try:
......@@ -208,7 +203,6 @@ class TestClientServer(base.IntegrationAsyncTestCase):
self.namespace,
5,
self.fault_func,
self.loop,
OPCUAConnectionStatus(),
)
try:
......@@ -230,7 +224,6 @@ class TestClientServer(base.IntegrationAsyncTestCase):
self.namespace,
5,
self.fault_func,
self.loop,
OPCUAConnectionStatus(),
)
......@@ -251,7 +244,6 @@ class TestClientServer(base.IntegrationAsyncTestCase):
5,
self.fault_func,
OPCUAConnectionStatus(),
self.loop,
)
try:
......
......@@ -3,7 +3,6 @@
# integration process, which may cause wedges in the gate later.
importlib-metadata<2.0.0,>=0.12;python_version<"3.8"
asynctest>=0.13.0 # Apache-2.0
bandit>=1.6.0 # Apache-2.0
virtualenv>=20.16.0 # MIT
build>=0.8.0 # MIT
......
......@@ -7,7 +7,6 @@ from unittest import mock
from typing import Dict
from typing import Union
import asynctest
import testscenarios
from tangostationcontrol.common.lofar_logging import configure_logger
......@@ -41,7 +40,7 @@ class TestCase(BaseTestCase):
super().setUp()
class AsyncTestCase(testscenarios.WithScenarios, asynctest.TestCase):
class AsyncTestCase(testscenarios.WithScenarios, unittest.IsolatedAsyncioTestCase):
"""Test case base class for all asyncio unit tests."""
def setUp(self):
......
......@@ -3,9 +3,10 @@
import asyncio
import io
import unittest
from unittest import mock
from unittest.mock import patch
import asynctest
import asyncua
import numpy
......@@ -15,8 +16,6 @@ from tangostationcontrol.clients.opcua_client import (
OPCUAConnectionStatus,
)
from test import base
class AttrProps:
def __init__(self, numpy_type):
......@@ -44,27 +43,39 @@ IMAGE_SHAPE = (2, 3)
DIMENSION_TESTS = [SCALAR_SHAPE, SPECTRUM_SHAPE, IMAGE_SHAPE]
class TestOPCua(base.AsyncTestCase):
@asynctest.patch.object(OPCUAConnection, "ping")
@asynctest.patch.object(OPCUAConnection, "_servername")
@asynctest.patch.object(opcua_client, "Client")
def create_mocked_coroutine(return_value=None):
if return_value is None:
return_value = mock.Mock()
async def mock_coro(*args, **kwargs):
return return_value
return mock.Mock(wraps=mock_coro)
class TestOPCua(unittest.IsolatedAsyncioTestCase):
@patch.object(OPCUAConnection, "ping")
@patch.object(OPCUAConnection, "_servername")
@patch.object(opcua_client, "Client")
async def test_opcua_connection(self, m_opc_client, m_servername, m_ping):
"""
This tests verifies whether the correct connection steps happen. It checks whether we can init an OPCUAConnection object
Whether we can set the namespace, and the OPCua client.
"""
m_opc_client_members = asynctest.asynctest.CoroutineMock()
m_opc_client_members.get_namespace_index = asynctest.asynctest.CoroutineMock(
m_opc_client_members = create_mocked_coroutine()
m_opc_client_members.get_namespace_index = create_mocked_coroutine(
return_value=42
)
m_opc_client_members.connect = asynctest.asynctest.CoroutineMock()
m_opc_client_members.disconnect = asynctest.asynctest.CoroutineMock()
m_opc_client_members.send_hello = asynctest.asynctest.CoroutineMock()
m_opc_client_members.update_protocol_attributes = (
asynctest.asynctest.CoroutineMock()
)
m_opc_client_members.connect = create_mocked_coroutine()
m_opc_client_members.disconnect = create_mocked_coroutine()
m_opc_client_members.send_hello = create_mocked_coroutine()
m_objects_node = mock.Mock()
m_objects_node.get_child = create_mocked_coroutine()
m_objects_node.get_children_descriptions = create_mocked_coroutine([])
m_opc_client_members.get_objects_node = mock.Mock(return_value=m_objects_node)
m_opc_client_members.update_protocol_attributes = create_mocked_coroutine()
m_opc_client.return_value = m_opc_client_members
test_client = OPCUAConnection(
......@@ -73,7 +84,6 @@ class TestOPCua(base.AsyncTestCase):
5,
mock.Mock(),
OPCUAConnectionStatus(),
self.loop,
)
try:
......@@ -89,10 +99,10 @@ class TestOPCua(base.AsyncTestCase):
finally:
await test_client.stop()
@asynctest.patch.object(OPCUAConnection, "ping")
@asynctest.patch.object(OPCUAConnection, "_servername")
@asynctest.patch.object(opcua_client, "Client")
@asynctest.patch.object(opcua_client, "ProtocolAttribute")
@patch.object(OPCUAConnection, "ping")
@patch.object(OPCUAConnection, "_servername")
@patch.object(opcua_client, "Client")
@patch.object(opcua_client, "ProtocolAttribute")
async def test_opcua_attr_setup(
self, m_protocol_attr, m_opc_client, m_servername, m_ping
):
......@@ -104,22 +114,18 @@ class TestOPCua(base.AsyncTestCase):
Test succeeds if there are no errors.
"""
m_opc_client_members = asynctest.asynctest.CoroutineMock()
m_opc_client_members.get_namespace_index = asynctest.asynctest.CoroutineMock(
m_opc_client_members = create_mocked_coroutine()
m_opc_client_members.get_namespace_index = create_mocked_coroutine(
return_value=2
)
m_opc_client_members.connect = asynctest.asynctest.CoroutineMock()
m_opc_client_members.disconnect = asynctest.asynctest.CoroutineMock()
m_opc_client_members.send_hello = asynctest.asynctest.CoroutineMock()
m_objects_node = asynctest.Mock()
m_objects_node.get_child = asynctest.asynctest.CoroutineMock()
m_objects_node.get_children_descriptions = asynctest.asynctest.CoroutineMock()
m_opc_client_members.get_objects_node = asynctest.Mock(
return_value=m_objects_node
)
m_opc_client_members.update_protocol_attributes = (
asynctest.asynctest.CoroutineMock()
)
m_opc_client_members.connect = create_mocked_coroutine()
m_opc_client_members.disconnect = create_mocked_coroutine()
m_opc_client_members.send_hello = create_mocked_coroutine()
m_objects_node = mock.Mock()
m_objects_node.get_child = create_mocked_coroutine()
m_objects_node.get_children_descriptions = create_mocked_coroutine([])
m_opc_client_members.get_objects_node = mock.Mock(return_value=m_objects_node)
m_opc_client_members.update_protocol_attributes = create_mocked_coroutine()
m_opc_client.return_value = m_opc_client_members
......@@ -153,7 +159,6 @@ class TestOPCua(base.AsyncTestCase):
5,
mock.Mock(),
OPCUAConnectionStatus(),
self.loop,
)
try:
......@@ -240,7 +245,7 @@ class TestOPCua(base.AsyncTestCase):
for i in ATTR_TEST_TYPES:
self._get_test_value(j, i.numpy_type)
m_node = asynctest.asynctest.CoroutineMock()
m_node = mock.Mock()
if len(j) == 1:
test = opcua_client.ProtocolAttribute(
......@@ -274,7 +279,7 @@ class TestOPCua(base.AsyncTestCase):
# test 0-2 dimensions of strings
for dims in range(0, 2):
m_node = asynctest.asynctest.CoroutineMock()
m_node = mock.Mock()
m_node.get_value.return_value = get_unicode_value(dims)
# create the ProtocolAttribute to test
......@@ -372,7 +377,7 @@ class TestOPCua(base.AsyncTestCase):
),
)
m_node = asynctest.asynctest.CoroutineMock()
m_node = mock.Mock()
m_node.set_data_value.return_value = asyncio.Future()
m_node.set_data_value.return_value.set_result(None)
......
......@@ -2,7 +2,7 @@
min_version = 4.3.3
requires =
tox-ignore-env-name-mismatch ~= 0.2.0
envlist = black,pep8,pylint,py310,docs
envlist = black,pep8,pylint,py3{10,11},docs
[testenv]
usedevelop = True
......@@ -10,8 +10,6 @@ package = wheel
wheel_build_env = .pkg
; Python and tox variables are used to access modules and binaries instead of
; directly. This makes the setup robust for using sitepackages=True.
install_command = {envbindir}/pip3 install {opts} {packages}
passenv = HOME
setenv =
PYTHONWARNINGS=default::DeprecationWarning
; Share the same envdir with as many jobs as possible due to extensive time it
......@@ -24,17 +22,12 @@ deps =
allowlist_externals =
{work_dir}/.tox/bin/python
commands_pre =
{work_dir}/.tox/bin/python -m tox --version
{envpython} --version
{work_dir}/.tox/bin/python -m tox --version
commands =
{envpython} -m pytest --version
{envpython} -m pytest -v --log-level=DEBUG --forked test/{posargs}
; We can't detect the current Python version for an environment dynamically
; so each Python version specific job needs its own envdir.
[testenv:py310]
envdir = {toxworkdir}/testenvpy310
[testenv:integration]
allowlist_externals =
{work_dir}/.tox/bin/python
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment