Skip to content

Commit

Permalink
More skips
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick91 committed Jul 14, 2023
1 parent b161dff commit eda70bf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
13 changes: 10 additions & 3 deletions tests/django/conftest.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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())
41 changes: 26 additions & 15 deletions tests/django/test_dataloaders.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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:
Expand Down

0 comments on commit eda70bf

Please sign in to comment.