Ejemplo n.º 1
0
 def test_from_query_value(self):
     attributes = [
         "population_max", "population_min", "population_avg",
     ]
     query_string = (
         "field=population|func=avg|to=population_avg|filters=rivers.length=<1500|early=0,"
         "field=population|func=max|to=population_max|filters=rivers.length=<1500|early=0,"
         "field=population|func=min|to=population_min|filters=rivers.length=<1500|early=0"
     )
     annotations = utils.split_list_values([query_string], ",")
     annotations = [Annotation.from_query_value(a, Country, False, self.censor) for a in annotations]
     query = Country.objects.all()
     for a in annotations:
         query = a.apply(query)
     query = [
         {a: getattr(c, a) for a in attributes} for c in query
     ]
     
     expected = Country.objects.all().annotate(
         population_max=models.Max("population", filter=Q(rivers__length__lt=1500)),
         population_min=models.Min("population", filter=Q(rivers__length__lt=1500)),
         population_avg=models.Avg("population", filter=Q(rivers__length__lt=1500)),
     )
     expected = [
         {a: getattr(c, a) for a in attributes} for c in expected
     ]
     self.assertEqual(expected, query)
Ejemplo n.º 2
0
 def test_invalid_early(self):
     query_string = (
         "field=population|func=max|to=population_max|filters=rivers.length=<1500|early=invalid"
     )
     annotations = utils.split_list_values([query_string], ",")
     
     with self.assertRaises(InvalidCommandError):
         [Annotation.from_query_value(a, Country, False, self.censor) for a in annotations]
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 def test_functions(self):
     attributes = [
         "rivers_length_max", "rivers_length_min", "rivers_length_avg", "rivers_length_sum",
         "rivers_length_dcnt", "rivers_length_cnt",  # "rivers_length_stddev", "rivers_length_var"
     ]
     annotations = [
         Annotation("rivers__length", "rivers_length_max", models.Max),
         Annotation("rivers__length", "rivers_length_min", models.Min),
         Annotation("rivers__length", "rivers_length_avg", models.Avg),
         Annotation("rivers__length", "rivers_length_sum", models.Sum),
         Annotation("rivers__length", "rivers_length_cnt", models.Count),
         Annotation("rivers__length", "rivers_length_dcnt", DistinctCount),
         # These function aren't supported by sqlite at the moment
         # Annotation("rivers__length", "rivers_length_stddev", models.StdDev),
         # Annotation("rivers__length", "rivers_length_var", models.Variance),
     ]
     query = Country.objects.all()
     for a in annotations:
         query = a.apply(query)
     query = [
         {a: getattr(c, a) for a in attributes} for c in query
     ]
     
     expected = Country.objects.all().annotate(
         rivers_length_max=models.Max("rivers__length"),
         rivers_length_min=models.Min("rivers__length"),
         rivers_length_avg=models.Avg("rivers__length"),
         rivers_length_sum=models.Sum("rivers__length"),
         rivers_length_cnt=models.Count("rivers__length"),
         rivers_length_dcnt=models.Count("rivers__length", distinct=True),
         # rivers_length_var=models.Variance("rivers__length"),
         # rivers_length_stddev=models.StdDev("rivers__length")
     )
     expected = [
         {a: getattr(c, a) for a in attributes} for c in expected
     ]
     self.assertEqual(expected, query)
Ejemplo n.º 5
0
 def test_no_equal_subquery_string(self):
     query_string = "population"
     annotations = utils.split_list_values([query_string], ",")
     
     with self.assertRaises(InvalidCommandError):
         [Annotation.from_query_value(a, Country, False, self.censor) for a in annotations]
Ejemplo n.º 6
0
 def test_early(self):
     attributes = [
         "rivers_length_max", "rivers_length_min", "rivers_length_avg", "rivers_length_sum",
         "rivers_length_cnt",
         # "rivers_length_stddev", "rivers_length_var",
         "rivers_length_max_early", "rivers_length_min_early", "rivers_length_avg_early",
         "rivers_length_sum_early", "rivers_length_cnt_early",
         # "rivers_length_stddev_early", "rivers_length_var_early",
     ]
     annotations = [
         Annotation("rivers__length", "rivers_length_max_early", models.Max, early=True),
         Annotation("rivers__length", "rivers_length_min_early", models.Min, early=True),
         Annotation("rivers__length", "rivers_length_avg_early", models.Avg, early=True),
         Annotation("rivers__length", "rivers_length_sum_early", models.Sum, early=True),
         Annotation("rivers__length", "rivers_length_cnt_early", models.Count, early=True),
         # These function aren't supported by sqlite at the moment
         # Annotation("rivers__length", "rivers_length_stddev", models.StdDev, early=True),
         # Annotation("rivers__length", "rivers_length_var", models.Variance, early=True),
         
         # Delayed annotation
         Annotation("rivers__length", "rivers_length_max", models.Max),
         Annotation("rivers__length", "rivers_length_min", models.Min),
         Annotation("rivers__length", "rivers_length_avg", models.Avg),
         Annotation("rivers__length", "rivers_length_sum", models.Sum),
         Annotation("rivers__length", "rivers_length_cnt", models.Count),
         # Annotation("rivers__length", "rivers_length_stddev_delayed", models.StdDev),
         # Annotation("rivers__length", "rivers_length_var_delayed", models.Variance),
     ]
     query = Country.objects.all()
     # Applying early annotation
     for a in annotations:
         if a.early:
             query = a.apply(query)
     query = query.filter(region__continent__name="Europe")
     # Applying late annotation
     for a in annotations:
         if not a.early:
             query = a.apply(query)
     query = [
         {a: getattr(c, a) for a in attributes} for c in query
     ]
     
     expected = Country.objects.all().annotate(
         rivers_length_max_early=models.Max("rivers__length"),
         rivers_length_min_early=models.Min("rivers__length"),
         rivers_length_avg_early=models.Avg("rivers__length"),
         rivers_length_sum_early=models.Sum("rivers__length"),
         rivers_length_cnt_early=models.Count("rivers__length"),
         # rivers_length_var_early=models.Variance("rivers__length"),
         # rivers_length_stddev_early=models.StdDev("rivers__length")
     ).filter(region__continent__name="Europe").annotate(
         rivers_length_max=models.Max("rivers__length"),
         rivers_length_min=models.Min("rivers__length"),
         rivers_length_avg=models.Avg("rivers__length"),
         rivers_length_sum=models.Sum("rivers__length"),
         rivers_length_cnt=models.Count("rivers__length"),
         # rivers_length_var=models.Variance("rivers__length"),
         # rivers_length_stddev=models.StdDev("rivers__length")
     )
     expected = [
         {a: getattr(c, a) for a in attributes} for c in expected
     ]
     self.assertEqual(expected, query)