Skip to content

Commit b8d8e59

Browse files
committed
Unit tests for Response class added
1 parent 6bd2738 commit b8d8e59

File tree

1 file changed

+92
-3
lines changed

1 file changed

+92
-3
lines changed

tests/tarantool/response.py

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@
88
import unittest
99

1010
py3 = sys.version_info.major >= 3
11+
from_hex = lambda x: binascii.unhexlify(''.join(x.split()))
12+
to_hex = lambda x: binascii.hexlify(x)
13+
1114

1215
import tarantool.response
1316

1417

1518
class field(unittest.TestCase):
19+
'''
20+
Tests for response.field class
21+
'''
1622

1723
def test__init_from_unicode(self):
18-
24+
'''
25+
Test field instantiation from str or unicode value
26+
'''
1927
# Word "Test" in cyrillic utf-8 encoded
2028
if py3:
2129
value = str(b"\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82", "utf-8")
@@ -30,7 +38,9 @@ def test__init_from_unicode(self):
3038

3139

3240
def test__init_from_bytes(self):
33-
41+
'''
42+
Test field instantiation from raw bytes value
43+
'''
3444
# Word "Test" in cyrillic utf-8 encoded
3545
value = b"\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82"
3646

@@ -42,7 +52,9 @@ def test__init_from_bytes(self):
4252

4353

4454
def test__init_from_int(self):
45-
55+
'''
56+
Test field instantiation from integer value
57+
'''
4658
self.assertEqual(
4759
tarantool.response.field(0),
4860
b"\x00\x00\x00\x00",
@@ -132,3 +144,80 @@ def test__cast_to_str(self):
132144

133145

134146

147+
class Response(unittest.TestCase):
148+
'''
149+
Tests for response.Response
150+
'''
151+
152+
def test__init_single(self):
153+
'''
154+
Test Response instance creation: unpack single record
155+
'''
156+
header = from_hex(
157+
"0d000000" # request_type = 0x0d ("insert")
158+
"1b000000" # body_length = 27
159+
"00000000" # request_id
160+
)
161+
162+
body = from_hex(
163+
"00000000" # return_code = 0
164+
"01000000" # count = 1
165+
"0b000000" # tuple_size = 11
166+
"02000000" # cardinality = 2
167+
# tuple = (1, "JKLMN")
168+
"04 01000000" + "05 4a4b4c4d4e")
169+
170+
self.assertEqual(
171+
tarantool.response.Response(header, body),
172+
[(b"\x01\x00\x00\x00", b"JKLMN")],
173+
"Create Response instance: single record"
174+
)
175+
176+
def test__init_multiple(self):
177+
'''
178+
Test Response instance creation: unpack multiple records
179+
'''
180+
header = from_hex(
181+
"11000000" # request_type = 0x11 ("select")
182+
"51000000" # body_length = 32
183+
"00000000" # request_id
184+
)
185+
body = from_hex(
186+
"00000000" # return_code = 0
187+
"03000000" # count = 3
188+
"10000000" # tuple_size = 16 (0x10)
189+
"02000000" # cardinality = 2
190+
# tuple = (1, "1111111111")
191+
"04 01000000" + "0a 31313131313131313131"
192+
"10000000" # tuple_size = 16 (0x10)
193+
"02000000" # cardinality = 2
194+
# tuple = (2, "2222222222")
195+
"04 02000000" + "0a 32323232323232323232"
196+
"11000000" # tuple_size = 17 (0x11)
197+
"04000000" # cardinality = 4
198+
# tuple = (3, "LLL", "MMM", "NNN")
199+
"04 03000000" + "03 4c4c4c" + "03 4d4d4d" + "03 4e4e4e"
200+
)
201+
202+
self.assertEqual(
203+
tarantool.response.Response(header, body),
204+
[(b"\x01\x00\x00\x00", b"1111111111"),
205+
(b"\x02\x00\x00\x00", b"2222222222"),
206+
(b"\x03\x00\x00\x00", b"LLL", b"MMM", b"NNN")],
207+
"Create Response instance - multiple records with multiple fields"
208+
)
209+
210+
def test__init_attrs(self):
211+
212+
# Check instanse attributes
213+
214+
header = from_hex("0d00000014000000 11223344")
215+
body = from_hex("00000000010000000400000002000000014b015a")
216+
r = tarantool.response.Response(header, body)
217+
218+
self.assertEqual(r.return_code, 0, "Check return_code property")
219+
self.assertIsNone(r.return_message, "Check return_message property")
220+
self.assertEqual(r.return_code, 0, "Check completion_status property")
221+
self.assertEqual(r.rowcount, 1, "Check rowcount property")
222+
self.assertEqual(r._body_length, 20, "Check _body_length attribute")
223+
self.assertEqual(r._request_id, 0x44332211, "Check _request_id attribute")

0 commit comments

Comments
 (0)