# Path: django_cassandra_engine/connection.py
# class CassandraConnection(object):
#     def __init__(self, alias, **options):
#         self.alias = alias
#         self.hosts = options.get("HOST").split(",")
#         self.keyspace = options.get("NAME")
#         self.user = options.get("USER")
#         self.password = options.get("PASSWORD")
#         self.options = options.get("OPTIONS", {})
#         self.cluster_options = self.options.get("connection", {})
#         self.session_options = self.options.get("session", {})
#         self.connection_options = {
#             "lazy_connect": self.cluster_options.pop("lazy_connect", False),
#             "retry_connect": self.cluster_options.pop("retry_connect", False),
#             "consistency": self.cluster_options.pop("consistency", None),
#         }
#         if self.user and self.password and "auth_provider" not in self.cluster_options:
#             self.cluster_options["auth_provider"] = PlainTextAuthProvider(
#                 username=self.user, password=self.password
#             )
# 
#         self.default = (
#             alias == "default"
#             or len(list(get_cassandra_connections())) == 1
#             or self.cluster_options.pop("default", False)
#         )
# 
#         self.register()
# 
#     def register(self):
#         try:
#             connection.get_connection(name=self.alias)
#         except CQLEngineException:
#             if self.default:
#                 from cassandra.cqlengine import models
# 
#                 models.DEFAULT_KEYSPACE = self.keyspace
# 
#             for option, value in self.session_options.items():
#                 setattr(Session, option, value)
#             if "cloud" in self.cluster_options:
#                 cluster = Cluster(**self.cluster_options)
#                 session = cluster.connect()
#                 connection.register_connection(
#                     self.alias, default=self.default, session=session
#                 )
#             else:
#                 connection.register_connection(
#                     self.alias,
#                     hosts=self.hosts,
#                     default=self.default,
#                     cluster_options=self.cluster_options,
#                     **self.connection_options
#                 )
# 
#     @property
#     def cluster(self):
#         return connection.get_cluster(connection=self.alias)
# 
#     @property
#     def session(self):
#         return connection.get_session(connection=self.alias)
# 
#     def commit(self):
#         pass
# 
#     def rollback(self):
#         pass
# 
#     def cursor(self):
#         return Cursor(self)
# 
#     def execute(self, qs, *args, **kwargs):
#         self.session.set_keyspace(self.keyspace)
#         return self.session.execute(qs, *args, **kwargs)
# 
#     def close(self):
#         """We would like to keep connection always open by default"""
# 
#     def unregister(self):
#         """Unregister this connection"""
#         connection.unregister_connection(self.alias)
# 
# Path: django_cassandra_engine/test.py
# class TestCase(DjangoTestCase):
#     databases = list(get_cassandra_db_aliases())
# 
#     def _should_reload_connections(self):
#         return False
# 
#     def _fixture_teardown(self):
#         """
#         Allow normal django TestCase fixture teardown, but also flush the test
#         database for each cassandra alias.
#         """
#         super(TestCase, self)._fixture_teardown()
# 
#         for alias, _ in get_cassandra_connections():
#             # Flush the database
#             call_command(
#                 "flush",
#                 verbosity=1,
#                 interactive=False,
#                 database=alias,
#                 skip_checks=True,
#                 reset_sequences=False,
#                 allow_cascade=False,
#                 inhibit_post_migrate=True,
#             )
# 
# Path: django_cassandra_engine/utils.py
# def get_cassandra_connection(alias=None, name=None):
#     """:return: cassandra connection matching alias or name or just first found."""
#     for _alias, connection in get_cassandra_connections():
#         if alias is not None:
#             if alias == _alias:
#                 return connection
#         elif name is not None:
#             if name == connection.settings_dict["NAME"]:
#                 return connection
#         else:
#             return connection
# 
# def get_cql_models(app, connection=None, keyspace=None):
#     """
#     :param app: django models module
#     :param connection: connection name
#     :param keyspace: keyspace
#     :return: list of all cassandra.cqlengine.Model within app that should be
#     synced to keyspace.
#     """
#     from .models import DjangoCassandraModel
# 
#     models = []
#     single_cassandra_connection = len(list(get_cassandra_connections())) == 1
#     is_default_connection = (
#         connection == DEFAULT_DB_ALIAS or single_cassandra_connection
#     )
# 
#     for name, obj in inspect.getmembers(app):
#         cql_model_types = (cqlengine.models.Model, DjangoCassandraModel)
#         if (
#             inspect.isclass(obj)
#             and issubclass(obj, cql_model_types)
#             and not obj.__abstract__
#         ):
#             if (
#                 obj.__connection__ == connection
#                 or (obj.__connection__ is None and is_default_connection)
#                 or obj.__connection__ is None
#                 and obj.__keyspace__ is not None
#                 and obj.__keyspace__ == keyspace
#             ):
#                 models.append(obj)
# 
#     return models
# 
# def get_installed_apps():
#     """Return list of all installed apps"""
#     from django.apps import apps
# 
#     return [
#         a.models_module for a in apps.get_app_configs() if a.models_module is not None
#     ]


from datetime import datetime
from django.core.management import call_command
from django.core.management.sql import sql_flush
from mock import Mock
from app.models import ExampleModel, ExampleModel2
from django_cassandra_engine.connection import CassandraConnection
from django_cassandra_engine.test import TestCase
from django_cassandra_engine.utils import (
    get_cassandra_connection,
    get_cql_models,
    get_installed_apps,
)




class DatabaseWrapperTestCase(TestCase):
    @classmethod
    def setUpClass(cls):
        super(DatabaseWrapperTestCase, cls).setUpClass()
        cls.connection = get_cassandra_connection()
        cls.all_models = []

