Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix example to work with Pydantic V2, add test for examples #99

Merged
merged 4 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@necaris any opinions on the version bump being in the same or separate PR? Happy to comply.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same PR seems fine to me.

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
Loading