-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from ESSS/fb-RFDAP-5724-refactor-rockSampleTyp…
…e-and-bodyShape RFDAP-5724 - Add EnumKeySerializer
- Loading branch information
Showing
20 changed files
with
115 additions
and
35 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 |
---|---|---|
@@ -1,10 +1,7 @@ | ||
from .enum_field import EnumKeyField | ||
from .field import Field | ||
from .nested_fields import ( | ||
NestedModelField, | ||
NestedAttributesField, | ||
PrimaryKeyField, | ||
NestedModelListField, | ||
) | ||
from .model_serializer import ModelSerializer | ||
from .nested_fields import ( | ||
NestedAttributesField, NestedModelField, NestedModelListField, PrimaryKeyField) | ||
from .polymorphic_serializer import PolymorphicModelSerializer | ||
from .serializer import ColumnSerializer, Serializer |
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
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 |
---|---|---|
|
@@ -12,5 +12,6 @@ email: null | |
firstname: Sarah | ||
id: 2 | ||
lastname: Kerrigan | ||
marital_status: Divorced | ||
password: null | ||
role: Employee |
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 |
---|---|---|
|
@@ -15,4 +15,5 @@ email: null | |
firstname: Jim | ||
id: 1 | ||
lastname: Raynor | ||
marital_status: Married | ||
role: Manager |
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 |
---|---|---|
|
@@ -20,4 +20,5 @@ email: null | |
firstname: Jim | ||
id: 1 | ||
lastname: Raynor | ||
marital_status: Married | ||
role: Manager |
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 |
---|---|---|
|
@@ -15,4 +15,5 @@ email: null | |
firstname: John | ||
id: null | ||
lastname: Doe | ||
marital_status: Married | ||
role: Employee |
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
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,19 +1,12 @@ | ||
import pytest | ||
|
||
from serialchemy._tests.sample_model import ( | ||
Address, | ||
Company, | ||
Department, | ||
Employee, | ||
Manager, | ||
Engineer, | ||
SpecialistEngineer, | ||
ContractType, | ||
) | ||
Address, Company, ContractType, Department, Employee, EmployeeSerializer, Engineer, Manager, | ||
MaritalStatus, SpecialistEngineer) | ||
from serialchemy.field import Field | ||
from serialchemy.func import dump | ||
from serialchemy.nested_fields import NestedAttributesField, NestedModelField, PrimaryKeyField | ||
from serialchemy.model_serializer import ModelSerializer | ||
from serialchemy.nested_fields import NestedAttributesField, NestedModelField, PrimaryKeyField | ||
from serialchemy.polymorphic_serializer import PolymorphicModelSerializer | ||
|
||
|
||
|
@@ -62,11 +55,11 @@ class EmployeeSerializerCreationOnlyField(ModelSerializer): | |
def seed_data(db_session): | ||
company = Company(id=5, name='Terrans', location='Korhal') | ||
emp1 = Manager( | ||
id=1, firstname='Jim', lastname='Raynor', role='Manager', _salary=400, company=company | ||
id=1, firstname='Jim', lastname='Raynor', role='Manager', _salary=400, company=company, marital_status=MaritalStatus.MARRIED | ||
) | ||
emp2 = Engineer(id=2, firstname='Sarah', lastname='Kerrigan', role='Engineer', company=company) | ||
emp2 = Engineer(id=2, firstname='Sarah', lastname='Kerrigan', role='Engineer', company=company, marital_status=MaritalStatus.MARRIED) | ||
emp3 = Employee( | ||
id=3, firstname='Tychus', lastname='Findlay', contract_type=ContractType.CONTRACTOR | ||
id=3, firstname='Tychus', lastname='Findlay', contract_type=ContractType.CONTRACTOR, marital_status=MaritalStatus.SINGLE | ||
) | ||
emp4 = SpecialistEngineer( | ||
id=4, firstname='Doran', lastname='Routhe', specialization='Mechanical' | ||
|
@@ -86,6 +79,12 @@ def test_model_dump(db_session, data_regression): | |
serialized = serializer.dump(emp) | ||
data_regression.Check(serialized) | ||
|
||
def test_enum_key_field_dump(db_session, data_regression): | ||
emp = db_session.query(Employee).get(1) | ||
serializer = EmployeeSerializer(Employee) | ||
serialized = serializer.dump(emp) | ||
data_regression.Check(serialized) | ||
|
||
|
||
def test_model_load(data_regression): | ||
serializer = ModelSerializer(Employee) | ||
|
@@ -94,6 +93,19 @@ def test_model_load(data_regression): | |
"lastname": "Kerrigan", | ||
"email": "[email protected]", | ||
"admission": "2152-01-02T00:00:00", | ||
"marital_status": "Married" | ||
} | ||
model = serializer.load(employee_dict) | ||
data_regression.Check(dump(model)) | ||
|
||
def test_enum_key_field_load(data_regression): | ||
serializer = EmployeeSerializer(Employee) | ||
employee_dict = { | ||
"firstname": "Sarah", | ||
"lastname": "Kerrigan", | ||
"email": "[email protected]", | ||
"admission": "2152-01-02T00:00:00", | ||
"marital_status": "MARRIED" | ||
} | ||
model = serializer.load(employee_dict) | ||
data_regression.Check(dump(model)) | ||
|
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 |
---|---|---|
|
@@ -7,5 +7,6 @@ email: null | |
firstname: Tychus | ||
id: 3 | ||
lastname: Findlay | ||
marital_status: Single | ||
password: null | ||
role: Employee |
20 changes: 20 additions & 0 deletions
20
src/serialchemy/_tests/test_serialization/test_enum_key_field_dump.yml
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,20 @@ | ||
address: | ||
city: Tarsonis | ||
id: 1 | ||
number: '943' | ||
state: null | ||
street: 5 Av | ||
zip: null | ||
address_id: 1 | ||
admission: '2000-01-01' | ||
company_id: 5 | ||
company_name: Terrans | ||
contacts: [] | ||
contract_type: null | ||
created_at: '2000-01-02T00:00:00' | ||
email: null | ||
firstname: Jim | ||
id: 1 | ||
lastname: Raynor | ||
marital_status: MARRIED | ||
role: Manager |
12 changes: 12 additions & 0 deletions
12
src/serialchemy/_tests/test_serialization/test_enum_key_field_load.yml
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,12 @@ | ||
address_id: null | ||
admission: '2152-01-02' | ||
company_id: null | ||
contract_type: null | ||
created_at: null | ||
email: [email protected] | ||
firstname: Sarah | ||
id: null | ||
lastname: Kerrigan | ||
marital_status: Married | ||
password: null | ||
role: Employee |
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 |
---|---|---|
|
@@ -7,5 +7,6 @@ email: null | |
firstname: Jim | ||
id: 1 | ||
lastname: Raynor | ||
marital_status: Married | ||
password: null | ||
role: Manager |
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 |
---|---|---|
|
@@ -7,5 +7,6 @@ email: [email protected] | |
firstname: Sarah | ||
id: null | ||
lastname: Kerrigan | ||
marital_status: Married | ||
password: null | ||
role: Employee |
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 |
---|---|---|
|
@@ -10,4 +10,5 @@ email: null | |
firstname: Sarah | ||
id: 2 | ||
lastname: Kerrigan | ||
marital_status: Married | ||
role: Engineer |
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,7 @@ | ||
from serialchemy.enum_serializer import EnumKeySerializer | ||
from serialchemy.field import Field | ||
|
||
|
||
class EnumKeyField(Field): | ||
def __init__(self, enum_class, dump_only=False, load_only=False, creation_only=False): | ||
super().__init__(dump_only=dump_only, load_only=load_only, creation_only=creation_only, serializer=EnumKeySerializer(enum_class)) |
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