[z3-checkins] r22555 - in z3/sqlos/trunk/src/sqlos: . ftests
jinty at codespeak.net
jinty at codespeak.net
Tue Jan 24 09:50:48 CET 2006
Author: jinty
Date: Tue Jan 24 09:50:43 2006
New Revision: 22555
Modified:
z3/sqlos/trunk/src/sqlos/connection.py
z3/sqlos/trunk/src/sqlos/container.py
z3/sqlos/trunk/src/sqlos/ftests/localutilities.txt
z3/sqlos/trunk/src/sqlos/ftests/test_doctest.py
Log:
Remove context=context from all utility lookups. This allows localsites to work
proprly, so I have fixed and enabled that functional test as well.
Modified: z3/sqlos/trunk/src/sqlos/connection.py
==============================================================================
--- z3/sqlos/trunk/src/sqlos/connection.py (original)
+++ z3/sqlos/trunk/src/sqlos/connection.py Tue Jan 24 09:50:43 2006
@@ -30,11 +30,10 @@
self.name = name
def __get__(self, inst, cls=None):
- context = inst is None and cls or inst
name = self.name
if name is None:
try:
- ut = zapi.getUtility(IConnectionName, context=context)
+ ut = zapi.getUtility(IConnectionName)
except ComponentLookupError:
return self
if ut is None:
@@ -43,9 +42,7 @@
SQLObjectWarning, 2)
return
name = ut.name
- newconn = zapi.queryUtility(IZopeDatabaseAdapter, name,
- default=None,
- context=context)
+ newconn = zapi.queryUtility(IZopeDatabaseAdapter, name)
if newconn is None:
warnings.warn("Couldn't find a rdb connection by the "
"name %s. Please verify your setup." % name,
Modified: z3/sqlos/trunk/src/sqlos/container.py
==============================================================================
--- z3/sqlos/trunk/src/sqlos/container.py (original)
+++ z3/sqlos/trunk/src/sqlos/container.py Tue Jan 24 09:50:43 2006
@@ -63,10 +63,10 @@
if ISQLObject.providedBy(obj):
# Look for the SQLObject class our object is from, so get all
# allowed factories
- for name, factory in zapi.getFactoriesFor(ISQLObject, context=self.context):
+ for name, factory in zapi.getFactoriesFor(ISQLObject):
if checkFactory(self, None, factory):
# get the sqlobject class
- utility = zapi.queryUtility(IISQLObject, name, context=self.context)
+ utility = zapi.queryUtility(IISQLObject, name)
if utility is None:
continue
if obj.sqlmeta.table == utility.sqlmeta.table:
@@ -85,7 +85,7 @@
pass
def _allowedFactories(self):
- for name, factory in zapi.getFactoriesFor(ISQLObject, context=self):
+ for name, factory in zapi.getFactoriesFor(ISQLObject):
if checkFactory(self, None, factory):
yield name
allowedFactories = deprecation.deprecated(_allowedFactories,
@@ -93,9 +93,9 @@
' please use _getAllowedIISQLObjectUtilities instead.')
def _getAllowedIISQLObjectUtilities(self):
- for name, factory in zapi.getFactoriesFor(ISQLObject, context=self):
+ for name, factory in zapi.getFactoriesFor(ISQLObject):
if checkFactory(self, None, factory):
- utility = zapi.queryUtility(IISQLObject, name, context=self)
+ utility = zapi.queryUtility(IISQLObject, name)
# Someone might have registered a factory implementing
# IISQLObject using <zope:factory> for whatever reason.
# in this case queryUtility returns None and we can just
Modified: z3/sqlos/trunk/src/sqlos/ftests/localutilities.txt
==============================================================================
--- z3/sqlos/trunk/src/sqlos/ftests/localutilities.txt (original)
+++ z3/sqlos/trunk/src/sqlos/ftests/localutilities.txt Tue Jan 24 09:50:43 2006
@@ -1,24 +1,16 @@
-This is a functional unit test to demonstrate a bug in sqlos in conjuction with
-a database registered as local utility. The bug will raise an exception in line 93
-when the person container is added to the zodb tree. The added event will call the
-sqlobject factory to create the items contained in person container. Since sqlos is not
-capable to access local utilities correctly it will not use the set up local database
-and instead use the hard coded global utility/databse (in ftesting.zcml). Since there
-are no tables defined in the global database, a DatabaseError: no such table will
-show up.
+This is a regression test to demonstrate an old bug in sqlos in conjunction
+with a database registered as local utility. Previously it was impossible to
+have sqlos use a database connection registered as a local utility.
+
+It is possible, but the local utility must be registered under the same name as
+is defined in the <sqlos:connectionName> directive.
First we need to set up some folder structure to set up a site.
>>> from zope.app import zapi
>>> from zope.app.folder import Folder
- >>> from zope.app.container.contained import Contained
- >>> from sqlos.testing.sampleperson import SamplePersonContainer, createTestingTables
>>> from zope.app.component import interfaces as componentInterfaces
- >>> from zope.app.component.site import LocalSiteManager, SiteManagerContainer
- >>> from sqlos.container import SQLObjectContainer
-
- >>> class LocalSitePersonContainer(SamplePersonContainer, SiteManagerContainer, Contained):
- ... pass
+ >>> from zope.app.component.site import LocalSiteManager
>>> root = getRootFolder()
>>> testsite = Folder()
@@ -31,43 +23,46 @@
>>> componentInterfaces.ISite.providedBy(testsite)
True
+Let's get the connection name:
+
+ >>> from sqlos.interfaces import IConnectionName
+ >>> connection_name = zapi.getUtility(IConnectionName).name
Now we set up a local database utility
>>> from zope.app.rdb.interfaces import IZopeDatabaseAdapter
- >>> from zope.app.component.interfaces.registration import ActiveStatus,InactiveStatus
+ >>> from zope.app.component.interfaces.registration import ActiveStatus
>>> from sqlos.testing.testdb import SQLiteda
>>> from zope.app.utility import UtilityRegistration
>>> dbAdapter = SQLiteda(u'dbi://:memory:')
- >>> reg = UtilityRegistration('sqlite',IZopeDatabaseAdapter,dbAdapter)
+ >>> reg = UtilityRegistration(connection_name,
+ ... IZopeDatabaseAdapter,
+ ... dbAdapter)
>>> default = sm['default']
>>> key = default.registrationManager.addRegistration(reg)
>>> zapi.traverse(default.registrationManager, key).status = ActiveStatus
- >>> localUtility = sm.queryUtility(IZopeDatabaseAdapter,'sqlite')
+ >>> localUtility = sm.queryUtility(IZopeDatabaseAdapter, connection_name)
>>> localUtility
<sqlos.testing.testdb.SQLiteda object at ...>
>>> localUtility is dbAdapter
True
-Ok. Now we have a site in root/testsite with a registered sqlite adapter.
-We make sure that our localUtility is not identical to the global sqlite utility
+Ok. Now we have a site in root/testsite with a registered sqlite adapter. We
+make sure that our localUtility is not identical to the global sqlite utility
that has been registered through ftesting.zcml.
>>> gsm = zapi.getGlobalSiteManager()
- >>> globalUtility = gsm.queryUtility(IZopeDatabaseAdapter,'sqlite')
+ >>> globalUtility = gsm.queryUtility(IZopeDatabaseAdapter, connection_name)
>>> globalUtility is not localUtility
True
-Now we populate testsite with data. We do this by using direct SQL, because there is currently
-no support for local sites in testcode.
-
- >>> from zope.component.interfaces import ISiteManager
+Now we populate testsite with data. We do this by using direct SQL, because
+there is currently no support for local sites in testcode.
>>> cursor = localUtility().cursor()
-
>>> cursor.execute(
... '''create table dog (
... id integer primary key,
@@ -89,10 +84,36 @@
>>> import transaction
>>> transaction.get().commit()
-Now that we have setup database tables we should be able to create the personcontainer without errors.
+Now that we have setup database tables we should be able to create the
+personcontainer without errors. We do this through the ZMI so as to use the
+local utility:
+
+So, get a testbrowser:
+
+ >>> from zope.testbrowser import Browser
+ >>> browser = Browser()
+ >>> browser.addHeader('Authorization', 'Basic mgr:mgrpw')
+ >>> browser.handleErrors = False
+
+Then add the container:
+
+ >>> browser.open('http://localhost/testsite/manage')
+ >>> browser.getLink('SQLObject Multi Container').click()
+ >>> browser.getControl(name='new_value').value = 'multicontainer1'
+ >>> browser.getControl('Apply').click()
+
+Now add a Sample Person to the container:
+
+ >>> browser.getLink('multicontainer1').click()
+ >>> browser.getLink('SamplePerson').click()
+ >>> browser.getControl(name='field.fullname').value = 'Bob'
+ >>> browser.getControl(name='field.username').value = 'bob'
+ >>> browser.getControl(name='field.password').value = 'obo'
+ >>> browser.getControl('Add').click()
- >>> personcontainer = LocalSitePersonContainer()
- >>> testsite['personcontainer'] = personcontainer
- >>> localUtility2 = ISiteManager(personcontainer).queryUtility(IZopeDatabaseAdapter,'sqlite')
- >>> localUtility2 is localUtility
- True
+And now try to see if the person has been actually created in the localUtility:
+
+ >>> cursor = localUtility().cursor()
+ >>> cursor.execute("""select * from sample_person""")
+ >>> cursor.fetchone()
+ [1, u'Bob', u'bob', u'obo']
Modified: z3/sqlos/trunk/src/sqlos/ftests/test_doctest.py
==============================================================================
--- z3/sqlos/trunk/src/sqlos/ftests/test_doctest.py (original)
+++ z3/sqlos/trunk/src/sqlos/ftests/test_doctest.py Tue Jan 24 09:50:43 2006
@@ -9,7 +9,6 @@
'adding.txt',
'connection.txt',
'containers.txt',
-# localutilities test is disabled as it is broken
-# 'localutilities.txt',
+ 'localutilities.txt',
'isolated_containers.txt']
return FunctionalDocFileSuite(*filelist)
More information about the z3-checkins
mailing list