Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pyramid.pdf
Скачиваний:
10
Добавлен:
24.03.2015
Размер:
3.82 Mб
Скачать

CHAPTER

FIFTYEIGHT

PYRAMID.SETTINGS

get_settings()

Return a deployment settings object for the current application. The object is a dictionary-like object that contains key/value pairs based on the dictionary passed as the settings argument to the pyramid.config.Configurator constructor or the pyramid.router.make_app()

API.

 

 

 

 

 

latex-warning.png

 

 

 

This method is deprecated as of Pyramid 1.0.

Use

 

pyramid.threadlocal.get_current_registry().settings

in-

 

stead or use the settings attribute of the registry available from the

request

 

(request.registry.settings).

 

 

 

 

 

asbool(s)

Return the boolean value True if the case-lowered value of string input s is any of t, true, y, on, or 1, otherwise return the boolean value False. If s is the value None, return False. If s is already one of the boolean values True or False, return it.

aslist(value, flatten=True)

Return a list of strings, separating the input based on newlines and, if flatten=True (the default), also split on spaces within each line.

655

58. PYRAMID.SETTINGS

656

CHAPTER

FIFTYNINE

PYRAMID.TESTING

setUp(registry=None, request=None, hook_zca=True, autocommit=True, settings=None)

Set Pyramid registry and request thread locals for the duration of a single unit test.

Use this function in the setUp method of a unittest test case which directly or indirectly uses:

•any method of the pyramid.config.Configurator object returned by this function.

•the pyramid.threadlocal.get_current_registry() or pyramid.threadlocal.get_current_request() functions.

If you use the get_current_* functions (or call Pyramid code that uses these functions) without calling setUp, pyramid.threadlocal.get_current_registry() will return a global application registry, which may cause unit tests to not be isolated with respect to registrations they perform.

If the registry argument is None, a new empty application registry will be created (an instance of the pyramid.registry.Registry class). If the registry argument is not None, the value passed in should be an instance of the pyramid.registry.Registry class or a suitable testing analogue.

After setUp is finished, the registry returned by the pyramid.threadlocal.get_current_request() function will be the passed (or constructed) registry until pyramid.testing.tearDown() is called (or pyramid.testing.setUp() is called again) .

If

the hook_zca argument

is True,

setUp will attempt to per-

form

the

operation

zope.component.getSiteManager.sethook(

657

59. PYRAMID.TESTING

pyramid.threadlocal.get_current_registry), which will cause the Zope Component Architecture global API (e.g. zope.component.getSiteManager(), zope.component.getAdapter(), and so on) to use the registry constructed by setUp as the value it returns from zope.component.getSiteManager(). If the zope.component package cannot be imported, or if hook_zca is False, the hook will not be set.

If settings is not None, it must be a dictionary representing the values passed to a Configurator as its settings= argument.

This

function

returns an

instance of the pyramid.config.Configurator

class,

which

can be

used

for further configuration to set

up an environment suitable

for

a

unit or integration

test.

The registry attribute

attached to the Configurator

in-

stance

represents the

‘current’ application registry; the

same registry will be returned

by

pyramid.threadlocal.get_current_registry() during the execution of the test.

 

tearDown(unhook_zca=True)

Undo the effects pyramid.testing.setUp(). Use this function in the tearDown method of a unit test that uses pyramid.testing.setUp() in its setUp method.

If the unhook_zca argument is True (the default), call zope.component.getSiteManager.reset(). This undoes the action of pyramid.testing.setUp() called with the argument hook_zca=True. If zope.component cannot be imported, ignore the argument.

cleanUp(*arg, **kw)

pyramid.testing.cleanUp() is an alias for pyramid.testing.setUp().

class DummyResource(__name__=None, __parent__=None, __provides__=None, **kw)

A dummy Pyramid resource object.

clone(__name__=<object object at 0x57db0b0>, __parent__=<object object at

0x57db0b0>, **kw)

Create a clone of the resource object. If __name__ or __parent__ arguments are passed, use these values to override the existing __name__ or __parent__ of the resource. If any extra keyword args are passed in via the kw argument, use these keywords to add to or override existing resource keywords (attributes).

items()

Return the items set by __setitem__

keys()

Return the keys set by __setitem__

values()

Return the values set by __setitem__

658

class DummyRequest(params=None, environ=None, headers=None, path=’/’, cookies=None,

post=None, **kw)

A DummyRequest object (incompletely) imitates a request object.

The params, environ, headers, path, and cookies arguments correspond to their WebOb equivalents.

The post argument, if passed, populates the request’s POST attribute, but not params, in order to allow testing that the app accepts data for a given view only from POST requests. This argument also sets self.method to “POST”.

Extra keyword arguments are assigned as attributes of the request itself.

Note that DummyRequest does not have complete fidelity with a “real” request. For example, by default, the DummyRequest GET and POST attributes are of type dict, unlike a normal Request’s GET and POST, which are of type MultiDict. If your code uses the features of MultiDict, you should either use a”real” pyramid.request.Request or adapt your DummyRequest by replacing the attributes with MultiDict instances.

Other similar incompatibilities exist. If you need all the features of a Request, use the pyramid.request.Request class itself rather than this class while writing tests.

class DummyTemplateRenderer(string_response=’‘)

An instance of this class is returned from pyramid.config.Configurator.testing_add_renderer

It has a helper function (assert_) that makes it possible to make an assertion which compares data passed to the renderer by the view function against expected key/value pairs.

assert_(**kw)

Accept an arbitrary set of assertion key/value pairs. For each assertion key/value pair assert that the renderer (eg. pyramid.renderer.render_to_response()) received the key with a value that equals the asserted value. If the renderer did not receive the key at all, or the value received by the renderer doesn’t match the assertion value, raise an

AssertionError.

659

59. PYRAMID.TESTING

660

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]