jeancochrane / jeancochrane/pytest-flask-sqlalchemy

Items created in `db_session` not accessible with `client_class`

Open
#19 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
253
Forks
45
PR merge metrics
No merged PRs in 30d

Description

I have a conftest:
```python
import os

import pytest
from flask_migrate import upgrade

from myapp import create_app, flask_app

@pytest.fixture(scope='session')
def database(request):
uri = flask_app.config.get('SQLALCHEMY_DATABASE_URI')
assert uri.startswith('sqlite:///'), 'In tests, use a Sqlite DB'
sqlite_path = uri.replace('sqlite:///', '')
if os.path.exists(sqlite_path):
os.remove(sqlite_path)

@request.addfinalizer
def drop_database():
os.remove(sqlite_path)

@pytest.fixture(scope='session')
def app(request, database):
"""
Used by pytest-flask
"""
app = create_app()
with app.app_context():
upgrade()
return app

@pytest.fixture(scope='session')
def _db(request, app):
"""
Used by pytest-flask-sqlalchemy
"""
db = app.extensions['sqlalchemy'].db
return db
```

And then have:

```python
import pytest
from mymodels import MyItem
from flask import url_for

@pytest.mark.usefixtures('client_class')
class TestMyApp:
def test_delete(self, db_session, uid):
item = MyItem(
name='x'
)
db_session.add(item)
db_session.commit()
res = self.client.delete(url_for('myapi.ItemResource', id=item.id))
assert res.status_code == 200
```

When i run the test, it always fails with a 404 error saying that the provided ID does not exist

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.