Skip to content

Commit 9a72f8d

Browse files
author
Mark Florisson
committed
Start on unit tests for Cython code
1 parent e671354 commit 9a72f8d

8 files changed

Lines changed: 85 additions & 28 deletions

File tree

cassandra/bytesio.pyx

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,13 @@ cdef class BytesIOReader:
2020
string is returned when EOF is encountered immediately.
2121
"""
2222
cdef Py_ssize_t newpos = self.pos + n
23-
cdef char *res
24-
2523
if n < 0:
2624
newpos = self.size
27-
28-
if newpos > self.size:
25+
elif newpos > self.size:
2926
# Raise an error here, as we do not want the caller to consume past the
3027
# end of the buffer
3128
raise EOFError("Cannot read past the end of the file")
32-
else:
33-
res = self.buf_ptr + self.pos
3429

30+
cdef char *res = self.buf_ptr + self.pos
3531
self.pos = newpos
3632
return res
37-
38-
39-
class PyBytesIOReader(BytesIOReader):
40-
"""
41-
Python-compatible BytesIOReader class
42-
"""
43-
44-
def read(self, n = -1):
45-
"""Read at most size bytes from the file
46-
(less if the read hits EOF before obtaining size bytes).
47-
48-
If the size argument is negative or omitted, read all data until EOF
49-
is reached. The bytes are returned as a string object. An empty
50-
string is returned when EOF is encountered immediately.
51-
"""
52-
if n is None or n < 0:
53-
newpos = self.len
54-
else:
55-
newpos = min(self.pos+n, self.len)
56-
r = self.buf[self.pos:newpos]
57-
self.pos = newpos
58-
return r

cassandra/datatypes.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@ def obj_array(list objs):
6060
arr[i] = obj
6161
return arr
6262

63+

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ def run_setup(extensions):
274274
exclude_failures=True))
275275
extensions.extend(cythonize("cassandra/*.pyx",
276276
compiler_directives=directives))
277+
extensions.extend(cythonize("tests/unit/cython/*.pyx",
278+
compiler_directives=directives))
277279
except ImportError:
278280
sys.stderr.write("Cython is not installed. Not compiling core driver files as extensions (optional).")
279281

tests/unit/cython/__init__.py

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from cassandra.bytesio cimport BytesIOReader
2+
3+
def test_read1(assert_equal, assert_raises):
4+
cdef BytesIOReader reader = BytesIOReader(b'abcdef')
5+
assert_equal(reader.read(2)[:2], b'ab')
6+
assert_equal(reader.read(2)[:2], b'cd')
7+
assert_equal(reader.read(0)[:0], b'')
8+
assert_equal(reader.read(2)[:2], b'ef')
9+
10+
def test_read2(assert_equal, assert_raises):
11+
cdef BytesIOReader reader = BytesIOReader(b'abcdef')
12+
reader.read(5)
13+
reader.read(1)
14+
15+
def test_read3(assert_equal, assert_raises):
16+
cdef BytesIOReader reader = BytesIOReader(b'abcdef')
17+
reader.read(6)
18+
19+
def test_read_eof(assert_equal, assert_raises):
20+
cdef BytesIOReader reader = BytesIOReader(b'abcdef')
21+
reader.read(5)
22+
# cannot convert reader.read to an object, do it manually
23+
# assert_raises(EOFError, reader.read, 2)
24+
try:
25+
reader.read(2)
26+
except EOFError:
27+
pass
28+
else:
29+
raise Exception("Expected an EOFError")
30+
reader.read(1) # see that we can still read this

tests/unit/cython/dummy_module.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This is a dummy module used by utils.py to determine whether
2+
# cassandra was build with Cython

tests/unit/cython/test_bytesio.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from tests.unit.cython.utils import cyimport, cythontest
2+
bytesio_testhelper = cyimport('tests.unit.cython.bytesio_testhelper')
3+
4+
try:
5+
import unittest2 as unittest
6+
except ImportError:
7+
import unittest # noqa
8+
9+
10+
class BytesIOTest(unittest.TestCase):
11+
"""Test Cython BytesIO proxy"""
12+
13+
@cythontest
14+
def test_reading(self):
15+
bytesio_testhelper.test_read1(self.assertEqual, self.assertRaises)
16+
bytesio_testhelper.test_read2(self.assertEqual, self.assertRaises)
17+
bytesio_testhelper.test_read3(self.assertEqual, self.assertRaises)
18+
19+
@cythontest
20+
def test_reading_error(self):
21+
bytesio_testhelper.test_read_eof(self.assertEqual, self.assertRaises)

tests/unit/cython/utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
try:
2+
import tests.unit.cython.dummy_module
3+
except ImportError:
4+
have_cython = False
5+
else:
6+
have_cython = True
7+
8+
try:
9+
import unittest2 as unittest
10+
except ImportError:
11+
import unittest # noqa
12+
13+
def cyimport(import_path):
14+
"""
15+
Import a Cython module if available, otherwise return None
16+
(and skip any relevant tests).
17+
"""
18+
try:
19+
return __import__(import_path, fromlist=True)
20+
except ImportError:
21+
if have_cython:
22+
raise
23+
return None
24+
25+
# @cythontest
26+
# def test_something(self): ...
27+
cythontest = unittest.skipUnless(have_cython, 'Cython is not available')

0 commit comments

Comments
 (0)