Skip to content

Commit

Permalink
Add test for monkey-patched decimal in Python 2.7
Browse files Browse the repository at this point in the history
For #6105
  • Loading branch information
kbussell committed Aug 7, 2018
1 parent 6a42128 commit 3ef0da3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import decimal
import os
import re
import unittest
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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 = {
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 3ef0da3

Please sign in to comment.