Python 测试sqlalchemy+;中session.add()出现UnappedInstanceError;postgresql+;普隆4.3
请提供帮助:在Plone产品中,没有完整db设置的表的unittests可以工作,但是当尝试包含db以进行测试时,出现了以下错误。这是一个使用sqlalchemy+postgresql的Plone 4.3产品Python 测试sqlalchemy+;中session.add()出现UnappedInstanceError;postgresql+;普隆4.3,python,postgresql,sqlalchemy,plone,plone-4.x,Python,Postgresql,Sqlalchemy,Plone,Plone 4.x,请提供帮助:在Plone产品中,没有完整db设置的表的unittests可以工作,但是当尝试包含db以进行测试时,出现了以下错误。这是一个使用sqlalchemy+postgresql的Plone 4.3产品 Traceback (most recent call last): File "/usr/lib/python2.7/unittest/case.py", ... in run testMethod() File "/home/....../tests/test_model.py", .
Traceback (most recent call last):
File "/usr/lib/python2.7/unittest/case.py", ... in run testMethod()
File "/home/....../tests/test_model.py", ..., in test_createClient
(ses, client) = createClient()
File "/home/...../tests/test_model.py", ..., in createClient
session.add(client1)
File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/session.py",.., in add
UnmappedInstanceError: Class 'my.product.model.Client' is not mapped
似乎没有为要添加的sqlalchemy会话创建表实例(client1)。下面是部分客户端表:
class Client(BaseObject):
__tablename__ = 'client'
id = schema.Column(types.Integer, primary_key=True)
name = schema.Column(types.String(50))
def client_name(self):
return self.name
test_model.py中的测试代码:
def createClient():
session = Session()
client1 = model.Client(name=u"ACME",)
session.add(client1)
session.flush()
return (session, client1)
class CreateClientTest(DatabaseTests):
def test_createClient(self):
(ses, client) = createClient()
self.assertEqual(client.name, client.client_name)
已测试代码的数据库测试设置:
class DatabaseTests(unittest.TestCase):
def setUp(self):
super(DatabaseTests, self).setUp()
from my.product import model
from z3c.saconfig import Session
from Products.Five import zcml
from Products.Five import fiveconfigure
import my.product.tests
fiveconfigure.debug_mode = True
zcml.load_config("configure.zcml", my.product.tests)
fiveconfigure.debug_mode = False
model.metadata.create_all(Session.bind, checkfirst=True)
(def teardown omitted here)
为测试配置.zcml:
<configure xmlns="http://namespaces.zope.org/zope"
xmlns:db="http://namespaces.zope.org/db">
<include package="z3c.saconfig" file="meta.zcml" />
<db:engine name="mydb" url="postgresql://pasd@localhost/mydb" />
<db:session engine="mydb" />
</configure>
Plone站点可以正常工作。mydb的Postgresql db已安装并在后台运行。表的单元测试工作。但不明白为什么没有创建/识别client1实例。非常感谢您的帮助 我认为您的模型未连接到会话。闻起来像是导入订单问题,或者您在任何时候都没有将
BaseObject
绑定到会话。同意。(抱歉这么晚才回复)回到db连接的绘图板-模型到会话。感谢您抽出时间。我认为您的模型未连接到会话。闻起来像是导入订单问题,或者您在任何时候都没有将BaseObject
绑定到会话。同意。(抱歉这么晚才回复)回到db连接的绘图板-模型到会话。谢谢你抽出时间。