Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions omymodels/models/enum/template.jinja2
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{% for _type in custom_types %}

class {{_type['name']}}({{_type['parents']}}):

{% for key, value in _type['properties']['values'].items() %}{{key}} = {{value}}
{% endfor %}{% endfor %}
{{ _type['name'] }} = Enum(
value='{{ _type['name'] }}',
names=[
{%- for key, value in _type['properties']['values'].items() %}
('{{ key }}', {{ value }}){% if not loop.last %},{% endif %}
{%- endfor %}
]
)
{% endfor %}
36 changes: 21 additions & 15 deletions tests/functional/converter/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ class Material:

db = Gino()

MaterialType = Enum(
value='MaterialType',
names=[
('article', article),
('video', video)
]
)

class MaterialType(str, Enum):

article = article
video = video


class Material(db.Model):

Expand Down Expand Up @@ -100,12 +102,14 @@ def test_from_sqlalchemy_to_gino():

db = Gino()

MaterialType = Enum(
value='MaterialType',
names=[
('article', 'article'),
('video', 'video')
]
)

class MaterialType(str, Enum):

article = 'article'
video = 'video'


class Material(db.Model):

Expand Down Expand Up @@ -161,12 +165,14 @@ def test_from_sqlalchemy_to_pydantic():
from typing import Optional
from pydantic import BaseModel, Json

MaterialType = Enum(
value='MaterialType',
names=[
('article', 'article'),
('video', 'video')
]
)

class MaterialType(str, Enum):

article = 'article'
video = 'video'


class Material(BaseModel):

Expand Down
36 changes: 21 additions & 15 deletions tests/functional/generator/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ def test_enums_in_dataclasses():
from typing import Union
from dataclasses import dataclass, field

MaterialType = Enum(
value='MaterialType',
names=[
('article', 'article'),
('video', 'video')
]
)

class MaterialType(str, Enum):

article = 'article'
video = 'video'


@dataclass
class Material:
Expand Down Expand Up @@ -119,12 +121,14 @@ def test_defaults_off():
from typing import Union
from dataclasses import dataclass, field

MaterialType = Enum(
value='MaterialType',
names=[
('article', 'article'),
('video', 'video')
]
)

class MaterialType(str, Enum):

article = 'article'
video = 'video'


@dataclass
class Material:
Expand Down Expand Up @@ -165,12 +169,14 @@ def test_upper_now_produces_same_result():
from typing import Union
from dataclasses import dataclass, field

MaterialType = Enum(
value='MaterialType',
names=[
('article', 'article'),
('video', 'video')
]
)

class MaterialType(str, Enum):

article = 'article'
video = 'video'


@dataclass
class Material:
Expand Down
62 changes: 44 additions & 18 deletions tests/functional/generator/test_enum_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ def test_enum_only():

expected = """from enum import Enum


class MaterialType(str, Enum):

article = 'article'
video = 'video'
"""
MaterialType = Enum(
value='MaterialType',
names=[
('article', 'article'),
('video', 'video')
]
)
"""

assert result == expected

Expand All @@ -33,18 +35,42 @@ def test_enum_models():
result = create_models(ddl)["code"]
expected = """from enum import Enum, IntEnum

ContentType = Enum(
value='ContentType',
names=[
('HTML', 'HTML'),
('MARKDOWN', 'MARKDOWN'),
('TEXT', 'TEXT')
]
)

class ContentType(str, Enum):

HTML = 'HTML'
MARKDOWN = 'MARKDOWN'
TEXT = 'TEXT'


class Period(IntEnum):
Period = Enum(
value='Period',
names=[
('zero', 0),
('one', 1),
('two', 2)
]
)
"""
assert expected == result

zero = 0
one = 1
two = 2
def test_enum_with_whitespace():
ddl = """
CREATE TYPE "enum_with_spaces" AS ENUM (
'some value',
'another value'
);
"""
assert expected == result
result = create_models(ddl)["code"]
expected = """from enum import Enum

EnumWithSpaces = Enum(
value='EnumWithSpaces',
names=[
('another value', 'another value'),
('some value', 'some value')
]
)
"""
assert result == expected
56 changes: 32 additions & 24 deletions tests/functional/generator/test_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ def test_enums_gino():

db = Gino(schema="schema--notification")

ContentType = Enum(
value='ContentType',
names=[
('HTML', 'HTML'),
('MARKDOWN', 'MARKDOWN'),
('TEXT', 'TEXT')
]
)

class ContentType(str, Enum):

HTML = 'HTML'
MARKDOWN = 'MARKDOWN'
TEXT = 'TEXT'


class Notification(db.Model):

Expand Down Expand Up @@ -51,20 +53,24 @@ def test_pydantic_models():
from typing import Optional
from pydantic import BaseModel

ContentType = Enum(
value='ContentType',
names=[
('HTML', 'HTML'),
('MARKDOWN', 'MARKDOWN'),
('TEXT', 'TEXT')
]
)

Period = Enum(
value='Period',
names=[
('zero', 0),
('one', 1),
('two', 2)
]
)

class ContentType(str, Enum):

HTML = 'HTML'
MARKDOWN = 'MARKDOWN'
TEXT = 'TEXT'


class Period(IntEnum):

zero = 0
one = 1
two = 2


class Notification(BaseModel):

Expand Down Expand Up @@ -102,12 +108,14 @@ def test_enum_works_with_lower_case():

db = Gino()

MaterialType = Enum(
value='MaterialType',
names=[
('article', 'article'),
('video', 'video')
]
)

class MaterialType(str, Enum):

article = 'article'
video = 'video'


class Material(db.Model):

Expand Down
24 changes: 14 additions & 10 deletions tests/functional/generator/test_pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ def test_enums_lower_case_names_works():
from typing import Optional
from pydantic import BaseModel, Json

MaterialType = Enum(
value='MaterialType',
names=[
('article', 'article'),
('video', 'video')
]
)

class MaterialType(str, Enum):

article = 'article'
video = 'video'


class Material(BaseModel):

Expand Down Expand Up @@ -157,12 +159,14 @@ def test_no_defaults():
from typing import Optional
from pydantic import BaseModel, Json

MaterialType = Enum(
value='MaterialType',
names=[
('article', 'article'),
('video', 'video')
]
)

class MaterialType(str, Enum):

article = 'article'
video = 'video'


class Material(BaseModel):

Expand Down
24 changes: 14 additions & 10 deletions tests/functional/generator/test_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ def test_with_enums():

Base = declarative_base()

MaterialType = Enum(
value='MaterialType',
names=[
('article', 'article'),
('video', 'video')
]
)

class MaterialType(str, Enum):

article = 'article'
video = 'video'


class Material(Base):

Expand Down Expand Up @@ -263,12 +265,14 @@ def test_upper_name_produces_the_same_result():

Base = declarative_base()

MaterialType = Enum(
value='MaterialType',
names=[
('article', 'article'),
('video', 'video')
]
)

class MaterialType(str, Enum):

article = 'article'
video = 'video'


class Material(Base):

Expand Down
Loading