Я пишу тесты для приложения flask
, используя pytest
. У меня есть следующее приспособление app
:
@pytest.fixture(scope="session")
def app(request):
"""Session-wide test `Flask` application."""
app = create_app("config.TestConfig")
with app.app_context() as ctx:
ctx.push()
def teardown():
ctx.pop()
request.addfinalizer(teardown)
return app
Когда я запускаю тест, я получаю ошибку sqlalchemy.exc.InvalidRequestError: This session is in 'committed' state; no further SQL can be emitted within this transaction.
def test_new_tag(app):
"""
Test for Article index create
"""
article_title = "ES title"
new_article = ArticleModel(title=article_title)
with app.app_context():
db.create_all()
db.session.add(new_article)
db.session.commit()
with pytest.raises(NotFoundError) as e:
es.get("articles", 0)
assert e.value.status_code == 404
resp = es.get(ARTICLE_INDEX, new_article.id)
assert resp.get("_source").get("id") == new_article.id
assert resp.get("_source").get("title") == article_title