Skip to content

Commit

Permalink
Fix example to work with Pydantic V2, add test for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dantheman39 authored Feb 1, 2024
2 parents 2a9dbea + 8cee335 commit 9712453
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ dmypy.json

# PyCharm / IntelliJ
.idea/

# vim
*.swp
*.swo
13 changes: 8 additions & 5 deletions examples/departments.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class SalaryModel(pydantic.BaseModel):


class EmployeeModel(PersonModel):
hired_on: datetime.datetime = None
salary: T.Optional[SalaryModel]
hired_on: T.Optional[datetime.datetime] = None
salary: T.Optional[SalaryModel] = None


class ManagerModel(EmployeeModel):
Expand Down Expand Up @@ -96,13 +96,13 @@ def resolve_list_departments(self, info):
salary=SalaryModel(rating="GS-9", amount=75000.23),
hired_on=datetime.datetime(2019, 1, 1, 15, 26),
),
EmployeeModel(id=uuid.uuid4(), name="Derek"),
EmployeeModel(id=uuid.uuid4(), name="Derek", salary=None),
],
)
]


if __name__ == "__main__":
def main():
schema = graphene.Schema(query=Query)
query = """
query {
Expand All @@ -128,7 +128,10 @@ def resolve_list_departments(self, info):
}
}
"""
result = schema.execute(query)
return schema.execute(query)


if __name__ == "__main__":
result = main()
print(result.errors)
print(json.dumps(result.data, indent=2))
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "graphene_pydantic"
version = "0.6.0"
version = "0.6.1"
description = "Graphene Pydantic integration"
readme = "README.md"
repository = "https://github.com/graphql-python/graphene-pydantic"
Expand Down
Empty file added tests/test_examples/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions tests/test_examples/test_departments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from examples import departments


def test_departments():
result = departments.main()
assert not result.errors

deps = result.data["listDepartments"]
assert len(deps) == 1

employees = deps[0]["employees"]
assert len(employees) == 3

def employee_by_name(employees, name):
return [e for e in employees if e["name"] == name][0]

jason = employee_by_name(employees, "Jason")
carmen = employee_by_name(employees, "Carmen")
derek = employee_by_name(employees, "Derek")

# Jason is a manager
assert jason["teamSize"] == 2
assert carmen.get("teamSize") is None

# some sanity checks on optional fields,
# knowing what the test data is
assert jason.get("hiredOn") is None
assert carmen.get("hiredOn") is not None
assert carmen["salary"]["rating"] == "GS-9"
assert derek["salary"] is None

0 comments on commit 9712453

Please sign in to comment.