Skip to content

Commit

Permalink
Merge pull request #6 from felixn/master
Browse files Browse the repository at this point in the history
[WIP] improve test coverage
  • Loading branch information
christoph2 committed Feb 9, 2019
2 parents ec089bb + 02a437d commit f261a02
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ script:
coverage run --source=pyxcp setup.py test

after_success:
- pylint app
- pylint pyxcp
- coveralls
- coverage xml && python-codacy-coverage -r coverage.xml
- codeclimate-test-reporter
45 changes: 36 additions & 9 deletions pyxcp/tests/test_master.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ class TestMaster(unittest.TestCase):
@mock.patch("pyxcp.transport.Eth")
def testConnect(self, Eth):
tr = Eth()
tr.request.return_value = bytes([0x1d, 0xc0, 0xff, 0xdc, 0x05, 0x01, 0x01])
tr.request.return_value = bytes(
[0x1d, 0xc0, 0xff, 0xdc, 0x05, 0x01, 0x01])
with Master(tr) as xm:
res = xm.connect()
self.assertEqual(res.maxCto, 255)
self.assertEqual(res.maxDto, 1500)
self.assertEqual(res.protocolLayerVersion, 1)
self.assertEqual(res.transportLayerVersion, 1)
self.assertEqual(res.resource.pgm , True)
self.assertEqual(res.resource.stim , True)
self.assertEqual(res.resource.daq , True)
self.assertEqual(res.resource.calpag , True)
self.assertEqual(res.resource.pgm, True)
self.assertEqual(res.resource.stim, True)
self.assertEqual(res.resource.daq, True)
self.assertEqual(res.resource.calpag, True)
self.assertEqual(res.commModeBasic.optional, True)
self.assertEqual(res.commModeBasic.slaveBlockMode, True)
self.assertEqual(res.commModeBasic.addressGranularity, 'BYTE')
Expand Down Expand Up @@ -67,7 +68,8 @@ def testSync(self, Eth):
@mock.patch("pyxcp.transport.Eth")
def testGetCommModeInfo(self, Eth):
tr = Eth()
tr.request.return_value = bytes([0x00, 0x01, 0xff, 0x02, 0x00, 0x00, 0x19])
tr.request.return_value = bytes(
[0x00, 0x01, 0xff, 0x02, 0x00, 0x00, 0x19])
with Master(tr) as xm:
res = xm.getCommModeInfo()
self.assertEqual(res.commModeOptional.interleavedMode, False)
Expand All @@ -80,20 +82,45 @@ def testGetCommModeInfo(self, Eth):
@mock.patch("pyxcp.transport.Eth")
def testGetID(self, Eth):
tr = Eth()
tr.request.return_value = bytes([0x00, 0x01, 0xff, 0x06, 0x00, 0x00, 0x00])
tr.request.return_value = bytes(
[0x00, 0x01, 0xff, 0x06, 0x00, 0x00, 0x00])
with Master(tr) as xm:
gid = xm.getID(0x01)
tr.request.return_value = bytes([0x58, 0x43, 0x50, 0x73, 0x69, 0x6d])
tr.request.return_value = bytes(
[0x58, 0x43, 0x50, 0x73, 0x69, 0x6d])
res = xm.upload(gid.length)
self.assertEqual(gid.mode, 0)
self.assertEqual(gid.reserved, 65281)
self.assertEqual(gid.length, 6)
self.assertEqual(res, b'XCPsim')

@mock.patch('socket.socket')
@mock.patch('selectors.DefaultSelector')
def testDownloadMax(self, mock_selector, mock_socket):
mock_socket.return_value.recv.side_effect = [
[0x01, 0x00, 0xff, 0x06], [0xff]]
mock_selector.return_value.select.side_effect = [[(0, 1)], [(0, 1)]]

tr = transport.Eth('localhost', loglevel="DEBUG")

mock_socket.assert_called()
mock_selector.assert_called()
mock_selector.return_value.register.assert_called()

with Master(tr) as xm:
data = [0xCA, 0xFE, 0xBA, 0xBE]
print("testDownloadMax: {} {}".format(len(data), data))
xm.downloadMax(data)

tr.close()

mock_socket.return_value.send.assert_called_with(bytes(
[0x05, 0x00, 0x00, 0x00, 0xee, 0xca, 0xfe, 0xba, 0xbe]))


def main():
unittest.main()


if __name__ == '__main__':
main()

0 comments on commit f261a02

Please sign in to comment.