-
Notifications
You must be signed in to change notification settings - Fork 53
Allow chaining noprocess fixtures - closes #890 #1259
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| sequenceDiagram | ||
| participant Test as Test | ||
| participant ProcF as base_proc Fixture | ||
| participant NoProc1 as seeded_noproc Fixture | ||
| participant NoProc2 as more_seeded_noproc Fixture | ||
| participant DB as PostgreSQL DB | ||
|
|
||
| Test->>ProcF: request base_proc | ||
| ProcF->>DB: init database & run load_schema | ||
| ProcF-->>Test: return PostgreSQLExecutor | ||
|
|
||
| Test->>NoProc1: request seeded_noproc (depends_on=base_proc) | ||
| NoProc1->>ProcF: read connection/template info | ||
| NoProc1->>DB: create layered DB / run load_data | ||
| NoProc1-->>Test: return NoopExecutor | ||
|
|
||
| Test->>NoProc2: request more_seeded_noproc (depends_on=seeded_noproc) | ||
| NoProc2->>NoProc1: read connection/template info | ||
| NoProc2->>DB: run load_more_data on layered DB | ||
| NoProc2-->>Test: return NoopExecutor | ||
|
|
||
| Test->>Test: validate tables and data across layers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Bump the minimum supported pytest version to 8.2. | ||
|
|
||
| The previous minimum was about two years old, and older pytest versions | ||
| can be flaky with fixture chaining that relies on `getfixturevalue` on | ||
| Python 3.12-3.13 when used alongside xdist. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Refactor ``DatabaseJanitor`` to use explicit template management. This includes a new ``as_template`` flag and making ``dbname`` a required parameter. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add a Mermaid sequence diagram to the documentation to illustrate fixture chaining and hierarchical cloning. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add ``depends_on`` parameter to ``postgresql_noproc`` factory to allow hierarchical cloning and chaining of process fixtures. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| pytest == 7.4; python_version >= "3.14" | ||
| pytest == 7.2; python_version < "3.14" | ||
| pytest == 8.2 | ||
| port-for == 0.7.3 | ||
| mirakuru == 2.6.0 | ||
| psycopg == 3.0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| """Chaining noprocess fixtures tests for pytest-postgresql.""" | ||
|
|
||
| import psycopg | ||
|
|
||
| from pytest_postgresql import factories | ||
| from pytest_postgresql.executor import PostgreSQLExecutor | ||
| from pytest_postgresql.executor_noop import NoopExecutor | ||
|
|
||
|
|
||
| def load_schema(host: str, port: int, user: str, dbname: str, password: str | None) -> None: | ||
| """Load schema into the database.""" | ||
| with psycopg.connect(host=host, port=port, user=user, dbname=dbname, password=password) as conn: | ||
| with conn.cursor() as cur: | ||
| cur.execute("CREATE TABLE schema_table (id serial PRIMARY KEY, name varchar);") | ||
| conn.commit() | ||
|
|
||
|
|
||
| def load_data(host: str, port: int, user: str, dbname: str, password: str | None) -> None: | ||
| """Load the first layer of data into the database.""" | ||
| with psycopg.connect(host=host, port=port, user=user, dbname=dbname, password=password) as conn: | ||
| with conn.cursor() as cur: | ||
| cur.execute("INSERT INTO schema_table (name) VALUES ('data_layer');") | ||
| cur.execute("CREATE TABLE data_table (id serial PRIMARY KEY, val varchar);") | ||
| conn.commit() | ||
|
|
||
|
|
||
| def load_more_data(host: str, port: int, user: str, dbname: str, password: str | None) -> None: | ||
| """Load the second layer of data into the database.""" | ||
| with psycopg.connect(host=host, port=port, user=user, dbname=dbname, password=password) as conn: | ||
| with conn.cursor() as cur: | ||
| cur.execute("INSERT INTO schema_table (name) VALUES ('more_data_layer');") | ||
| cur.execute("CREATE TABLE more_data_table (id serial PRIMARY KEY, extra varchar);") | ||
| conn.commit() | ||
|
|
||
|
|
||
| # Chaining: proc -> noproc -> client | ||
| base_proc = factories.postgresql_proc(load=[load_schema]) | ||
| seeded_noproc = factories.postgresql_noproc(depends_on="base_proc", load=[load_data]) | ||
| client_layered = factories.postgresql("seeded_noproc") | ||
|
|
||
| # Deeper chaining: proc -> noproc -> noproc -> client | ||
| more_seeded_noproc = factories.postgresql_noproc(depends_on="seeded_noproc", load=[load_more_data]) | ||
| client_deep_layered = factories.postgresql("more_seeded_noproc") | ||
|
|
||
|
|
||
| def test_chaining_two_layers(client_layered: psycopg.Connection) -> None: | ||
| """Test that data from both proc and noproc layers is present.""" | ||
| with client_layered.cursor() as cur: | ||
| # From base_proc (load_schema) | ||
| cur.execute("SELECT count(*) FROM information_schema.tables WHERE table_name = 'schema_table';") | ||
| res = cur.fetchone() | ||
| assert res | ||
| assert res[0] == 1 | ||
|
|
||
| # From seeded_noproc (load_data) | ||
| cur.execute("SELECT count(*) FROM information_schema.tables WHERE table_name = 'data_table';") | ||
| res = cur.fetchone() | ||
| assert res | ||
| assert res[0] == 1 | ||
|
|
||
| # Data inserted in seeded_noproc | ||
| cur.execute("SELECT name FROM schema_table;") | ||
| res = cur.fetchone() | ||
| assert res | ||
| assert res[0] == "data_layer" | ||
|
|
||
|
|
||
| def test_chaining_three_layers(client_deep_layered: psycopg.Connection) -> None: | ||
| """Test that data from all three layers is present.""" | ||
| with client_deep_layered.cursor() as cur: | ||
| # From base_proc | ||
| cur.execute("SELECT count(*) FROM information_schema.tables WHERE table_name = 'schema_table';") | ||
| res = cur.fetchone() | ||
| assert res | ||
| assert res[0] == 1 | ||
|
|
||
| # From seeded_noproc | ||
| cur.execute("SELECT count(*) FROM information_schema.tables WHERE table_name = 'data_table';") | ||
| res = cur.fetchone() | ||
| assert res | ||
| assert res[0] == 1 | ||
|
|
||
| # From more_seeded_noproc | ||
| cur.execute("SELECT count(*) FROM information_schema.tables WHERE table_name = 'more_data_table';") | ||
| res = cur.fetchone() | ||
| assert res | ||
| assert res[0] == 1 | ||
|
|
||
| # Data from multiple layers | ||
| cur.execute("SELECT name FROM schema_table ORDER BY id;") | ||
| results = cur.fetchall() | ||
| assert results[0][0] == "data_layer" | ||
| assert results[1][0] == "more_data_layer" | ||
|
|
||
|
|
||
| def test_inheritance(base_proc: PostgreSQLExecutor, seeded_noproc: NoopExecutor) -> None: | ||
| """Verify that connection parameters are inherited from the base fixture.""" | ||
| assert seeded_noproc.host == base_proc.host | ||
| assert seeded_noproc.port == base_proc.port | ||
| assert seeded_noproc.user == base_proc.user | ||
| assert seeded_noproc.password == base_proc.password |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.