Skip to content

Commit 99dea50

Browse files
author
Mark Florisson
committed
Add cython protocol integration test
1 parent 27e3505 commit 99dea50

3 files changed

Lines changed: 82 additions & 40 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*.so
55
*.egg
66
*.egg-info
7+
*.attr
78
.tox
89
.python-version
910
build
@@ -19,6 +20,7 @@ setuptools*.egg
1920

2021
cassandra/*.c
2122
!cassandra/murmur3.c
23+
cassandra/*.html
2224

2325
# OSX
2426
.DS_Store
@@ -38,3 +40,4 @@ cassandra/*.c
3840

3941
#iPython
4042
*.ipynb
43+

tests/integration/standard/test_custom_protocol_handler.py

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
from cassandra.query import tuple_factory
2222
from cassandra.cluster import Cluster
2323
from tests.integration import use_singledc, PROTOCOL_VERSION, execute_until_pass
24-
from tests.integration.datatype_utils import update_datatypes, PRIMITIVE_DATATYPES, get_sample
24+
from tests.integration.datatype_utils import update_datatypes, PRIMITIVE_DATATYPES
25+
from tests.integration.standard.utils import create_table_with_all_types, get_all_primitive_params
2526
from six import binary_type
2627

2728
import uuid
@@ -106,55 +107,18 @@ def test_custom_raw_row_results_all_types(self):
106107
session.client_protocol_handler = CustomProtocolHandlerResultMessageTracked
107108
session.row_factory = tuple_factory
108109

109-
columns_string = create_table_with_all_types("test_table", session)
110+
columns_string = create_table_with_all_types("alltypes", session)
110111

111112
# verify data
112113
params = get_all_primitive_params()
113-
results = session.execute("SELECT {0} FROM alltypes WHERE zz=0".format(columns_string))[0]
114+
results = session.execute("SELECT {0} FROM alltypes WHERE pimkey=0".format(columns_string))[0]
114115
for expected, actual in zip(params, results):
115116
self.assertEqual(actual, expected)
116117
# Ensure we have covered the various primitive types
117118
self.assertEqual(len(CustomResultMessageTracked.checked_rev_row_set), len(PRIMITIVE_DATATYPES)-1)
118119
session.shutdown()
119120

120121

121-
def create_table_with_all_types(table_name, session):
122-
"""
123-
Method that given a table_name and session construct a table that contains all possible primitive types
124-
:param table_name: Name of table to create
125-
:param session: session to use for table creation
126-
:return: a string containing and columns. This can be used to query the table.
127-
"""
128-
# create table
129-
alpha_type_list = ["zz int PRIMARY KEY"]
130-
col_names = ["zz"]
131-
start_index = ord('a')
132-
for i, datatype in enumerate(PRIMITIVE_DATATYPES):
133-
alpha_type_list.append("{0} {1}".format(chr(start_index + i), datatype))
134-
col_names.append(chr(start_index + i))
135-
136-
session.execute("CREATE TABLE alltypes ({0})".format(', '.join(alpha_type_list)), timeout=120)
137-
138-
# create the input
139-
params = get_all_primitive_params()
140-
141-
# insert into table as a simple statement
142-
columns_string = ', '.join(col_names)
143-
placeholders = ', '.join(["%s"] * len(col_names))
144-
session.execute("INSERT INTO alltypes ({0}) VALUES ({1})".format(columns_string, placeholders), params, timeout=120)
145-
return columns_string
146-
147-
148-
def get_all_primitive_params():
149-
"""
150-
Simple utility method used to give back a list of all possible primitive data sample types.
151-
"""
152-
params = [0]
153-
for datatype in PRIMITIVE_DATATYPES:
154-
params.append((get_sample(datatype)))
155-
return params
156-
157-
158122
class CustomResultMessageRaw(ResultMessage):
159123
"""
160124
This is a custom Result Message that is used to return raw results, rather then
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""Test the various Cython-based message deserializers"""
2+
3+
# Based on test_custom_protocol_handler.py
4+
5+
try:
6+
import unittest2 as unittest
7+
except ImportError:
8+
import unittest
9+
10+
from cassandra.cluster import Cluster
11+
from tests.integration import use_singledc, PROTOCOL_VERSION
12+
from tests.integration.datatype_utils import update_datatypes
13+
from tests.integration.standard.utils import create_table_with_all_types, get_all_primitive_params
14+
from six import next
15+
16+
try:
17+
from cassandra.cython_protocol_handler import make_protocol_handler
18+
except ImportError as e:
19+
raise unittest.skip("Skipping test, not compiled with Cython enabled")
20+
21+
from cassandra.numpyparser import NumpyParser
22+
from cassandra.objparser import ListParser, LazyParser
23+
24+
25+
def setup_module():
26+
use_singledc()
27+
update_datatypes()
28+
29+
30+
class CustomProtocolHandlerTest(unittest.TestCase):
31+
32+
@classmethod
33+
def setUpClass(cls):
34+
cls.cluster = Cluster(protocol_version=PROTOCOL_VERSION)
35+
cls.session = cls.cluster.connect()
36+
cls.session.execute("CREATE KEYSPACE testspace WITH replication = "
37+
"{ 'class' : 'SimpleStrategy', 'replication_factor': '1'}")
38+
39+
@classmethod
40+
def tearDownClass(cls):
41+
cls.session.execute("DROP KEYSPACE testspace")
42+
cls.cluster.shutdown()
43+
44+
def test_cython_parser(self):
45+
"""
46+
Test Cython-based parser that returns a list of tuples
47+
"""
48+
self.cython_parser(ListParser())
49+
50+
def test_cython_lazy_parser(self):
51+
"""
52+
Test Cython-based parser that returns a list of tuples
53+
"""
54+
self.cython_parser(LazyParser())
55+
56+
def cython_parser(self, colparser):
57+
session = Cluster().connect()
58+
session.set_keyspace("smallspace")
59+
60+
# use our custom protocol handler
61+
session.client_protocol_handler = make_protocol_handler(colparser)
62+
# session.row_factory = tuple_factory
63+
create_table_with_all_types("test_table", session)
64+
65+
# verify data
66+
params = get_all_primitive_params()
67+
[first_result] = session.execute("SELECT * FROM test_table WHERE primkey=0")
68+
self.assertEqual(len(params), len(first_result),
69+
msg="Not the right number of columns?")
70+
print(first_result)
71+
assert False
72+
for expected, actual in zip(params, first_result):
73+
self.assertEqual(actual, expected)
74+
75+
session.shutdown()

0 commit comments

Comments
 (0)