Skip to content

Commit ffea7ce

Browse files
authored
Merge pull request #32 from cloudblue/LITE-21432-Trim-decimal-places
LITE-21432:Trim decimal places
2 parents a314996 + 69a671d commit ffea7ce

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

dj_rql/filter_cls.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# Copyright © 2021 Ingram Micro Inc. All rights reserved.
33
#
4-
4+
import decimal
55
from collections import defaultdict
66
from datetime import datetime
77
from uuid import uuid4
@@ -38,7 +38,6 @@
3838

3939
from lark.exceptions import LarkError
4040

41-
4241
iterable_types = (list, tuple)
4342

4443

@@ -847,7 +846,7 @@ def _get_typed_value(cls, filter_name, filter_lookup, str_value, django_field,
847846

848847
typed_value = cls._convert_value(django_field, str_value, use_repr=use_repr)
849848
return typed_value
850-
except (ValueError, TypeError):
849+
except (ValueError, TypeError, decimal.InvalidOperation):
851850
raise RQLFilterValueError(**cls._get_error_details(
852851
filter_name, filter_lookup, str_value,
853852
))
@@ -890,7 +889,10 @@ def _convert_value(cls, django_field, str_value, use_repr=False):
890889
return float(val)
891890

892891
elif filter_type == FilterTypes.DECIMAL:
893-
return round(float(val), django_field.decimal_places)
892+
if '.' in val:
893+
integer_part, fractional_part = val.split('.', 1)
894+
val = integer_part + '.' + fractional_part[:django_field.decimal_places]
895+
return decimal.Decimal(val)
894896

895897
elif filter_type == FilterTypes.DATE:
896898
return cls._convert_date_value(val)

requirements/test.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pytest-django==4.4.0
66
pytest-mock==3.6.1
77
pytest-deadfixtures==2.2.1
88
pytest-randomly==3.10.1
9+
pytz==2021.3
910
django-fsm==2.7.1
1011
django-model-utils==4.1.1
1112
djangorestframework==3.12.4

tests/dj_rf/urls.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
# Copyright © 2021 Ingram Micro Inc. All rights reserved.
33
#
44

5-
from django.conf.urls import include, re_path
5+
from django.conf.urls import include
6+
from django.urls import re_path
67

78
from rest_framework.routers import SimpleRouter
89

tests/test_filter_cls/test_fields_filtering.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,24 @@ def test_current_price():
8181
filter_name = 'current_price'
8282
books = [
8383
Book.objects.create(current_price=5.23),
84-
Book.objects.create(current_price=0.0121),
84+
Book.objects.create(current_price=1.5554),
85+
Book.objects.create(current_price=222.5556),
86+
Book.objects.create(current_price=-35.4567),
87+
Book.objects.create(current_price=0.0123),
8588
]
8689
assert filter_field(filter_name, CO.EQ, books[0].current_price) == [books[0]]
87-
assert filter_field(filter_name, CO.EQ, 5.2300123) == [books[0]]
90+
assert filter_field(filter_name, CO.EQ, 1.55549) == [books[1]]
91+
assert filter_field(filter_name, CO.EQ, 222.55567) == [books[2]]
92+
assert filter_field(filter_name, CO.EQ, -35.456789) == [books[3]]
93+
assert filter_field(filter_name, CO.EQ, 0.0123456) == [books[4]]
8894
assert filter_field(filter_name, CO.EQ, 2) == []
89-
assert filter_field(filter_name, CO.NE, books[1].current_price) == [books[0]]
90-
assert filter_field(filter_name, CO.LE, books[0].current_price) == books
91-
assert filter_field(filter_name, CO.GT, books[1].current_price) == [books[0]]
95+
assert filter_field(filter_name, CO.GT, books[1].current_price) == [books[0], books[2]]
96+
assert filter_field(filter_name, CO.NE, books[1].current_price) == [
97+
books[0], books[2], books[3], books[4],
98+
]
99+
assert filter_field(filter_name, CO.LE, books[0].current_price) == [
100+
books[0], books[1], books[3], books[4],
101+
]
92102

93103

94104
@pytest.mark.django_db

0 commit comments

Comments
 (0)