summaryrefslogtreecommitdiffstats
path: root/test/test_otultra2.py
blob: 6ffea8380ecb28a9518c4d2fd07616c41d40fd48 (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
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
"""Tests for the LifeScan OneTouch Ultra 2 driver."""

__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

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

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 testMissingChecksum(self):
    self.mock_readline.return_value = bytes('INVALID', 'ascii')

    self.assertRaises(lifescan_common.MissingChecksum,
                      self.device.get_serial_number)

  def testShortResponse(self):
    self.mock_readline.return_value = bytes('.\r', 'ascii')

    self.assertRaises(exceptions.InvalidResponse,
                      self.device.get_serial_number)

  def testInvalidResponse(self):
    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" E105\r', 'ascii')

    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__':
    unittest.main()