Skip to content

Commit a48ef09

Browse files
authored
test: SQLAlchemy Numeric deserialization behaviour (regression test for #241) (#381)
1 parent d8dca2d commit a48ef09

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

tests/test_sqla.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from decimal import Decimal
2+
13
import pytest
24
from flask import Flask, url_for
35
from flask_sqlalchemy import SQLAlchemy
@@ -76,6 +78,7 @@ class BookModel(db.Model):
7678
__tablename__ = "book"
7779
id = db.Column(db.Integer, primary_key=True)
7880
title = db.Column(db.String(255))
81+
price = db.Column(db.Numeric(scale=2))
7982
author_id = db.Column(db.Integer, db.ForeignKey("author.id"))
8083
author = db.relationship("AuthorModel", backref="books")
8184

@@ -289,3 +292,25 @@ class Meta:
289292

290293
deserialized = author_schema.load(author_result)
291294
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

Comments
 (0)