Skip to content

Commit

Permalink
Add tests for pydantic conversion for contrained list
Browse files Browse the repository at this point in the history
  • Loading branch information
tjeerddie committed Jul 31, 2023
1 parent fce75cc commit 7e869b1
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
29 changes: 29 additions & 0 deletions tests/experimental/pydantic/schema/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,32 @@ def user(self) -> User:
assert not result.errors
assert result.data["user"]["age"] == 1
assert result.data["user"]["password"] is None


def test_basic_type_with_constrained_list():
class FriendList(pydantic.ConstrainedList):
min_items = 1

class UserModel(pydantic.BaseModel):
age: int
friend_names: FriendList[str]

@strawberry.experimental.pydantic.type(UserModel)
class User:
age: strawberry.auto
friend_names: strawberry.auto

@strawberry.type
class Query:
@strawberry.field
def user(self) -> User:
return User(age=1, friend_names=["A", "B"])

schema = strawberry.Schema(query=Query)

query = "{ user { friendNames } }"

result = schema.execute_sync(query)

assert not result.errors
assert result.data["user"]["friendNames"] == ["A", "B"]
65 changes: 63 additions & 2 deletions tests/experimental/pydantic/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import re
import sys
from enum import Enum
from typing import Any, Dict, List, NewType, Optional, Union, cast
from typing import Any, Dict, List, NewType, Optional, TypeVar, Union, cast

import pytest
from pydantic import BaseConfig, BaseModel, Field, ValidationError
from pydantic import BaseConfig, BaseModel, ConstrainedList, Field, ValidationError
from pydantic.fields import ModelField
from pydantic.typing import NoArgAnyCallable

Expand Down Expand Up @@ -1198,3 +1198,64 @@ class Test:

assert test.optional_list == [1, 2, 3]
assert test.optional_str is None


def test_can_convert_pydantic_type_to_strawberry_with_constrained_list():
class WorkModel(BaseModel):
name: str

class workList(ConstrainedList):
min_items = 1

class UserModel(BaseModel):
work: workList[WorkModel]

@strawberry.experimental.pydantic.type(WorkModel)
class Work:
name: strawberry.auto

@strawberry.experimental.pydantic.type(UserModel)
class User:
work: strawberry.auto

origin_user = UserModel(
work=[WorkModel(name="developer"), WorkModel(name="tester")]
)

user = User.from_pydantic(origin_user)

assert user == User(work=[Work(name="developer"), Work(name="tester")])


SI = TypeVar("SI", covariant=True) # pragma: no mutate


class SpecialList(List[SI]):
pass


def test_can_convert_pydantic_type_to_strawberry_with_specialized_list():
class WorkModel(BaseModel):
name: str

class workList(SpecialList[SI]):
min_items = 1

class UserModel(BaseModel):
work: workList[WorkModel]

@strawberry.experimental.pydantic.type(WorkModel)
class Work:
name: strawberry.auto

@strawberry.experimental.pydantic.type(UserModel)
class User:
work: strawberry.auto

origin_user = UserModel(
work=[WorkModel(name="developer"), WorkModel(name="tester")]
)

user = User.from_pydantic(origin_user)

assert user == User(work=[Work(name="developer"), Work(name="tester")])

0 comments on commit 7e869b1

Please sign in to comment.