1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
import datetime as dt
import decimal
import uuid
from typing import cast
import pytest
import sqlalchemy as sa
from marshmallow import Schema, fields, validate
from sqlalchemy import Integer, String
from sqlalchemy.dialects import mysql, postgresql
from sqlalchemy.orm import Mapped, Session, column_property
from marshmallow_sqlalchemy import (
ModelConversionError,
ModelConverter,
column2field,
field_for,
fields_for_model,
property2field,
)
from marshmallow_sqlalchemy.fields import Related, RelatedList
from .conftest import CourseLevel, mapped_column
def contains_validator(field, v_type):
for v in field.validators:
if isinstance(v, v_type):
return v
return False
class TestModelFieldConversion:
def test_fields_for_model_types(self, models):
fields_ = fields_for_model(models.Student, include_fk=True)
assert type(fields_["id"]) is fields.Int
assert type(fields_["full_name"]) is fields.Str
assert type(fields_["dob"]) is fields.Date
assert type(fields_["current_school_id"]) is fields.Int
assert type(fields_["date_created"]) is fields.DateTime
def test_fields_for_model_handles_exclude(self, models):
fields_ = fields_for_model(models.Student, exclude=("dob",))
assert type(fields_["id"]) is fields.Int
assert type(fields_["full_name"]) is fields.Str
assert fields_["dob"] is None
def test_fields_for_model_handles_custom_types(self, models):
fields_ = fields_for_model(models.Course, include_fk=True)
assert type(fields_["grade"]) is fields.Int
assert type(fields_["transcription"]) is fields.Str
def test_fields_for_model_saves_doc(self, models):
fields_ = fields_for_model(models.Student, include_fk=True)
assert (
fields_["date_created"].metadata["description"]
== "date the student was created"
)
def test_length_validator_set(self, models):
fields_ = fields_for_model(models.Student)
validator = contains_validator(fields_["full_name"], validate.Length)
assert validator
assert validator.max == 255
def test_none_length_validator_not_set(self, models):
fields_ = fields_for_model(models.Course)
assert not contains_validator(fields_["transcription"], validate.Length)
def test_sets_allow_none_for_nullable_fields(self, models):
fields_ = fields_for_model(models.Student)
assert fields_["dob"].allow_none is True
def test_enum_with_choices_converted_to_field_with_validator(self, models):
fields_ = fields_for_model(models.Course)
validator = contains_validator(fields_["level"], validate.OneOf)
assert validator
assert list(validator.choices) == ["Primary", "Secondary"]
def test_enum_with_class_converted_to_enum_field(self, models):
fields_ = fields_for_model(models.Course)
field = fields_["level_with_enum_class"]
assert type(field) is fields.Enum
assert contains_validator(field, validate.OneOf) is False
assert field.enum is CourseLevel
def test_many_to_many_relationship(self, models):
student_fields = fields_for_model(models.Student, include_relationships=True)
courses_field = student_fields["courses"]
assert type(courses_field) is RelatedList
assert courses_field.required is False
course_fields = fields_for_model(models.Course, include_relationships=True)
students_field = course_fields["students"]
assert type(students_field) is RelatedList
assert students_field.required is False
def test_many_to_one_relationship(self, models):
student_fields = fields_for_model(models.Student, include_relationships=True)
current_school_field = student_fields["current_school"]
assert type(current_school_field) is Related
assert current_school_field.allow_none is False
assert current_school_field.required is True
school_fields = fields_for_model(models.School, include_relationships=True)
assert type(school_fields["students"]) is RelatedList
teacher_fields = fields_for_model(models.Teacher, include_relationships=True)
current_school_field = teacher_fields["current_school"]
assert type(current_school_field) is Related
assert current_school_field.required is False
def test_many_to_many_uselist_false_relationship(self, models):
teacher_fields = fields_for_model(models.Teacher, include_relationships=True)
substitute_field = teacher_fields["substitute"]
assert type(substitute_field) is Related
assert substitute_field.required is False
def test_include_fk(self, models):
student_fields = fields_for_model(models.Student, include_fk=False)
assert "current_school_id" not in student_fields
student_fields2 = fields_for_model(models.Student, include_fk=True)
assert "current_school_id" in student_fields2
def test_overridden_with_fk(self, models):
graded_paper_fields = fields_for_model(models.GradedPaper, include_fk=False)
assert "id" in graded_paper_fields
def test_rename_key(self, models):
class RenameConverter(ModelConverter):
def _get_field_name(self, prop):
if prop.key == "name":
return "title"
return prop.key
converter = RenameConverter()
fields = converter.fields_for_model(models.Paper)
assert "title" in fields
assert "name" not in fields
def test_subquery_proxies(self, session: Session, Base: type, models):
# Model from a subquery, columns are proxied.
# https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/383
first_graders = session.query(models.Student).filter(
models.Student.courses.any(models.Course.grade == 1)
)
class FirstGradeStudent(Base):
__table__ = first_graders.subquery("first_graders")
fields_ = fields_for_model(FirstGradeStudent)
assert fields_["dob"].allow_none is True
def make_property(*column_args, **column_kwargs):
return column_property(sa.Column(*column_args, **column_kwargs))
class TestPropertyFieldConversion:
@pytest.fixture
def converter(self):
return ModelConverter()
def test_convert_custom_type_mapping_on_schema(self):
class MyDateTimeField(fields.DateTime):
pass
class MySchema(Schema):
TYPE_MAPPING = Schema.TYPE_MAPPING.copy()
TYPE_MAPPING.update({dt.datetime: MyDateTimeField})
converter = ModelConverter(schema_cls=MySchema)
prop = make_property(sa.DateTime())
field = converter.property2field(prop)
assert type(field) is MyDateTimeField
@pytest.mark.parametrize(
("sa_type", "field_type"),
(
(sa.String, fields.Str),
(sa.Unicode, fields.Str),
(sa.LargeBinary, fields.Str),
(sa.Text, fields.Str),
(sa.Date, fields.Date),
(sa.DateTime, fields.DateTime),
(sa.Boolean, fields.Bool),
(sa.Float, fields.Float),
(sa.SmallInteger, fields.Int),
(sa.Interval, fields.TimeDelta),
(postgresql.UUID, fields.UUID),
(postgresql.MACADDR, fields.Str),
(postgresql.INET, fields.Str),
(postgresql.BIT, fields.Integer),
(postgresql.OID, fields.Integer),
(postgresql.CIDR, fields.String),
(postgresql.DATE, fields.Date),
(postgresql.TIME, fields.Time),
(mysql.INTEGER, fields.Integer),
(mysql.DATETIME, fields.DateTime),
),
)
def test_convert_types(self, converter, sa_type, field_type):
prop = make_property(sa_type())
field = converter.property2field(prop)
assert type(field) is field_type
def test_convert_Numeric(self, converter):
prop = make_property(sa.Numeric(scale=2))
field = converter.property2field(prop)
assert type(field) is fields.Decimal
assert field.places == decimal.Decimal((0, (1,), -2))
def test_convert_ARRAY_String(self, converter):
prop = make_property(postgresql.ARRAY(sa.String()))
field = converter.property2field(prop)
assert type(field) is fields.List
inner_field = getattr(field, "inner", getattr(field, "container", None))
assert type(inner_field) is fields.Str
def test_convert_ARRAY_Integer(self, converter):
prop = make_property(postgresql.ARRAY(sa.Integer))
field = converter.property2field(prop)
assert type(field) is fields.List
inner_field = getattr(field, "inner", getattr(field, "container", None))
assert type(inner_field) is fields.Int
@pytest.mark.parametrize(
"array_property",
(
pytest.param(make_property(sa.ARRAY(sa.Enum(CourseLevel))), id="sa.ARRAY"),
pytest.param(
make_property(postgresql.ARRAY(sa.Enum(CourseLevel))),
id="postgresql.ARRAY",
),
),
)
def test_convert_ARRAY_Enum(self, converter, array_property):
field = converter.property2field(array_property)
assert type(field) is fields.List
inner_field = field.inner
assert type(inner_field) is fields.Enum
@pytest.mark.parametrize(
"array_property",
(
pytest.param(
make_property(sa.ARRAY(sa.Float, dimensions=2)), id="sa.ARRAY"
),
pytest.param(
make_property(postgresql.ARRAY(sa.Float, dimensions=2)),
id="postgresql.ARRAY",
),
),
)
def test_convert_multidimensional_ARRAY(self, converter, array_property):
field = converter.property2field(array_property)
assert type(field) is fields.List
assert type(field.inner) is fields.List
assert type(field.inner.inner) is fields.Float
def test_convert_one_dimensional_ARRAY(self, converter):
prop = make_property(postgresql.ARRAY(sa.Float, dimensions=1))
field = converter.property2field(prop)
assert type(field) is fields.List
assert type(field.inner) is fields.Float
def test_convert_TSVECTOR(self, converter):
prop = make_property(postgresql.TSVECTOR)
with pytest.raises(ModelConversionError):
converter.property2field(prop)
def test_convert_default(self, converter):
prop = make_property(sa.String, default="ack")
field = converter.property2field(prop)
assert field.required is False
def test_convert_server_default(self, converter):
prop = make_property(sa.String, server_default=sa.text("sysdate"))
field = converter.property2field(prop)
assert field.required is False
def test_convert_autoincrement(self, models, converter):
prop = models.Course.__mapper__.attrs.get("id")
field = converter.property2field(prop)
assert field.required is False
def test_handle_expression_based_column_property(self, models, converter):
"""
Tests ability to handle a column_property with a mapped expression value.
Such properties should be marked as dump_only, and the type should be properly
inferred.
"""
prop = models.Student.__mapper__.attrs.get("course_count")
field = converter.property2field(prop)
assert type(field) is fields.Integer
assert field.dump_only is True
def test_handle_simple_column_property(self, models, converter):
"""
Tests handling of column properties that do not derive directly from Column
"""
prop = models.Seminar.__mapper__.attrs.get("label")
field = converter.property2field(prop)
assert type(field) is fields.String
assert field.dump_only is True
class TestPropToFieldClass:
def test_property2field(self):
prop = make_property(sa.Integer())
field = property2field(prop, instance=True)
assert type(field) is fields.Int
field_cls = property2field(prop, instance=False)
assert field_cls is fields.Int
def test_can_pass_extra_kwargs(self):
prop = make_property(sa.String())
field = property2field(
prop, instance=True, metadata=dict(description="just a string")
)
assert field.metadata["description"] == "just a string"
class TestColumnToFieldClass:
def test_column2field(self):
column = sa.Column(sa.String(255))
field = column2field(column, instance=True)
assert type(field) is fields.String
field_cls = column2field(column, instance=False)
assert field_cls is fields.String
def test_can_pass_extra_kwargs(self):
column = sa.Column(sa.String(255))
field = column2field(
column, instance=True, metadata=dict(description="just a string")
)
assert field.metadata["description"] == "just a string"
def test_uuid_column2field(self):
class UUIDType(sa.types.TypeDecorator):
python_type = uuid.UUID
impl = sa.BINARY(16)
column = sa.Column(UUIDType)
assert issubclass(column.type.python_type, uuid.UUID) # Test against test check
assert hasattr(column.type, "length") # Test against test check
assert column.type.length == 16 # Test against test
field = column2field(column, instance=True)
uuid_val = uuid.uuid4()
assert field.deserialize(str(uuid_val)) == uuid_val
class TestFieldFor:
def test_field_for(self, models):
field = field_for(models.Student, "full_name")
assert type(field) is fields.Str
field = field_for(models.Student, "current_school")
assert type(field) is Related
field = field_for(models.Student, "full_name", field_class=fields.Date)
assert type(field) is fields.Date
def test_related_initialization_warning(self, models, session):
with pytest.warns(
DeprecationWarning,
match="column` parameter is deprecated and will be removed in future releases. Use `columns` instead.",
):
Related(column="TestCol")
def test_related_initialization_with_columns(self, models, session):
ret = Related(columns=["TestCol"])
assert len(ret.columns) == 1
assert ret.columns[0] == "TestCol"
ret = Related(columns="TestCol")
assert isinstance(ret.columns, list)
assert len(ret.columns) == 1
assert ret.columns[0] == "TestCol"
def test_field_for_can_override_validators(self, models, session):
field = field_for(
models.Student, "full_name", validate=[validate.Length(max=20)]
)
assert len(field.validators) == 1
validator = cast(validate.Length, field.validators[0])
assert validator.max == 20
field = field_for(models.Student, "full_name", validate=[])
assert field.validators == []
def tests_postgresql_array_with_args(self, Base: type):
# regression test for #392
class ModelWithArray(Base):
__tablename__ = "model_with_array"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
bar: Mapped[list[str]] = mapped_column(postgresql.ARRAY(String))
field = field_for(ModelWithArray, "bar", dump_only=True)
assert type(field) is fields.List
assert field.dump_only is True
|