def test_prefetch_unique(self): j = JoinQuery.from_query_value("field=region|sort=name", Country, False, self.censor) rows = list() with CaptureQueriesContext(connections['default']): for c in Country.objects.all(): rows.append(j.fetch(c)) self.assertEqual("Southern Asia", rows[0]["name"]) j = JoinMixin() j.add_join( "region__continent", JoinQuery.from_query_value("field=region.countries|sort=name", Country, False, self.censor), Country, self.censor) rows = list() with CaptureQueriesContext(connections['default']): for c in Country.objects.all(): rows.append(j.joins["region"].fetch(c)) self.assertEqual("Afghanistan", rows[0]["continent"][0]["name"]) j = JoinMixin() j.add_join( "region__continent__regions__name", JoinQuery.from_query_value( "field=region.continent.regions|sort=name", Country, False, self.censor), Country, self.censor) rows = list() with CaptureQueriesContext(connections['default']): for c in Country.objects.all(): rows.append(j.joins["region"].fetch(c)) self.assertEqual("Central Asia", rows[0]["continent"]["regions"][0]["name"]) # noqa
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_prefetch_depth_3(self): j = JoinMixin() j.add_join( "regions__countries__mountains", JoinQuery.from_query_value( "field=regions.countries.mountains|filters=height=>1500|sort=-height|limit=1", Continent, False, self.censor), Continent, self.censor) rows_without_prefetch = list() with CaptureQueriesContext(connections['default']) as context: for c in Continent.objects.all(): rows_without_prefetch.append(j.joins['regions'].fetch(c)) num_queries_without_prefetch = len(context.captured_queries) queryset = Continent.objects.all() queryset = j.joins['regions'].prefetch(queryset) # noqa rows_with_prefetch = list() with CaptureQueriesContext(connections['default']) as context: for c in queryset: rows_with_prefetch.append(j.joins['regions'].fetch(c)) num_queries_with_prefetch = len(context.captured_queries) self.assertLess(num_queries_with_prefetch, num_queries_without_prefetch) self.assertEqual(rows_without_prefetch, rows_with_prefetch) self.assertEqual( "Mount Tahat", rows_with_prefetch[0][0]["countries"][0]["mountains"][0]["name"])
def test_prefetch_reduce_num_queries_and_time_unique_and_many(self): j = JoinQuery.from_query_value("field=region", Country, False, self.censor) other = JoinQuery.from_query_value("field=mountains", Country, False, self.censor) j.add_join("mountains", other, Country, self.censor) with CaptureQueriesContext(connections['default']) as context: for c in Country.objects.all(): j.fetch(c) num_queries_without_prefetch = len(context.captured_queries) queryset = Country.objects.all() queryset = j.prefetch(queryset) with CaptureQueriesContext(connections['default']) as context: for c in queryset: j.fetch(c) num_queries_with_prefetch = len(context.captured_queries) self.assertLess(num_queries_with_prefetch, num_queries_without_prefetch)
def test_prefetch_many(self): j = JoinQuery.from_query_value( "field=mountains|sort=-height|distinct=1", Country, False, self.censor) rows = list() with CaptureQueriesContext(connections['default']): for c in Country.objects.all(): rows.append(j.fetch(c)) self.assertEqual("Noshaq", rows[0][0]["name"]) j = JoinMixin() j.add_join( "regions__continent", JoinQuery.from_query_value("field=regions.continent|sort=name", Continent, False, self.censor), Continent, self.censor) rows = list() with CaptureQueriesContext(connections['default']): for c in Continent.objects.all(): rows.append(j.joins["regions"].fetch(c)) self.assertEqual("Africa", rows[0][0]["continent"]["name"])
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_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_join_from_query_filter_invalid(self): subquery = ("field=mountains|show=name|start=1|limit=1|sort=-height|" "filters=height=<3000'height1500") with self.assertRaises(InvalidCommandError): JoinQuery.from_query_value(subquery, Country, False, self.censor)
def test_join_from_query_not_a_related_field(self): subquery = ("field=population|show=name|start=1|limit=1|sort=-height|" "filters=height=<3000'height=>1500") with self.assertRaises(NotARelatedFieldError): JoinQuery.from_query_value(subquery, Country, False, self.censor)