From cfbf51d6a090626accfc8437f5bd586112178a34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Mon, 1 Jan 2018 11:51:23 +0000 Subject: otultraeasy: factor out the construct Timestamp implementation. This adds tests to ensure this works right in the general case, so that it can be used with different parameters. The adapter will be reused in the otverio2015 driver. --- test/test_construct_extras.py | 75 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 test/test_construct_extras.py (limited to 'test') diff --git a/test/test_construct_extras.py b/test/test_construct_extras.py new file mode 100644 index 0000000..faccabf --- /dev/null +++ b/test/test_construct_extras.py @@ -0,0 +1,75 @@ +# -*- coding: utf-8 -*- +"""Tests for the common routines.""" + +__author__ = 'Diego Elio Pettenò' +__email__ = 'flameeyes@flameeyes.eu' +__copyright__ = 'Copyright © 2018, Diego Elio Pettenò' +__license__ = 'MIT' + +import datetime +import os +import sys +import unittest + +import construct + +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +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(unittest.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.FieldError): + 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') + + +if __name__ == '__main__': + unittest.main() -- cgit v1.2.3