# Path: django_spanner/utils.py
# def check_django_compatability(supported_django_versions):
#     """
#     Verify that this version of django-spanner is compatible with the installed
#     version of Django. For example, django-spanner is compatible
#     with Django 2.2.y and 3.2.z
#     """
#     from . import __version__
# 
#     if django.VERSION[:2] not in supported_django_versions:
#         raise ImproperlyConfigured(
#             "You must use the latest version of django-spanner {A}.{B}.x "
#             "with Django {A}.{B}.y (found django-spanner {C}).".format(
#                 A=django.VERSION[0], B=django.VERSION[1], C=__version__
#             )
#         )
# 
# Path: django_spanner/utils.py
# def add_dummy_where(sql):
#     """
#     Cloud Spanner requires a WHERE clause on UPDATE and DELETE statements.
#     Add a dummy WHERE clause if necessary.
# 
#     :type sql: str
#     :param sql: A SQL statement.
# 
#     :rtype: str
#     :returns: A SQL statement with dummy WHERE clause.
#     """
#     if any(
#         isinstance(token, sqlparse.sql.Where)
#         for token in sqlparse.parse(sql)[0]
#     ):
#         return sql
# 
#     return sql + " WHERE 1=1"
# 
# Path: tests/unit/django_spanner/simple_test.py
# class SpannerSimpleTestClass(OpenTelemetryBase):
#     @classmethod
#     def setUpClass(cls):
#         super(SpannerSimpleTestClass, cls).setUpClass()
#         cls.PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
# 
#         cls.INSTANCE_ID = "instance_id"
#         cls.DATABASE_ID = "database_id"
#         cls.USER_AGENT = "django_spanner/2.2.0a1"
#         cls.OPTIONS = {"option": "dummy"}
# 
#         cls.settings_dict = {
#             "PROJECT": cls.PROJECT,
#             "INSTANCE": cls.INSTANCE_ID,
#             "NAME": cls.DATABASE_ID,
#             "user_agent": cls.USER_AGENT,
#             "OPTIONS": cls.OPTIONS,
#         }
#         cls.db_client = DatabaseClient(cls.settings_dict)
#         cls.db_wrapper = cls.connection = DatabaseWrapper(cls.settings_dict)
#         cls.db_operations = DatabaseOperations(cls.connection)


from django_spanner.utils import check_django_compatability
from django.core.exceptions import ImproperlyConfigured
from django_spanner.utils import add_dummy_where
from tests.unit.django_spanner.simple_test import SpannerSimpleTestClass
import django
import django_spanner


class TestUtils(SpannerSimpleTestClass):
    SQL_WITH_WHERE = "Select 1 from Table WHERE 1=1"
    SQL_WITHOUT_WHERE = "Select 1 from Table"
    # Only active LTS django versions (2.2.*, 3.2.*) are supported by this library right now.
    SUPPORTED_DJANGO_VERSIONS = [(2, 2), (3, 2)]

    def test_check_django_compatability_match(self):
        """
        Checks django compatibility match.
        """
        django_spanner.__version__ = "2.2"
        django.VERSION = (2, 2, 19, "alpha", 0)
        check_django_compatability(self.SUPPORTED_DJANGO_VERSIONS)

    def test_check_django_compatability_mismatch(self):
        """
        Checks django compatibility mismatch.
        """
        django_spanner.__version__ = "2.2"
        django.VERSION = (3, 1, 19, "alpha", 0)
        with self.assertRaises(ImproperlyConfigured):
            check_django_compatability(self.SUPPORTED_DJANGO_VERSIONS)

    def test_add_dummy_where_with_where_present_and_not_added(self):
        """
        Checks if dummy where clause is not added when present in select
        statement.
        """

# Path: django_spanner/utils.py
# def check_django_compatability(supported_django_versions):
#     """
#     Verify that this version of django-spanner is compatible with the installed
#     version of Django. For example, django-spanner is compatible
#     with Django 2.2.y and 3.2.z
#     """
#     from . import __version__
# 
#     if django.VERSION[:2] not in supported_django_versions:
#         raise ImproperlyConfigured(
#             "You must use the latest version of django-spanner {A}.{B}.x "
#             "with Django {A}.{B}.y (found django-spanner {C}).".format(
#                 A=django.VERSION[0], B=django.VERSION[1], C=__version__
#             )
#         )
# 
# Path: django_spanner/utils.py
# def add_dummy_where(sql):
#     """
#     Cloud Spanner requires a WHERE clause on UPDATE and DELETE statements.
#     Add a dummy WHERE clause if necessary.
# 
#     :type sql: str
#     :param sql: A SQL statement.
# 
#     :rtype: str
#     :returns: A SQL statement with dummy WHERE clause.
#     """
#     if any(
#         isinstance(token, sqlparse.sql.Where)
#         for token in sqlparse.parse(sql)[0]
#     ):
#         return sql
# 
#     return sql + " WHERE 1=1"
# 
# Path: tests/unit/django_spanner/simple_test.py
# class SpannerSimpleTestClass(OpenTelemetryBase):
#     @classmethod
#     def setUpClass(cls):
#         super(SpannerSimpleTestClass, cls).setUpClass()
#         cls.PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
# 
#         cls.INSTANCE_ID = "instance_id"
#         cls.DATABASE_ID = "database_id"
#         cls.USER_AGENT = "django_spanner/2.2.0a1"
#         cls.OPTIONS = {"option": "dummy"}
# 
#         cls.settings_dict = {
#             "PROJECT": cls.PROJECT,
#             "INSTANCE": cls.INSTANCE_ID,
#             "NAME": cls.DATABASE_ID,
#             "user_agent": cls.USER_AGENT,
#             "OPTIONS": cls.OPTIONS,
#         }
#         cls.db_client = DatabaseClient(cls.settings_dict)
#         cls.db_wrapper = cls.connection = DatabaseWrapper(cls.settings_dict)
#         cls.db_operations = DatabaseOperations(cls.connection)


# Path: tests/unit/django_spanner/test_utils.py
from django_spanner.utils import check_django_compatability
from django.core.exceptions import ImproperlyConfigured
from django_spanner.utils import add_dummy_where
from tests.unit.django_spanner.simple_test import SpannerSimpleTestClass
import django
import django_spanner



class TestUtils(SpannerSimpleTestClass):
    SQL_WITH_WHERE = "Select 1 from Table WHERE 1=1"
    SQL_WITHOUT_WHERE = "Select 1 from Table"
    # Only active LTS django versions (2.2.*, 3.2.*) are supported by this library right now.
    SUPPORTED_DJANGO_VERSIONS = [(2, 2), (3, 2)]

    def test_check_django_compatability_match(self):
        """
        Checks django compatibility match.
        """
        django_spanner.__version__ = "2.2"
        django.VERSION = (2, 2, 19, "alpha", 0)
        check_django_compatability(self.SUPPORTED_DJANGO_VERSIONS)

    def test_check_django_compatability_mismatch(self):
        """
        Checks django compatibility mismatch.
        """
        django_spanner.__version__ = "2.2"
        django.VERSION = (3, 1, 19, "alpha", 0)
        with self.assertRaises(ImproperlyConfigured):
            check_django_compatability(self.SUPPORTED_DJANGO_VERSIONS)

    def test_add_dummy_where_with_where_present_and_not_added(self):
        """
        Checks if dummy where clause is not added when present in select
        statement.
        """

        updated_sql = add_dummy_where(self.SQL_WITH_WHERE)