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>""")


@@ -73,16 +73,52 @@
 terals%0A%0A
+from datetime import datetime, time%0A
 import d
@@ -409,24 +409,30 @@
 elds import 
+(%0A    
 RangeField%0Af
@@ -433,99 +433,66 @@
 ield
-%0Afrom django_filters.fields import LookupTypeField%0Afrom django_filters.fields import Lookup
+, LookupTypeField, Lookup, DateRangeField, TimeRangeField)
 %0A%0Ade
@@ -838,24 +838,24 @@
 4', '55'%5D),%0A
-
             
@@ -882,24 +882,791 @@
 o_d(55)))%0A%0A%0A
+class DateRangeFieldTests(TestCase):%0A%0A    def test_field(self):%0A        f = DateRangeField()%0A        self.assertEqual(len(f.fields), 2)%0A%0A    def test_clean(self):%0A        w = RangeWidget()%0A        f = DateRangeField(widget=w)%0A%0A        self.assertEqual(%0A            f.clean(%5B'2015-01-01', '2015-01-10'%5D),%0A            slice(datetime(2015, 1, 1, 0, 0 , 0),%0A                  datetime(2015, 1, 10, 23, 59, 59, 999999)))%0A%0A%0Aclass TimeRangeFieldTests(TestCase):%0A%0A    def test_field(self):%0A        f = DateRangeField()%0A        self.assertEqual(len(f.fields), 2)%0A%0A    def test_clean(self):%0A        w = RangeWidget()%0A        f = TimeRangeField(widget=w)%0A%0A        self.assertEqual(%0A            f.clean(%5B'10:15', '12:30'%5D),%0A            slice(time(10, 15, 0), time(12, 30, 0)))%0A%0A%0A
 class Lookup
@@ -3738,29 +3738,28 @@
 %3E%0A            %3C/select%3E%22%22%22)%0A
-%0A

Add tests