Exemple #1
0
 def test_time(self):
     values = ["1"]
     dgeq = GenericQuery(Country, QueryDict())
     commands.Time()(dgeq, "c:time", values)
     dgeq.evaluate()
     self.assertIn("time", dgeq.result)
     self.assertLess(dgeq.result["time"], 2)
Exemple #2
0
 def test_start_limit(self):
     dgeq = GenericQuery(Country, QueryDict())
     commands.Subset()(dgeq, "c:start", ["4"])
     commands.Subset()(dgeq, "c:limit", ["52"])
     rows = dgeq._evaluate()
     self.assertEqual(Country.objects.all()[4:56].count(), len(rows))
     self.assertEqual(Country.objects.all()[4:56][0].id, rows[0]["id"])
Exemple #3
0
 def test_private(self):
     query_dict = QueryDict("c:join=field=rivers&c:sort=name&c:limit=1")
     dgeq = GenericQuery(Country,
                         query_dict,
                         private_fields={
                             River: ["countries", "discharge"],
                             Country: ["forests", "mountains", "disasters"]
                         })
     res = dgeq.evaluate()
     expected = {
         "status":
         True,
         "rows": [{
             "area":
             652864,
             "id":
             1,
             "population":
             36296100,
             "name":
             "Afghanistan",
             "region":
             15,
             "rivers": [{
                 "length": 2620,
                 "id": 37,
                 "name": "Amu Darya–Panj"
             }, {
                 "length": 1130,
                 "id": 165,
                 "name": "Helmand"
             }]
         }]
     }
     self.assertEqual(expected, res)
Exemple #4
0
 def test_sort_desc(self):
     values = ["-population"]
     dgeq = GenericQuery(Country, QueryDict())
     commands.Sort()(dgeq, "c:sort", values)
     rows = dgeq._evaluate()
     self.assertEqual(
         Country.objects.order_by("-population").first().id, rows[0]["id"])
Exemple #5
0
 def test_public_cause_unknown_field(self):
     query_dict = QueryDict("area=>100000000")
     dgeq = GenericQuery(Country,
                         query_dict,
                         public_fields={Country: ["population"]})
     res = dgeq.evaluate()
     self.assertIn("code", res)
     self.assertEqual("UNKNOWN_FIELD", res["code"])
Exemple #6
0
 def test_dgeq_error(self):
     query_dict = QueryDict("c:case=invalid")
     dgeq = GenericQuery(Country, query_dict)
     res = dgeq.evaluate()
     self.assertEqual(False, res["status"])
     self.assertEqual("INVALID_COMMAND_ERROR", res["code"])
     self.assertIn("command", res)
     self.assertEqual("c:case", res["command"])
Exemple #7
0
 def test_evaluate_simple(self):
     query_dict = QueryDict("name=Germany")
     dgeq = GenericQuery(Country, query_dict)
     res = dgeq.evaluate()
     self.assertEqual(True, res["status"])
     self.assertEqual(
         Country.objects.get(name="Germany").id, res["rows"][0]["id"])
     self.assertEqual("Germany", res["rows"][0]["name"])
     self.assertEqual(1, len(res["rows"]))
Exemple #8
0
    def test_case(self):
        values = ["0"]
        dgeq = GenericQuery(Country, QueryDict())
        commands.Case()(dgeq, "c:case", values)
        self.assertEqual(False, dgeq.case)

        values = ["1"]
        dgeq = GenericQuery(Country, QueryDict())
        commands.Case()(dgeq, "c:case", values)
        self.assertEqual(True, dgeq.case)
Exemple #9
0
    def test_count(self):
        values = ["0"]
        dgeq = GenericQuery(Country, QueryDict())
        commands.Count()(dgeq, "c:count", values)
        self.assertNotIn("count", dgeq.result)

        values = ["1"]
        dgeq = GenericQuery(Country, QueryDict())
        commands.Count()(dgeq, "c:count", values)
        self.assertIn("count", dgeq.result)
        self.assertEqual(Country.objects.all().count(), dgeq.result["count"])
Exemple #10
0
    def test_distinct_false(self):
        dgeq = GenericQuery(Forest, QueryDict())
        commands.Filtering()(dgeq, "countries.region.name", ["South America"])
        without_distinct = dgeq.queryset.count()

        dgeq = GenericQuery(Forest, QueryDict())
        commands.Filtering()(dgeq, "countries.region.name", ["South America"])
        commands.Distinct()(dgeq, "c:distinct", ["0"])
        with_distinct = dgeq.queryset.count()

        self.assertEqual(10, without_distinct)
        self.assertEqual(10, with_distinct)
