selftests: Add avocado.utils.data_structures unit tests

Couple of avocado.utils.data_structure unit tests.
Signed-off-by: NLukáš Doktor <ldoktor@redhat.com>
上级 1b41a3e8
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()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册