Skip to content

Commit 9c92a50

Browse files
Adding a few missing code coverage tests
1 parent 061991f commit 9c92a50

5 files changed

Lines changed: 108 additions & 2 deletions

File tree

tests/unit/test_connection.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from cassandra.cluster import Cluster
1415

1516
try:
1617
import unittest2 as unittest
@@ -152,3 +153,18 @@ def test_not_implemented(self):
152153
self.assertRaises(NotImplementedError, c.close)
153154
self.assertRaises(NotImplementedError, c.register_watcher, None, None)
154155
self.assertRaises(NotImplementedError, c.register_watchers, None)
156+
157+
def test_set_keyspace_blocking(self):
158+
c = self.make_connection()
159+
160+
self.assertEqual(c.keyspace, None)
161+
c.set_keyspace_blocking(None)
162+
self.assertEqual(c.keyspace, None)
163+
164+
c.keyspace = 'ks'
165+
c.set_keyspace_blocking('ks')
166+
self.assertEqual(c.keyspace, 'ks')
167+
168+
def test_set_connection_class(self):
169+
cluster = Cluster(connection_class='test')
170+
self.assertEqual('test', cluster.connection_class)

tests/unit/test_marshalling.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from cassandra.marshal import bitlength
1415

1516
try:
1617
import unittest2 as unittest
@@ -131,3 +132,8 @@ def test_marshalling(self):
131132
self.assertEqual(type(whatwegot), type(serializedval),
132133
msg='Marshaller for %s (%s) gave wrong type (%s instead of %s)'
133134
% (valtype, marshaller, type(whatwegot), type(serializedval)))
135+
136+
def test_bitlength(self):
137+
self.assertEqual(bitlength(9), 4)
138+
self.assertEqual(bitlength(-10), 0)
139+
self.assertEqual(bitlength(0), 0)

tests/unit/test_metadata.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ def test_simple_strategy_make_token_replica_map(self):
147147
self.assertItemsEqual(rf3_replicas[MD5Token(200)], [host3, host1, host2])
148148

149149

150+
def test_ss_equals(self):
151+
self.assertNotEqual(SimpleStrategy(1), NetworkTopologyStrategy(2))
152+
150153
class TestNameEscaping(unittest.TestCase):
151154

152155
def test_protect_name(self):

tests/unit/test_policies.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ def test_non_implemented(self):
5555
self.assertRaises(NotImplementedError, policy.on_add, host)
5656
self.assertRaises(NotImplementedError, policy.on_remove, host)
5757

58+
def test_instance_check(self):
59+
try:
60+
Cluster(load_balancing_policy=RoundRobinPolicy)
61+
self.fail()
62+
except TypeError:
63+
pass
64+
5865

5966
class TestRoundRobinPolicy(unittest.TestCase):
6067

tests/unit/test_types.py

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import tempfile
1415

1516
try:
1617
import unittest2 as unittest
@@ -22,8 +23,9 @@
2223
import cassandra
2324
from cassandra.cqltypes import (BooleanType, lookup_casstype_simple, lookup_casstype,
2425
LongType, DecimalType, SetType, cql_typename,
25-
CassandraType, UTF8Type, parse_casstype_args)
26-
from cassandra.decoder import named_tuple_factory
26+
CassandraType, UTF8Type, parse_casstype_args,
27+
EmptyValue, _CassandraType, DateType)
28+
from cassandra.decoder import named_tuple_factory, write_string, read_longstring, write_stringmap, read_stringmap, read_inet, write_inet, cql_quote
2729

2830

2931
class TypeTests(unittest.TestCase):
@@ -170,3 +172,75 @@ class BarType(FooType):
170172
self.assertEquals(UTF8Type, ctype.subtypes[2])
171173

172174
self.assertEquals(['city', None, 'zip'], ctype.names)
175+
176+
def test_empty_value(self):
177+
self.assertEqual(str(EmptyValue()), 'EMPTY')
178+
179+
def test_CassandraType(self):
180+
cassandra_type = _CassandraType('randomvaluetocheck')
181+
self.assertEqual(cassandra_type.val, 'randomvaluetocheck')
182+
self.assertEqual(cassandra_type.validate('randomvaluetocheck2'), 'randomvaluetocheck2')
183+
self.assertEqual(cassandra_type.val, 'randomvaluetocheck')
184+
185+
def test_DateType(self):
186+
now = datetime.datetime.now()
187+
date_type = DateType(now)
188+
self.assertEqual(date_type.my_timestamp(), now)
189+
try:
190+
date_type.interpret_datestring('fakestring')
191+
self.fail()
192+
except ValueError:
193+
pass
194+
195+
def test_write_read_string(self):
196+
# BUG?
197+
# Traceback (most recent call last):
198+
# File "/Users/joaquin/repos/python-driver/tests/unit/test_types.py", line 199, in test_write_read_string
199+
# self.assertEqual(read_longstring(f), u'test')
200+
# AssertionError: u'st' != u'test'
201+
# - st
202+
# + test
203+
# ? ++
204+
with tempfile.TemporaryFile() as f:
205+
value = u'test'
206+
write_string(f, value)
207+
f.seek(0)
208+
self.assertEqual(read_longstring(f), value)
209+
210+
def test_write_read_stringmap(self):
211+
with tempfile.TemporaryFile() as f:
212+
value = {'key': 'value'}
213+
write_stringmap(f, value)
214+
f.seek(0)
215+
self.assertEqual(read_stringmap(f), value)
216+
217+
def test_write_read_inet(self):
218+
# BUG? I didn't think it mattered, but just wanted to confirm
219+
# Traceback (most recent call last):
220+
# File "/Users/joaquin/repos/python-driver/tests/unit/test_types.py", line 228, in test_write_read_inet
221+
# self.assertEqual(read_inet(f), value)
222+
# AssertionError: Tuples differ: ('2001:db8:0:f101::1', 9042) != ('2001:0db8:0:f101::1', 9042)
223+
#
224+
# First differing element 0:
225+
# 2001:db8:0:f101::1
226+
# 2001:0db8:0:f101::1
227+
#
228+
# - ('2001:db8:0:f101::1', 9042)
229+
# + ('2001:0db8:0:f101::1', 9042)
230+
# ? +
231+
with tempfile.TemporaryFile() as f:
232+
value = ('192.168.1.1', 9042)
233+
write_inet(f, value)
234+
f.seek(0)
235+
self.assertEqual(read_inet(f), value)
236+
237+
with tempfile.TemporaryFile() as f:
238+
value = ('2001:0db8:0:f101::1', 9042)
239+
write_inet(f, value)
240+
f.seek(0)
241+
self.assertEqual(read_inet(f), value)
242+
243+
def test_cql_quote(self):
244+
self.assertEqual(cql_quote(u'test'), "'test'")
245+
self.assertEqual(cql_quote('test'), "'test'")
246+
self.assertEqual(cql_quote(0), '0')

0 commit comments

Comments
 (0)