Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
r4fek committed Feb 5, 2016
1 parent 75c774f commit ab545ff
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
38 changes: 28 additions & 10 deletions testproject/app/tests/test_connection.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from copy import deepcopy

from cassandra import ConsistencyLevel
from cassandra.auth import PlainTextAuthProvider
from cassandra.cluster import Cluster
from mock import patch
from mock import patch, Mock

from django.test import TestCase
from unittest import TestCase

from django_cassandra_engine.connection import CassandraConnection, Cursor
from django_cassandra_engine.utils import get_cassandra_connection
Expand All @@ -13,8 +15,8 @@ class CassandraConnectionTestCase(TestCase):

def setUp(self):
self.cassandra_connection = get_cassandra_connection()
self.connection = CassandraConnection(
**self.cassandra_connection.settings_dict)
self.connection = self.cassandra_connection.connection
self.cassandra_connection.connect()

def test_cursor(self):

Expand Down Expand Up @@ -46,28 +48,42 @@ def test_connection_options(self):
self.assertEqual(
self.connection.connection_options, connection_options)

@patch("cassandra.cqlengine.connection")
@patch("django_cassandra_engine.connection.connection")
def test_connection_setup_called_first_time_with_proper_options(
self, connection_mock):

settings = self.cassandra_connection.settings_dict
connection_mock.cluster = None
connection_mock.get_session.return_value = None
connection = CassandraConnection(**settings)

connection_mock.setup.assert_called_once_with(
connection.hosts, connection.keyspace,
**settings['OPTIONS']['connection'])

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

settings = self.cassandra_connection.settings_dict
connection_mock.cluster = Cluster()
session_mock = Mock()
session_mock.is_shutdown = False
connection_mock.get_session.return_value = session_mock
CassandraConnection(**settings)

self.assertFalse(connection_mock.setup.called)

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

settings = self.cassandra_connection.settings_dict
session_mock = Mock()
session_mock.is_shutdown = True
connection_mock.get_session.return_value = session_mock
CassandraConnection(**settings)

self.assertTrue(connection_mock.setup.called)

def test_connection_auth_provider_added_to_connection_options(self):

settings = self.cassandra_connection.settings_dict
Expand All @@ -78,9 +94,10 @@ def test_connection_auth_provider_added_to_connection_options(self):
self.assertIsInstance(connection.connection_options['auth_provider'],
PlainTextAuthProvider)

def test_connection_auth_provider_not_changed(self):
@patch('django_cassandra_engine.connection.CassandraConnection.setup')
def test_connection_auth_provider_not_changed(self, setup_mock):

settings = self.cassandra_connection.settings_dict
settings = deepcopy(self.cassandra_connection.settings_dict)
settings['USER'] = 'user'
settings['PASSWORD'] = 'pass'
settings['OPTIONS']['connection'] = {}
Expand All @@ -89,6 +106,7 @@ def test_connection_auth_provider_not_changed(self):

self.assertEqual(connection.connection_options['auth_provider'],
settings['OPTIONS']['connection']['auth_provider'])
setup_mock.assert_called_once()

def test_connection_session_options_default_timeout(self):

Expand Down
1 change: 0 additions & 1 deletion testproject/settings/default_cassandra.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
'replication_factor': 1,
},
'connection': {
'lazy_connect': True,
'retry_connect': True,
'consistency': ConsistencyLevel.ALL
},
Expand Down
1 change: 0 additions & 1 deletion testproject/settings/default_only_cassandra.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
'replication_factor': 1,
},
'connection': {
'lazy_connect': True,
'retry_connect': True,
'consistency': ConsistencyLevel.ALL
},
Expand Down
2 changes: 0 additions & 2 deletions testproject/settings/multi_cassandra.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
'replication_factor': 1,
},
'connection': {
'lazy_connect': True,
'retry_connect': True,
'consistency': ConsistencyLevel.ALL
},
Expand All @@ -41,7 +40,6 @@
'replication_factor': 1,
},
'connection': {
'lazy_connect': True,
'retry_connect': True,
'consistency': ConsistencyLevel.ALL
},
Expand Down
1 change: 0 additions & 1 deletion testproject/settings/secondary_cassandra.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
'replication_factor': 1,
},
'connection': {
'lazy_connect': True,
'retry_connect': True,
'consistency': ConsistencyLevel.ALL
},
Expand Down

0 comments on commit ab545ff

Please sign in to comment.