Ejemplo n.º 1
0
    def test_numericrange_filter_range_choices(self):
        # If data is more than max_links, we should get a range
        filter1 = NumericRangeFilter('price', Book, MultiValueDict(), max_links=8)

        qs = Book.objects.all()
        # Should take 3 queries - one to find out how many distinct values,
        # one to find a range, one to get the counts.
        with self.assertNumQueries(3):
            choices = filter1.get_choices(qs)

        self.assertTrue(len(choices) <= 8)
        total_count = sum(c.count for c in choices)
        self.assertEqual(total_count, qs.count())

        # First choice should be inclusive on first and last
        p0 = choices[0].params.getlist('price')[0]
        self.assertTrue('..' in p0)
        self.assertTrue('i' in p0.split('..')[0])
        self.assertTrue('i' in p0.split('..')[1])

        # Second choice should be exlusive on first,
        # inclusive on second.
        p1 = choices[1].params.getlist('price')[0]
        self.assertTrue('..' in p1)
        self.assertTrue('i' not in p1.split('..')[0])
        self.assertTrue('i' in p1.split('..')[1])
Ejemplo n.º 2
0
    def test_numericrange_filter_manual_ranges(self):
        """
        Test we can specify 'ranges' and it works as expected.
        """
        # Also tests that aggregation works as expected with regards to
        # lower/upper limits.
        qs = Book.objects.all()

        ranges = [(Decimal('3.50'), Decimal('5.00')),
                  (Decimal('5.00'), Decimal('6.00'))]
        # There are books with prices exactly equal to 3.50/5.00/6.00 which
        # makes this test real.

        self.assertTrue(qs.filter(price=Decimal('3.50')).exists())
        self.assertTrue(qs.filter(price=Decimal('5.00')).exists())
        self.assertTrue(qs.filter(price=Decimal('6.00')).exists())

        filter1 = NumericRangeFilter('price',
                                     Book,
                                     MultiValueDict(),
                                     ranges=ranges)
        choices = filter1.get_choices(qs)
        self.assertEqual(
            choices[0].count,
            qs.filter(price__gte=Decimal('3.50'),
                      price__lte=Decimal('5.00')).count())
        self.assertEqual(
            choices[1].count,
            qs.filter(price__gt=Decimal('5.00'),
                      price__lte=Decimal('6.00')).count())
Ejemplo n.º 3
0
    def test_numericrange_filter_range_choices(self):
        # If data is more than max_links, we should get a range
        filter1 = NumericRangeFilter('price',
                                     Book,
                                     MultiValueDict(),
                                     max_links=8)

        qs = Book.objects.all()
        # Should take 3 queries - one to find out how many distinct values,
        # one to find a range, one to get the counts.
        with self.assertNumQueries(3):
            choices = filter1.get_choices(qs)

        self.assertTrue(len(choices) <= 8)
        total_count = sum(c.count for c in choices)
        self.assertEqual(total_count, qs.count())

        # First choice should be inclusive on first and last
        p0 = choices[0].params.getlist('price')[0]
        self.assertTrue('..' in p0)
        self.assertTrue('i' in p0.split('..')[0])
        self.assertTrue('i' in p0.split('..')[1])

        # Second choice should be exlusive on first,
        # inclusive on second.
        p1 = choices[1].params.getlist('price')[0]
        self.assertTrue('..' in p1)
        self.assertTrue('i' not in p1.split('..')[0])
        self.assertTrue('i' in p1.split('..')[1])
Ejemplo n.º 4
0
    def test_numericrange_filter_drilldown(self):
        # Can specify to turn off drilldown
        # We shouldn't get drilldown if ranges is specified manually.

        params1 = MultiValueDict({'price': ['3.50i..5.00i']})
        filter1 = NumericRangeFilter('price', Book, params1, drilldown=False)

        qs = Book.objects.all()
        qs_filtered1 = filter1.apply_filter(qs)
        choices = filter1.get_choices(qs_filtered1)
        self.assertEqual(len(choices), 1)
        self.assertEqual(choices[0].link_type, FILTER_REMOVE)
Ejemplo n.º 5
0
    def test_numericrange_filter_drilldown(self):
        # Can specify to turn off drilldown
        # We shouldn't get drilldown if ranges is specified manually.

        params1 = MultiValueDict({'price': ['3.50i..5.00i']})
        filter1 = NumericRangeFilter('price', Book, params1, drilldown=False)

        qs = Book.objects.all()
        qs_filtered1 = filter1.apply_filter(qs)
        choices = filter1.get_choices(qs_filtered1)
        self.assertEqual(len(choices), 1)
        self.assertEqual(choices[0].link_type, FILTER_REMOVE)
Ejemplo n.º 6
0
    def test_numericrange_filter_manual_ranges_labels(self):
        """
        Test we can specify 'ranges' with manual labels
        """
        qs = Book.objects.all()

        ranges = [(Decimal('1.00'), Decimal('4.00'), "$4 or less"),
                  (Decimal('4.00'), Decimal('6.00'), "$4.00 - $6.00"),
                  (Decimal('6.00'), Decimal('100.00'), "More than $6")]

        filter1 = NumericRangeFilter('price', Book, MultiValueDict(), ranges=ranges)
        choices = filter1.get_choices(qs)
        self.assertEqual(choices[0].label, "$4 or less")
        self.assertEqual(choices[1].label, "$4.00 - $6.00")
