Skip to content

Commit 621b723

Browse files
committed
Added tests.
1 parent b5fe24f commit 621b723

File tree

7 files changed

+131
-8
lines changed

7 files changed

+131
-8
lines changed

docs/library.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Additionally, the ``with`` statement is supported for easy opening and closing.
138138
>>> interface.ping(10)
139139
140140
The class instance has a public member variable named ``device`` which
141-
contains the device definitions and its exported methods.
141+
contains the device definitions and its exported method definitions.
142142

143143
.. code:: python
144144

simple_rpc/simple_rpc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ def _load(self: object, handle: TextIO=None) -> None:
129129
:arg handle: Open file handle.
130130
"""
131131
self.device = load(handle, Loader=FullLoader)
132-
_assert_protocol(self.device['protocol'])
133-
_assert_version(self.device['version'])
132+
_assert_protocol(self.device.get('protocol', ''))
133+
_assert_version(self.device.get('version', (0, 0, 0)))
134134

135135
def is_open(self: object) -> bool:
136136
"""Query interface state."""

tests/conf.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
1+
from simple_rpc.simple_rpc import _version
2+
3+
14
_devices = {
25
'serial': '/dev/ttyACM0',
36
'wifi': 'socket://192.168.21.53:1025',
47
'bt': '/dev/rfcomm0'}
8+
_interface = """
9+
endianness: <
10+
methods:
11+
ping:
12+
doc: Echo a value.
13+
index: 0
14+
name: ping
15+
parameters:
16+
- doc: Value.
17+
fmt: B
18+
name: data
19+
typename: int
20+
return:
21+
doc: Value of data.
22+
fmt: B
23+
typename: int
24+
protocol: simpleRPC
25+
size_t: H
26+
version: !!python/tuple
27+
- 3
28+
- 0
29+
- 0
30+
""".format(''.join(map('- {}\n'.format, _version)))

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ def test_device(request: object, device: str) -> None:
1919

2020
def pytest_configure(config: object) -> None:
2121
config.addinivalue_line(
22-
"markers", "test_device(device): test the given device")
22+
'markers', 'test_device(device): test the given device')

tests/test_cli.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from io import StringIO
22

33
from pytest import mark
4+
from yaml import FullLoader, load
45

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

8-
from conf import _devices
9+
from conf import _devices, _interface
910

1011

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

3334

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

4142

43+
@mark.test_device('serial')
44+
def test_rpc_list_save() -> None:
45+
handle = StringIO()
46+
iface_handle = StringIO()
47+
48+
rpc_list(handle, _devices['serial'], 9600, 1, iface_handle)
49+
iface_handle.seek(0)
50+
device = load(iface_handle, Loader=FullLoader)
51+
assert device['methods']['ping']['doc'] == 'Echo a value.'
52+
53+
4254
@mark.test_device('serial')
4355
def test_rpc_call() -> None:
4456
handle = StringIO()
4557

4658
rpc_call(handle, _devices['serial'], 9600, 1, None, 'ping', ['10'])
4759
assert handle.getvalue() == '10\n'
60+
61+
62+
@mark.test_device('serial')
63+
def test_rpc_call_load() -> None:
64+
handle = StringIO()
65+
iface_handle = StringIO(_interface)
66+
67+
rpc_call(
68+
handle, _devices['serial'], 9600, 1, iface_handle, 'ping', ['10'])
69+
assert handle.getvalue() == '10\n'
70+
71+
72+
@mark.test_device('serial')
73+
def test_rpc_call_load_() -> None:
74+
handle = StringIO()
75+
iface_handle = StringIO(_interface)
76+
77+
try:
78+
rpc_call(
79+
handle, _devices['serial'], 9600, 1, iface_handle, 'inc', ['1'])
80+
except ValueError as error:
81+
assert str(error) == 'invalid method name: inc'
82+
else:
83+
assert False

tests/test_device.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
from io import StringIO
2+
13
from pytest import mark
4+
from yaml import FullLoader, load
25

36
from simple_rpc import Interface
47
from simple_rpc.simple_rpc import _version
58

6-
from conf import _devices
9+
from conf import _devices, _interface
710

811

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

62+
def test_save(self: object) -> None:
63+
iface_handle = StringIO()
64+
65+
self._interface.save(iface_handle)
66+
iface_handle.seek(0)
67+
device = load(iface_handle, Loader=FullLoader)
68+
assert device['methods']['ping']['doc'] == 'Echo a value.'
69+
5970
def test_close(self: object) -> None:
6071
assert self._interface.is_open()
6172
assert self._interface.device['methods'] != {}
@@ -65,6 +76,15 @@ def test_post_close(self: object) -> None:
6576
assert not self._interface.is_open()
6677
assert self._interface.device['methods'] == {}
6778

79+
def test_open_load(self: object) -> None:
80+
iface_handle = StringIO(_interface)
81+
82+
self._interface.open(iface_handle)
83+
assert (
84+
self._interface.device['methods']['ping']['doc'] ==
85+
'Echo a value.')
86+
assert not self._interface.device['methods'].get('inc', None)
87+
6888

6989
@mark.test_device('serial')
7090
class TestSerial(_TestDevice):

tests/test_simple_rpc.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from simple_rpc.simple_rpc import (
2+
SerialInterface, SocketInterface, Interface,
3+
_assert_protocol, _assert_version, _protocol, _version)
4+
5+
from conf import _devices
6+
7+
8+
def test_assert_protocol_pass() -> None:
9+
_assert_protocol(_protocol)
10+
11+
12+
def test_assert_protocol_fail() -> None:
13+
try:
14+
_assert_protocol('')
15+
except ValueError as error:
16+
assert str(error) == 'invalid protocol header'
17+
else:
18+
assert False
19+
20+
21+
def test_assert_version_pass() -> None:
22+
_assert_version(_version)
23+
24+
25+
def test_assert_version_fail() -> None:
26+
try:
27+
_assert_version((0, 0, 0))
28+
except ValueError as error:
29+
assert str(error).startswith('version mismatch')
30+
else:
31+
assert False
32+
33+
34+
def test_SerialInterface() -> None:
35+
interface = Interface(_devices['serial'], autoconnect=False)
36+
assert isinstance(interface, SerialInterface)
37+
38+
39+
def test_SocketInterface() -> None:
40+
interface = Interface(_devices['wifi'], autoconnect=False)
41+
assert isinstance(interface, SocketInterface)

0 commit comments

Comments
 (0)