Permalink
Browse files

adding more tests

  • Loading branch information...
bashwork committed Sep 7, 2011
1 parent e7b9319 commit 43e6640339c152a2c46ff3b932660c3fc4d14d9c
Showing with 77 additions and 3 deletions.
  1. +1 −1 pymodbus/__init__.py
  2. +1 −1 pymodbus/file_message.py
  3. +12 −1 test/test_diag_messages.py
  4. +63 −0 test/test_file_message.py
View
@@ -34,4 +34,4 @@ def emit(self, record):
try:
True, False
except NameError:
True, False = 1, 0
True, False = (1 == 1), (0 == 1)
View
@@ -361,7 +361,7 @@ def execute(self, context):
return self.doException(merror.IllegalValue)
if not context.validate(self.function_code, self.address, 1):
return self.doException(merror.IllegalAddress)
values = context.getValues(self.function_code, self.address, 1)
values = context.getValues(self.function_code, self.address, 1)[0]
values = ((values & self.and_mask) | self.or_mask)
context.setValues(self.function_code, self.address, [values])
return MaskWriteRegisterResponse(self.address, self.and_mask, self.or_mask)
View
@@ -1,6 +1,7 @@
#!/usr/bin/env python
import unittest
from pymodbus.exceptions import *
from pymodbus.constants import ModbusPlusOperation
from pymodbus.diag_message import *
from pymodbus.diag_message import DiagnosticStatusRequest
from pymodbus.diag_message import DiagnosticStatusResponse
@@ -115,7 +116,7 @@ def testReturnQueryDataResponse(self):
message = ReturnQueryDataResponse(0x0000)
self.assertEqual(message.encode(), '\x00\x00\x00\x00');
def testtRestartCommunicationsOption(self):
def testRestartCommunicationsOption(self):
''' Testing diagnostic message execution '''
request = RestartCommunicationsOptionRequest(True);
self.assertEqual(request.encode(), '\x00\x01\xff\x00')
@@ -127,6 +128,16 @@ def testtRestartCommunicationsOption(self):
response = RestartCommunicationsOptionResponse(False);
self.assertEqual(response.encode(), '\x00\x01\x00\x00')
def testGetClearModbusPlusRequestExecute(self):
''' Testing diagnostic message execution '''
request = GetClearModbusPlusRequest(ModbusPlusOperation.ClearStatistics);
response = request.execute()
self.assertEqual(response.message, None)
request = GetClearModbusPlusRequest(ModbusPlusOperation.GetStatistics);
response = request.execute()
self.assertEqual(response.message, [0x00] * 55)
#---------------------------------------------------------------------------#
# Main
#---------------------------------------------------------------------------#
View
@@ -242,6 +242,69 @@ def testWriteFileRecordResponseRtuFrameSize(self):
size = handle.calculateRtuFrameSize(request)
self.assertEqual(size, 0x0d)
#-----------------------------------------------------------------------#
# Mask Write Register Request
#-----------------------------------------------------------------------#
def testMaskWriteRegisterRequestEncode(self):
''' Test basic bit message encoding/decoding '''
handle = MaskWriteRegisterRequest(0x0000, 0x0101, 0x1010)
result = handle.encode()
self.assertEqual(result, '\x00\x00\x01\x01\x10\x10')
def testMaskWriteRegisterRequestDecode(self):
''' Test basic bit message encoding/decoding '''
request = '\x00\x04\x00\xf2\x00\x25'
handle = MaskWriteRegisterRequest()
handle.decode(request)
self.assertEqual(handle.address, 0x0004)
self.assertEqual(handle.and_mask, 0x00f2)
self.assertEqual(handle.or_mask, 0x0025)
def testMaskWriteRegisterRequestExecute(self):
''' Test write register request valid execution '''
context = MockContext(valid=True, default=0x0000)
handle = MaskWriteRegisterRequest(0x0000, 0x0101, 0x1010)
result = handle.execute(context)
self.assertTrue(isinstance(result, MaskWriteRegisterResponse))
def testMaskWriteRegisterRequestInvalidExecute(self):
''' Test write register request execute with invalid data '''
context = MockContext(valid=False, default=0x0000)
handle = MaskWriteRegisterRequest(0x0000, -1, 0x1010)
result = handle.execute(context)
self.assertEqual(ModbusExceptions.IllegalValue,
result.exception_code)
handle = MaskWriteRegisterRequest(0x0000, 0x0101, -1)
result = handle.execute(context)
self.assertEqual(ModbusExceptions.IllegalValue,
result.exception_code)
handle = MaskWriteRegisterRequest(0x0000, 0x0101, 0x1010)
result = handle.execute(context)
self.assertEqual(ModbusExceptions.IllegalAddress,
result.exception_code)
#-----------------------------------------------------------------------#
# Mask Write Register Response
#-----------------------------------------------------------------------#
def testMaskWriteRegisterResponseEncode(self):
''' Test basic bit message encoding/decoding '''
handle = MaskWriteRegisterResponse(0x0000, 0x0101, 0x1010)
result = handle.encode()
self.assertEqual(result, '\x00\x00\x01\x01\x10\x10')
def testMaskWriteRegisterResponseDecode(self):
''' Test basic bit message encoding/decoding '''
request = '\x00\x04\x00\xf2\x00\x25'
handle = MaskWriteRegisterResponse()
handle.decode(request)
self.assertEqual(handle.address, 0x0004)
self.assertEqual(handle.and_mask, 0x00f2)
self.assertEqual(handle.or_mask, 0x0025)
#---------------------------------------------------------------------------#
# Main
#---------------------------------------------------------------------------#

0 comments on commit 43e6640

Please sign in to comment.