summaryrefslogtreecommitdiffstats
path: root/test/test_otultra2.py
blob: b421c797d3075166b005e732b1ac8a5eb48fc825 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
"""Tests for the LifeScan OneTouch Ultra 2 driver."""

__author__ = 'Diego Elio Pettenò'
__email__ = 'flameeyes@flameeyes.eu'
__copyright__ = 'Copyright © 2013-2018, Diego Elio Pettenò'
__license__ = 'MIT'

import os
import sys
import unittest
from unittest import mock

from absl.testing import parameterized

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from glucometerutils.drivers import otultra2
from glucometerutils.support import lifescan
from glucometerutils import exceptions

class TestOTUltra2(parameterized.TestCase):

    def test_checksum(self):
        # pylint: disable=protected-access
        checksum = otultra2._calculate_checksum(b'T')
        self.assertEqual(0x0054, checksum)

    def test_checksum_full(self):
        # pylint: disable=protected-access
        checksum = otultra2._calculate_checksum(
            b'T "SAT","08/03/13","22:12:00   "')
        self.assertEqual(0x0608, checksum)

    @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()