-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OneToOne and ManyToMany models are nullable (#8)
These foreign key relationships should be nullable and as such this should be reflected in the schema and when making a model instance into a schema. Prior to this this would throw an error, as it was not recognized as nullable.
- Loading branch information
1 parent
999984f
commit c1e68e3
Showing
3 changed files
with
63 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,38 @@ | ||
import pytest | ||
from pydantic import ConfigDict | ||
from testapp.models import Configuration, Listing, Preference, Record, Searchable | ||
from testapp.models import Configuration, Listing, Preference, Record, Searchable, User | ||
|
||
from djantic import ModelSchema | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_schema_without_include_and_exclude(): | ||
""" | ||
Using a schema without include and exclude populates userschema correctly | ||
Optional foreign key handled gracefully. | ||
""" | ||
|
||
user = User.objects.create( | ||
first_name="Jordan", last_name="Eremieff", email="[email protected]" | ||
) | ||
|
||
class UserSchema(ModelSchema): | ||
model_config = ConfigDict(model=User) | ||
|
||
dumped = UserSchema.from_django(user).model_dump() | ||
# These values are here, but a hassle to use freeze gun. | ||
dumped.pop("updated_at") | ||
dumped.pop("created_at") | ||
|
||
assert dumped == { | ||
"first_name": "Jordan", | ||
"id": 1, | ||
"email": "[email protected]", | ||
"profile": None, | ||
"last_name": "Eremieff", | ||
} | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_unhandled_field_type(): | ||
class SearchableSchema(ModelSchema): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters