Added tests.util.get_api() function to create a standard (api, home) tuple for unit testing

This commit is contained in:
Jason Gerard DeRose 2008-10-30 01:34:46 -06:00
parent ddb5449c7f
commit 2fee6a3e20
4 changed files with 29 additions and 32 deletions

View File

@ -35,13 +35,11 @@ from frontend import Command, Object, Method, Property, Application
from ipa_types import Bool, Int, Unicode, Enum
from frontend import Param, DefaultFrom
def get_standard_api(unit_test=False):
api = plugable.API(
def get_standard_api():
return plugable.API(
Command, Object, Method, Property, Application,
Backend, Context,
)
if unit_test is True:
api.env.mode = 'unit_test'
return api
api = get_standard_api()

View File

@ -21,8 +21,7 @@
Test the `ipalib.cli` module.
"""
from tests.util import raises, getitem, no_set, no_del, read_only, ClassChecker
from tests.util import TempHome
from tests.util import raises, get_api, ClassChecker
from ipalib import cli, plugable, frontend, backend
@ -92,8 +91,6 @@ from_cli_conf = overridden in default.conf
"""
class test_CLI(ClassChecker):
"""
Test the `ipalib.cli.CLI` class.
@ -101,17 +98,7 @@ class test_CLI(ClassChecker):
_cls = cli.CLI
def new(self, argv=tuple()):
home = TempHome()
api = plugable.API(
frontend.Command,
frontend.Object,
frontend.Method,
frontend.Property,
frontend.Application,
backend.Backend,
)
api.env.mode = 'unit_test'
api.env.in_tree = True
(api, home) = get_api()
o = self.cls(api, argv)
assert o.api is api
return (o, api, home)

View File

@ -23,7 +23,7 @@ Test the `ipalib.plugable` module.
from tests.util import raises, no_set, no_del, read_only
from tests.util import getitem, setitem, delitem
from tests.util import ClassChecker, TempHome
from tests.util import ClassChecker, get_api
from ipalib import plugable, errors
@ -771,13 +771,6 @@ class test_API(ClassChecker):
_cls = plugable.API
def new(self, *bases):
home = TempHome()
api = self.cls(*bases)
api.env.mode = 'unit_test'
api.env.in_tree = True
return (api, home)
def test_API(self):
"""
Test the `ipalib.plugable.API` class.
@ -802,6 +795,8 @@ class test_API(ClassChecker):
return n + 1
api = plugable.API(base0, base1)
api.env.mode = 'unit_test'
api.env.in_tree = True
r = api.register
assert isinstance(r, plugable.Registrar)
assert read_only(api, 'register') is r
@ -884,7 +879,7 @@ class test_API(ClassChecker):
"""
Test the `ipalib.plugable.API.bootstrap` method.
"""
(o, home) = self.new()
(o, home) = get_api()
assert o.env._isdone('_bootstrap') is False
assert o.env._isdone('_finalize_core') is False
assert o.isdone('bootstrap') is False
@ -900,7 +895,7 @@ class test_API(ClassChecker):
"""
Test the `ipalib.plugable.API.load_plugins` method.
"""
(o, home) = self.new()
(o, home) = get_api()
assert o.isdone('bootstrap') is False
assert o.isdone('load_plugins') is False
o.load_plugins()

View File

@ -26,7 +26,8 @@ import os
from os import path
import tempfile
import shutil
from ipalib import errors
import ipalib
class TempDir(object):
@ -206,5 +207,21 @@ def check_TypeError(value, type_, name, callback, *args, **kw):
assert e.type is type_
assert e.name == name
assert type(e.name) is str
assert str(e) == errors.TYPE_FORMAT % (name, type_, value)
assert str(e) == ipalib.errors.TYPE_FORMAT % (name, type_, value)
return e
def get_api(**kw):
"""
Returns (api, home) tuple.
This function returns a tuple containing an `ipalib.plugable.API`
instance and a `TempHome` instance.
"""
home = TempHome()
api = ipalib.get_standard_api()
api.env.mode = 'unit_test'
api.env.in_tree = True
for (key, value) in kw.iteritems():
api.env[key] = value
return (api, home)