From f3a3cf0660ba4d9477434805e5c5d3c2bfa52eb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Thu, 28 Dec 2017 13:25:28 +0000 Subject: tests: fix OneTouch tests to use Python's own mock, and leverage absl-py. Since I've increased the minimum Python version to 3.4, mock is now available within Python. And since I already used absl-py parameterized for test_common, make use of it to avoid boilerplate. --- test/test_otultra2.py | 69 +++++++++++++++--------------------------------- test/test_otultraeasy.py | 10 +++---- 2 files changed, 26 insertions(+), 53 deletions(-) diff --git a/test/test_otultra2.py b/test/test_otultra2.py index 3099604..5713987 100644 --- a/test/test_otultra2.py +++ b/test/test_otultra2.py @@ -3,14 +3,15 @@ __author__ = 'Diego Elio Pettenò' __email__ = 'flameeyes@flameeyes.eu' -__copyright__ = 'Copyright © 2013, Diego Elio Pettenò' +__copyright__ = 'Copyright © 2013-2017, Diego Elio Pettenò' __license__ = 'MIT' import os import sys import unittest +from unittest import mock -import mock +from absl.testing import parameterized sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) @@ -18,61 +19,33 @@ from glucometerutils.drivers import otultra2 from glucometerutils.support import lifescan from glucometerutils import exceptions -class TestOTUltra2(unittest.TestCase): - def setUp(self): - self.addCleanup(mock.patch.stopall) - - mock_serial = mock.patch('serial.Serial').start() - self.mock_readline = mock_serial.return_value.readline - - self.device = otultra2.Device('mockdevice') - - def _set_return_string(self, string): - self.mock_readline.return_value = bytes(string, 'ascii') +class TestOTUltra2(parameterized.TestCase): def test_checksum(self): checksum = otultra2._calculate_checksum(bytes('T', 'ascii')) self.assertEqual(0x0054, checksum) + def test_checksum_full(self): checksum = otultra2._calculate_checksum( bytes('T "SAT","08/03/13","22:12:00 "', 'ascii')) self.assertEqual(0x0608, checksum) - def test_missing_checksum(self): - self._set_return_string('INVALID') - - self.assertRaises(lifescan.MissingChecksum, - self.device.get_serial_number) - - def test_short_response(self): - self._set_return_string('.\r') - - self.assertRaises(exceptions.InvalidResponse, - self.device.get_serial_number) - - def test_invalid_response(self): - self._set_return_string('% 2500\r') - - self.assertRaises(exceptions.InvalidResponse, - self.device.get_serial_number) - - def test_invalid_serial_number(self): - self._set_return_string('@ "12345678O" 0297\r') - - self.assertRaises(lifescan.InvalidSerialNumber, - self.device.get_serial_number) - - def test_invalid_checksum(self): - self._set_return_string('% 1337\r') - - self.assertRaises(exceptions.InvalidChecksum, - self.device.get_serial_number) - - def test_broken_checksum(self): - self._set_return_string('% 13AZ\r') - - self.assertRaises(lifescan.MissingChecksum, - self.device.get_serial_number) + @parameterized.named_parameters( + ('_missing_checksum', b'INVALID', lifescan.MissingChecksum), + ('_short', b'.\r', exceptions.InvalidResponse), + ('_generic', b'% 2500\r', exceptions.InvalidResponse), + ('_invalid_serial_number', b'@ "12345678O" 0297\r', + lifescan.InvalidSerialNumber), + ('_invalid_checksum', b'% 1337\r', exceptions.InvalidChecksum), + ('_broken_checksum', b'% 13AZ\r', lifescan.MissingChecksum), + ) + def test_invalid_response(self, returned_string, expected_exception): + with mock.patch('serial.Serial') as mock_serial: + mock_serial.return_value.readline.return_value = returned_string + + device = otultra2.Device('mockdevice') + with self.assertRaises(expected_exception): + device.get_serial_number() if __name__ == '__main__': unittest.main() diff --git a/test/test_otultraeasy.py b/test/test_otultraeasy.py index 7b91d56..a1d4c02 100644 --- a/test/test_otultraeasy.py +++ b/test/test_otultraeasy.py @@ -3,15 +3,14 @@ __author__ = 'Diego Elio Pettenò' __email__ = 'flameeyes@flameeyes.eu' -__copyright__ = 'Copyright © 2013, Diego Elio Pettenò' +__copyright__ = 'Copyright © 2013-2017, Diego Elio Pettenò' __license__ = 'MIT' import array import os import sys import unittest - -import mock +from unittest import mock sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) @@ -28,17 +27,18 @@ class TestOTUltraMini(unittest.TestCase): self.device = otultraeasy.Device('mockdevice') - def testCrc(self): + def test_crc(self): self.assertEqual( 0x41cd, lifescan.crc_ccitt(b'\x02\x06\x06\x03')) + def test_crc_array(self): cmd_array = array.array('B', b'\x02\x06\x08\x03') self.assertEqual( 0x62C2, lifescan.crc_ccitt(cmd_array)) - def testPacketUpdateChecksum(self): + def test_packet_update_checksum(self): packet = otultraeasy._Packet() packet.build_command('') -- cgit v1.2.3