-
Notifications
You must be signed in to change notification settings - Fork 44
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
Question: How to add a load_database functionality when using sqalchemy #797
Comments
@RosanneZe the load parameter and load_database sample might be of help. So first: And this would result in more-or less this: def load_database(**kwargs):
connection = f"postgresql+psycopg2://{kwargs['user']}:@{kwargs['host']}:{kwargs['port']}/{kwargs['dbname']}"
engine = create_engine(connection)
Base.metadata.create_all(engine)
# Createa a process fixture referencing the load_database
postgresql_proc = factories.postgresql_proc(
load=[load_database],
)
@pytest.fixture
def dbsession(postgresql):
connection = f'postgresql+psycopg2://{postgresql.info.user}:@{postgresql.info.host}:{postgresql.info.port}/{postgresql.info.dbname}'
engine = create_engine(connection)
session = scoped_session(sessionmaker(bind=engine))
Base.metadata.create_all(engine) # should not be needed but If I recall, should not hurt. Candidate to be dropped anyway
yield session
Base.metadata.drop_all(engine) |
@fizyk Thank you so much! My apologies for the slow reply, I was on holiday. I've been playing around with it, and I can indeed remove the I tried just adding a |
@RosanneZe would you be able to provide a sample repository? Or PR here with sample behaviour? |
@RosanneZe please take a look at the PoC I made at my sqlalchemy based project: |
@fizyk thank you! That looks promising, I'll try to get it to work next week and let you know how it works out. |
We got it to work! This is what it looks like without pyramid_basemodel: def load_database(**kwargs):
connection = f"postgresql+psycopg2://{kwargs['user']}:@{kwargs['host']}:{kwargs['port']}/{kwargs['dbname']}"
engine = create_engine(connection)
Base.metadata.create_all(engine)
session = scoped_session(sessionmaker(bind=engine))
# add things to session
session.commit()
postgresql_proc = factories.postgresql_proc(load=[load_database])
postgresql = factories.postgresql('postgresql_proc') # still need to check if this is actually needed or not
@pytest.fixture
def dbsession(postgresql):
connection = f'postgresql+psycopg2://{postgresql.info.user}:@{postgresql.info.host}:{postgresql.info.port}/{postgresql.info.dbname}'
engine = create_engine(connection)
session = scoped_session(sessionmaker(bind=engine))
yield session
# 'Base.metadata.drop_all(engine)' here specifically does not work. It is also not needed. If you leave out the session.close()
# all the tests still run, but you get a warning/error at the end of the tests.
session.close() |
SQLAlchemy example for database initial state - closes #797
We started using this library recently and managed to get our tests working. The problem is that they are really slow, so I wanted to try and use something like the
load_database
that is mentioned in the documentation. The problem is that I have no idea how to do that in our setup. I was wondering if someone knows how to do this, or could point me to an example that is more relevant for our setup.It's a pyramid app and the
dbsession
fixture that now works is:The text was updated successfully, but these errors were encountered: