Skip to content

Commit

Permalink
Drop support for loading schema and data fixture from client fixture -
Browse files Browse the repository at this point in the history
…closes #1087
  • Loading branch information
fizyk committed Feb 13, 2025
1 parent d9f03fb commit 649ec44
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 68 deletions.
41 changes: 8 additions & 33 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,45 +90,20 @@ Pre-populating the database for tests

If you want the database fixture to be automatically pre-populated with your schema and data, there are two lewels you can achieve it:

#. per test in a client fixture
#. per test in a client fixture, by an intermediary fixture between client and your test (or other fixtures)
#. per session in a process fixture

Both fixtures are accepting same set of possible loaders:
The process fixture accepts a load parameter, which accepts these loaders:

* sql file path - which will load and execute sql files
* loading functions - either by string import path, actual callable. Loading functions will receive **host**, **port**, **user**, **dbname** and **password** arguments and will have to perform
* loading functions - either by string import path, actual callable.
Loading functions will receive **host**, **port**, **user**, **dbname** and **password** arguments and will have to perform
connection to the database inside. Or start session in the ORM of your choice to perform actions with given ORM.
This way, you'd be able to trigger ORM based data manipulations, or even trigger database migrations programmatically.


Per test in a client fixture
++++++++++++++++++++++++++++

Client fixture loads are performed the database each test. Are useful if you create several clients for single process fixture.


.. code-block:: python
from pathlib import Path
postgresql_my_with_schema = factories.postgresql(
'postgresql_my_proc',
load=[Path("schemafile.sql"), Path("otherschema.sql"), "import.path.to.function", "import.path.to:otherfunction", load_this]
)
.. warning::

The database is dropped after each test and before each test using this fixture, it will load the schema/data again.

.. warning::

client level pre-population is deprecated. Same functionality can be achieved by intermediary fixture between client and test itself.


Per session in a process fixture
++++++++++++++++++++++++++++++++

The process fixture pre-populates the database once per test session, and loads the schema and data into the template database.
Client fixture then creates test database out of the template database each test, which significantly **speeds up the tests**.
The process fixture pre-populates the database once per test session (at the start of the process fixture),
and loads the schema and data into the template database. Client fixture then creates test database out of the template database each test,
which significantly **speeds up the tests**.

.. code-block:: python
Expand All @@ -138,7 +113,7 @@ Client fixture then creates test database out of the template database each test
)
Additional benefit, is that test code might safely use separate database connection, and can safely test it's behaviour with transactions and rollbacks,
as tests and code will work on separate database client instances.
as tests and code will work on separate database connections.

Defining pre-populate on command line:

Expand Down
22 changes: 2 additions & 20 deletions pytest_postgresql/factories/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with pytest-postgresql. If not, see <http://www.gnu.org/licenses/>.
"""Fixture factory for postgresql client."""
import warnings
from pathlib import Path
from typing import Callable, Iterator, List, Optional, Union
from typing import Callable, Iterator, Optional, Union

import psycopg
import pytest
Expand All @@ -33,15 +31,12 @@
def postgresql(
process_fixture_name: str,
dbname: Optional[str] = None,
load: Optional[List[Union[Callable, str, Path]]] = None,
isolation_level: "Optional[psycopg.IsolationLevel]" = None,
) -> Callable[[FixtureRequest], Iterator[Connection]]:
"""Return connection fixture factory for PostgreSQL.
:param process_fixture_name: name of the process fixture
:param dbname: database name
:param load: SQL, function or function import paths to automatically load
into our test database
:param isolation_level: optional postgresql isolation level
defaults to server's default
:returns: function which makes a connection to postgresql
Expand All @@ -64,17 +59,6 @@ def postgresql_factory(request: FixtureRequest) -> Iterator[Connection]:
pg_password = proc_fixture.password
pg_options = proc_fixture.options
pg_db = dbname or proc_fixture.dbname
pg_load = load or []
if pg_load:
warnings.warn(
message=(
"Load is deprecated on a client fixture. "
"You should either process fixture load parameter to pre-fill database, "
"or add a fixture between client and a test, "
"that will fill the database with the data."
),
category=DeprecationWarning,
)
with DatabaseJanitor(
user=pg_user,
host=pg_host,
Expand All @@ -84,7 +68,7 @@ def postgresql_factory(request: FixtureRequest) -> Iterator[Connection]:
version=proc_fixture.version,
password=pg_password,
isolation_level=isolation_level,
) as janitor:
):
db_connection: Connection = psycopg.connect(
dbname=pg_db,
user=pg_user,
Expand All @@ -93,8 +77,6 @@ def postgresql_factory(request: FixtureRequest) -> Iterator[Connection]:
port=pg_port,
options=pg_options,
)
for load_element in pg_load:
janitor.load(load_element)
yield db_connection
db_connection.close()

Expand Down
14 changes: 2 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,5 @@

postgresql_proc2 = factories.postgresql_proc(port=None)
postgresql2 = factories.postgresql("postgresql_proc2", dbname="test-db")
postgresql_load_1 = factories.postgresql(
"postgresql_proc2",
dbname="test-load-db",
load=[
TEST_SQL_FILE,
],
)
postgresql_load_2 = factories.postgresql(
"postgresql_proc2",
dbname="test-load-moredb",
load=[TEST_SQL_FILE, TEST_SQL_FILE2],
)
postgresql_load_1 = factories.postgresql("postgresql_proc2")
postgresql_load_2 = factories.postgresql("postgresql_proc2", dbname="test-load-moredb")
4 changes: 1 addition & 3 deletions tests/docker/test_noproc_docker.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Noproc fixture tests."""

import pathlib

import pytest
from psycopg import Connection

Expand All @@ -11,7 +9,7 @@

postgresql_my_proc = pytest_postgresql.factories.noprocess.postgresql_noproc()
postgres_with_schema = pytest_postgresql.factories.client.postgresql(
"postgresql_my_proc", dbname="test", load=[pathlib.Path("tests/test_sql/eidastats.sql")]
"postgresql_my_proc", dbname="test"
)

postgresql_my_proc_template = pytest_postgresql.factories.noprocess.postgresql_noproc(
Expand Down

0 comments on commit 649ec44

Please sign in to comment.