diff options
| author | Hugo van Kemenade <hugovk@users.noreply.github.com> | 2019-10-20 12:58:52 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-10-20 12:58:52 +0300 |
| commit | 7879fef65a152f34238a6a415d998e6480e163c4 (patch) | |
| tree | d7795065d14600e927f86fe4fb67edbf592baf73 /tests | |
| parent | 825de0193ba049c6cfd06b32dfa01a4873bfd381 (diff) | |
| parent | b8bff1190e97184f475f7c9834123274599bcca4 (diff) | |
| download | tablib-7879fef65a152f34238a6a415d998e6480e163c4.tar.gz | |
Remove Python 2 code (#410)
Remove Python 2 code
Diffstat (limited to 'tests')
| -rwxr-xr-x | tests/test_tablib.py | 88 | ||||
| -rw-r--r-- | tests/test_tablib_dbfpy_packages_utils.py | 170 |
2 files changed, 255 insertions, 3 deletions
diff --git a/tests/test_tablib.py b/tests/test_tablib.py index 77f2ef6..5464a8d 100755 --- a/tests/test_tablib.py +++ b/tests/test_tablib.py @@ -4,6 +4,7 @@ import datetime import doctest import json +import pickle import unittest from uuid import uuid4 @@ -202,7 +203,7 @@ class TablibTestCase(BaseTestCase): self.assertEqual(self.founders[2:], [self.tom]) def test_row_slicing(self): - """Verify Row's __getslice__ method. Issue #184.""" + """Verify Row slicing. Issue #184.""" john = Row(self.john) @@ -486,6 +487,87 @@ class TablibTestCase(BaseTestCase): self.founders.append(('First\nSecond', 'Name', 42)) self.founders.export('xlsx') + def test_row_repr(self): + """Row repr.""" + # Arrange + john = Row(self.john) + + # Act + output = str(john) + + # Assert + self.assertEqual(output, "['John', 'Adams', 90]") + + def test_row_pickle_unpickle(self): + """Row __setstate__ and __getstate__.""" + # Arrange + before_pickle = Row(self.john) + + # Act + output = pickle.loads(pickle.dumps(before_pickle)) + + # Assert + self.assertEqual(output[0], before_pickle[0]) + self.assertEqual(output[1], before_pickle[1]) + self.assertEqual(output[2], before_pickle[2]) + + def test_row_lpush(self): + """Row lpush.""" + # Arrange + john = Row(self.john) + george = Row(self.george) + + # Act + john.lpush(george) + + # Assert + self.assertEqual(john[-1], george) + + def test_row_append(self): + """Row append.""" + # Arrange + john = Row(self.john) + george = Row(self.george) + + # Act + john.append(george) + + # Assert + self.assertEqual(john[0], george) + + def test_row_contains(self): + """Row __contains__.""" + # Arrange + john = Row(self.john) + + # Act / Assert + self.assertIn("John", john) + + def test_row_no_tag(self): + """Row has_tag.""" + # Arrange + john = Row(self.john) + + # Act / Assert + self.assertFalse(john.has_tag("not found")) + self.assertFalse(john.has_tag(None)) + + def test_row_has_tag(self): + """Row has_tag.""" + # Arrange + john = Row(self.john, tags=["tag1"]) + + # Act / Assert + self.assertTrue(john.has_tag("tag1")) + + def test_row_has_tags(self): + """Row has_tag.""" + # Arrange + john = Row(self.john, tags=["tag1", "tag2"]) + + # Act / Assert + self.assertTrue(john.has_tag(["tag2", "tag1"])) + class HTMLTests(BaseTestCase): def test_html_export(self): @@ -1012,7 +1094,7 @@ class DBFTests(BaseTestCase): for reg_char, data_char in zip(_dbf, data.dbf): so_far += chr(data_char) if reg_char != data_char and index not in [1, 2, 3]: - raise AssertionError('Failing at char %s: %s vs %s %s' % ( + raise AssertionError('Failing at char {}: {} vs {} {}'.format( index, reg_char, data_char, so_far)) index += 1 @@ -1055,7 +1137,7 @@ class DBFTests(BaseTestCase): # found_so_far += chr(data_char) if reg_char != data_char and index not in [1, 2, 3]: raise AssertionError( - 'Failing at char %s: %s vs %s (found %s)' % ( + 'Failing at char {}: {} vs {} (found {})'.format( index, reg_char, data_char, found_so_far)) index += 1 diff --git a/tests/test_tablib_dbfpy_packages_utils.py b/tests/test_tablib_dbfpy_packages_utils.py new file mode 100644 index 0000000..9288916 --- /dev/null +++ b/tests/test_tablib_dbfpy_packages_utils.py @@ -0,0 +1,170 @@ +#!/usr/bin/env python +"""Tests for tablib.packages.dbfpy.""" + +import datetime +import unittest + +from tablib.packages.dbfpy import utils + + +class UtilsUnzfillTestCase(unittest.TestCase): + """dbfpy.utils.unzfill test cases.""" + + def test_unzfill_with_nul(self): + # Arrange + text = b"abc\0xyz" + + # Act + output = utils.unzfill(text) + + # Assert + self.assertEqual(output, b"abc") + + def test_unzfill_without_nul(self): + # Arrange + text = b"abcxyz" + + # Act + output = utils.unzfill(text) + + # Assert + self.assertEqual(output, b"abcxyz") + + +class UtilsGetDateTestCase(unittest.TestCase): + """dbfpy.utils.getDate test cases.""" + + def test_getDate_none(self): + # Arrange + value = None + + # Act + output = utils.getDate(value) + + # Assert + self.assertIsInstance(output, datetime.date) + + def test_getDate_datetime_date(self): + # Arrange + value = datetime.date(2019, 10, 19) + + # Act + output = utils.getDate(value) + + # Assert + self.assertIsInstance(output, datetime.date) + self.assertEqual(output, value) + + def test_getDate_datetime_datetime(self): + # Arrange + value = datetime.datetime(2019, 10, 19, 12, 00, 00) + + # Act + output = utils.getDate(value) + + # Assert + self.assertIsInstance(output, datetime.date) + self.assertEqual(output, value) + + def test_getDate_datetime_timestamp(self): + # Arrange + value = 1571515306 + + # Act + output = utils.getDate(value) + + # Assert + self.assertIsInstance(output, datetime.date) + self.assertEqual(output, datetime.date(2019, 10, 19)) + + def test_getDate_datetime_string_yyyy_mm_dd(self): + # Arrange + value = "20191019" + + # Act + output = utils.getDate(value) + + # Assert + self.assertIsInstance(output, datetime.date) + self.assertEqual(output, datetime.date(2019, 10, 19)) + + def test_getDate_datetime_string_yymmdd(self): + # Arrange + value = "191019" + + # Act + output = utils.getDate(value) + + # Assert + self.assertIsInstance(output, datetime.date) + self.assertEqual(output, datetime.date(2019, 10, 19)) + + +class UtilsGetDateTimeTestCase(unittest.TestCase): + """dbfpy.utils.getDateTime test cases.""" + + def test_getDateTime_none(self): + # Arrange + value = None + + # Act + output = utils.getDateTime(value) + + # Assert + self.assertIsInstance(output, datetime.datetime) + + def test_getDateTime_datetime_datetime(self): + # Arrange + value = datetime.datetime(2019, 10, 19, 12, 00, 00) + + # Act + output = utils.getDateTime(value) + + # Assert + self.assertIsInstance(output, datetime.date) + self.assertEqual(output, value) + + def test_getDateTime_datetime_date(self): + # Arrange + value = datetime.date(2019, 10, 19) + + # Act + output = utils.getDateTime(value) + + # Assert + self.assertIsInstance(output, datetime.date) + self.assertEqual(output, datetime.datetime(2019, 10, 19, 00, 00)) + + def test_getDateTime_datetime_timestamp(self): + # Arrange + value = 1571515306 + + # Act + output = utils.getDateTime(value) + + # Assert + self.assertIsInstance(output, datetime.datetime) + + def test_getDateTime_datetime_string(self): + # Arrange + value = "20191019" + + # Act / Assert + with self.assertRaises(NotImplementedError): + output = utils.getDateTime(value) + + +class InvalidValueTestCase(unittest.TestCase): + """dbfpy.utils._InvalidValue test cases.""" + + def test_sanity(self): + # Arrange + INVALID_VALUE = utils.INVALID_VALUE + + # Act / Assert + self.assertEqual(INVALID_VALUE, INVALID_VALUE) + self.assertNotEqual(INVALID_VALUE, 123) + self.assertEqual(int(INVALID_VALUE), 0) + self.assertEqual(float(INVALID_VALUE), 0.0) + self.assertEqual(str(INVALID_VALUE), "") + self.assertEqual(repr(INVALID_VALUE), "<INVALID>") |
