-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
cleanup_database
option to postgres fixture.
Note, this could **potentially** reveal preexisting issues in code under test, that could be perceived as a "breaking" change. By deleting the database under test at the end of the test's execution, any database connections left connected to the database might cause the `DELETE DATABASE` command to fail. PMR will **try** to use the `WITH FORCE` option on database versions >= 13.0, but that option does not exist on prior versions of postgres. In any case, if this happens, it **is** ultimately revealing a "bug" in the code it is testing. Additionally, you can simply turn off database cleanup in one of various ways.
- Loading branch information
Showing
8 changed files
with
167 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "pytest-mock-resources" | ||
version = "2.12.1" | ||
version = "2.13.0" | ||
description = "A pytest plugin for easily instantiating reproducible mock resources." | ||
authors = [ | ||
"Omar Khan <[email protected]>", | ||
|
This file contains 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 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 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,45 @@ | ||
from sqlalchemy import Column, String, text | ||
|
||
from pytest_mock_resources import create_postgres_fixture, Rows | ||
from pytest_mock_resources.compat.sqlalchemy import declarative_base | ||
|
||
Base = declarative_base() | ||
|
||
|
||
class User(Base): | ||
__tablename__ = "user" | ||
|
||
name = Column(String, primary_key=True) | ||
|
||
|
||
rows = Rows(User(name="Harold"), User(name="Gump")) | ||
|
||
|
||
pg_no_clean = create_postgres_fixture(Base, rows, session=True, cleanup_database=False) | ||
|
||
|
||
non_cleaned_database_name = None | ||
|
||
|
||
def test_not_to_be_cleaned_up(pg_no_clean): | ||
global non_cleaned_database_name | ||
non_cleaned_database_name = pg_no_clean.pmr_credentials.database | ||
|
||
names = [u.name for u in pg_no_clean.query(User).all()] | ||
assert names == ["Harold", "Gump"] | ||
|
||
names = pg_no_clean.execute(text("SELECT datname FROM pg_database")).all() | ||
unique_names = {name for (name,) in names} | ||
assert non_cleaned_database_name in unique_names | ||
|
||
|
||
def test_database_is_not_cleaned_up(pg_no_clean): | ||
global non_cleaned_database_name | ||
|
||
assert non_cleaned_database_name is not None | ||
|
||
assert non_cleaned_database_name != pg_no_clean.pmr_credentials.database | ||
|
||
names = pg_no_clean.execute(text("SELECT datname FROM pg_database")).all() | ||
unique_names = {name for (name,) in names} | ||
assert non_cleaned_database_name in unique_names |
This file contains 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 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 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 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