launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #13117
[Merge] lp:~jtv/maas/maascli-fake-config into lp:maas
Jeroen T. Vermeulen has proposed merging lp:~jtv/maas/maascli-fake-config into lp:maas.
Commit message:
Extract creation of a fake maascli ProfileConfig for testing.
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
For more details, see:
https://code.launchpad.net/~jtv/maas/maascli-fake-config/+merge/128466
This is one of those places where the code needed breaking up in order for my real work to progress. It has been remarked upon that the creation of the fake is really horrible code (or perhaps it's fairer to say that the data structure is horrible) and so I would like to avoid having to duplicate it in later tests for different modules.
Jeroen
--
https://code.launchpad.net/~jtv/maas/maascli-fake-config/+merge/128466
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~jtv/maas/maascli-fake-config into lp:maas.
=== added directory 'src/maascli/testing'
=== added file 'src/maascli/testing/__init__.py'
=== added file 'src/maascli/testing/config.py'
--- src/maascli/testing/config.py 1970-01-01 00:00:00 +0000
+++ src/maascli/testing/config.py 2012-10-08 11:13:22 +0000
@@ -0,0 +1,47 @@
+# Copyright 2012 Canonical Ltd. This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""Fake `ProfileConfig` for testing."""
+
+from __future__ import (
+ absolute_import,
+ print_function,
+ unicode_literals,
+ )
+
+__metaclass__ = type
+__all__ = []
+
+from maastesting.factory import factory
+
+
+class FakeConfig(dict):
+ """Fake `ProfileConfig`. A dict that's also a context manager."""
+ def __enter__(self, *args, **kwargs):
+ return self
+
+ def __exit__(self, *args, **kwargs):
+ pass
+
+
+def make_configs(number_of_configs=1):
+ """Create a dict mapping config names to `FakeConfig`."""
+ result = {}
+ while len(result) < number_of_configs:
+ profile = factory.make_name('profile')
+ result[profile] = {
+ 'name': profile,
+ 'url': 'http://%s.example.com/' % profile,
+ 'description': {
+ 'handlers': [{
+ 'name': factory.make_name('handler'),
+ 'doc': "Short\n\nLong",
+ 'params': [],
+ 'actions': [{
+ 'name': factory.make_name('action'),
+ 'doc': "Doc\n\nstring",
+ }],
+ }],
+ },
+ }
+ return FakeConfig(result)
=== modified file 'src/maascli/tests/test_api.py'
--- src/maascli/tests/test_api.py 2012-10-08 10:18:55 +0000
+++ src/maascli/tests/test_api.py 2012-10-08 11:13:22 +0000
@@ -24,6 +24,7 @@
)
from maascli.command import CommandError
from maascli.config import ProfileConfig
+from maascli.testing.config import make_configs
from maastesting.factory import factory
from maastesting.testcase import TestCase
from mock import (
@@ -38,44 +39,13 @@
)
-class FakeConfig(dict):
- """Fake `ProfileConfig`. A dict that's also a context manager."""
- def __enter__(self, *args, **kwargs):
- return self
-
- def __exit__(self, *args, **kwargs):
- pass
-
-
class TestRegisterAPICommands(TestCase):
"""Tests for `register_api_commands`."""
- def make_profile(self):
- """Fake a profile."""
- profile = factory.make_name('profile')
- url = 'http://%s.example.com/' % profile
- fake_open = self.patch(ProfileConfig, 'open')
- fake_open.return_value = FakeConfig({
- profile: {
- 'name': profile,
- 'url': url,
- 'description': {
- 'handlers': [{
- 'name': factory.make_name('handler'),
- 'doc': "Short\n\nLong",
- 'params': [],
- 'actions': [{
- 'name': factory.make_name('action'),
- 'doc': "Doc\n\nstring",
- }],
- }],
- },
- },
- })
- return profile
-
def test_registers_subparsers(self):
- profile = self.make_profile()
+ config = make_configs()
+ profile = config.keys()[0]
+ self.patch(ProfileConfig, 'open').return_value = config
parser = ArgumentParser()
self.assertIsNone(parser._subparsers)
api.register_api_commands(parser)