From 9ec822d4c084ac9577f871db4af1d3c6dfe7ed1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Doktor?= Date: Mon, 23 May 2016 12:02:34 +0200 Subject: [PATCH] selftests: Add avocado.utils.data_structures unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Couple of avocado.utils.data_structure unit tests. Signed-off-by: Lukáš Doktor --- selftests/unit/test_data_structures.py | 82 ++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 selftests/unit/test_data_structures.py diff --git a/selftests/unit/test_data_structures.py b/selftests/unit/test_data_structures.py new file mode 100644 index 00000000..a6fd30f1 --- /dev/null +++ b/selftests/unit/test_data_structures.py @@ -0,0 +1,82 @@ +import sys +if sys.version_info[:2] == (2, 6): + import unittest2 as unittest +else: + import unittest + +from avocado.utils import data_structures + + +class TestDataStructures(unittest.TestCase): + + """ + Unit tests for avocado.utils.data_structures + """ + + def test_ordered_list_unique(self): + """ + Verify only unique items are there and the order is kept + """ + orig = [1, 5, unittest, 1, 3, 'a', 7, 7, None, 0, unittest] + self.assertEqual(data_structures.ordered_list_unique(orig), + [1, 5, unittest, 3, 'a', 7, None, 0]) + + def test_geometric_mean(self): + """ + Verify the correct value is produced and it allows processing of long + lists of values where some algorithm fails. + """ + self.assertEqual(data_structures.geometric_mean(xrange(1, 180)), + 67.1555819421869) + + def test_lazy_property(self): + """ + Verify the value is initialized lazily with the correct value + """ + class DummyClass(object): + value = False + + @data_structures.LazyProperty + def dummy_method(self): + return not self.value + + item = DummyClass() + self.assertNotIn('dummy_method', item.__dict__) + self.assertEqual(item.dummy_method, True) + self.assertIn('dummy_method', item.__dict__) + + def test_callback_register(self): + """ + Checks CallbackRegister + """ + class Log(object): + msgs = [] + + def error(self, *args, **kwargs): + self.msgs.append((args, kwargs)) + + def ret_arg(arg): + return arg + + log = Log() + register = data_structures.CallbackRegister("MyName", log) + # Register few correct functions + register.register(ret_arg, [True], {}) + register.register(ret_arg, [], {"arg": False}) + # Register few incorrect functions (incorrect number of arguments) + register.register(ret_arg, [True, "incorrect_twice"], {}) + register.register(ret_arg, [True, "incorrect_twice"], {}) + register.register(ret_arg, [True, "incorrect_unique_only"], {}, True) + # Register incorrect function, which is removed before cleanup + register.register(ret_arg, [True, "incorrect_removed"], {}) + register.unregister(ret_arg, [True, "incorrect_removed"], {}) + # Run registered functions and check errors were produced accordingly + register.run() + self.assertEqual(len(log.msgs), 3) + self.assertIn("incorrect_unique_only", str(log.msgs[0])) + self.assertIn("incorrect_twice", str(log.msgs[1])) + self.assertIn("incorrect_twice", str(log.msgs[2])) + + +if __name__ == "__main__": + unittest.main() -- GitLab