# Path: tests/testapp/utils.py
# class CaptureLastQuery:
#     def __init__(self, conn=None):
#         if conn is None:  # pragma: no branch
#             conn = connection
#         self.conn = conn
# 
#     def __enter__(self):
#         self.capturer = CaptureQueriesContext(self.conn)
#         self.capturer.__enter__()
#         return self
# 
#     def __exit__(self, a, b, c):
#         self.capturer.__exit__(a, b, c)
# 
#     @property
#     def query(self):
#         return self.capturer.captured_queries[-1]["sql"]


from django.db import connection
from django.test import TestCase
from django.test.utils import override_settings
from django_mysql.rewrite_query import rewrite_query
from tests.testapp.utils import CaptureLastQuery
    def test_index_hint_for_group_by(self):
        assert rewrite_query(
            "SELECT col_a, SUM(col_b) FROM `sometable` "
            + "WHERE (/*QueryRewrite':index=`sometable` FORCE FOR GROUP BY "
            + "`idx`*/1) GROUP BY col_a"
        ) == (
            "SELECT col_a, SUM(col_b) FROM `sometable` FORCE INDEX FOR "
            + "GROUP BY (`idx`) WHERE (1) GROUP BY col_a"
        )

    def test_index_hint_for_order_by(self):
        assert rewrite_query(
            "SELECT col_a FROM `sometable` "
            + "WHERE (/*QueryRewrite':index=`sometable` USE FOR ORDER BY "
            + "`idx` */1) ORDER BY col_a"
        ) == (
            "SELECT col_a FROM `sometable` USE INDEX FOR ORDER BY (`idx`) "
            + "WHERE (1) ORDER BY col_a"
        )

    def test_it_is_instrumented(self):
        with CaptureLastQuery() as cap, connection.cursor() as cursor:
            cursor.execute(
                "SELECT 1 FROM DUAL WHERE (/*QueryRewrite':STRAIGHT_JOIN*/1)"
            )
        assert cap.query == "SELECT STRAIGHT_JOIN 1 FROM DUAL WHERE (1)"

    @override_settings(DJANGO_MYSQL_REWRITE_QUERIES=False)
    def test_instrumentation_can_be_disabled(self):
        query = "SELECT 1 FROM DUAL WHERE (/*QueryRewrite':STRAIGHT_JOIN*/1)"

# Path: tests/testapp/utils.py
# class CaptureLastQuery:
#     def __init__(self, conn=None):
#         if conn is None:  # pragma: no branch
#             conn = connection
#         self.conn = conn
# 
#     def __enter__(self):
#         self.capturer = CaptureQueriesContext(self.conn)
#         self.capturer.__enter__()
#         return self
# 
#     def __exit__(self, a, b, c):
#         self.capturer.__exit__(a, b, c)
# 
#     @property
#     def query(self):
#         return self.capturer.captured_queries[-1]["sql"]


# Path: tests/testapp/test_rewrite_query.py
from django.db import connection
from django.test import TestCase
from django.test.utils import override_settings
from django_mysql.rewrite_query import rewrite_query
from tests.testapp.utils import CaptureLastQuery

    def test_index_hint_for_group_by(self):
        assert rewrite_query(
            "SELECT col_a, SUM(col_b) FROM `sometable` "
            + "WHERE (/*QueryRewrite':index=`sometable` FORCE FOR GROUP BY "
            + "`idx`*/1) GROUP BY col_a"
        ) == (
            "SELECT col_a, SUM(col_b) FROM `sometable` FORCE INDEX FOR "
            + "GROUP BY (`idx`) WHERE (1) GROUP BY col_a"
        )

    def test_index_hint_for_order_by(self):
        assert rewrite_query(
            "SELECT col_a FROM `sometable` "
            + "WHERE (/*QueryRewrite':index=`sometable` USE FOR ORDER BY "
            + "`idx` */1) ORDER BY col_a"
        ) == (
            "SELECT col_a FROM `sometable` USE INDEX FOR ORDER BY (`idx`) "
            + "WHERE (1) ORDER BY col_a"
        )

    def test_it_is_instrumented(self):
        with CaptureLastQuery() as cap, connection.cursor() as cursor:
            cursor.execute(
                "SELECT 1 FROM DUAL WHERE (/*QueryRewrite':STRAIGHT_JOIN*/1)"
            )
        assert cap.query == "SELECT STRAIGHT_JOIN 1 FROM DUAL WHERE (1)"

    @override_settings(DJANGO_MYSQL_REWRITE_QUERIES=False)
    def test_instrumentation_can_be_disabled(self):
        query = "SELECT 1 FROM DUAL WHERE (/*QueryRewrite':STRAIGHT_JOIN*/1)"

        with CaptureLastQuery() as cap, connection.cursor() as cursor: