-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'canassa/feat/metadata-fields'
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import datetime | ||
from dataclasses import dataclass, field | ||
|
||
import pytest | ||
from marshmallow import fields, ValidationError | ||
|
||
from dataclasses_json import DataClassJsonMixin | ||
|
||
|
||
@dataclass | ||
class Car(DataClassJsonMixin): | ||
license_number: str = field(metadata={'mm': fields.String(required=False)}) | ||
|
||
|
||
@dataclass | ||
class StringDate(DataClassJsonMixin): | ||
string_date: datetime.datetime = field(metadata={'mm': fields.String(required=False)}) | ||
|
||
|
||
car_schema = Car.schema() | ||
string_date_schema = StringDate.schema() | ||
|
||
|
||
class TestMetadataField: | ||
|
||
def test_validation_error_raises(self): | ||
with pytest.raises(ValidationError) as e: | ||
car_schema.load({'license_number': 123}) | ||
assert e.value.messages == {'license_number': ['Not a valid string.']} | ||
|
||
def test_mm_field_takes_precedence_over_types(self): | ||
obj = string_date_schema.load({'string_date': 'yesterday'}) | ||
assert isinstance(obj, StringDate) | ||
assert obj.string_date == 'yesterday' |