def test_lte_float(self): queryset = Country.objects.all() expected = queryset.filter(population__lte=600000.0) f = Filter("population", f"]600000.0", False) queryset = f.apply(queryset) self.assertEqual(list(expected), list(queryset))
def test_equal_str_case_sensitive(self): queryset = Country.objects.all() c = queryset[random.randint(0, queryset.count() - 1)] f = Filter("name", c.name, True) queryset = f.apply(queryset) self.assertEqual([c], list(queryset))
def test_different_int(self): queryset = Country.objects.all() expected = queryset.exclude(population=600000) f = Filter("population", f"!600000", False) queryset = f.apply(queryset) self.assertEqual(list(expected), list(queryset))
def test_equal_int(self): queryset = Country.objects.all() c = queryset[random.randint(0, queryset.count() - 1)] f = Filter("population", str(c.population), False) queryset = f.apply(queryset) self.assertEqual([c], list(queryset))
def test_gt_int(self): queryset = Country.objects.all() expected = queryset.filter(population__gt=600000) f = Filter("population", f">600000", False) queryset = f.apply(queryset) self.assertEqual(list(expected), list(queryset))
def test_join_from_query_ok(self): subquery = ("field=mountains|start=1|limit=1|sort=-height|distinct=0|" "filters=height=<3000'height=>1500") j = JoinQuery.from_query_value(subquery, Country, False, self.censor) self.assertEqual(Mountain, j.model) self.assertEqual("mountains", j.field) self.assertEqual({"height", "id", "name"}, j.fields) self.assertEqual({"countries"}, j._many_fields) self.assertEqual(set(), j._one_fields) self.assertEqual(1, j.limit) self.assertEqual(1, j.start) self.assertEqual(["-height"], j.sort) self.assertEqual([ Filter("height", "<3000", False), Filter("height", ">1500", False), ], j.filters)
def test_endswith_str_case_sensitive(self): queryset = Country.objects.all() c = queryset[random.randint(0, queryset.count() - 1)] expected = queryset.filter(name__endswith=c.name[-1]) f = Filter("name", f"${c.name[-1]}", True) queryset = f.apply(queryset) self.assertEqual(list(expected), list(queryset))
def test_lte_str(self): queryset = Country.objects.all() c = queryset[random.randint(0, queryset.count() - 1)] expected = queryset.filter(name__lte=c.name) f = Filter("name", f"]{c.name}", False) queryset = f.apply(queryset) self.assertEqual(list(expected), list(queryset))
def test_contains_str_case_insensitive(self): queryset = Country.objects.all() c = queryset[random.randint(0, queryset.count() - 1)] expected = queryset.filter(name__icontains=c.name[-1]) f = Filter("name", f"*{c.name[-1].lower()}", False) queryset = f.apply(queryset) self.assertEqual(list(expected), list(queryset))
def test_doesnt_contains_str_case_sensitive(self): queryset = Country.objects.all() c = queryset[random.randint(0, queryset.count() - 1)] expected = queryset.exclude(name__contains=c.name[-1]) f = Filter("name", f"~{c.name[-1]}", True) queryset = f.apply(queryset) self.assertEqual(list(expected), list(queryset))
def test_different_datetime(self): queryset = Disaster.objects.all() d = queryset[random.randint(0, queryset.count() - 1)] expected = queryset.exclude(date=d.date) f = Filter("date", f"!{d.date.isoformat()}", False) queryset = f.apply(queryset) self.assertEqual(list(expected), list(queryset))
def test_join_from_query_show_prevail_over_hide_ok(self): subquery = ( "field=mountains|show=height|hide=name|start=1|limit=1|sort=-height|" "filters=height=<3000'height=>1500") j = JoinQuery.from_query_value(subquery, Country, False, self.censor) self.assertEqual(Mountain, j.model) self.assertEqual("mountains", j.field) self.assertEqual({"height"}, j.fields) self.assertEqual(set(), j._many_fields) self.assertEqual(set(), j._one_fields) self.assertEqual(1, j.limit) self.assertEqual(1, j.start) self.assertEqual(["-height"], j.sort) self.assertEqual([ Filter("height", "<3000", False), Filter("height", ">1500", False), ], j.filters)
def test_different_str_case_sensitive(self): queryset = Country.objects.all() c = queryset[random.randint(0, queryset.count() - 1)] expected = queryset.exclude(name__exact=c.name) f = Filter("name", f"!{c.name}", True) queryset = f.apply(queryset) self.assertEqual(list(expected), list(queryset))
def test_equal_datetime(self): queryset = Disaster.objects.all() d = queryset[random.randint(0, queryset.count() - 1)] f = Filter("date", d.date.isoformat(), False) queryset = f.apply(queryset) self.assertEqual(list(Disaster.objects.filter(date=d.date)), list(queryset))
def test_filters(self): filters = [Filter("rivers.length", ">2000", False)] annotations = [Annotation("rivers__length", "rivers_length_count", models.Count, filters)] query = Country.objects.all() for a in annotations: query = a.apply(query) query = [{"name": c.name, "rivers_length_count": c.rivers_length_count} for c in query] expected = Country.objects.all().annotate( rivers_length_count=models.Count("rivers__length", filter=Q(rivers__length__gt=2000)), ) expected = [ {"name": c.name, "rivers_length_count": c.rivers_length_count} for c in expected ] self.assertEqual(expected, query)
def test_different_float(self): queryset = Country.objects.all() f = Filter("population", f"!{str(float())}", True) with self.assertRaises(SearchModifierError): f.apply(queryset)
def test_lte_none(self): queryset = River.objects.all() f = Filter("discharge", "]", True) with self.assertRaises(SearchModifierError): f.apply(queryset)
def test_equal_none(self): queryset = River.objects.all() expected = queryset.filter(discharge=None) f = Filter("discharge", "", False) queryset = f.apply(queryset) self.assertEqual(list(expected), list(queryset))
def test_disabled_lookup(self): f = Filter("population", f"!10", True) with self.assertRaises(SearchModifierError): f.get()
def test_different_none(self): queryset = River.objects.all() expected = queryset.exclude(discharge=None) f = Filter("discharge", "!", False) queryset = f.apply(queryset) self.assertEqual(list(expected), list(queryset))