Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #5 - turn fucntional doc tests into unittests
- Loading branch information
Showing
11 changed files
with
116 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ build | |
develop-eggs | ||
dist | ||
lib | ||
local | ||
include | ||
man | ||
parts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,15 @@ | ||
Please refer to src/zope/globalrequest/README.rst. | ||
zope.globalrequest | ||
================== | ||
|
||
Introduction | ||
------------ | ||
|
||
This package provides a global way to retrieve the currently active request object in a zope-based web framework. | ||
To do so you simply need to do the following:: | ||
|
||
from zope.globalrequest import getRequest | ||
request = getRequest() | ||
|
||
This package is mainly intended to be used with the Zope2/Plone stack. | ||
While it also works with the Zope3 framework, | ||
the latter promotes a clean separation of concerns and the pattern of having a globally available request object is discouraged. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
[buildout] | ||
develop = . | ||
parts = interpreter test | ||
parts = interpreter test | ||
|
||
[interpreter] | ||
recipe = zc.recipe.egg | ||
eggs = zope.globalrequest | ||
interpreter = python | ||
interpreter = zopepy | ||
|
||
[test] | ||
recipe = zc.recipe.testrunner | ||
eggs = zope.globalrequest [test] | ||
defaults = ['-v'] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,93 @@ | ||
from unittest import TestSuite | ||
from zope.configuration import config | ||
from zope.configuration import xmlconfig | ||
from zope.testing.cleanup import cleanUp | ||
import doctest | ||
import zope.app.wsgi.testlayer | ||
import zope.globalrequest | ||
import zope.testbrowser.wsgi | ||
# -*- coding: utf-8 -*- | ||
import unittest | ||
|
||
|
||
def zcml(source): | ||
context = config.ConfigurationMachine() | ||
xmlconfig.registerCommonDirectives(context) | ||
xmlconfig.string(source, context) | ||
class TestGlobalrequest(unittest.TestCase): | ||
|
||
def tearDown(self): | ||
# reset the threading local context | ||
from threading import local | ||
import zope.globalrequest.local | ||
zope.globalrequest.local.localData = local() | ||
|
||
def tearDown(test): | ||
cleanUp() | ||
def test_unset_local(self): | ||
# test with unset values | ||
import zope.globalrequest.local | ||
self.assertIs(zope.globalrequest.local.getLocal('unsetkey'), None) | ||
|
||
def test_set_get_local(self): | ||
# test with simple values | ||
import zope.globalrequest.local | ||
zope.globalrequest.local.setLocal('testkey', 'testvalue') | ||
self.assertIs( | ||
zope.globalrequest.local.getLocal('testkey'), | ||
'testvalue' | ||
) | ||
|
||
class Layer(zope.testbrowser.wsgi.TestBrowserLayer, | ||
zope.app.wsgi.testlayer.BrowserLayer): | ||
"""Layer to prepare zope.testbrowser using the WSGI app.""" | ||
def test_unset_local_with_factory(self): | ||
# test with a factory given and marker set/not set | ||
dummy_default = 'dummy_test_default_value' | ||
|
||
testLayer = Layer(zope.globalrequest) | ||
def dummy_default_factory(): | ||
return dummy_default | ||
|
||
import zope.globalrequest.local | ||
|
||
self.assertEqual( | ||
zope.globalrequest.local.getLocal( | ||
'testdefaultkey', | ||
factory=dummy_default_factory | ||
), | ||
dummy_default | ||
) | ||
|
||
def test_unset_global_request(self): | ||
# get w/o any value set returns None | ||
import zope.globalrequest | ||
self.assertIs(zope.globalrequest.getRequest(), None) | ||
|
||
def test_set_get_globalrequest(self): | ||
# set a value and get it back | ||
import zope.globalrequest | ||
test_request = dict(value='I am a dummy request') | ||
zope.globalrequest.setRequest(test_request) | ||
self.assertIs(zope.globalrequest.getRequest(), test_request) | ||
|
||
def test_clear_global_request(self): | ||
# set a value and get it back | ||
import zope.globalrequest | ||
test_request = dict(value='I am a dummy request') | ||
zope.globalrequest.setRequest(test_request) | ||
zope.globalrequest.clearRequest() | ||
self.assertIs(zope.globalrequest.getRequest(), None) | ||
|
||
def test_set_subscriber(self): | ||
import zope.globalrequest.subscribers | ||
test_request = dict(value='I am a dummy request') | ||
|
||
class DummyEvent(object): | ||
request = test_request | ||
|
||
dummy_event = DummyEvent() | ||
|
||
# test set | ||
zope.globalrequest.subscribers.set(None, dummy_event) | ||
self.assertIs(zope.globalrequest.getRequest(), test_request) | ||
|
||
def test_clear_subscriber(self): | ||
import zope.globalrequest.subscribers | ||
test_request = dict(value='I am a dummy request') | ||
|
||
class DummyEvent(object): | ||
request = test_request | ||
|
||
dummy_event = DummyEvent() | ||
|
||
# test clear | ||
zope.globalrequest.subscribers.set(None, dummy_event) | ||
zope.globalrequest.subscribers.clearRequest() | ||
self.assertIs(zope.globalrequest.getRequest(), None) | ||
|
||
|
||
def test_suite(): | ||
flags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS | ||
readme = doctest.DocFileSuite( | ||
'README.rst', | ||
package='zope.globalrequest', | ||
globs={'zcml': zcml}, | ||
optionflags=flags, | ||
tearDown=tearDown) | ||
readme.layer = testLayer | ||
return TestSuite((readme,)) | ||
return unittest.defaultTestLoader.loadTestsFromName(__name__) |
Oops, something went wrong.