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)
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))
def test_sort(self): values = ["name"] dgeq = GenericQuery(Country, QueryDict()) commands.Sort()(dgeq, "c:sort", values) rows = dgeq._evaluate() self.assertEqual( Country.objects.order_by("name").first().id, rows[0]["id"])
def test_sort_multiple(self): values = ["-region.name,population"] dgeq = GenericQuery(Country, QueryDict()) commands.Sort()(dgeq, "c:sort", values) rows = dgeq._evaluate() self.assertEqual( Country.objects.order_by("-region__name", "population").first().id, rows[0]["id"])
def test_start(self): dgeq = GenericQuery(Country, QueryDict()) commands.Subset()(dgeq, "c:start", ["4"]) commands.Subset()(dgeq, "c:limit", ["0"]) rows = dgeq._evaluate() self.assertEqual(Country.objects.all()[4:DGEQ_MAX_LIMIT + 4].count(), len(rows)) self.assertEqual(Country.objects.all()[4:][0].id, rows[0]["id"])
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"])
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)
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"])
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"]))
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))
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"])
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))
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"])
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)
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)
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))
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)
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))
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))
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)
def test_no_sort(self): dgeq = GenericQuery(Country, QueryDict()) rows = dgeq._evaluate() self.assertEqual(Country.objects.first().id, rows[0]["id"])
def test_distinct_sliced(self): dgeq = GenericQuery(Forest, QueryDict()) dgeq.sliced = True with self.assertRaises(InvalidCommandError): commands.Distinct()(dgeq, "c:distinct", ["1"])
def test_show(self): values = ["name"] dgeq = GenericQuery(Country, QueryDict()) commands.Show()(dgeq, "c:show", values) self.assertEqual({"name"}, dgeq.fields)
def test_evaluate_invalid(self): values = ["invalid"] dgeq = GenericQuery(Country, QueryDict()) with self.assertRaises(InvalidCommandError): commands.Evaluate()(dgeq, "c:evaluate", values)
def test_not_evaluate(self): values = ["0"] dgeq = GenericQuery(Country, QueryDict()) commands.Evaluate()(dgeq, "c:evaluate", values) self.assertFalse(dgeq.evaluated)
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)
def test_no_time(self): values = ["0"] dgeq = GenericQuery(Country, QueryDict()) commands.Time()(dgeq, "c:time", values) dgeq.evaluate() self.assertNotIn("time", dgeq.result)
def test_distinct_unknown_value(self): dgeq = GenericQuery(Forest, QueryDict()) with self.assertRaises(InvalidCommandError): commands.Distinct()(dgeq, "c:distinct", ["unknown"])
def test_count_invalid_value(self): values = ["invalid"] dgeq = GenericQuery(Country, QueryDict()) with self.assertRaises(InvalidCommandError): commands.Count()(dgeq, "c:count", values)
def test_filtering_sliced(self): dgeq = GenericQuery(Country, QueryDict()) dgeq.sliced = True with self.assertRaises(InvalidCommandError): commands.Filtering()(dgeq, "population", [">1000000"])