Ejemplo n.º 7
0
    def test_numericrange_filter_simple_vals(self):
        # If data is less than max_links, we should get a simple list of values.
        filter1 = NumericRangeFilter('price', Book, MultiValueDict(), max_links=20)

        # Limit to single value to force the case
        qs = Book.objects.filter(price=Decimal('3.50'))

        # Should only take 2 queries - one to find out how many distinct values,
        # one to get the counts.
        with self.assertNumQueries(2):
            choices = filter1.get_choices(qs)

        self.assertEqual(len(choices), 1)
        self.assertTrue('3.5' in choices[0].label)
Ejemplo n.º 8
0
    def test_numericrange_filter_manual_ranges_labels(self):
        """
        Test we can specify 'ranges' with manual labels
        """
        qs = Book.objects.all()

        ranges = [(Decimal('1.00'), Decimal('4.00'), "$4 or less"),
                  (Decimal('4.00'), Decimal('6.00'), "$4.00 - $6.00"),
                  (Decimal('6.00'), Decimal('100.00'), "More than $6")]

        filter1 = NumericRangeFilter('price',
                                     Book,
                                     MultiValueDict(),
                                     ranges=ranges)
        choices = filter1.get_choices(qs)
        self.assertEqual(choices[0].label, "$4 or less")
        self.assertEqual(choices[1].label, "$4.00 - $6.00")
Ejemplo n.º 9
0
    def test_numericrange_filter_simple_vals(self):
        # If data is less than max_links, we should get a simple list of values.
        filter1 = NumericRangeFilter('price',
                                     Book,
                                     MultiValueDict(),
                                     max_links=20)

        # Limit to single value to force the case
        qs = Book.objects.filter(price=Decimal('3.50'))

        # Should only take 2 queries - one to find out how many distinct values,
        # one to get the counts.
        with self.assertNumQueries(2):
            choices = filter1.get_choices(qs)

        self.assertEqual(len(choices), 1)
        self.assertTrue('3.5' in choices[0].label)
Ejemplo n.º 10
0
    def test_numericrange_filter_apply_filter(self):
        qs = Book.objects.all()

        # exclusive
        params1 = MultiValueDict({'price': ['3.50..4.00']})
        filter1 = NumericRangeFilter('price', Book, params1)
        qs_filtered1 = filter1.apply_filter(qs)
        self.assertEqual(list(qs_filtered1),
                         list(qs.filter(price__gt=Decimal('3.50'),
                                        price__lt=Decimal('4.00'))))

        # inclusive
        params2 = MultiValueDict({'price': ['3.50i..4.00i']})
        filter2 = NumericRangeFilter('price', Book, params2)
        qs_filtered2 = filter2.apply_filter(qs)
        self.assertEqual(list(qs_filtered2),
                         list(qs.filter(price__gte=Decimal('3.50'),
                                        price__lte=Decimal('4.00'))))
Ejemplo n.º 11
0
    def test_numericrange_filter_manual_ranges(self):
        """
        Test we can specify 'ranges' and it works as expected.
        """
        # Also tests that aggregation works as expected with regards to
        # lower/upper limits.
        qs = Book.objects.all()

        ranges = [(Decimal('3.50'), Decimal('5.00')),
                  (Decimal('5.00'), Decimal('6.00'))]
        # There are books with prices exactly equal to 3.50/5.00/6.00 which
        # makes this test real.

        self.assertTrue(qs.filter(price=Decimal('3.50')).exists())
        self.assertTrue(qs.filter(price=Decimal('5.00')).exists())
        self.assertTrue(qs.filter(price=Decimal('6.00')).exists())

        filter1 = NumericRangeFilter('price', Book, MultiValueDict(), ranges=ranges)
        choices = filter1.get_choices(qs)
        self.assertEqual(choices[0].count, qs.filter(price__gte=Decimal('3.50'), price__lte=Decimal('5.00')).count())
        self.assertEqual(choices[1].count, qs.filter(price__gt=Decimal('5.00'), price__lte=Decimal('6.00')).count())
Ejemplo n.º 12
0
    def test_numericrange_filter_apply_filter(self):
        qs = Book.objects.all()

        # exclusive
        params1 = MultiValueDict({'price': ['3.50..4.00']})
        filter1 = NumericRangeFilter('price', Book, params1)
        qs_filtered1 = filter1.apply_filter(qs)
        self.assertEqual(
            list(qs_filtered1),
            list(
                qs.filter(price__gt=Decimal('3.50'),
                          price__lt=Decimal('4.00'))))

        # inclusive
        params2 = MultiValueDict({'price': ['3.50i..4.00i']})
        filter2 = NumericRangeFilter('price', Book, params2)
        qs_filtered2 = filter2.apply_filter(qs)
        self.assertEqual(
            list(qs_filtered2),
            list(
                qs.filter(price__gte=Decimal('3.50'),
                          price__lte=Decimal('4.00'))))