|
| 1 | +from decimal import Decimal |
| 2 | + |
1 | 3 | import pytest |
2 | 4 | from flask import Flask, url_for |
3 | 5 | from flask_sqlalchemy import SQLAlchemy |
@@ -76,6 +78,7 @@ class BookModel(db.Model): |
76 | 78 | __tablename__ = "book" |
77 | 79 | id = db.Column(db.Integer, primary_key=True) |
78 | 80 | title = db.Column(db.String(255)) |
| 81 | + price = db.Column(db.Numeric(scale=2)) |
79 | 82 | author_id = db.Column(db.Integer, db.ForeignKey("author.id")) |
80 | 83 | author = db.relationship("AuthorModel", backref="books") |
81 | 84 |
|
@@ -289,3 +292,25 @@ class Meta: |
289 | 292 |
|
290 | 293 | deserialized = author_schema.load(author_result) |
291 | 294 | assert deserialized["books"][0] == book |
| 295 | + |
| 296 | + @requires_sqlalchemyschema |
| 297 | + def test_auto_field_numeric_deserializes_to_decimal(self, extma, models): |
| 298 | + class BookSchema(extma.SQLAlchemyAutoSchema): |
| 299 | + class Meta: |
| 300 | + model = models.Book |
| 301 | + load_instance = False |
| 302 | + |
| 303 | + price = extma.auto_field() |
| 304 | + |
| 305 | + schema = BookSchema() |
| 306 | + field = schema.fields["price"] |
| 307 | + |
| 308 | + assert isinstance(field, extma.Decimal) |
| 309 | + assert field.as_string is False |
| 310 | + |
| 311 | + loaded = schema.load({"price": "12.34"}) |
| 312 | + assert loaded["price"] == Decimal("12.34") |
| 313 | + assert isinstance(loaded["price"], Decimal) |
| 314 | + |
| 315 | + dumped = schema.dump(models.Book(price=Decimal("12.34"))) |
| 316 | + assert dumped["price"] == Decimal("12.34") |
0 commit comments