From 26c9f4c8818e9904dab838ac95839c0d527219b8 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Sat, 19 Jul 2008 08:31:46 +0000 Subject: 7: Roughed out API.finalize(); added corresponding unit tests --- ipalib/base.py | 27 +++++++++++++++++---------- ipalib/tests/test_base.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 12 deletions(-) (limited to 'ipalib') diff --git a/ipalib/base.py b/ipalib/base.py index 97fb7c90c..51324f938 100644 --- a/ipalib/base.py +++ b/ipalib/base.py @@ -158,7 +158,7 @@ class NameSpace(object): class API(object): - __commands = None + __cmd = None __objects = None __locked = False @@ -171,9 +171,9 @@ class API(object): return self.__objects objects = property(__get_objects) - def __get_commands(self): - return self.__commands - commands = property(__get_commands) + def __get_cmd(self): + return self.__cmd + cmd = property(__get_cmd) def __merge(self, base, cls, override): assert issubclass(base, Named) @@ -184,17 +184,24 @@ class API(object): raise exceptions.DuplicateError(cls.__name__, id(cls)) if cls.__name__ in self.__names and not override: raise exceptions.OverrideError(cls.__name__) + prefix = base.prefix + assert cls.__name__.startswith(prefix) self.__classes.add(cls) self.__names.add(cls.__name__) - if base not in self.__stage: - self.__stage[base.prefix] = {} - self.__stage[base.prefix][cls.__name__] = cls + if prefix not in self.__stage: + self.__stage[prefix] = {} + self.__stage[prefix][cls.__name__] = cls def register_command(self, cls, override=False): self.__merge(Command, cls, override) def finalize(self): - pass - #i = cls() - #assert cls.__name__ == (base.prefix + '_' + i.name) + for (prefix, d) in self.__stage.items(): + n = {} + for cls in d.values(): + i = cls() + assert cls.__name__ == (prefix + '_' + i.name) + n[i.name] = i + if prefix == 'cmd': + self.__cmd = NameSpace(n) diff --git a/ipalib/tests/test_base.py b/ipalib/tests/test_base.py index bf727ed6f..81794d7ac 100644 --- a/ipalib/tests/test_base.py +++ b/ipalib/tests/test_base.py @@ -267,8 +267,7 @@ class test_API: Test expectations of a fresh API instance. """ api = self.new() - assert read_only(api, 'objects') is None - assert read_only(api, 'objects') is None + assert read_only(api, 'cmd') is None def test_register_command(self): api = self.new() @@ -314,3 +313,28 @@ class test_API: # Check that override=True works: api.register_command(cmd_my_command, override=True) + + def test_finalize(self): + api = self.new() + assert read_only(api, 'cmd') is None + + class cmd_my_command(base.Command): + pass + class cmd_another_command(base.Command): + pass + + api.register_command(cmd_my_command) + api.register_command(cmd_another_command) + + api.finalize() + + cmd = read_only(api, 'cmd') + assert isinstance(cmd, base.NameSpace) + assert api.cmd is cmd + + assert len(cmd) == 2 + assert list(cmd) == ['another_command', 'my_command'] + assert isinstance(cmd.my_command, cmd_my_command) + assert cmd.my_command is cmd['my_command'] + assert isinstance(cmd.another_command, cmd_another_command) + assert cmd.another_command is cmd['another_command'] -- cgit