【发布时间】:2016-08-18 18:42:26
【问题描述】:
如果存在具有相同 id 的模型资源,我正在尝试测试烧瓶药水的模型资源是否在创建时发生冲突:
class Device(db.Model):
__tablename__ = 'device'
uuid = db.Column(UUID, primary_key=True, default=lambda: str(uuid4()))
make = db.Column(db.String(150), nullable=False)
model = db.Column(db.String(150), nullable=False)
category = db.Column(db.String(150), nullable=False)
class DeviceResource(ModelResource):
class Meta:
model = Device
id_converter = 'string'
class Schema:
uuid = fields.UUID(io='wr')
并且测试是作为 pytest yield 夹具完成的:
@pytest.fixture(scope='session')
def application():
flask_app = create_app()
# return a webtest.TestApp
test_app = TestApp(flask_app)
test_app.current_app_context = flask_app.app_context
return test_app
@pytest.yield_fixture(scope='function')
def single_device(application):
obj = Device(**DEVICE_JSON)
with application.current_app_context():
db.session.add(obj)
db.session.commit()
yield obj
for device in Device.query.all():
db.session.delete(device)
db.session.commit()
def test_create_device_fail(single_device, application):
response = application.post_json('/api/v1/device', params=DEVICE_JSON, expect_errors=True)
assert response.status_code == 409
但是当我运行时出现以下错误:
sqlalchemy.exc.InvalidRequestError: This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). Original exception was: New instance <Device at 0x7fb5e83d5d68> with identity key (<class 'complex_service.db.Device'>, ('1ec67df1-e673-4505-8e00-5be58ec1656a',)) conflicts with persistent instance <Device at 0x7fb5e84419e8>
一起
sqlalchemy.orm.exc.FlushError: New instance <Device at 0x7fb5e83d5d68> with identity key (<class 'complex_service.db.Device'>, ('1ec67df1-e673-4505-8e00-5be58ec1656a',)) conflicts with persistent instance <Device at 0x7fb5e84419e8>
即使我没有通过测试中的夹具,而是执行了双重请求,它也不起作用,失败并出现相同的错误
def test_create_device_fail(single_device, application):
application.post_json('/api/v1/device', params=DEVICE_JSON, expect_errors=True)
assert response.status_code == 200
response = application.post_json('/api/v1/device', params=DEVICE_JSON, expect_errors=True)
assert response.status_code == 409
关于如何解决它的任何想法?
【问题讨论】:
-
不确定 pytest 究竟是如何工作的,但似乎在
single_device()添加设备,然后在yield输入测试?您可能应该在屈服之前离开应用上下文,然后在测试后再次输入以删除设备。 -
像这样制作夹具:
@pytest.yield_fixture(scope='function') def single_device(application): obj = Device(**DEVICE_JSON) with application.current_app_context(): db.session.add(obj) db.session.commit() yield obj with application.current_app_context(): for device in Device.query.all(): db.session.delete(device) db.session.commit()我收到一个新错误:sqlalchemy.orm.exc.DetachedInstanceError: Instance <Device at 0x7fab4e154ef0> is not bound to a Session; attribute refresh operation cannot proceed
标签: python-3.x flask sqlalchemy pytest flask-potion