-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Address the issue where pytest-postgresql incorrectly determined the …
…path to sql file - closes #638
- Loading branch information
Showing
14 changed files
with
108 additions
and
39 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
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,3 @@ | ||
Now all sql files used to initialise database for tests, has to be passed as pathlib.Path instance. | ||
|
||
This helps the DatabaseJanitor choose correct behaviour based on parameter. |
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
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,32 @@ | ||
"""Loader helper functions.""" | ||
|
||
import re | ||
from functools import partial | ||
from pathlib import Path | ||
from typing import Any, Callable, Union | ||
|
||
import psycopg | ||
|
||
|
||
def build_loader(load: Union[Callable, str, Path]) -> Callable: | ||
"""Build a loader callable.""" | ||
if isinstance(load, Path): | ||
return partial(sql, load) | ||
elif isinstance(load, str): | ||
loader_parts = re.split("[.:]", load, 2) | ||
import_path = ".".join(loader_parts[:-1]) | ||
loader_name = loader_parts[-1] | ||
_temp_import = __import__(import_path, globals(), locals(), fromlist=[loader_name]) | ||
_loader: Callable = getattr(_temp_import, loader_name) | ||
return _loader | ||
else: | ||
return load | ||
|
||
|
||
def sql(sql_filename: Path, **kwargs: Any) -> None: | ||
"""Database loader for sql files.""" | ||
db_connection = psycopg.connect(**kwargs) | ||
with open(sql_filename, "r") as _fd: | ||
with db_connection.cursor() as cur: | ||
cur.execute(_fd.read()) | ||
db_connection.commit() |
This file was deleted.
Oops, something went wrong.
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,2 @@ | ||
CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar); | ||
INSERT INTO test VALUES(1, 2, 'c'); |
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,12 @@ | ||
"""All tests for pytest-postgresql.""" | ||
|
||
from psycopg import Connection | ||
|
||
|
||
def test_postgres_load_one_file(postgresql: Connection) -> None: | ||
"""Check postgresql fixture can load one file.""" | ||
cur = postgresql.cursor() | ||
cur.execute("SELECT * FROM test;") | ||
results = cur.fetchall() | ||
assert len(results) == 1 | ||
cur.close() |
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,20 @@ | ||
"""Tests for the `build_loader` function.""" | ||
|
||
from pathlib import Path | ||
|
||
from pytest_postgresql.loader import build_loader, sql | ||
from tests.loader import load_database | ||
|
||
|
||
def test_loader_callables() -> None: | ||
"""Test handling callables in build_loader.""" | ||
assert load_database == build_loader(load_database) | ||
assert load_database == build_loader("tests.loader:load_database") | ||
|
||
|
||
def test_loader_sql() -> None: | ||
"""Test returning partial running sql for the sql file path.""" | ||
sql_path = Path("test_sql/eidastats.sql") | ||
loader_func = build_loader(sql_path) | ||
assert loader_func.args == (sql_path,) # type: ignore | ||
assert loader_func.func == sql # type: ignore |
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