summaryrefslogtreecommitdiffstats
path: root/test/test_otultraeasy.py
blob: 326aeea4705b85c2467bd55e063f99bb26fdaed4 (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
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
"""Tests for the LifeScan OneTouch Ultra Mini driver."""

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

import array
import os
import sys
import unittest

import mock

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

from glucometerutils.drivers import lifescan_common
from glucometerutils.drivers import otultraeasy
from glucometerutils import exceptions

class TestOTUltraMini(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 = otultraeasy.Device('mockdevice')

    def testCrc(self):
        self.assertEqual(
            0x41cd,
            otultraeasy._Packet._crc(b'\x02\x06\x06\x03'))

        cmd_array = array.array('B', b'\x02\x06\x08\x03')
        self.assertEqual(
            0x62C2,
            otultraeasy._Packet._crc(cmd_array))

    def testPacketUpdateChecksum(self):
        packet = otultraeasy._Packet()

        packet.build_command('')
        packet.disconnect = True

        packet.update_checksum()
        self.assertEqual(
            b'\x02\x06\x08\x03\xC2\x62',
            packet.tobytes())

        packet.validate_checksum()
        packet.disconnect = False

        with self.assertRaises(lifescan_common.InvalidChecksum):
            packet.validate_checksum()


if __name__ == '__main__':
    unittest.main()