# 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)
# 
# class Cursor(object):
#     def __init__(self, connection):
#         self.connection = connection
# 
#     def execute(self, *args, **kwargs):
#         return self.connection.execute(*args, **kwargs)
# 
#     def close(self):
#         pass
# 
#     def fetchmany(self, _):
#         return []
# 
#     def __enter__(self):
#         return self
# 
#     def __exit__(self, type, value, traceback):
#         self.close()
# 
# 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


from copy import copy
from unittest import TestCase
from cassandra import ConsistencyLevel
from cassandra.auth import PlainTextAuthProvider
from cassandra.cluster import Cluster
from cassandra.cqlengine import CQLEngineException
from mock import Mock, patch
from django_cassandra_engine.connection import CassandraConnection, Cursor
from django_cassandra_engine.utils import get_cassandra_connection
        from cassandra.cqlengine import connection as cql_connection
        from cassandra.cqlengine import connection as cql_connection
        from cassandra.cqlengine import connection as cql_connection
import mock
            self.connection.session,
            cql_connection._connections[self.connection.alias].session,
        )

    def test_cluster_property(self):


        self.assertEqual(
            self.connection.cluster,
            cql_connection._connections[self.connection.alias].cluster,
        )

    def test_connection_options(self):
        connection_options = self.cassandra_connection.settings_dict["OPTIONS"][
            "connection"
        ]

        opts = {
            "lazy_connect": connection_options.pop("lazy_connect", False),
            "retry_connect": connection_options.pop("retry_connect", False),
            "consistency": connection_options.pop("consistency", None),
        }
        self.assertEqual(self.connection.connection_options, opts)

    @patch("django_cassandra_engine.connection.connection")
    def test_register_connection_called_first_time_with_proper_options(
        self, connection_mock
    ):

        settings = self.cassandra_connection.settings_dict

# 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)
# 
# class Cursor(object):
#     def __init__(self, connection):
#         self.connection = connection
# 
#     def execute(self, *args, **kwargs):
#         return self.connection.execute(*args, **kwargs)
# 
#     def close(self):
#         pass
# 
#     def fetchmany(self, _):
#         return []
# 
#     def __enter__(self):
#         return self
# 
#     def __exit__(self, type, value, traceback):
#         self.close()
# 
# 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


# Path: testproject/app/tests/test_connection.py
from copy import copy
from unittest import TestCase
from cassandra import ConsistencyLevel
from cassandra.auth import PlainTextAuthProvider
from cassandra.cluster import Cluster
from cassandra.cqlengine import CQLEngineException
from mock import Mock, patch
from django_cassandra_engine.connection import CassandraConnection, Cursor
from django_cassandra_engine.utils import get_cassandra_connection
        from cassandra.cqlengine import connection as cql_connection
        from cassandra.cqlengine import connection as cql_connection
        from cassandra.cqlengine import connection as cql_connection
import mock

            self.connection.session,
            cql_connection._connections[self.connection.alias].session,
        )

    def test_cluster_property(self):


        self.assertEqual(
            self.connection.cluster,
            cql_connection._connections[self.connection.alias].cluster,
        )

    def test_connection_options(self):
        connection_options = self.cassandra_connection.settings_dict["OPTIONS"][
            "connection"
        ]

        opts = {
            "lazy_connect": connection_options.pop("lazy_connect", False),
            "retry_connect": connection_options.pop("retry_connect", False),
            "consistency": connection_options.pop("consistency", None),
        }
        self.assertEqual(self.connection.connection_options, opts)

    @patch("django_cassandra_engine.connection.connection")
    def test_register_connection_called_first_time_with_proper_options(
        self, connection_mock
    ):

        settings = self.cassandra_connection.settings_dict

        connection = CassandraConnection("default", **settings)