Skip to content

Commit

Permalink
remove choices leftovers
Browse files Browse the repository at this point in the history
  • Loading branch information
collerek committed Feb 18, 2024
1 parent ac00c78 commit 2d79866
Show file tree
Hide file tree
Showing 14 changed files with 15 additions and 30 deletions.
3 changes: 0 additions & 3 deletions docs_src/models/docs005.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class Course(ormar.Model):
'sql_nullable': False,
'index': False,
'unique': False,
'choices': False,
'virtual': None,
'is_multi': None,
'is_relation': None,
Expand Down Expand Up @@ -79,7 +78,6 @@ class Course(ormar.Model):
'sql_nullable': False,
'index': False,
'unique': False,
'choices': False,
'virtual': None,
'is_multi': None,
'is_relation': None,
Expand Down Expand Up @@ -120,7 +118,6 @@ class Course(ormar.Model):
'sql_nullable': True,
'index': False,
'unique': False,
'choices': False,
'virtual': None,
'is_multi': None,
'is_relation': None,
Expand Down
1 change: 0 additions & 1 deletion docs_src/queries/docs002.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class Book(ormar.Model):
genre: str = ormar.String(
max_length=100,
default="Fiction",
choices=["Fiction", "Adventure", "Historic", "Fantasy"],
)


Expand Down
1 change: 0 additions & 1 deletion docs_src/queries/docs003.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class Book(ormar.Model):
genre: str = ormar.String(
max_length=100,
default="Fiction",
choices=["Fiction", "Adventure", "Historic", "Fantasy"],
)


Expand Down
1 change: 0 additions & 1 deletion docs_src/queries/docs005.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class Book(ormar.Model):
genre: str = ormar.String(
max_length=100,
default="Fiction",
choices=["Fiction", "Adventure", "Historic", "Fantasy"],
)


Expand Down
3 changes: 1 addition & 2 deletions ormar/fields/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Type, Union
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type, Union

import sqlalchemy
from pydantic.fields import FieldInfo, _Unset
Expand Down Expand Up @@ -43,7 +43,6 @@ def __init__(self, **kwargs: Any) -> None:
self.sql_nullable: bool = kwargs.pop("sql_nullable", False)
self.index: bool = kwargs.pop("index", False)
self.unique: bool = kwargs.pop("unique", False)
self.choices: Sequence = kwargs.pop("choices", False)

self.virtual: bool = kwargs.pop(
"virtual", None
Expand Down
4 changes: 2 additions & 2 deletions ormar/fields/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ def encode_json(value: Any) -> Optional[str]:

def re_dump_value(value: str) -> Union[str, bytes]:
"""
Rw-dumps choices due to different string representation in orjson and json
Re-dumps value due to different string representation in orjson and json
:param value: string to re-dump
:type value: str
:return: re-dumped choices
:return: re-dumped value
:rtype: List[str]
"""
try:
Expand Down
9 changes: 3 additions & 6 deletions ormar/models/helpers/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def overwrite_binary_format(schema: Dict[str, Any], model: Type["Model"]) -> Non
prop["format"] = "base64"


def construct_schema_function_without_choices() -> Callable:
def construct_schema_function() -> Callable:
"""
Modifies model example and description if needed.
Expand All @@ -221,13 +221,10 @@ def schema_extra(schema: Dict[str, Any], model: Type["Model"]) -> None:

def modify_schema_example(model: Type["Model"]) -> None: # noqa CCR001
"""
Checks if Model has any fields with choices set.
If yes it adds choices validation into pre root validators.
Modifies the schema example in openapi schema.
:param model: newly constructed Model
:type model: Model class
"""
if not config_field_not_set(model=model, field_name="model_fields"):
model.model_config["json_schema_extra"] = (
construct_schema_function_without_choices()
)
model.model_config["json_schema_extra"] = construct_schema_function()
2 changes: 1 addition & 1 deletion ormar/models/mixins/save_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def populate_default_values(cls, new_kwargs: Dict) -> Dict:
return new_kwargs

@classmethod
def validate_choices(cls, new_kwargs: Dict) -> Dict:
def validate_enums(cls, new_kwargs: Dict) -> Dict:
"""
Receives dictionary of model that is about to be saved and validates the
fields with choices set to see if the value is allowed.
Expand Down
1 change: 0 additions & 1 deletion ormar/models/quick_access_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"__private_attributes__",
"__same__",
"_calculate_keys",
"_choices_fields",
"_convert_json",
"_extract_db_related_names",
"_extract_model_db_fields",
Expand Down
2 changes: 1 addition & 1 deletion ormar/queryset/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ async def update(self, each: bool = False, **kwargs: Any) -> int:
self.model.extract_related_names()
)
updates = {k: v for k, v in kwargs.items() if k in self_fields}
updates = self.model.validate_choices(updates)
updates = self.model.validate_enums(updates)
updates = self.model.translate_columns_to_aliases(updates)

