Giter Club home page Giter Club logo

Comments (3)

javierseixas avatar javierseixas commented on July 26, 2024

Hi! Just wondering if there has been any advance on this, since I'm looking for this functionality.

from flask-fixtures.

retr0h avatar retr0h commented on July 26, 2024

Any news @mbegoc ?

from flask-fixtures.

Reimirno avatar Reimirno commented on July 26, 2024

For any future visitors this is my workaround.

from server.models import db as sqlalchemy_db

@pytest.fixture()
def seeded_db(app):
    from flask_fixtures import load_fixtures_from_file

    with app.app_context():
        sqlalchemy_db.drop_all()
        sqlalchemy_db.create_all()
		sqlalchemy_db.session.rollback()

        ... # prepares seed_files_names and seed_dir_paths

        for filename in seed_files_names:
            load_fixtures_from_file(sqlalchemy_db, filename, seed_dir_paths)

        yield sqlalchemy_db

        sqlalchemy_db.session.expunge_all()
        sqlalchemy_db.drop_all()

I didn't use the flask_fixtures.setup or flask_fixtures.teardown becuase I want to manage context myself. I also disliked how it finds seeding fixture files. But those are not really important.

And here is how you use that pytest fixture in a test case:

def test_seeded_db(seeded_db):
    """
    Test that seeded database works
	We have a User table and a user.json seeding fixture file. So there should be some users in table.
    """
    assert User.query.count() > 0
    # now remove all user
    User.query.delete()
    seeded_db.session.commit()
    assert User.query.count() == 0


def test_seeded_db_rollback(seeded_db):
    """
    Test that the previous test did not affect the seeded database
    """
    assert User.query.count() > 0

Seems to work for me as of now.

If you have question you could reply to this thread.

from flask-fixtures.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.