Skip to content

Commit

Permalink
#5 Improve tests for 'function_factory()'.
Browse files Browse the repository at this point in the history
  • Loading branch information
OrangeTux committed Nov 9, 2015
1 parent 64ffdd5 commit 01154e3
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from mock import MagicMock

from umodbus.route import Map
from umodbus.exceptions import IllegalDataValueError, IllegalDataAddressError
from umodbus.exceptions import (IllegalFunctionError, IllegalDataValueError,
IllegalDataAddressError)
from umodbus.functions import (function_factory, ReadCoils,
WriteMultipleValueFunction, ReadDiscreteInputs,
ReadInputRegisters, ReadHoldingRegisters,
Expand Down Expand Up @@ -103,30 +104,12 @@ def test_function_factory(pdu, cls):
assert isinstance(function_factory(pdu), cls)


def test_caching_of_function_factory():
""" Equal calls to :meth:`function_factory` should return same response.
def test_function_factory_raising_illegal_function_error():
""" Calling function with PDU containing invalid function code should result
in an IllegalFunctionError.
"""
function_code = 1
starting_address = 100
quantity = 3

pdu = struct.pack('>BHH', function_code, starting_address, quantity)

# Call method twice, both with same input...
function_1 = function_factory(pdu)
function_2 = function_factory(pdu)

# ...output should be the same.
assert id(function_1) == id(function_2)

starting_address = 101
pdu = struct.pack('>BHH', function_code, starting_address, quantity)

# But when called with different pdu...
function_3 = function_factory(pdu)

# ...output should not be the same as previous calls.
assert id(function_1) is not id(function_3)
with pytest.raises(IllegalFunctionError):
function_factory(b'\x00')


class TestReadFunction:
Expand Down

0 comments on commit 01154e3

Please sign in to comment.