Cast numeric columns for order expression on SQLite
Closes #13397 (grafted from 97e889240413ac521a15dc81453499bb888039c0) --HG-- branch : 7.0
This commit is contained in:
1 parent
a8bb16e78c
commit
ceace63df5
2 files changed
+31
No files matched your search
@@ -6,6 +6,7 @@ from sql import Cast, CombiningQuery, Literal, Select
|
||||
|
||||
from trytond import backend
|
||||
|
||||
from .field import order_method
|
||||
from .float import Float
|
||||
|
||||
|
||||
@@ -17,6 +18,14 @@ class Numeric(Float):
|
||||
_sql_type = 'NUMERIC'
|
||||
_py_type = Decimal
|
||||
|
||||
@order_method
|
||||
def convert_order(self, name, tables, Model):
|
||||
columns = super().convert_order(name, tables, Model)
|
||||
if backend.name == 'sqlite':
|
||||
# Must be cast because Decimal is stored as bytes
|
||||
columns = [Cast(c, self.sql_type().base) for c in columns]
|
||||
return columns
|
||||
|
||||
def _domain_column(self, operator, column):
|
||||
column = super()._domain_column(operator, column)
|
||||
if backend.name == 'sqlite':
|
||||
|
||||
@@ -461,6 +461,28 @@ class FieldNumericTestCase(unittest.TestCase):
|
||||
|
||||
self.assertEqual(numerics, [numeric1])
|
||||
|
||||
@with_transaction()
|
||||
def test_search_order(self):
|
||||
"Test search order"
|
||||
pool = Pool()
|
||||
Numeric = pool.get('test.numeric')
|
||||
|
||||
Numeric.create([{
|
||||
'numeric': Decimal('10.00'),
|
||||
}, {
|
||||
'numeric': Decimal('7.00'),
|
||||
}])
|
||||
|
||||
records_asc = Numeric.search([], order=[('numeric', 'ASC')])
|
||||
records_desc = Numeric.search([], order=[('numeric', 'DESC')])
|
||||
|
||||
self.assertEqual(
|
||||
[r.numeric for r in records_asc],
|
||||
[Decimal('7.00'), Decimal('10.00')])
|
||||
self.assertEqual(
|
||||
[r.numeric for r in records_desc],
|
||||
[Decimal('10.00'), Decimal('7.00')])
|
||||
|
||||
@with_transaction()
|
||||
def test_write(self):
|
||||
"Test write numeric"
|
||||
|
||||
Reference in new issue
Block a user