Skip to content

Commit

Permalink
Added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfjlaros committed May 29, 2021
1 parent b5fe24f commit 621b723
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/library.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Additionally, the ``with`` statement is supported for easy opening and closing.
>>> interface.ping(10)
The class instance has a public member variable named ``device`` which
contains the device definitions and its exported methods.
contains the device definitions and its exported method definitions.

.. code:: python
Expand Down
4 changes: 2 additions & 2 deletions simple_rpc/simple_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ def _load(self: object, handle: TextIO=None) -> None:
:arg handle: Open file handle.
"""
self.device = load(handle, Loader=FullLoader)
_assert_protocol(self.device['protocol'])
_assert_version(self.device['version'])
_assert_protocol(self.device.get('protocol', ''))
_assert_version(self.device.get('version', (0, 0, 0)))

def is_open(self: object) -> bool:
"""Query interface state."""
Expand Down
26 changes: 26 additions & 0 deletions tests/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
from simple_rpc.simple_rpc import _version


_devices = {
'serial': '/dev/ttyACM0',
'wifi': 'socket://192.168.21.53:1025',
'bt': '/dev/rfcomm0'}
_interface = """
endianness: <
methods:
ping:
doc: Echo a value.
index: 0
name: ping
parameters:
- doc: Value.
fmt: B
name: data
typename: int
return:
doc: Value of data.
fmt: B
typename: int
protocol: simpleRPC
size_t: H
version: !!python/tuple
- 3
- 0
- 0
""".format(''.join(map('- {}\n'.format, _version)))
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def test_device(request: object, device: str) -> None:

def pytest_configure(config: object) -> None:
config.addinivalue_line(
"markers", "test_device(device): test the given device")
'markers', 'test_device(device): test the given device')
42 changes: 39 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from io import StringIO

from pytest import mark
from yaml import FullLoader, load

from simple_rpc.cli import _describe_method, rpc_call, rpc_list
from simple_rpc.extras import json_utf8_decode, json_utf8_encode

from conf import _devices
from conf import _devices, _interface


def test_json_utf8_encode() -> None:
Expand All @@ -27,8 +28,8 @@ def test_describe_method() -> None:
{'name': 'b', 'typename': 'str', 'doc': 'Parameter b.'}],
'return': {
'fmt': b'f', 'typename': 'float', 'doc': 'Return value.'}}) ==
"test a b\n Test.\n\n int a: Parameter a.\n"
" str b: Parameter b.\n\n returns float: Return value.")
'test a b\n Test.\n\n int a: Parameter a.\n'
' str b: Parameter b.\n\n returns float: Return value.')


@mark.test_device('serial')
Expand All @@ -39,9 +40,44 @@ def test_rpc_list() -> None:
assert 'ping data\n Echo a value.\n' in handle.getvalue()


@mark.test_device('serial')
def test_rpc_list_save() -> None:
handle = StringIO()
iface_handle = StringIO()

rpc_list(handle, _devices['serial'], 9600, 1, iface_handle)
iface_handle.seek(0)
device = load(iface_handle, Loader=FullLoader)
assert device['methods']['ping']['doc'] == 'Echo a value.'


@mark.test_device('serial')
def test_rpc_call() -> None:
handle = StringIO()

rpc_call(handle, _devices['serial'], 9600, 1, None, 'ping', ['10'])
assert handle.getvalue() == '10\n'


@mark.test_device('serial')
def test_rpc_call_load() -> None:
handle = StringIO()
iface_handle = StringIO(_interface)

rpc_call(
handle, _devices['serial'], 9600, 1, iface_handle, 'ping', ['10'])
assert handle.getvalue() == '10\n'


@mark.test_device('serial')
def test_rpc_call_load_() -> None:
handle = StringIO()
iface_handle = StringIO(_interface)

try:
rpc_call(
handle, _devices['serial'], 9600, 1, iface_handle, 'inc', ['1'])
except ValueError as error:
assert str(error) == 'invalid method name: inc'
else:
assert False
22 changes: 21 additions & 1 deletion tests/test_device.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from io import StringIO

from pytest import mark
from yaml import FullLoader, load

from simple_rpc import Interface
from simple_rpc.simple_rpc import _version

from conf import _devices
from conf import _devices, _interface


class _TestDevice(object):
Expand Down Expand Up @@ -56,6 +59,14 @@ def test_doc_2(self: object) -> None:
self._interface.device['methods']['ping']['parameters'][0]['doc']
== 'Value.')

def test_save(self: object) -> None:
iface_handle = StringIO()

self._interface.save(iface_handle)
iface_handle.seek(0)
device = load(iface_handle, Loader=FullLoader)
assert device['methods']['ping']['doc'] == 'Echo a value.'

def test_close(self: object) -> None:
assert self._interface.is_open()
assert self._interface.device['methods'] != {}
Expand All @@ -65,6 +76,15 @@ def test_post_close(self: object) -> None:
assert not self._interface.is_open()
assert self._interface.device['methods'] == {}

def test_open_load(self: object) -> None:
iface_handle = StringIO(_interface)

self._interface.open(iface_handle)
assert (
self._interface.device['methods']['ping']['doc'] ==
'Echo a value.')
assert not self._interface.device['methods'].get('inc', None)


@mark.test_device('serial')
class TestSerial(_TestDevice):
Expand Down
41 changes: 41 additions & 0 deletions tests/test_simple_rpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from simple_rpc.simple_rpc import (
SerialInterface, SocketInterface, Interface,
_assert_protocol, _assert_version, _protocol, _version)

from conf import _devices


def test_assert_protocol_pass() -> None:
_assert_protocol(_protocol)


def test_assert_protocol_fail() -> None:
try:
_assert_protocol('')
except ValueError as error:
assert str(error) == 'invalid protocol header'
else:
assert False


def test_assert_version_pass() -> None:
_assert_version(_version)


def test_assert_version_fail() -> None:
try:
_assert_version((0, 0, 0))
except ValueError as error:
assert str(error).startswith('version mismatch')
else:
assert False


def test_SerialInterface() -> None:
interface = Interface(_devices['serial'], autoconnect=False)
assert isinstance(interface, SerialInterface)


def test_SocketInterface() -> None:
interface = Interface(_devices['wifi'], autoconnect=False)
assert isinstance(interface, SocketInterface)

0 comments on commit 621b723

Please sign in to comment.