# Path: django_cassandra_engine/connection.py
# class CassandraConnection(object):
#     def __init__(self, alias, **options):
#         self.alias = alias
#         self.hosts = options.get("HOST").split(",")
#         self.keyspace = options.get("NAME")
#         self.user = options.get("USER")
#         self.password = options.get("PASSWORD")
#         self.options = options.get("OPTIONS", {})
#         self.cluster_options = self.options.get("connection", {})
#         self.session_options = self.options.get("session", {})
#         self.connection_options = {
#             "lazy_connect": self.cluster_options.pop("lazy_connect", False),
#             "retry_connect": self.cluster_options.pop("retry_connect", False),
#             "consistency": self.cluster_options.pop("consistency", None),
#         }
#         if self.user and self.password and "auth_provider" not in self.cluster_options:
#             self.cluster_options["auth_provider"] = PlainTextAuthProvider(
#                 username=self.user, password=self.password
#             )
# 
#         self.default = (
#             alias == "default"
#             or len(list(get_cassandra_connections())) == 1
#             or self.cluster_options.pop("default", False)
#         )
# 
#         self.register()
# 
#     def register(self):
#         try:
#             connection.get_connection(name=self.alias)
#         except CQLEngineException:
#             if self.default:
#                 from cassandra.cqlengine import models
# 
#                 models.DEFAULT_KEYSPACE = self.keyspace
# 
#             for option, value in self.session_options.items():
#                 setattr(Session, option, value)
#             if "cloud" in self.cluster_options:
#                 cluster = Cluster(**self.cluster_options)
#                 session = cluster.connect()
#                 connection.register_connection(
#                     self.alias, default=self.default, session=session
#                 )
#             else:
#                 connection.register_connection(
#                     self.alias,
#                     hosts=self.hosts,
#                     default=self.default,
#                     cluster_options=self.cluster_options,
#                     **self.connection_options
#                 )
# 
#     @property
#     def cluster(self):
#         return connection.get_cluster(connection=self.alias)
# 
#     @property
#     def session(self):
#         return connection.get_session(connection=self.alias)
# 
#     def commit(self):
#         pass
# 
#     def rollback(self):
#         pass
# 
#     def cursor(self):
#         return Cursor(self)
# 
#     def execute(self, qs, *args, **kwargs):
#         self.session.set_keyspace(self.keyspace)
#         return self.session.execute(qs, *args, **kwargs)
# 
#     def close(self):
#         """We would like to keep connection always open by default"""
# 
#     def unregister(self):
#         """Unregister this connection"""
#         connection.unregister_connection(self.alias)
# 
# Path: django_cassandra_engine/test.py
# class TestCase(DjangoTestCase):
#     databases = list(get_cassandra_db_aliases())
# 
#     def _should_reload_connections(self):
#         return False
# 
#     def _fixture_teardown(self):
#         """
#         Allow normal django TestCase fixture teardown, but also flush the test
#         database for each cassandra alias.
#         """
#         super(TestCase, self)._fixture_teardown()
# 
#         for alias, _ in get_cassandra_connections():
#             # Flush the database
#             call_command(
#                 "flush",
#                 verbosity=1,
#                 interactive=False,
#                 database=alias,
#                 skip_checks=True,
#                 reset_sequences=False,
#                 allow_cascade=False,
#                 inhibit_post_migrate=True,
#             )
# 
# Path: django_cassandra_engine/utils.py
# def get_cassandra_connection(alias=None, name=None):
#     """:return: cassandra connection matching alias or name or just first found."""
#     for _alias, connection in get_cassandra_connections():
#         if alias is not None:
#             if alias == _alias:
#                 return connection
#         elif name is not None:
#             if name == connection.settings_dict["NAME"]:
#                 return connection
#         else:
#             return connection
# 
# def get_cql_models(app, connection=None, keyspace=None):
#     """
#     :param app: django models module
#     :param connection: connection name
#     :param keyspace: keyspace
#     :return: list of all cassandra.cqlengine.Model within app that should be
#     synced to keyspace.
#     """
#     from .models import DjangoCassandraModel
# 
#     models = []
#     single_cassandra_connection = len(list(get_cassandra_connections())) == 1
#     is_default_connection = (
#         connection == DEFAULT_DB_ALIAS or single_cassandra_connection
#     )
# 
#     for name, obj in inspect.getmembers(app):
#         cql_model_types = (cqlengine.models.Model, DjangoCassandraModel)
#         if (
#             inspect.isclass(obj)
#             and issubclass(obj, cql_model_types)
#             and not obj.__abstract__
#         ):
#             if (
#                 obj.__connection__ == connection
#                 or (obj.__connection__ is None and is_default_connection)
#                 or obj.__connection__ is None
#                 and obj.__keyspace__ is not None
#                 and obj.__keyspace__ == keyspace
#             ):
#                 models.append(obj)
# 
#     return models
# 
# def get_installed_apps():
#     """Return list of all installed apps"""
#     from django.apps import apps
# 
#     return [
#         a.models_module for a in apps.get_app_configs() if a.models_module is not None
#     ]


# Path: testproject/app/tests/test_databasewrapper.py
from datetime import datetime
from django.core.management import call_command
from django.core.management.sql import sql_flush
from mock import Mock
from app.models import ExampleModel, ExampleModel2
from django_cassandra_engine.connection import CassandraConnection
from django_cassandra_engine.test import TestCase
from django_cassandra_engine.utils import (
    get_cassandra_connection,
    get_cql_models,
    get_installed_apps,
)





class DatabaseWrapperTestCase(TestCase):
    @classmethod
    def setUpClass(cls):
        super(DatabaseWrapperTestCase, cls).setUpClass()
        cls.connection = get_cassandra_connection()
        cls.all_models = []

        apps = get_installed_apps()