Exemple #11
0
 def test_permission_cause_unknown_field(self):
     query_dict = QueryDict("rivers.length=>1000")
     user = User.objects.create_user("test")
     user.user_permissions.add(
         Permission.objects.get(codename='view_country'))
     dgeq = GenericQuery(Country,
                         query_dict,
                         user=user,
                         use_permissions=True)
     res = dgeq.evaluate()
     self.assertIn("code", res)
     self.assertEqual("UNKNOWN_FIELD", res["code"])
Exemple #12
0
 def test_advanced_4(self):
     """Count if there is at least five regions where the country with the
     lowest population is less more 100000.
     
     The five regions are the first when sorted by the value of the lower
     population."""
     query_dict = QueryDict(
         "c:annotate=field=countries.population|func=min|to=pop_min|early=1"
         "&pop_min=>10000&c:evaluate=0&c:sort=pop_min&c:limit=5&c:count=1")
     dgeq = GenericQuery(Region, query_dict)
     res = dgeq.evaluate()
     expected = {"status": True, "count": 5}
     self.assertDictEqual(expected, res)
Exemple #13
0
    def test_invalid_value(self):
        values = ["-1"]
        dgeq = GenericQuery(Country, QueryDict())
        with self.assertRaises(InvalidCommandError):
            commands.Subset()(dgeq, "c:limit", values)

        dgeq = GenericQuery(Country, QueryDict())
        with self.assertRaises(InvalidCommandError):
            commands.Subset()(dgeq, "c:start", values)

        dgeq = GenericQuery(Country, QueryDict())
        with self.assertRaises(InvalidCommandError):
            commands.Subset()(dgeq, "c:limit", [f"{DGEQ_MAX_LIMIT + 1}"])
Exemple #14
0
 def test_show_hide(self):
     show = ["name"]
     hide = ["population,rivers,region,mountains,name"]
     dgeq = GenericQuery(Country, QueryDict())
     commands.Show()(dgeq, "c:show", show)
     commands.Show()(dgeq, "c:hide", hide)
     self.assertEqual({"disasters", "id", "area", "forests"}, dgeq.fields)
Exemple #15
0
 def test_hide_show(self):
     show = ["name"]
     hide = ["population,rivers,region,mountains,name"]
     dgeq = GenericQuery(Country, QueryDict())
     commands.Show()(dgeq, "c:hide", hide)
     commands.Show()(dgeq, "c:show", show)
     self.assertEqual({"name"}, dgeq.fields)
Exemple #16
0
 def test_no_show(self):
     dgeq = GenericQuery(Country, QueryDict())
     fields = {
         "name", "disasters", "id", "area", "forests", "population",
         "rivers", "region", "mountains"
     }
     self.assertEqual(fields, dgeq.fields)
Exemple #17
0
    def test_join_ok(self):
        values = ["field=rivers,field=region", "field=mountains"]
        dgeq = GenericQuery(Country, QueryDict())
        commands.Join()(dgeq, "c:join", values)

        self.assertIn("rivers", dgeq.joins)
        self.assertIn("mountains", dgeq.joins)
        self.assertIn("region", dgeq.joins)
Exemple #18
0
 def test_filtering_ok(self):
     dgeq = GenericQuery(Country, QueryDict())
     commands.Filtering()(dgeq, "population", [">1000000"])
     commands.Filtering()(dgeq, "name", ["*republic"])
     self.assertEqual(
         list(
             Country.objects.filter(population__gt=1000000,
                                    name__contains="republic")),
         list(dgeq.queryset))
Exemple #19
0
 def test__evaluate_no_related(self):
     dgeq = GenericQuery(Country, QueryDict())
     dgeq.fields = {"name", "population", "rivers", "region"}
     dgeq.arbitrary_fields = set()
     dgeq.queryset = Country.objects.all()
     dgeq.related = False
     rows = dgeq._evaluate()
     self.assertEqual(DGEQ_DEFAULT_LIMIT, len(rows))
Exemple #20
0
 def test_aggregate(self):
     subquery = "field=population|func=avg|to=population_avg"
     dgeq = GenericQuery(Country, QueryDict())
     commands.Aggregate()(dgeq, "c:aggregate", [subquery])
     self.assertEqual(["status", "population_avg"],
                      list(dgeq.result.keys()))
     self.assertEqual(
         Country.objects.all().aggregate(
             population_avg=models.Avg("population"))["population_avg"],
         dgeq.result["population_avg"])
