Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bfouts-osaro/46139d71507de64725b03f6852191da4 to your computer and use it in GitHub Desktop.
Save bfouts-osaro/46139d71507de64725b03f6852191da4 to your computer and use it in GitHub Desktop.
gpapi1
import pytest
from odin.hardware.devices.plc import gpapi_plc
class MockRamdisk:
RESULT = None
def __init__(self, *args, **kwargs):
pass
def write(self, value):
self.__class__.RESULT = value
class MockStoredValue:
def __init__(self, **kwargs):
self.ack_job_complete = kwargs.get('ack_job_complete', False)
# Each expected value defined here
def getValues(self, *args, **kwargs):
return [
self.ack_job_complete, False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,
0,
0,
False,False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,
False,False,False,False,False,False,False,False,
False,False, False,
False,False,False,False,False,False,False,False,
False,False,False,False,
0,0,0,
0,0,0,
0,0,0,
0,0,0,
False, False, False,
False,False,False,False,False,False,False,False,
0,
False, False, False, False,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
False, False, False, False,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]
class MockContext:
def __init__(self, *args, **kwargs):
self.store = {
"h": MockStoredValue(*args, **kwargs)
}
class MockBinaryPayloadDecoder:
def __init__(self, read_registers, byteorder, wordorder):
self.values = read_registers
self.byteorder = byteorder
self.wordorder = wordorder
self.position = 0
@classmethod
def fromRegisters(cls, read_registers, byteorder=None, wordorder=None):
return cls(read_registers, byteorder, wordorder)
def decode_bits(self):
position = self.position
self.position += 8
return self.values[position], self.values[position + 1], self.values[position + 2], \
self.values[position + 3], self.values[position + 4], self.values[position + 5], \
self.values[position + 6], self.values[position + 7]
def decode_16bit_int(self):
result = self.values[self.position]
self.position += 1
return result
def decode_16bit_uint(self):
return self.decode_16bit_int()
def skip_bytes(self, byte_count):
self.position += byte_count
gpapi_plc.PickledRamDisk = MockRamdisk
gpapi_plc.BinaryPayloadDecoder = MockBinaryPayloadDecoder
def _get_result(**kwargs):
hlc = gpapi_plc.GPAPIModbusProcess(name="gpapi_tests")
hlc.context = MockContext(**kwargs)
hlc.read()
return MockRamdisk.RESULT
def test_read_job_is_complete():
result = _get_result(ack_job_complete=True)
assert result['ack_job_complete'] == True
def test_read_job_is_not_complete():
result = _get_result(ack_job_complete=False)
assert result['ack_job_complete'] == False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment