Skip to content

Commit

Permalink
Test with unexpected result
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-s committed Jun 11, 2024
1 parent 8c7ffc1 commit 6f3bf44
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
from pydantic import ConfigDict
from testapp.models import Configuration, Listing, Preference, Record, Searchable, User

from pydantic import (
ValidationInfo,
field_validator,
)

from djantic import ModelSchema


Expand Down Expand Up @@ -33,6 +38,31 @@ class UserSchema(ModelSchema):
}


@pytest.mark.django_db
def test_context_for_field():

def get_context():
return {'check_title': lambda x: x.istitle()}

class UserSchema(ModelSchema):
model_config = ConfigDict(model=User)

@field_validator('first_name', mode="before", check_fields=False)
@classmethod
def validate_choice(cls, v: str, info: ValidationInfo):
print(info.context)
if not info.context:
return v
breakpoint()
check_title = info.context.get('check_title')
if check_title and check_title(v):
raise ValueError('First name needs to be a title')
return v

user = User.objects.create(first_name="hello", email="a@a.com")
UserSchema.from_django(user, context=get_context())


@pytest.mark.django_db
def test_unhandled_field_type():
class SearchableSchema(ModelSchema):
Expand Down

0 comments on commit 6f3bf44

Please sign in to comment.