Exemple #21
0
    def test__evaluate_joins(self):
        dgeq = GenericQuery(Country, QueryDict())
        dgeq.fields = {"name", "population", "rivers", "region"}
        dgeq.arbitrary_fields = set()
        dgeq.queryset = Country.objects.all()

        j_rivers = JoinQuery.from_query_value("field=rivers", Country, False,
                                              self.censor)
        j_region = JoinQuery.from_query_value("field=region", Country, False,
                                              self.censor)
        dgeq.add_join("rivers", j_rivers, Country, self.censor)
        dgeq.add_join("region", j_region, Country, self.censor)

        rows = dgeq._evaluate()
        self.assertEqual(DGEQ_DEFAULT_LIMIT, len(rows))
Exemple #22
0
    def test_annotate(self):
        subquery = "field=rivers.length|func=avg|to=river_length|filters=rivers.length=>3000|early=0"
        dgeq = GenericQuery(Country, QueryDict())
        commands.Annotate()(dgeq, "c:annotate", [subquery])

        queryset = Country.objects.all().annotate(river_length=models.Avg(
            "rivers__length", filter=models.Q(rivers__length__gt=3000)))
        self.assertGreater(queryset.count(), 0)
        self.assertEqual(queryset.count(), dgeq.queryset.count())
        for c1, c2 in zip(queryset, dgeq.queryset):
            self.assertEqual(c1, c2)
            self.assertTrue(hasattr(c1, "river_length"))
            self.assertTrue(hasattr(c2, "river_length"))
            self.assertEqual(c1.river_length, c2.river_length)
Exemple #23
0
 def test_permission_and_private(self):
     query_dict = QueryDict("c:join=field=rivers&c:sort=name&c:limit=1")
     user = User.objects.create_user("test")
     user.user_permissions.add(
         Permission.objects.get(codename='view_country'))
     user.user_permissions.add(
         Permission.objects.get(codename='view_river'))
     dgeq = GenericQuery(Country,
                         query_dict,
                         private_fields={River: ["discharge", "countries"]},
                         user=user,
                         use_permissions=True)
     res = dgeq.evaluate()
     expected = {
         "status":
         True,
         "rows": [{
             "area":
             652864,
             "id":
             1,
             "population":
             36296100,
             "name":
             "Afghanistan",
             "rivers": [{
                 "length": 2620,
                 "id": 37,
                 "name": "Amu Darya–Panj"
             }, {
                 "length": 1130,
                 "id": 165,
                 "name": "Helmand"
             }]
         }]
     }
     self.assertEqual(expected, res)
Exemple #24
0
 def test__evaluate_each_field_type(self):
     dgeq = GenericQuery(Country, QueryDict())
     dgeq.fields = {
         "name", "population", "rivers", "region", "rivers_length_avg"
     }
     dgeq.arbitrary_fields = {"rivers_length_avg"}
     dgeq.queryset = Country.objects.all().annotate(
         rivers_length_avg=models.Avg("rivers__length"))
     rows = dgeq._evaluate()
     self.assertEqual(constants.DGEQ_DEFAULT_LIMIT, len(rows))
Exemple #25
0
 def test_no_sort(self):
     dgeq = GenericQuery(Country, QueryDict())
     rows = dgeq._evaluate()
     self.assertEqual(Country.objects.first().id, rows[0]["id"])
Exemple #26
0
 def test_limit_gt_0(self):
     values = ["89"]
     dgeq = GenericQuery(Country, QueryDict())
     commands.Subset()(dgeq, "c:limit", values)
     rows = dgeq._evaluate()
     self.assertEqual(89, len(rows))
Exemple #27
0
 def test_limit_0(self):
     values = ["0"]
     dgeq = GenericQuery(Country, QueryDict())
     commands.Subset()(dgeq, "c:limit", values)
     rows = dgeq._evaluate()
     self.assertEqual(settings.DGEQ_MAX_LIMIT, len(rows))
Exemple #28
0
 def test_sort_after_slicing(self):
     values = ["-region.name,population"]
     dgeq = GenericQuery(Country, QueryDict())
     dgeq.sliced = True
     with self.assertRaises(InvalidCommandError):
         commands.Sort()(dgeq, "c:sort", values)
Exemple #29
0
 def test_count_invalid_value(self):
     values = ["invalid"]
     dgeq = GenericQuery(Country, QueryDict())
     with self.assertRaises(InvalidCommandError):
         commands.Count()(dgeq, "c:count", values)
Exemple #30
0
 def test_hide(self):
     values = ["population,rivers,region,mountains"]
     dgeq = GenericQuery(Country, QueryDict())
     commands.Show()(dgeq, "c:hide", values)
     fields = {"name", "disasters", "id", "area", "forests"}
     self.assertEqual(fields, dgeq.fields)