From f7fde8b5a659b5b7de7d00cddb28c20d3c691d37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Sat, 3 Aug 2013 21:28:43 +0100 Subject: Add support for checksum verification for the otultra2 driver. This introduced some changes in the Exception class to simplify the code, and at the same time it adds a module for multiple lifescan drivers to share code (multiple OneTouch protocols share the same checksum for instance). --- test/test_lifescan.py | 29 +++++++++++++++++++++++++++++ test/test_otultra2.py | 23 +++++++++++++++++++---- 2 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 test/test_lifescan.py (limited to 'test') diff --git a/test/test_lifescan.py b/test/test_lifescan.py new file mode 100644 index 0000000..774567d --- /dev/null +++ b/test/test_lifescan.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +"""Tests for the LifeScan Common functions.""" + +__author__ = 'Diego Elio Pettenò' +__email__ = 'flameeyes@flameeyes.eu' +__copyright__ = 'Copyright © 2013, Diego Elio Pettenò' +__license__ = 'GPL v3 or later' + +import os +import sys +import unittest + +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +from glucometerutils import common +from glucometerutils.drivers import lifescan_common +from glucometerutils import exceptions + + +class TestOTUltra2(unittest.TestCase): + def testChecksum(self): + checksum = lifescan_common.calculate_checksum(bytes('T', 'ascii')) + self.assertEqual(0x5400, checksum) + + checksum = lifescan_common.calculate_checksum(bytes('TestString', 'ascii')) + self.assertEqual(0x0643, checksum) + +if __name__ == '__main__': + unittest.main() diff --git a/test/test_otultra2.py b/test/test_otultra2.py index ba577c3..6ffea83 100644 --- a/test/test_otultra2.py +++ b/test/test_otultra2.py @@ -15,6 +15,7 @@ import mock sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from glucometerutils import common +from glucometerutils.drivers import lifescan_common from glucometerutils.drivers import otultra2 from glucometerutils import exceptions @@ -30,7 +31,7 @@ class TestOTUltra2(unittest.TestCase): def testMissingChecksum(self): self.mock_readline.return_value = bytes('INVALID', 'ascii') - self.assertRaises(otultra2.MissingChecksum, + self.assertRaises(lifescan_common.MissingChecksum, self.device.get_serial_number) def testShortResponse(self): @@ -40,16 +41,30 @@ class TestOTUltra2(unittest.TestCase): self.device.get_serial_number) def testInvalidResponse(self): - self.mock_readline.return_value = bytes('% 1337\r', 'ascii') + self.mock_readline.return_value = bytes('% 2500\r', 'ascii') self.assertRaises(exceptions.InvalidResponse, self.device.get_serial_number) def testInvalidSerialNumber(self): self.mock_readline.return_value = bytes( - '@ "12345678O" 1337\r', 'ascii') + '@ "12345678O" E105\r', 'ascii') - self.assertRaises(otultra2.InvalidSerialNumber, + self.assertRaises(lifescan_common.InvalidSerialNumber, + self.device.get_serial_number) + + def testInvalidChecksum(self): + self.mock_readline.return_value = bytes( + '% 1337\r', 'ascii') + + self.assertRaises(lifescan_common.InvalidChecksum, + self.device.get_serial_number) + + def testBrokenChecksum(self): + self.mock_readline.return_value = bytes( + '% 13AZ\r', 'ascii') + + self.assertRaises(lifescan_common.MissingChecksum, self.device.get_serial_number) if __name__ == '__main__': -- cgit v1.2.3