Skip to content

Commit

Permalink
fix CI failures.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCardin committed Apr 15, 2024
1 parent 1f32eee commit 80fea86
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
10 changes: 10 additions & 0 deletions src/pytest_mock_resources/compat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import importlib.resources
import sys

from pytest_mock_resources.compat.import_ import ImportAdaptor

# isort: split
Expand Down Expand Up @@ -70,6 +73,13 @@
pymysql = ImportAdaptor("pymysql", "mysql")


def get_resource(package: str) -> str:
if sys.version_info >= (3, 9):
return str(importlib.resources.files(package))

return str(importlib.resources.path(package, "").__enter__())


__all__ = [
"sqlalchemy",
"psycopg2",
Expand Down
31 changes: 16 additions & 15 deletions src/pytest_mock_resources/fixture/postgresql.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import importlib.resources
import inspect
import logging
from typing import cast
from typing import cast, Tuple, TYPE_CHECKING

import pytest
import sqlalchemy
from sqlalchemy import text
from sqlalchemy.engine import Connection, Engine

from pytest_mock_resources.compat import get_resource
from pytest_mock_resources.container.base import (
async_retry,
DEFAULT_RETRIES,
Expand All @@ -29,6 +29,9 @@
normalize_actions,
)

if TYPE_CHECKING:
from sqlalchemy.ext.asyncio import AsyncEngine

log = logging.getLogger(__name__)


Expand Down Expand Up @@ -118,6 +121,7 @@ def create_postgres_fixture(
name=scope_fixture_name,
cleanup_databases=cleanup_databases,
async_=async_,
uses=("pmr_postgres_container",),
)

if async_:
Expand Down Expand Up @@ -167,6 +171,7 @@ def register_scope_fixture(
name,
cleanup_databases: bool = False,
async_: bool = False,
uses: Tuple[str, ...] = (),
):
if async_:
scope_fixture = _async_scope_fixture
Expand All @@ -177,7 +182,7 @@ def register_scope_fixture(

caller_globals = find_caller_globals()
if name not in caller_globals:
caller_globals[name] = fixture_fn(
fixture = fixture_fn(
scope_fixture(
engine_manager_kwargs,
engine_kwargs=engine_kwargs,
Expand All @@ -186,6 +191,8 @@ def register_scope_fixture(
scope="session",
name=name,
)
fixture_uses = pytest.mark.usefixtures(*uses)
caller_globals[name] = fixture_uses(fixture)


def _sync_scope_fixture(
Expand Down Expand Up @@ -280,11 +287,9 @@ def _async_scope_fixture(
cleanup_databases: bool = False,
name="postgres",
):
from sqlalchemy.ext.asyncio import AsyncEngine

async def fixture(pmr_postgres_config):
root_engine = cast(
AsyncEngine,
"AsyncEngine",
get_sqlalchemy_engine(
pmr_postgres_config,
pmr_postgres_config.root_database,
Expand Down Expand Up @@ -314,7 +319,7 @@ async def fixture(pmr_postgres_config):
assert template_database

engine = cast(
AsyncEngine,
"AsyncEngine",
get_sqlalchemy_engine(
pmr_postgres_config, template_database, **engine_kwargs, async_=True
),
Expand Down Expand Up @@ -342,10 +347,8 @@ async def _async_fixture(
engine_manager: EngineManager,
cleanup_databases: bool = False,
):
from sqlalchemy.ext.asyncio import AsyncEngine

root_engine = cast(
AsyncEngine,
"AsyncEngine",
get_sqlalchemy_engine(
pmr_config,
pmr_config.root_database,
Expand Down Expand Up @@ -480,13 +483,11 @@ def _sync_drop_database(conn: Connection, database_name: str):


def find_caller_globals():
ignore_paths = (
str(importlib.resources.files("pytest_mock_resources")),
str(importlib.resources.files("pdb")),
)
"""Return the stackframe of the calling function of the `create_<x_fixture` call."""
ignore_path = get_resource("pytest_mock_resources")
stack = inspect.stack()
for item in stack:
if item.filename.startswith(ignore_paths):
if item.filename.startswith(ignore_path) or item.filename.endswith("pdb.py"):
continue

frame = item.frame
Expand Down
1 change: 1 addition & 0 deletions src/pytest_mock_resources/fixture/redshift/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def create_redshift_fixture(
name=scope_fixture_name,
cleanup_databases=cleanup_databases,
async_=async_,
uses=("pmr_redshift_container",),
)

if async_:
Expand Down

0 comments on commit 80fea86

Please sign in to comment.