Skip to content

Commit

Permalink
Add failing tests for lazy lists
Browse files Browse the repository at this point in the history
  • Loading branch information
ddoroshev authored and jeich committed Feb 20, 2024
1 parent b310b16 commit 7b88a53
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 2 deletions.
8 changes: 7 additions & 1 deletion tests/b.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING, List, Optional
from typing_extensions import Annotated

import strawberry
Expand All @@ -19,6 +19,12 @@ async def a(self) -> Annotated[A, strawberry.lazy("tests.a"), object()]:

return A(id=self.id)

@strawberry.field
async def a_list(self) -> List[Annotated[A, strawberry.lazy("tests.a")]]:
from tests.a import A

Check warning on line 24 in tests/b.py

View check run for this annotation

Codecov / codecov/patch

tests/b.py#L24

Added line #L24 was not covered by tests

return [A(id=self.id)]

Check warning on line 26 in tests/b.py

View check run for this annotation

Codecov / codecov/patch

tests/b.py#L26

Added line #L26 was not covered by tests

@strawberry.field
async def optional_a(
self,
Expand Down
8 changes: 8 additions & 0 deletions tests/c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations

import strawberry


@strawberry.type
class C:
id: strawberry.ID
20 changes: 20 additions & 0 deletions tests/d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from __future__ import annotations

from typing import TYPE_CHECKING, List
from typing_extensions import Annotated

import strawberry

if TYPE_CHECKING:
from tests.c import C


@strawberry.type
class D:
id: strawberry.ID

@strawberry.field
async def c_list(self) -> List[Annotated[C, strawberry.lazy("tests.c")]]:
from tests.c import C

Check warning on line 18 in tests/d.py

View check run for this annotation

Codecov / codecov/patch

tests/d.py#L18

Added line #L18 was not covered by tests

return [C(id=self.id)]

Check warning on line 20 in tests/d.py

View check run for this annotation

Codecov / codecov/patch

tests/d.py#L20

Added line #L20 was not covered by tests
6 changes: 6 additions & 0 deletions tests/schema/test_lazy/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class Query:
type TypeB {
typeA: TypeA!
typeAList: [TypeA!]!
typeCList: [TypeC!]!
}
type TypeC {
name: String!
}
"""

Expand Down
6 changes: 6 additions & 0 deletions tests/schema/test_lazy/test_lazy_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,17 @@ class Query:
type TypeB {
typeA: TypeA!
typeAList: [TypeA!]!
typeCList: [TypeC!]!
}
type TypeBEdge {
node: TypeB!
}
type TypeC {
name: String!
}
"""
).strip()

Expand Down
28 changes: 27 additions & 1 deletion tests/schema/test_lazy/type_b.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, List
from typing_extensions import Annotated

import strawberry

if TYPE_CHECKING:
from .type_a import TypeA
from .type_c import TypeC

ListTypeA = List[TypeA]
ListTypeC = List[TypeC]
else:
TypeA = Annotated["TypeA", strawberry.lazy("tests.schema.test_lazy.type_a")]
ListTypeA = List[
Annotated["TypeA", strawberry.lazy("tests.schema.test_lazy.type_a")]
]
ListTypeC = List[
Annotated["TypeC", strawberry.lazy("tests.schema.test_lazy.type_c")]
]


@strawberry.type
Expand All @@ -18,3 +28,19 @@ def type_a(
from .type_a import TypeA

return TypeA()

@strawberry.field()
def type_a_list(
self,
) -> ListTypeA:
from .type_a import TypeA

Check warning on line 36 in tests/schema/test_lazy/type_b.py

View check run for this annotation

Codecov / codecov/patch

tests/schema/test_lazy/type_b.py#L36

Added line #L36 was not covered by tests

return [TypeA()]

Check warning on line 38 in tests/schema/test_lazy/type_b.py

View check run for this annotation

Codecov / codecov/patch

tests/schema/test_lazy/type_b.py#L38

Added line #L38 was not covered by tests

@strawberry.field()
def type_c_list(
self,
) -> ListTypeC:
from .type_c import TypeC

Check warning on line 44 in tests/schema/test_lazy/type_b.py

View check run for this annotation

Codecov / codecov/patch

tests/schema/test_lazy/type_b.py#L44

Added line #L44 was not covered by tests

return [TypeC()]

Check warning on line 46 in tests/schema/test_lazy/type_b.py

View check run for this annotation

Codecov / codecov/patch

tests/schema/test_lazy/type_b.py#L46

Added line #L46 was not covered by tests
32 changes: 32 additions & 0 deletions tests/test_forward_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from strawberry.scalars import JSON
from strawberry.type import StrawberryList, StrawberryOptional
from tests.a import A
from tests.d import D


def test_forward_reference():
Expand Down Expand Up @@ -74,6 +75,7 @@ async def a(self) -> A:
type B {
id: ID!
a: A!
aList: [A!]!
optionalA: A
optionalA2: A
}
Expand All @@ -87,6 +89,36 @@ async def a(self) -> A:
assert print_schema(schema) == textwrap.dedent(expected_representation).strip()


@pytest.mark.skipif(
sys.version_info < (3, 9),
reason="Python 3.8 and previous can't properly resolve this.",
)
def test_lazy_forward_reference_schema_with_a_list_only():
@strawberry.type
class Query:
@strawberry.field
async def d(self) -> D:
return D(id=strawberry.ID("1"))

Check warning on line 101 in tests/test_forward_references.py

View check run for this annotation

Codecov / codecov/patch

tests/test_forward_references.py#L101

Added line #L101 was not covered by tests

expected_representation = """
type C {
id: ID!
}
type D {
id: ID!
cList: [C!]!
}
type Query {
d: D!
}
"""

schema = strawberry.Schema(query=Query)
assert print_schema(schema) == textwrap.dedent(expected_representation).strip()


def test_with_resolver():
global User

Expand Down

0 comments on commit 7b88a53

Please sign in to comment.