Enable skipped tests from test_models.py

Enable test_modelbase_iteritems and test_modelbase_iter which were
always skipped. These tests need a mapped object instead of
ModelBase instance.

Refactor ModelBaseTest a bit - move creation of ModelBase and
ExtraKeysModel instances to setUp().

Refactor test_modelbase_iteritems and test_modelbase_iter
to check if we can iterate over columns and extra_keys.

Co-Authored-By: Oleksii Chuprykov <ochuprykov@mirantis.com>

Closes-Bug: #1312220

Change-Id: If19137cc81959031764c1b78f990b67a8912a304
This commit is contained in:
Julia Varlamova 2014-05-20 17:17:08 +04:00 committed by Oleksii Chuprykov
parent 44bd28704c
commit 35afdf1746

View File

@ -26,6 +26,10 @@ BASE = declarative_base()
class ModelBaseTest(test_base.DbTestCase): class ModelBaseTest(test_base.DbTestCase):
def setUp(self):
super(ModelBaseTest, self).setUp()
self.mb = models.ModelBase()
self.ekm = ExtraKeysModel()
def test_modelbase_has_dict_methods(self): def test_modelbase_has_dict_methods(self):
dict_methods = ('__getitem__', dict_methods = ('__getitem__',
@ -41,16 +45,14 @@ class ModelBaseTest(test_base.DbTestCase):
self.assertTrue(hasattr(models.ModelBase, method)) self.assertTrue(hasattr(models.ModelBase, method))
def test_modelbase_set(self): def test_modelbase_set(self):
mb = models.ModelBase() self.mb['world'] = 'hello'
mb['world'] = 'hello' self.assertEqual(self.mb['world'], 'hello')
self.assertEqual(mb['world'], 'hello')
def test_modelbase_update(self): def test_modelbase_update(self):
mb = models.ModelBase()
h = {'a': '1', 'b': '2'} h = {'a': '1', 'b': '2'}
mb.update(h) self.mb.update(h)
for key in h.keys(): for key in h.keys():
self.assertEqual(mb[key], h[key]) self.assertEqual(self.mb[key], h[key])
def test_modelbase_contains(self): def test_modelbase_contains(self):
mb = models.ModelBase() mb = models.ModelBase()
@ -63,31 +65,33 @@ class ModelBaseTest(test_base.DbTestCase):
self.assertFalse('non-existent-key' in mb) self.assertFalse('non-existent-key' in mb)
def test_modelbase_iteritems(self): def test_modelbase_iteritems(self):
self.skipTest("Requires DB")
mb = models.ModelBase()
h = {'a': '1', 'b': '2'} h = {'a': '1', 'b': '2'}
mb.update(h) expected = {
for key, value in mb.iteritems(): 'id': None,
self.assertEqual(h[key], value) 'smth': None,
'name': 'NAME',
'a': '1',
'b': '2',
}
self.ekm.update(h)
self.assertEqual(dict(self.ekm.iteritems()), expected)
def test_modelbase_iter(self): def test_modelbase_iter(self):
self.skipTest("Requires DB") expected = {
mb = models.ModelBase() 'id': None,
h = {'a': '1', 'b': '2'} 'smth': None,
mb.update(h) 'name': 'NAME',
i = iter(mb) }
i = iter(self.ekm)
min_items = len(h)
found_items = 0 found_items = 0
while True: while True:
r = next(i, None) r = next(i, None)
if r is None: if r is None:
break break
self.assertEqual(expected[r[0]], r[1])
self.assertTrue(r in h)
found_items += 1 found_items += 1
self.assertEqual(min_items, found_items) self.assertEqual(len(expected), found_items)
def test_modelbase_several_iters(self): def test_modelbase_several_iters(self):
mb = ExtraKeysModel() mb = ExtraKeysModel()
@ -100,17 +104,14 @@ class ModelBaseTest(test_base.DbTestCase):
def test_extra_keys_empty(self): def test_extra_keys_empty(self):
"""Test verifies that by default extra_keys return empty list.""" """Test verifies that by default extra_keys return empty list."""
mb = models.ModelBase() self.assertEqual(self.mb._extra_keys, [])
self.assertEqual(mb._extra_keys, [])
def test_extra_keys_defined(self): def test_extra_keys_defined(self):
"""Property _extra_keys will return list with attributes names.""" """Property _extra_keys will return list with attributes names."""
ekm = ExtraKeysModel() self.assertEqual(self.ekm._extra_keys, ['name'])
self.assertEqual(ekm._extra_keys, ['name'])
def test_model_with_extra_keys(self): def test_model_with_extra_keys(self):
item = ExtraKeysModel() data = dict(self.ekm)
data = dict(item)
self.assertEqual(data, {'smth': None, self.assertEqual(data, {'smth': None,
'id': None, 'id': None,
'name': 'NAME'}) 'name': 'NAME'})