From eda70bfed27fa9e3f245f9bac8f1f2800ddc41aa Mon Sep 17 00:00:00 2001 From: Patrick Arminio Date: Fri, 14 Jul 2023 15:11:28 +0100 Subject: [PATCH] More skips --- tests/django/conftest.py | 13 +++++++--- tests/django/test_dataloaders.py | 41 ++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/tests/django/conftest.py b/tests/django/conftest.py index 12330d59b8..133f09e760 100644 --- a/tests/django/conftest.py +++ b/tests/django/conftest.py @@ -1,12 +1,15 @@ +from __future__ import annotations + import pathlib +from typing import TYPE_CHECKING, List import pytest -from django.test.client import Client -from strawberry.django.test import GraphQLTestClient +if TYPE_CHECKING: + from strawberry.django.test import GraphQLTestClient -def pytest_collection_modifyitems(config, items): +def pytest_collection_modifyitems(config: pytest.Config, items: List[pytest.Item]): # automatically mark tests with 'django' if they are in the django subfolder rootdir = pathlib.Path(config.rootdir) @@ -20,4 +23,8 @@ def pytest_collection_modifyitems(config, items): @pytest.fixture() def graphql_client() -> GraphQLTestClient: + from django.test.client import Client + + from strawberry.django.test import GraphQLTestClient + return GraphQLTestClient(Client()) diff --git a/tests/django/test_dataloaders.py b/tests/django/test_dataloaders.py index 1036094fc3..835e6a5c1e 100644 --- a/tests/django/test_dataloaders.py +++ b/tests/django/test_dataloaders.py @@ -1,38 +1,49 @@ import json -from typing import List +from typing import List, Tuple -import django import pytest from asgiref.sync import sync_to_async -from django.test.client import RequestFactory +from pytest_mock import MockerFixture import strawberry from strawberry.dataloader import DataLoader -from strawberry.django.views import AsyncGraphQLView -from .app.models import Example +try: + import django + + DJANGO_VERSION: Tuple[int, int, int] = django.VERSION +except ImportError: + DJANGO_VERSION = (0, 0, 0) + pytestmark = [ pytest.mark.asyncio, pytest.mark.skipif( - django.VERSION < (3, 1), + DJANGO_VERSION < (3, 1), reason="Async views are only supported in Django >= 3.1", ), ] def _prepare_db(): - ids = [] - - for index in range(5): - ids.append(Example.objects.create(name=f"This is a demo async {index}").id) + from .app.models import Example - return ids + return [ + Example.objects.create(name=f"This is a demo async {index}").pk + for index in range(5) + ] +@pytest.mark.django @pytest.mark.django_db -async def test_fetch_data_from_db(mocker): - def _sync_batch_load(keys): +async def test_fetch_data_from_db(mocker: MockerFixture): + from django.test.client import RequestFactory + + from strawberry.django.views import AsyncGraphQLView + + from .app.models import Example + + def _sync_batch_load(keys: List[str]): data = Example.objects.filter(id__in=keys) return list(data) @@ -42,12 +53,12 @@ def _sync_batch_load(keys): ids = await prepare_db() - async def idx(keys) -> List[Example]: + async def idx(keys: List[str]) -> List[Example]: return await batch_load(keys) mock_loader = mocker.Mock(side_effect=idx) - loader = DataLoader(load_fn=mock_loader) + loader = DataLoader[str, Example](load_fn=mock_loader) @strawberry.type class Query: