-
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
psycopg.Connection instance created by DatabaseJanitor's _loader() omits configuration parameters #902
Comments
@michaeljsnyder you'd have to actually have custom loader function and pass it to the fixture instead of the SQL file path. |
Out of curiosity, why are you creating additional database in there? |
Ahh, thanks very much @fizyk -- I saw the note in the docs about the load reference being a function but wasn't entirely sure how to use that approach. Your examples above cleared it up for me and everything is working as expected on my end now. Much appreciated. In case it's helpful for others, this is what I ended up with: from pathlib import Path
from typing import Any
import os, importlib_resources
import psycopg
from pytest_postgresql import factories
# Catalog references to SQL files. Replace \\ with / for Windows path compatibility.
sql_path = str(importlib_resources.files("my_app.config.database_schemas"))
sql_files = [str(Path(os.path.join(sql_path, schema))).replace("\\\\", "/") for schema in os.listdir(sql_path) if schema.endswith(".sql")]
def load_database_with_autocommit(**kwargs: Any) -> None:
"""
Re-implement the native pytest_postgresql SQL loader to enforce autocommit=True. The host, port, user, dbname, and
password are supplied automatically via kwargs.
The set of SQL files to load is pre-cataloged outside this function.
"""
db_connection = psycopg.connect(autocommit=True, **kwargs)
for sql_filename in sql_files:
with open(sql_filename, "r") as sql_file:
with db_connection.cursor() as cur:
cur.execute(sql_file.read())
db_connection.commit()
# Initialize the test databases.
postgresql_my_noproc = factories.postgresql_noproc(
load=[load_database_with_autocommit]
)
postgresql_my = factories.postgresql("postgresql_my_noproc")
The app that I'm working on tests for owns a handful of I think your question is highlighting that I'm not benefitting from the teardown capabilities of the fixtures (was that the misalignment you were seeing?), because the |
@michaeljsnyder the immediate solution that comes to my mind would be an autouse session fixture that does the cleaning at teardown. Are the additional databases you create a part of the SQL file you hand over, or do you need those to test the SQL statements? I'm considering whether the proc fixture could benefit from having a multiple dbnames configured on a single one. |
@fizyk this is a good point, yes the databases that get created are subsequently needed to test the app. I essentially have a set of For example, one partial approach could involve me running a regex on the A cleaner approach probably involves restructuring my |
Thanks for the great package--I might be missing something, but am hitting an issue with one of my current use cases. I have an
.sql
file that I'm trying to use to populate a running database with as part of an integration test, so I'm usingpostgresql_noproc
. The.sql
file requires autocommit to be turned on, but I don't see a way to enforce this with the wayDatabaseJanitor
is currently structured.My simplified Python code:
Inside
pytest.ini
I have my database information specified:And my
test.sql
file has, along with a lot of other content:Running this results in:
psycopg.errors.ActiveSqlTransaction: CREATE DATABASE cannot run inside a transaction block
, which makes sense.Ideally I'd be able to specify the additional
kwargs
I want to pass topsycopg.Connection()
such asautocommit=True
(they default it toFalse
) via thepostgresql_options
key inpytest.ini
(or via some otherini
parameter, if there's a more suitable one), but these configurations are not passed to the connection constructor.DatabaseJanitor calls
_loader()
here which then ultimately callspsycopg.connect()
here, but only thehost
,port
,user
,dbname
, andpassword
arguments are supplied, so there's no way to provide additional configuration topsycopg
.Is there another way to supply a
psycopg.Connection
instance to theDatabaseJanitor
such that I can configure it before the.sql
file loading occurs, or can we adjust the loading functionality to pass through more arguments frompytest.ini
?The text was updated successfully, but these errors were encountered: