From 649ec440ca8cc966e7452ed4acb656c607b74934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=C5=9Aliwi=C5=84ski?= Date: Thu, 13 Feb 2025 12:33:55 +0100 Subject: [PATCH] Drop support for loading schema and data fixture from client fixture - closes #1087 --- README.rst | 41 ++++++--------------------- pytest_postgresql/factories/client.py | 22 ++------------ tests/conftest.py | 14 ++------- tests/docker/test_noproc_docker.py | 4 +-- 4 files changed, 13 insertions(+), 68 deletions(-) diff --git a/README.rst b/README.rst index cca12931..59e129c3 100644 --- a/README.rst +++ b/README.rst @@ -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 @@ -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: diff --git a/pytest_postgresql/factories/client.py b/pytest_postgresql/factories/client.py index d016c532..4351552f 100644 --- a/pytest_postgresql/factories/client.py +++ b/pytest_postgresql/factories/client.py @@ -16,9 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with pytest-postgresql. If not, see . """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 @@ -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 @@ -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, @@ -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, @@ -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() diff --git a/tests/conftest.py b/tests/conftest.py index a75e526e..c1855f0a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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") diff --git a/tests/docker/test_noproc_docker.py b/tests/docker/test_noproc_docker.py index 79b8c1eb..0a357acf 100644 --- a/tests/docker/test_noproc_docker.py +++ b/tests/docker/test_noproc_docker.py @@ -1,7 +1,5 @@ """Noproc fixture tests.""" -import pathlib - import pytest from psycopg import Connection @@ -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(