Skip to content

Commit

Permalink
Merge pull request #8073 from cclauss/bump-pydantic
Browse files Browse the repository at this point in the history
Run bump-pydantic migration tool for Pydantic v2
  • Loading branch information
jimchamp authored Jul 20, 2023
2 parents f78e0a8 + 2a3f76c commit ef9a338
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 37 deletions.
50 changes: 16 additions & 34 deletions openlibrary/plugins/importapi/import_validator.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,35 @@
from typing import Any
from typing import Annotated, Any, TypeVar

from annotated_types import MinLen
from pydantic import BaseModel, ValidationError

from pydantic import BaseModel, ValidationError, validator
T = TypeVar("T")

NonEmptyList = Annotated[list[T], MinLen(1)]
NonEmptyStr = Annotated[str, MinLen(1)]

class Author(BaseModel):
name: str

@validator("name")
def author_name_must_not_be_an_empty_string(cls, v):
if not v:
raise ValueError("name must not be an empty string")
return v
class Author(BaseModel):
name: NonEmptyStr


class Book(BaseModel):
title: str
source_records: list[str]
authors: list[Author]
publishers: list[str]
publish_date: str

@validator("source_records", "authors", "publishers")
def list_must_not_be_empty(cls, v):
if not v:
raise ValueError("Lists must not be empty")
return v

@validator("source_records", "publishers", each_item=True)
def list_items_must_not_be_empty(cls, v):
assert v != '', 'Empty strings are not permitted'
return v

@validator('title', 'publish_date')
def strings_must_not_be_empty(cls, v):
if not len(v):
raise ValueError("Field must have a non-empty string value")
return v
title: NonEmptyStr
source_records: NonEmptyList[NonEmptyStr]
authors: NonEmptyList[Author]
publishers: NonEmptyList[NonEmptyStr]
publish_date: NonEmptyStr


class import_validator:
def validate(self, data: dict[str, Any]):
"""Validates the given import data.
"""Validate the given import data.
Returns True if the import object is valid.
Return True if the import object is valid.
"""

try:
Book.parse_obj(data)
Book.model_validate(data)
except ValidationError as e:
raise e

Expand Down
41 changes: 39 additions & 2 deletions openlibrary/plugins/importapi/tests/test_import_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

from pydantic import ValidationError

from openlibrary.plugins.importapi.import_validator import import_validator
from openlibrary.plugins.importapi.import_validator import import_validator, Author


def test_create_an_author_with_no_name():
Author(name="Valid Name")
with pytest.raises(ValidationError):
Author(name="")


valid_values = {
"title": "Beowulf",
Expand All @@ -18,7 +25,37 @@

def test_validate():
assert validator.validate(valid_values) is True


@pytest.mark.parametrize(
'field', ["title", "source_records", "authors", "publishers", "publish_date"]
)
def test_validate_record_with_missing_required_fields(field):
invalid_values = valid_values.copy()
del invalid_values[field]
with pytest.raises(ValidationError):
validator.validate(invalid_values)


@pytest.mark.parametrize('field', ['title', 'publish_date'])
def test_validate_empty_string(field):
invalid_values = valid_values.copy()
invalid_values[field] = ""
with pytest.raises(ValidationError):
validator.validate(invalid_values)


@pytest.mark.parametrize('field', ['source_records', 'authors', 'publishers'])
def test_validate_empty_list(field):
invalid_values = valid_values.copy()
invalid_values[field] = []
with pytest.raises(ValidationError):
validator.validate(invalid_values)


@pytest.mark.parametrize('field', ['source_records', 'publishers'])
def test_validate_list_with_an_empty_string(field):
invalid_values = valid_values.copy()
del invalid_values['authors']
invalid_values[field] = [""]
with pytest.raises(ValidationError):
validator.validate(invalid_values)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ luqum==0.11.0
lxml==4.9.3
Pillow==10.0.0
psycopg2==2.9.6
pydantic==1.10.11
pydantic==2.0.3
pymarc==5.1.0
python-dateutil==2.8.2
python-memcached==1.59
Expand Down

0 comments on commit ef9a338

Please sign in to comment.