expr = FilterQuery(filter_clauses=self.filter_clauses).apply(
Expand Down
11 changes: 5 additions & 6 deletions tests/test_model_definition/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class LargeBinaryStr(ormar.Model):

id: int = ormar.Integer(primary_key=True)
test_binary: str = ormar.LargeBinary(
max_length=100000, choices=[blob3, blob4], represent_as_base64_str=True
max_length=100000, represent_as_base64_str=True
)


Expand All @@ -54,7 +54,6 @@ class LargeBinaryNullableStr(ormar.Model):
id: int = ormar.Integer(primary_key=True)
test_binary: str = ormar.LargeBinary(
max_length=100000,
choices=[blob3, blob4],
represent_as_base64_str=True,
nullable=True,
)
Expand Down Expand Up @@ -466,9 +465,9 @@ async def test_model_first():

@pytest.mark.asyncio
async def test_model_choices():
"""Test that choices work properly for various types of fields."""
"""Test that enum work properly for various types of fields."""
async with base_ormar_config.database:
# Test valid choices.
# Test valid enums values.
await asyncio.gather(
Country.objects.create(name="Canada", taxed=True, country_code=1),
Country.objects.create(name="Algeria", taxed=True, country_code=213),
Expand Down Expand Up @@ -505,8 +504,8 @@ async def test_model_choices():


@pytest.mark.asyncio
async def test_nullable_field_model_choices():
"""Test that choices work properly for according to nullable setting"""
async def test_nullable_field_model_enum():
"""Test that enum work properly for according to nullable setting"""
async with base_ormar_config.database:
c1 = await NullableCountry(name=None).save()
assert c1.name is None
Expand Down
1 change: 0 additions & 1 deletion tests/test_queries/test_queryset_level_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class Book(ormar.Model):
genre: str = ormar.String(
max_length=100,
default="Fiction",
choices=["Fiction", "Adventure", "Historic", "Fantasy"],
)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_relations/test_foreign_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Organisation(ormar.Model):
ormar_config = base_ormar_config.copy(tablename="org")

id: int = ormar.Integer(primary_key=True)
ident: str = ormar.String(max_length=100, choices=["ACME Ltd", "Other ltd"])
ident: str = ormar.String(max_length=100)


class Team(ormar.Model):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ class User(PrimaryKeyMixin, ormar.Model):

mobile: str = ormar.String(unique=True, index=True, max_length=10)
password: str = ormar.String(max_length=128)
level: str = ormar.String(
max_length=1, choices=list(Level), default=Level.STAFF.value
)
level: Level = ormar.Enum(default=Level.STAFF, enum_class=Level)
email: Optional[str] = ormar.String(max_length=255, nullable=True, default=None)
avatar: Optional[str] = ormar.String(max_length=255, nullable=True, default=None)
fullname: Optional[str] = ormar.String(max_length=64, nullable=True, default=None)
Expand Down

0 comments on commit 2d79866

Please sign in to comment.