Skip to content

Commit

Permalink
Merge pull request #37 from ESSS/fb-RFDAP-5724-refactor-rockSampleTyp…
Browse files Browse the repository at this point in the history
…e-and-bodyShape

RFDAP-5724 - Add EnumKeySerializer
  • Loading branch information
armpico authored Apr 10, 2023
2 parents 3851a36 + aa56abd commit 26f04ae
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 35 deletions.
9 changes: 3 additions & 6 deletions src/serialchemy/__init__.py
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
16 changes: 12 additions & 4 deletions src/serialchemy/_tests/sample_model.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from datetime import datetime
from enum import Enum

from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, Table, Float, Date
from sqlalchemy import Column, Date, DateTime, Float, ForeignKey, Integer, String, Table
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import object_session, relationship
from sqlalchemy.sql import sqltypes
from sqlalchemy_utils import ChoiceType

from serialchemy.model_serializer import ModelSerializer
from serialchemy.enum_field import EnumKeyField
from serialchemy.field import Field
from serialchemy.model_serializer import ModelSerializer
from serialchemy.nested_fields import NestedModelField, NestedModelListField
from sqlalchemy.ext.hybrid import hybrid_property

Base = declarative_base()

Expand Down Expand Up @@ -74,6 +75,10 @@ class ContractType(Enum):
CONTRACTOR = 'Contractor'
OTHER = 'Other'

class MaritalStatus(Enum):
SINGLE = 'Single'
MARRIED = 'Married'
DIVORCED = 'Divorced'

class Employee(Base):

Expand All @@ -94,6 +99,8 @@ class Employee(Base):
role = Column(String)
_salary = Column(Float)
contract_type = Column(ChoiceType(ContractType))
marital_status = Column(sqltypes.Enum(MaritalStatus))


password = Column(String)
created_at = Column(DateTime, default=datetime(2000, 1, 2))
Expand Down Expand Up @@ -158,3 +165,4 @@ class EmployeeSerializer(ModelSerializer):
company_name = Field(dump_only=True)
address = NestedModelField(Address)
contacts = NestedModelListField(Contact)
marital_status = EnumKeyField(MaritalStatus)
4 changes: 2 additions & 2 deletions src/serialchemy/_tests/test_func.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import pytest

from serialchemy import func
from serialchemy._tests.sample_model import Company, Employee
from serialchemy._tests.sample_model import Company, Employee, MaritalStatus


@pytest.fixture(autouse=True)
def seed_data(db_session):
company = Company(id=5, name='Terrans', location='Korhal')
employee = Employee(id=2, firstname='Sarah', lastname='Kerrigan', company=company)
employee = Employee(id=2, firstname='Sarah', lastname='Kerrigan', company=company, marital_status=MaritalStatus.DIVORCED)
db_session.add(employee)
db_session.commit()

Expand Down
1 change: 1 addition & 0 deletions src/serialchemy/_tests/test_func/test_dump.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ email: null
firstname: Sarah
id: 2
lastname: Kerrigan
marital_status: Divorced
password: null
role: Employee
13 changes: 4 additions & 9 deletions src/serialchemy/_tests/test_nested_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@

from serialchemy import ModelSerializer
from serialchemy._tests.sample_model import (
Address,
Company,
Employee,
Manager,
Engineer,
SpecialistEngineer,
)
Address, Company, Employee, Engineer, Manager, MaritalStatus, SpecialistEngineer)
from serialchemy.field import Field
from serialchemy.nested_fields import NestedAttributesField, NestedModelField

Expand Down Expand Up @@ -40,12 +34,12 @@ class CompanySerializer(ModelSerializer):
def setup(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)
emp3 = Employee(id=3, firstname='Tychus', lastname='Findlay')
emp4 = SpecialistEngineer(
id=4, firstname='Doran', lastname='Routhe', specialization='Mechanical'
id=4, firstname='Doran', lastname='Routhe', specialization='Mechanical', marital_status=MaritalStatus.MARRIED
)

addr1 = Address(street="5 Av", number="943", city="Tarsonis")
Expand Down Expand Up @@ -80,6 +74,7 @@ def test_deserialize_with_custom_serializer(db_session, data_regression):
serialized = {
"firstname": "John",
"lastname": "Doe",
"marital_status": "Married",
"company_id": 5,
"admission": "2004-06-01T00:00:00",
"address": {"id": 1, "number": "245", "street": "6 Av", "zip": "88088-000"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ email: null
firstname: Jim
id: 1
lastname: Raynor
marital_status: Married
role: Manager
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ email: null
firstname: Jim
id: 1
lastname: Raynor
marital_status: Married
role: Manager
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ email: null
firstname: John
id: null
lastname: Doe
marital_status: Married
role: Employee
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ master_engeneer:
firstname: Doran
id: 4
lastname: Routhe
marital_status: Married
password: null
role: Specialist Engineer
specialization: Mechanical
Expand All @@ -26,6 +27,7 @@ master_manager:
id: 1
lastname: Raynor
manager_name: null
marital_status: Married
password: null
role: Manager
master_manager_id: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ master_manager:
id: 1
lastname: Raynor
manager_name: null
marital_status: Married
password: null
role: Manager
master_manager_id: null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ master_engeneer:
firstname: Doran
id: 4
lastname: Routhe
marital_status: Married
password: null
role: Specialist Engineer
specialization: Mechanical
Expand Down
38 changes: 25 additions & 13 deletions src/serialchemy/_tests/test_serialization.py
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


Expand Down Expand Up @@ -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'
Expand All @@ -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)
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ email: null
firstname: Tychus
id: 3
lastname: Findlay
marital_status: Single
password: null
role: Employee
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ email: null
firstname: Jim
id: 1
lastname: Raynor
marital_status: Married
password: null
role: Manager
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ email: [email protected]
firstname: Sarah
id: null
lastname: Kerrigan
marital_status: Married
password: null
role: Employee
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ email: null
firstname: Sarah
id: 2
lastname: Kerrigan
marital_status: Married
role: Engineer
7 changes: 7 additions & 0 deletions src/serialchemy/enum_field.py
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))
19 changes: 18 additions & 1 deletion src/serialchemy/enum_serializer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from .serializer import ColumnSerializer
from typing import Type

from enum import Enum

from .serializer import ColumnSerializer, Serializer


class EnumSerializer(ColumnSerializer):
Expand All @@ -10,3 +14,16 @@ def dump(self, value):
def load(self, serialized, session=None):
enum = getattr(self.column.type, 'enum_class')
return enum(serialized)

class EnumKeySerializer(Serializer):
def __init__(self, enum_class: Type[Enum]) -> None:
super().__init__()
self.enum_class = enum_class

def dump(self, value):
if not value:
return None
return value.name

def load(self, serialized, session=None):
return self.enum_class[serialized]

0 comments on commit 26f04ae

Please sign in to comment.