def test_select_related_by_source(self):
     response = self.client.get(reverse("select-related-by-source"))
     self.assertEqual(response.status_code, 200)
     self.assertEqual(len(response.data), 27)
     self.assertEqual(response.data[0]["name"], "m1")
     self.assertEqual(response.data[0]["fk_2_name"], "m2")
     self.assertEqual(test_utils.TestQueryCounter().get_counter(), 1)
     query_stack = test_utils.TestQueryCounter().get_queries_stack()
     self.assertIn("tests_autooptimization1model", query_stack[0][0])
     self.assertIn("tests_autooptimization2model", query_stack[0][0])
     self.assertNotIn("tests_autooptimization3model", query_stack[0][0])
 def test_select_related_with_filters(self):
     response = self.client.get(reverse("simple-select-related"), {"fields": "name,fk_2_data,fk_2_data__name"})
     self.assertEqual(response.status_code, 200)
     self.assertEqual(len(response.data), 27)
     self.assertEqual(response.data[0]["name"], "m1")
     self.assertEqual(response.data[0]["fk_2_data"]["name"], "m2")
     self.assertEqual(test_utils.TestQueryCounter().get_counter(), 1)
     query_stack = test_utils.TestQueryCounter().get_queries_stack()
     self.assertIn("tests_autooptimization1model", query_stack[0][0])
     self.assertIn("tests_autooptimization2model", query_stack[0][0])
     self.assertNotIn("tests_autooptimization3model", query_stack[0][0])
    def test_query_counting_client(self):
        client = test_utils.QueryCountingAPIClient()
        for method in ("get", "post", "put", "patch"):
            getattr(client, method)(reverse("sample"))
            self.assertEqual(test_utils.TestQueryCounter().get_counter(), 1)

        # test sending warning
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            client.post(reverse("calls", kwargs={"n": 3}))
            self.assertEqual(len(w), 1)

        # test raising error
        with self.assertRaises(test_utils.TooManySQLQueriesException):
            client.post(reverse("calls", kwargs={"n": 4}))

        # freeze counting
        client.post(
            reverse("calls-partially-frozen", kwargs={"counted": 2, "frozen": 3})
        )
        self.assertEqual(test_utils.TestQueryCounter().get_counter(), 2)
    def test_prefetch_related_with_filters(self):
        response = self.client.get(reverse("simple-prefetch-related"), {"fields": "name,reverse_2_1,reverse_2_1_data"})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data["name"], "m3")
        self.assertEqual(len(response.data["reverse_2_1"]), 3)
        self.assertEqual(len(response.data["reverse_2_1_data"]), 3)
        self.assertEqual(response.data["reverse_2_1_data"][0]["name"], "m2")
        self.assertEqual(len(response.data["reverse_2_1_data"][0]["reverse_1"]), 3)
        self.assertEqual(response.data["reverse_2_1_data"][0]["reverse_1_data"][0]["name"], "m1")

        # main object, reverse_2_1, reverse_2_1__reverse_1
        self.assertEqual(test_utils.TestQueryCounter().get_counter(), 3)
    def test_forced_prefetch_related(self):
        response = self.client.get(reverse("prefetch-related-forced"))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 3)
        self.assertEqual(response.data[0]["name"], "m3")
        self.assertEqual(response.data[0]["sample_data"]["a"], "a")
        self.assertEqual(len(response.data[0]["reverse_2_1"]), 3)
        self.assertEqual(len(response.data[0]["reverse_2_2"]), 3)
        self.assertEqual(len(response.data[0]["reverse_2_1_data"]), 3)
        self.assertEqual(len(response.data[0]["reverse_2_2_data"]), 3)
        self.assertEqual(response.data[0]["reverse_2_1_data"][0]["name"], "m2")
        self.assertEqual(response.data[0]["reverse_2_2_data"][0]["name"], "m2")
        self.assertEqual(response.data[0]["reverse_2_1_data"][0]["sample_data"]["a"], "a")
        self.assertEqual(response.data[0]["reverse_2_2_data"][0]["sample_data"]["a"], "a")
        self.assertEqual(len(response.data[0]["reverse_2_1_data"][0]["reverse_1"]), 3)
        self.assertEqual(len(response.data[0]["reverse_2_2_data"][0]["reverse_1_data"]), 3)
        self.assertEqual(response.data[0]["reverse_2_2_data"][0]["reverse_1_data"][0]["name"], "m1")

        # main objects list, reverse_2_1, reverse_2_2, reverse_2_1__sample, reverse_2_2__sample,
        # reverse_2_1__reverse_1, reverse_2_2__reverse_1
        self.assertEqual(test_utils.TestQueryCounter().get_counter(), 8)
    def test_prefetch_with_select_related_with_include_fields(self):
        response = self.client.get(reverse("prefetch-with-select-related"), {
            "include_fields": "reverse_2_1_data__reverse_1_data__sample_m2m_data"
        })
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data), 3)
        self.assertEqual(response.data[0]["name"], "m3")
        self.assertEqual(response.data[0]["sample_data"]["a"], "a")
        self.assertEqual(len(response.data[0]["reverse_2_1"]), 3)
        self.assertEqual(len(response.data[0]["reverse_2_2"]), 3)
        self.assertEqual(len(response.data[0]["reverse_2_1_data"]), 3)
        self.assertEqual(len(response.data[0]["reverse_2_2_data"]), 3)
        self.assertEqual(response.data[0]["reverse_2_1_data"][0]["name"], "m2")
        self.assertEqual(response.data[0]["reverse_2_2_data"][0]["name"], "m2")
        self.assertEqual(response.data[0]["reverse_2_1_data"][0]["sample_data"]["a"], "a")
        self.assertEqual(response.data[0]["reverse_2_2_data"][0]["sample_data"]["a"], "a")
        self.assertEqual(len(response.data[0]["reverse_2_1_data"][0]["reverse_1"]), 3)
        self.assertEqual(len(response.data[0]["reverse_2_2_data"][0]["reverse_1_data"]), 3)
        self.assertEqual(response.data[0]["reverse_2_2_data"][0]["reverse_1_data"][0]["name"], "m1")

        # main objects list, reverse_2_1, reverse_2_2, reverse_2_1__sample, reverse_2_2__sample,
        # reverse_2_1__reverse_1, reverse_2_2__reverse_1, reverse_2_1__reverse_1__sample_m2m
        self.assertEqual(test_utils.TestQueryCounter().get_counter(), 8)