我试图测试烧瓶药水的ModelResource在创建时是否存在冲突,如果存在具有相同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 fixture进行的:
@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
有关如何修复它的任何想法?
答案 0 :(得分:0)
感谢@lyschoening的建议,我设法让它发挥作用,
@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 None
with application.current_app_context():
for device in Device.query.all():
db.session.delete(device)
db.session.commit()
所以基本上没有让对象产生测试并且有两个不同的应用程序上下文