Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with SQLAlchemy tests #83

Open
Matelin opened this issue Nov 24, 2023 · 0 comments
Open

Issue with SQLAlchemy tests #83

Matelin opened this issue Nov 24, 2023 · 0 comments

Comments

@Matelin
Copy link

Matelin commented Nov 24, 2023

Hi.

I think I found some misconfiuration in a part where you implement SQLAlchemy in place of a standard sqlite3 library to initiate and test the db.
The following part doesn't seem to work:

@pytest.fixture
def client():
    BASE_DIR = Path(__file__).resolve().parent.parent
    app.config["TESTING"] = True
    app.config["DATABASE"] = BASE_DIR.joinpath(TEST_DB)
    app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{BASE_DIR.joinpath(TEST_DB)}"

    with app.app_context():
        db.create_all()  # setup
        yield app.test_client()  # tests run here
        db.drop_all()  # teardown

The Client Fixture is not creating a new test.db everytime the tests are run. Instead the flaskr.db gets created, breaking the app every time you run the test. Seems like sqlalchemy is always using the app.config from the app.py instead the ones from the test file.
If you follow the guide step-by-step it's hard to notice, because the old test.db is still there after implementing SQLAlchemy (you mention to only delete the old flaskr.db) and all the tests passes the first time you run them after SQLAlchemy is there.

Also the part I don't quite understand yet, is why the flaskr.db database needs to be created manually using the create_db.py script. If I delete the db and let the app.py create it on first run, the flaskr.db gets created, but the post table isn't there.

I'll try to figure it out myself but I would be grateful for any tips :)

I'm still a total Python noob so forgive me if I missed something obvious here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant