[PEAK] proper unit testing
R. David Murray
rdmurray at bitdance.com
Thu Nov 4 17:56:41 EST 2004
So far I've been getting along with using integration tests run
from my peak runIni script. Now I'm trying to write proper
unit tests to speed up my test cycle. I think I've managed to get
a configuration root created and an configuration file loaded into
it. At least, the first error message about being unable to find
a configuration key is gone now. However, it seems to be having
trouble finding one of the config keys that is the same file as the
configuration key it is now finding. The one it can't find is
my LinkRef to the database, from the [Named Services] section.
Hmm. I guess I'm going to have to post the code (*frown*). And
yeah, I know the TIMESTAMP thing is gnarly, fixing that is one of
the reasons I want to write these unit test (*grin*).
tests/tests.ini
-----------------------------------------------------------
[Load Settings From]
file = config.fileNearModule('fcgbilling', 'fcgbilling.ini')
[Named Services]
fcgbilling.db = naming.LinkRef('sqlite:/home/rdmurray/proj/fcgbilling/tests/data/testdb')
[fcgbilling]
datadir = config.fileNearModule('fcgbilling.tests', "data")
-----------------------------------------------------------
fcgbilling.ini
-----------------------------------------------------------
[Import on Demand]
datetime = "datetime"
[sqlite.sql_types]
TIMESTAMP = lambda descr,value: value and value!='None' and datetime.datetime(*(map(int, value.replace("-", " ").replace(":", " ").replace(".", " ").split()))) or None
BOOLEAN = lambda descr, value: value=='True' and True or False
[Named Services]
fcgbilling.db = naming.LinkRef('sqlite:/home/rdmurray/proj/fcgbilling/data/db')
[fcgbilling]
dbdd = config.fileNearModule('fcgbilling', 'SQLSchema')
datadir = "/home/rdmurray/proj/fcgbilling/data"
-----------------------------------------------------------
tests/__init__.py
-----------------------------------------------------------
from peak.api import config, binding
allSuites = [
'fcgbilling.tests.storage:test_suite',
]
def test_suite():
from peak.util.imports import importSuite
return importSuite(allSuites)
# shared, default config root for testing
_root = None
def testRoot():
global _root
if _root is None:
from peak.api import config
_root = binding.Configurable(config.makeRoot())
config.loadConfigFile(_root,
config.packageFile(__name__, 'tests.ini'))
return _root
-----------------------------------------------------------
tests/storage.py
-----------------------------------------------------------
from unittest import TestCase, makeSuite, TestSuite
from peak.api import *
from fcgbilling.model import *
from fcgbilling.tests import testRoot
from fcgbilling.storage import AccountDM
class TestAccountDM(TestCase):
def setUp(self):
self.root = testRoot()
self.a = AccountDM()
self.a.setParentComponent(self.root)
def checkSomething(self):
storage.beginTransaction(self.root)
a = self.a
a.newItem()
a.id = '1'
a.username = 'frank'
storage.commitTransaction(self.root)
storage.beginTransaction(self.root)
o = self.a['1']
self.assertEQ(o.id, '1')
self.assertEQ(o.username, 'frank')
storage.commitTransaction(self.root)
TestClasses = (
TestAccountDM,
)
def test_suite():
s = []
for t in TestClasses:
s.append(makeSuite(t,'check'))
return TestSuite(s)
-----------------------------------------------------------
The error that all this gives me is:
NameNotFound: config.MultiKey(<class 'peak.binding.interfaces.IComponentFactory'>, PropertyName('fcgbilling.db')) [resolvedObj=<peak.config.config_components.ConfigurationRoot object at 0x864682c>]
Feel free to point at anthing I'm doing wrong (or just the hard way) aside from
the issue causing the problem, too. My understanding of how PEAK fits
together is seemingly still in its infancy....
--David
More information about the PEAK
mailing list