From 12392e2a7e23388b783a8d51c6b4ca803ef1742f Mon Sep 17 00:00:00 2001 From: Adrian Schlatter <10478149+adrianschlatter@users.noreply.github.com> Date: Fri, 15 Sep 2023 19:32:37 +0200 Subject: [PATCH] Clean-up Tests Migrated "smoke" tests and dropped some of them (without reducing test coverage). work on issue #14 --- setup.cfg | 2 +- tests/test_datamatrix.py | 76 ++++++++++++++++++++++++- tests/test_smoke.py | 117 --------------------------------------- 3 files changed, 75 insertions(+), 120 deletions(-) delete mode 100644 tests/test_smoke.py diff --git a/setup.cfg b/setup.cfg index 2e58115..f2e09c9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -59,7 +59,7 @@ per-file-ignores = test_C40.py: F401 test_text.py: F401 # bare except: We *are* trying to crash it: - test_smoke.py: E722 + test_datamatrix.py: E722 filename = */src/*.py */docs/*.py diff --git a/tests/test_datamatrix.py b/tests/test_datamatrix.py index b0fbd2c..03bf77b 100644 --- a/tests/test_datamatrix.py +++ b/tests/test_datamatrix.py @@ -8,11 +8,43 @@ .. author: Adrian Schlatter """ -import unittest import ppf.datamatrix as put +import unittest +import random +from .common import EDIFACT, ASCII + + +class Test_DataMatrix_Attributes(unittest.TestCase): + """Test DataMatrix attribute acces for unexpected exceptions""" + + def setUp(self): + try: + self.dm = put.DataMatrix(EDIFACT) + except: + self.fail('Exception upon valid instantiation') + + def test_methods(self): + """Run each method and test for exceptions.""" + methods = ['svg', '__repr__', '_repr_svg_'] + + for method in methods: + try: + getattr(self.dm, method)() + except: + self.fail(f'DataMatrix.{method} raises exception') + + def test_properties(self): + """Access each property and test for exceptions.""" + props = ['message', 'matrix'] + + for prop in props: + try: + getattr(self.dm, prop) + except: + self.fail(f'DataMatrix.{prop} raises exception') -class Test_DataMatrix(unittest.TestCase): +class Test_DataMatrix_Invalid_Codec(unittest.TestCase): """Test DataMatrix""" def test_invalid_codec(self): @@ -51,3 +83,43 @@ def test_rect_matrix(self): [1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] self.assertTrue(dm.matrix, truth) + + +class Test_CornerCases(unittest.TestCase): + """Test corner cases such as rarely used branches etc.""" + + def test_corner_B_omit_upper_left_loop_exit(self): + """Test said branch of code to avoid endless loop.""" + msg = ')*+,-./01' + # make sure that this does not go into endless loop: + datamatrix = put.DataMatrix(msg) + self.assertTrue(len(datamatrix.matrix) > 0) + + def test_l_greater_255_blocks(self): + """Test branch l>255 #blocks in DataMatrix.matrix property.""" + datamatrix = put.DataMatrix('A' * 230) + self.assertTrue(len(datamatrix.matrix) > 0) + + def test_long_square_message(self): + """Test very long messages for same behavior as datamatrix-svg.""" + m = put.DataMatrix('~' * 1558).matrix + self.assertTrue(len(m) > 0) + with self.assertRaises(ValueError): + m = put.DataMatrix('~' * 1559).matrix + + def test_long_rect_message(self): + """Test very long rect messages for same behavior as datamatrix-svg.""" + m = put.DataMatrix('~' * 49, rect=True).matrix + self.assertTrue(len(m) < len(m[0])) + + m = put.DataMatrix('~' * 50, rect=True).matrix + self.assertTrue(len(m) == len(m[0])) + + @unittest.skip + def test_random_messages(self): + """Test random messages.""" + while True: + n = random.randint(0, 1024) + msg = ''.join(random.choices(ASCII, k=n)) + datamatrix = put.DataMatrix(msg) + self.assertTrue(len(datamatrix.matrix) > 0) diff --git a/tests/test_smoke.py b/tests/test_smoke.py deleted file mode 100644 index 6689af3..0000000 --- a/tests/test_smoke.py +++ /dev/null @@ -1,117 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Unittests - -.. author: Adrian Schlatter -""" - -import unittest -import random -from .common import ASCII, EDIFACT, BASE256 -import ppf.datamatrix as put - - -class Smoke_Test(object): - """Template class to test for unexpected exceptions.""" - - def setUp(self): - """Implement in Child class to setUp instance of your class.""" - pass - - def test_methods(self): - """Run each method and test for exceptions.""" - methods = ['svg', '__repr__', '_repr_svg_'] - - for method in methods: - try: - getattr(self.dm, method)() - except: - self.fail(f'DataMatrix.{method} raises exception') - - def test_properties(self): - """Access each property and test for exceptions.""" - props = ['message', 'matrix'] - - for prop in props: - try: - getattr(self.dm, prop) - except: - self.fail(f'DataMatrix.{prop} raises exception') - - -class Test_EDIFACT(Smoke_Test, unittest.TestCase): - """Smoke test EDIFACT DataMatrix.""" - - def setUp(self): - try: - self.dm = put.DataMatrix(EDIFACT) - except: - self.fail('Exception upon valid instantiation') - - -class Test_ASCII(Smoke_Test, unittest.TestCase): - """Smoke test ASCII DataMatrix.""" - - def setUp(self): - try: - self.dm = put.DataMatrix(ASCII) - except: - self.fail('Exception upon valid instantiation') - - -@unittest.skip('Not implemented yet') -class Test_BASE256(Smoke_Test, unittest.TestCase): - """Smoke test BASE256 DataMatrix.""" - - def setUp(self): - try: - self.dm = put.DataMatrix(BASE256) - except: - self.fail('Exception upon valid instantiation') - - -class Test_CornerCases(unittest.TestCase): - """Test corner cases such as rarely used branches etc.""" - - def test_corner_B_omit_upper_left_loop_exit(self): - """Test said branch of code to avoid endless loop.""" - msg = ')*+,-./01' - # make sure that this does not go into endless loop: - datamatrix = put.DataMatrix(msg) - self.assertTrue(len(datamatrix.matrix) > 0) - - def test_l_greater_255_blocks(self): - """Test branch l>255 #blocks in DataMatrix.matrix property.""" - datamatrix = put.DataMatrix('A' * 230) - self.assertTrue(len(datamatrix.matrix) > 0) - - def test_long_square_message(self): - """Test very long messages for same behavior as datamatrix-svg.""" - m = put.DataMatrix('~' * 1558).matrix - self.assertTrue(len(m) > 0) - with self.assertRaises(ValueError): - m = put.DataMatrix('~' * 1559).matrix - - def test_long_rect_message(self): - """Test very long rect messages for same behavior as datamatrix-svg.""" - m = put.DataMatrix('~' * 49, rect=True).matrix - self.assertTrue(len(m) < len(m[0])) - - m = put.DataMatrix('~' * 50, rect=True).matrix - self.assertTrue(len(m) == len(m[0])) - - @unittest.skip - def test_random_messages(self): - """Test random messages.""" - while True: - n = random.randint(0, 1024) - msg = ''.join(random.choices(ASCII, k=n)) - datamatrix = put.DataMatrix(msg) - self.assertTrue(len(datamatrix.matrix) > 0) - - -if __name__ == '__main__': - # This enables running the unit tests by running this script which is - # much more convenient than 'python setup.py test' while developing tests. - # Note: package-under-test needs to be in python-path - unittest.main()