Skip to content

Commit

Permalink
Fix exception raised when the wrong tags are provided
Browse files Browse the repository at this point in the history
Change-Id: Ib6e20d128728e532b31dd44cd98046270bcb5635
  • Loading branch information
lavagetto committed Aug 8, 2018
1 parent d265424 commit 6e360f0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
12 changes: 5 additions & 7 deletions conftool/kvobject.py
Expand Up @@ -227,10 +227,8 @@ class Entity(KVObject):
def __init__(self, *tags):
if len(tags) != (len(self._tags) + 1):
raise ValueError(
"Need %s as tags, %s provided",
",".join(self._tags),
",".join(tags[:-1]))

"Need %s as tags, %s provided" % (",".join(self._tags), ",".join(tags[:-1]))
)
self._name = tags[-1]
self._key = self.kvpath(*tags)
self._current_tags = {}
Expand All @@ -254,9 +252,9 @@ def pprint(self):
@classmethod
def dir(cls, *tags):
if len(tags) != len(cls._tags):
raise ValueError("Need %s as tags, %s provided",
",".join(cls._tags),
",".join(tags))
raise ValueError(
"Need %s as tags, %s provided" % (",".join(cls._tags), ",".join(tags))
)
return os.path.join(cls.base_path(), *tags)


Expand Down
22 changes: 19 additions & 3 deletions conftool/tests/unit/test_kvobject.py
Expand Up @@ -23,9 +23,6 @@ def test_tags(self):
ent = MockBasicEntity()
self.assertEqual(ent.tags, {'A': 'a', 'B': 'b', 'C': 'c', 'D': 'd'})

def test_pprint(self):
self.assertEqual(self.entity.pprint(), 'Foo/Bar/test')

def test_kvpath(self):
"""
Test `KVObject.kvpath`
Expand Down Expand Up @@ -170,6 +167,25 @@ def test_from_yaml_no_tags(self):
self.assertEqual(KVObject._kv_from_yaml("test"), "test")
self.assertEqual({'a': None}, KVObject._from_yaml(['a']))

class TestEntity(unittest.TestCase):
def setUp(self):
KVObject.backend = MockBackend({})
KVObject.config = configuration.Config(driver="")
self.entity = MockEntity('Foo', 'Bar', 'test')

def test_init(self):
self.assertEqual(self.entity.tags, {'foo': 'Foo', 'bar': 'Bar'})
self.assertEqual(self.entity.name, 'test')
# Now let's pass some wrong tags
self.assertRaises(ValueError, MockEntity, 'Foo', 'Bar', 'Baz', 'test')

def test_pprint(self):
self.assertEqual(self.entity.pprint(), 'Foo/Bar/test')

def test_dir(self):
self.assertEqual(MockEntity.dir('Foo', 'Bar'), 'Mock/entity/Foo/Bar')
self.assertRaises(ValueError, MockEntity.dir, 'Foo')

class TestFreeSchemaObject(unittest.TestCase):

def setUp(self):
Expand Down

0 comments on commit 6e360f0

Please sign in to comment.