Skip to content

Commit

Permalink
#28 Test with both signed and unsigned values.
Browse files Browse the repository at this point in the history
  • Loading branch information
OrangeTux committed Jan 22, 2016
1 parent 70606fb commit 3ae2142
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
27 changes: 20 additions & 7 deletions tests/system/responses/test_succesful_responses.py
@@ -1,8 +1,21 @@
import pytest

from umodbus import conf
from umodbus.client import tcp


@pytest.fixture(scope='module', autouse=True)
def enable_signed_values(request):
""" Use signed values when running tests it this module. """
tmp = conf.SIGNED_VALUES
conf.SIGNED_VALUES = True

def fin():
conf.SIGNED_VALUES = tmp

request.addfinalizer(fin)


@pytest.mark.parametrize('function', [
tcp.read_coils,
tcp.read_discrete_inputs,
Expand All @@ -28,21 +41,21 @@ def test_response_on_multi_bit_value_read_requests(sock, function):
slave_id, starting_address, quantity = (1, 0, 10)
req_adu = function(slave_id, starting_address, quantity)

assert tcp.send_message(req_adu, sock) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
assert tcp.send_message(req_adu, sock) == [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]


@pytest.mark.parametrize('function', [
tcp.write_single_coil,
tcp.write_single_register,
@pytest.mark.parametrize('function, value', [
(tcp.write_single_coil, 0),
(tcp.write_single_register, -1337),
])
def test_response_single_value_write_request(sock, function):
def test_response_single_value_write_request(sock, function, value):
""" Validate responde of succesful Read Single Coil and Read Single
Register request.
"""
slave_id, starting_address, quantity = (1, 0, 0)
slave_id, starting_address, quantity = (1, 0, value)
req_adu = function(slave_id, starting_address, quantity)

assert tcp.send_message(req_adu, sock) == 0
assert tcp.send_message(req_adu, sock) == value


@pytest.mark.parametrize('function, values', [
Expand Down
6 changes: 4 additions & 2 deletions tests/system/server.py
Expand Up @@ -3,7 +3,9 @@
except ImportError:
from SocketServer import TCPServer

from umodbus import get_server, RequestHandler
from umodbus import get_server, RequestHandler, conf

conf.SIGNED_VALUES = True

app = get_server(TCPServer, ('localhost', 0), RequestHandler)

Expand All @@ -15,7 +17,7 @@ def read_status(slave_id, address):

@app.route(slave_ids=[1], function_codes=[3, 4], addresses=list(range(0, 10)))
def read_register(slave_id, address):
return address
return -address


@app.route(slave_ids=[1], function_codes=[5, 15], addresses=list(range(0, 10)))
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/conftest.py
@@ -1,8 +1,21 @@
import pytest

from umodbus import conf
from umodbus.config import Config


@pytest.fixture(scope='module', autouse=True)
def enable_signed_values(request):
""" Use signed values when running tests it this module. """
tmp = conf.SIGNED_VALUES
conf.SIGNED_VALUES = False

def fin():
conf.SIGNED_VALUES = tmp

request.addfinalizer(fin)


@pytest.fixture
def config():
return Config()
14 changes: 10 additions & 4 deletions umodbus/_functions/__init__.py
Expand Up @@ -744,12 +744,18 @@ def value(self):

@value.setter
def value(self, value):
""" Data must be value between and including 0 and 0xFFFF. """
if 0 <= value <= 0xFFFF:
self._value = value
else:
""" Value to be written on register.
:param value: An integer.
:raises: IllegalDataValueError when value isn't in range.
"""
try:
struct.pack('>' + conf.TYPE_CHAR, value)
except struct.error:
raise IllegalDataValueError

self._value = value

@property
def request_pdu(self):
""" Build request PDU to write single register.
Expand Down

0 comments on commit 3ae2142

Please sign in to comment.