Skip to content

Commit 3c3f0e1

Browse files
committed
Some unit tests added
1 parent 0a38120 commit 3c3f0e1

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

tests/__init__.py

Whitespace-only changes.

tests/setup_command.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# -*- coding: utf-8 -*-
2+
import setuptools
3+
from glob import glob
4+
import os.path
5+
import sys
6+
import unittest
7+
8+
class test(setuptools.Command):
9+
user_options = []
10+
description = 'Run unit tests'
11+
user_options = [
12+
('verbosity=', 'v', 'Output verbosity')
13+
]
14+
15+
def initialize_options(self):
16+
self.verbosity = 1
17+
18+
def finalize_options(self):
19+
try:
20+
self.verbosity = int(self.verbosity)
21+
except ValueError:
22+
self.verbosity = 1
23+
24+
def run(self):
25+
'''
26+
Find all tests in test/tarantool/ and run them
27+
'''
28+
root = os.path.dirname(os.path.dirname(__file__))
29+
sys.path.insert(0, root)
30+
sys.path.insert(1, os.path.join(root, "src"))
31+
32+
testfiles = []
33+
for filename in glob(os.path.join(root, "tests", "tarantool", "*.py")):
34+
if filename.endswith('__init__.py'): continue
35+
testfiles.append("tests.tarantool." + os.path.splitext(os.path.basename(filename))[0])
36+
37+
tests = unittest.defaultTestLoader.loadTestsFromNames(testfiles)
38+
test_runner = unittest.TextTestRunner(verbosity = self.verbosity)
39+
test_runner.run(tests)

tests/tarantool/__init__.py

Whitespace-only changes.

tests/tarantool/request.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# -*- coding: utf-8 -*-
2+
# pylint: disable=C0301,W0105,W0401,W0614
3+
'''
4+
Tests for tarantool.request module
5+
'''
6+
7+
import binascii
8+
import unittest
9+
10+
11+
import tarantool.request
12+
13+
14+
class RequestInsert(unittest.TestCase):
15+
16+
def test__cast_to_bytes(self):
17+
'''
18+
Test binary INSERT request representation
19+
'''
20+
self.assertEqual(
21+
bytes(tarantool.request.RequestInsert(1, (1, 2000, 30000), False)),
22+
binascii.unhexlify("0d0000001b00000000000000010000000000000003000000040100000004d00700000430750000")
23+
)
24+
25+
self.assertEqual(
26+
bytes(tarantool.request.RequestInsert(1, (b"AAA", b"BBBB", b"CCCCCC"), False)),
27+
binascii.unhexlify("0d0000001c0000000000000001000000000000000300000003414141044242424206434343434343")
28+
)
29+
30+
31+
class RequestDelete(unittest.TestCase):
32+
33+
def test__cast_to_bytes(self):
34+
'''
35+
Test binary DELETE request representation
36+
'''
37+
self.assertEqual(
38+
bytes(tarantool.request.RequestDelete(1, 1, False)),
39+
binascii.unhexlify("1500000011000000000000000100000000000000010000000401000000")
40+
)
41+
42+
self.assertEqual(
43+
bytes(tarantool.request.RequestDelete(1, b"AAA", False)),
44+
binascii.unhexlify("15000000100000000000000001000000000000000100000003414141")
45+
)
46+
47+
# Raises a TypeError exception because the primary key must be a scalar value (int or str)
48+
with self.assertRaises(TypeError):
49+
tarantool.request.RequestDelete(1, [1,2], False)
50+
51+
52+
class RequestSelect(unittest.TestCase):
53+
54+
def test__cast_to_bytes(self):
55+
'''
56+
Test binary SELECT request representation
57+
'''
58+
# select * from t1 where k0 = 1
59+
self.assertEqual(
60+
bytes(tarantool.request.RequestSelect(1, 0, [(1,)], 0, 0xffff)),
61+
binascii.unhexlify("110000001d00000000000000010000000000000000000000ffff000001000000010000000401000000")
62+
)
63+
64+
# select * from t1 where k0 = "AAA"
65+
self.assertEqual(
66+
bytes(tarantool.request.RequestSelect(1, 0, [(b"AAA",)], 0, 0xffff)),
67+
binascii.unhexlify("110000001c00000000000000010000000000000000000000ffff0000010000000100000003414141")
68+
)
69+
70+
# select * from t1 where k0 in (1, 2, 3)
71+
self.assertEqual(
72+
bytes(tarantool.request.RequestSelect(1, 0, [(1,), (2,), (3,)], 0, 0xffff)),
73+
binascii.unhexlify("110000002f00000000000000010000000000000000000000ffff000003000000010000000401000000010000000402000000010000000403000000")
74+
)

0 commit comments

Comments
 (0)