From f5143327b243fc616f37252d76bd31f2690b088d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Mon, 16 Mar 2020 18:51:27 +0000 Subject: Move tests to be organized within the source directory. This should simplify maintaining the tests in the long run, and allows mypy to more thoroughly check for types. --- glucometerutils/support/tests/__init__.py | 0 .../support/tests/test_construct_extras.py | 72 ++++++++++++++++++++++ glucometerutils/support/tests/test_freestyle.py | 22 +++++++ glucometerutils/support/tests/test_lifescan.py | 20 ++++++ 4 files changed, 114 insertions(+) create mode 100644 glucometerutils/support/tests/__init__.py create mode 100644 glucometerutils/support/tests/test_construct_extras.py create mode 100644 glucometerutils/support/tests/test_freestyle.py create mode 100755 glucometerutils/support/tests/test_lifescan.py (limited to 'glucometerutils/support') diff --git a/glucometerutils/support/tests/__init__.py b/glucometerutils/support/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/glucometerutils/support/tests/test_construct_extras.py b/glucometerutils/support/tests/test_construct_extras.py new file mode 100644 index 0000000..6bba873 --- /dev/null +++ b/glucometerutils/support/tests/test_construct_extras.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- +# +# SPDX-License-Identifier: MIT +"""Tests for the common routines.""" + +# pylint: disable=protected-access,missing-docstring + +import datetime + +import construct + +from absl.testing import absltest +from glucometerutils.support import construct_extras + +_TEST_DATE1 = datetime.datetime(1970, 1, 2, 0, 0) +_TEST_DATE2 = datetime.datetime(1971, 1, 1, 0, 0) +_TEST_DATE3 = datetime.datetime(1970, 1, 1, 0, 0) + +_NEW_EPOCH = 31536000 # datetime.datetime(1971, 1, 1, 0, 0) + + +class TestTimestamp(absltest.TestCase): + def test_build_unix_epoch(self): + self.assertEqual( + construct_extras.Timestamp(construct.Int32ul).build(_TEST_DATE1), + b"\x80\x51\x01\x00", + ) + + def test_parse_unix_epoch(self): + self.assertEqual( + construct_extras.Timestamp(construct.Int32ul).parse(b"\x803\xe1\x01"), + _TEST_DATE2, + ) + + def test_build_custom_epoch(self): + self.assertEqual( + construct_extras.Timestamp(construct.Int32ul, epoch=_NEW_EPOCH).build( + _TEST_DATE2 + ), + b"\x00\x00\x00\x00", + ) + + def test_parse_custom_epoch(self): + self.assertEqual( + construct_extras.Timestamp(construct.Int32ul, epoch=_NEW_EPOCH).parse( + b"\x00\x00\x00\x00" + ), + _TEST_DATE2, + ) + + def test_build_custom_epoch_negative_failure(self): + with self.assertRaises(construct.core.FormatFieldError): + construct_extras.Timestamp(construct.Int32ul, epoch=_NEW_EPOCH).build( + _TEST_DATE1 + ) + + def test_build_custom_epoch_negative_success(self): + self.assertEqual( + construct_extras.Timestamp(construct.Int32sl, epoch=_NEW_EPOCH).build( + _TEST_DATE1 + ), + b"\x00\x1e\x20\xfe", + ) + + def test_build_varint(self): + self.assertEqual( + construct_extras.Timestamp(construct.VarInt).build(_TEST_DATE3), b"\x00" + ) + + def test_invalid_value(self): + with self.assertRaises(AssertionError): + construct_extras.Timestamp(construct.Int32ul).build("foo") diff --git a/glucometerutils/support/tests/test_freestyle.py b/glucometerutils/support/tests/test_freestyle.py new file mode 100644 index 0000000..fb3f3b9 --- /dev/null +++ b/glucometerutils/support/tests/test_freestyle.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# +# SPDX-License-Identifier: MIT +"""Tests for the common FreeStyle functions..""" + +# pylint: disable=protected-access,missing-docstring + +from absl.testing import absltest +from glucometerutils.support import freestyle + + +class TestFreeStyle(absltest.TestCase): + def test_outgoing_command(self): + """Test the generation of a new outgoing message.""" + + self.assertEqual( + b"\0\x17\7command\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" + b"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", + freestyle._FREESTYLE_MESSAGE.build( + {"message_type": 23, "command": b"command"} + ), + ) diff --git a/glucometerutils/support/tests/test_lifescan.py b/glucometerutils/support/tests/test_lifescan.py new file mode 100755 index 0000000..b50b5d6 --- /dev/null +++ b/glucometerutils/support/tests/test_lifescan.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# +# SPDX-License-Identifier: MIT +"""Tests for the LifeScan OneTouch Ultra Mini driver.""" + +# pylint: disable=protected-access,missing-docstring + +import array + +from absl.testing import absltest +from glucometerutils.support import lifescan + + +class TestChecksum(absltest.TestCase): + 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)) -- cgit v1.2.3