forked from carltongibson/django-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_fields.py
89 lines (72 loc) · 2.92 KB
/
test_fields.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from __future__ import absolute_import
from __future__ import unicode_literals
import decimal
import sys
if sys.version_info >= (2, 7):
import unittest
else: # pragma: nocover
from django.utils import unittest # noqa
import django
from django import forms
from django.test import TestCase
from django_filters.widgets import RangeWidget
from django_filters.fields import RangeField
from django_filters.fields import LookupTypeField
from django_filters.fields import Lookup
def to_d(float_value):
return decimal.Decimal('%.2f' % float_value)
class RangeFieldTests(TestCase):
def test_field(self):
f = RangeField()
self.assertEqual(len(f.fields), 2)
def test_clean(self):
w = RangeWidget()
f = RangeField(widget=w)
self.assertEqual(
f.clean(['12.34', '55']),
slice(to_d(12.34), to_d(55)))
class LookupTypeFieldTests(TestCase):
def test_field(self):
inner = forms.DecimalField()
f = LookupTypeField(inner, [('gt', 'gt'), ('lt', 'lt')])
self.assertEqual(len(f.fields), 2)
def test_clean(self):
inner = forms.DecimalField()
f = LookupTypeField(inner, [('gt', 'gt'), ('lt', 'lt')])
self.assertEqual(
f.clean(['12.34', 'lt']),
Lookup(to_d(12.34), 'lt'))
@unittest.skipIf(django.VERSION >= (1, 6),
'Django 1.6 uses html5 fields')
def test_render(self):
inner = forms.DecimalField()
f = LookupTypeField(inner, [('gt', 'gt'), ('lt', 'lt')])
self.assertHTMLEqual(f.widget.render('price', ''), """
<input type="text" name="price_0" />
<select name="price_1">
<option value="gt">gt</option>
<option value="lt">lt</option>
</select>""")
self.assertHTMLEqual(f.widget.render('price', ['abc', 'lt']), """
<input type="text" name="price_0" value="abc" />
<select name="price_1">
<option value="gt">gt</option>
<option selected="selected" value="lt">lt</option>
</select>""")
@unittest.skipUnless(django.VERSION >= (1, 6),
'Django 1.6 uses html5 fields')
def test_render_used_html5(self):
inner = forms.DecimalField()
f = LookupTypeField(inner, [('gt', 'gt'), ('lt', 'lt')])
self.assertHTMLEqual(f.widget.render('price', ''), """
<input type="number" step="any" name="price_0" />
<select name="price_1">
<option value="gt">gt</option>
<option value="lt">lt</option>
</select>""")
self.assertHTMLEqual(f.widget.render('price', ['abc', 'lt']), """
<input type="number" step="any" name="price_0" value="abc" />
<select name="price_1">
<option value="gt">gt</option>
<option selected="selected" value="lt">lt</option>
</select>""")