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

Allow running tests against physical hardware #7

Closed
wants to merge 3 commits into from
Closed
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
55 changes: 30 additions & 25 deletions clickplc/tests/test_driver.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
"""Test the driver correctly parses a tags file and responds with correct data."""
import asyncio
from unittest import mock

import pytest

from clickplc import command_line
from clickplc.mock import ClickPLC

ADDRESS = 'fakeip'
# from clickplc.driver import ClickPLC
# ADDRESS = '172.16.0.168'

@pytest.fixture
def plc_driver():
"""Confirm the driver correctly initializes without a tags file."""
return ClickPLC('fake ip')

@pytest.fixture(scope="session")
def event_loop():
"""Override the default event_loop fixture (which is function-scoped) to be session-scoped."""
loop = asyncio.new_event_loop()
yield loop
loop.close()

@pytest.fixture
def tagged_driver():
"""Confirm the driver correctly initializes with a good tags file."""
return ClickPLC('fake ip', 'clickplc/tests/plc_tags.csv')

@pytest.fixture(scope='session')
async def plc_driver():
"""Confirm the driver correctly initializes without a tags file."""
async with ClickPLC(ADDRESS) as c:
yield c

@pytest.fixture
def expected_tags():
Expand Down Expand Up @@ -47,7 +54,7 @@ def expected_tags():
@mock.patch('clickplc.ClickPLC', ClickPLC)
def test_driver_cli(capsys):
"""Confirm the commandline interface works without a tags file."""
command_line(['fakeip'])
command_line([ADDRESS])
captured = capsys.readouterr()
assert 'x816' in captured.out
assert 'c100' in captured.out
Expand All @@ -57,33 +64,30 @@ def test_driver_cli(capsys):
@mock.patch('clickplc.ClickPLC', ClickPLC)
def test_driver_cli_tags(capsys):
"""Confirm the commandline interface works with a tags file."""
command_line(['fakeip', 'clickplc/tests/plc_tags.csv'])
command_line([ADDRESS, 'clickplc/tests/plc_tags.csv'])
captured = capsys.readouterr()
assert 'P_101' in captured.out
assert 'VAHH_101_OK' in captured.out
assert 'TI_101' in captured.out
with pytest.raises(SystemExit):
command_line(['fakeip', 'tags', 'bogus'])
command_line([ADDRESS, 'tags', 'bogus'])


def test_get_tags(tagged_driver, expected_tags):
"""Confirm that the driver returns correct values on get() calls."""
assert expected_tags == tagged_driver.get_tags()


def test_unsupported_tags():
@pytest.mark.asyncio
async def test_unsupported_tags():
"""Confirm the driver detects an improper tags file."""
with pytest.raises(TypeError, match='unsupported data type'):
ClickPLC('fake ip', 'clickplc/tests/bad_tags.csv')
ClickPLC(ADDRESS, 'clickplc/tests/bad_tags.csv')


@pytest.mark.asyncio
async def test_tagged_driver(tagged_driver, expected_tags):
async def test_tagged_driver(expected_tags):
"""Test a roundtrip with the driver using a tags file."""
await tagged_driver.set('VAH_101_OK', True)
state = await tagged_driver.get()
assert state.get('VAH_101_OK')
assert expected_tags.keys() == state.keys()
async with ClickPLC(ADDRESS, 'clickplc/tests/plc_tags.csv') as tagged_driver:
await tagged_driver.set('VAH_101_OK', True)
state = await tagged_driver.get()
assert state.get('VAH_101_OK')
assert expected_tags == tagged_driver.get_tags()


@pytest.mark.asyncio
Expand All @@ -108,8 +112,8 @@ async def test_c_roundtrip(plc_driver):
@pytest.mark.asyncio
async def test_df_roundtrip(plc_driver):
"""Confirm df floats are read back correctly after being set."""
await plc_driver.set('df2', 2.0)
await plc_driver.set('df3', [3.0, 4.0])
await plc_driver.set('df1', 0.0)
await plc_driver.set('df2', [2.0, 3.0, 4.0, 0.0])
expected = {'df1': 0.0, 'df2': 2.0, 'df3': 3.0, 'df4': 4.0, 'df5': 0.0}
assert expected == await plc_driver.get('df1-df5')

Expand Down Expand Up @@ -156,6 +160,7 @@ async def test_get_xy_error_handling(plc_driver, prefix):
with pytest.raises(ValueError, match=r'address must be in \[001, 816\].'):
await plc_driver.get(f'{prefix}1-{prefix}1001')


@pytest.mark.asyncio
async def test_set_y_error_handling(plc_driver):
"""Ensure errors are handled for invalid set requests of y registers."""
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ ignore_missing_imports = True

[tool:pytest]
addopts = --cov=clickplc
asyncio_mode = auto