diff --git a/tests/test_fields.py b/tests/test_fields.py index aa3391a726b..b08c0206858 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -1,4 +1,5 @@ import datetime +import decimal import os import re import unittest @@ -7,6 +8,7 @@ import pytest import pytz +from _pytest.monkeypatch import MonkeyPatch from django.core.exceptions import ValidationError as DjangoValidationError from django.http import QueryDict from django.test import TestCase, override_settings @@ -17,6 +19,11 @@ from rest_framework import exceptions, serializers from rest_framework.fields import DjangoImageField, is_simple_callable +try: + import cdecimal +except ImportError: + cdecimal = False + try: import typings except ImportError: @@ -1128,6 +1135,19 @@ def test_part_precision_string_quantized_value_for_decimal(self): expected_digit_tuple = (0, (1, 2, 0, 0), -2) assert value == expected_digit_tuple + @unittest.skipUnless(cdecimal, 'requires python 2.7') + def test_quantize_on_monkey_patched_cdecimal(self): + # Monkey-patch cdecimal to replace decimal in for DecimalField + monkeypatch = MonkeyPatch() + + monkeypatch.setattr('rest_framework.fields.decimal', cdecimal) + + try: + f = rest_framework.fields.DecimalField(max_digits=4, decimal_places=2) + f.quantize(cdecimal.Decimal('1.234')) + finally: + monkeypatch.setattr('rest_framework.fields.decimal', decimal) + class TestNoDecimalPlaces(FieldValues): valid_inputs = { diff --git a/tox.ini b/tox.ini index 11c37ccfc3e..0a4e34020f3 100644 --- a/tox.ini +++ b/tox.ini @@ -24,6 +24,7 @@ deps = django20: Django>=2.0,<2.1 django21: Django>=2.1b1,<2.2 djangomaster: https://github.com/django/django/archive/master.tar.gz + py27: m3-cdecimal -rrequirements/requirements-testing.txt -rrequirements/requirements-optionals.txt