|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# pylint: disable=C0301,W0105,W0401,W0614 |
| 3 | +''' |
| 4 | +Tests for tarantool.response module |
| 5 | +''' |
| 6 | +import binascii |
| 7 | +import sys |
| 8 | +import unittest |
| 9 | + |
| 10 | +py3 = sys.version_info.major >= 3 |
| 11 | + |
| 12 | +import tarantool.response |
| 13 | + |
| 14 | + |
| 15 | +class field(unittest.TestCase): |
| 16 | + |
| 17 | + def test__init_from_unicode(self): |
| 18 | + |
| 19 | + # Word "Test" in cyrillic utf-8 encoded |
| 20 | + if py3: |
| 21 | + value = str(b"\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82", "utf-8") |
| 22 | + else: |
| 23 | + value = unicode(b"\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82", "utf-8") |
| 24 | + |
| 25 | + self.assertEqual( |
| 26 | + tarantool.response.field(value), |
| 27 | + b"\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82", |
| 28 | + "Instantiate field from unicode string" |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | + def test__init_from_bytes(self): |
| 33 | + |
| 34 | + # Word "Test" in cyrillic utf-8 encoded |
| 35 | + value = b"\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82" |
| 36 | + |
| 37 | + self.assertEqual( |
| 38 | + tarantool.response.field(value), |
| 39 | + b"\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82", |
| 40 | + "Instantiate field from bytes" |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | + def test__init_from_int(self): |
| 45 | + |
| 46 | + self.assertEqual( |
| 47 | + tarantool.response.field(0), |
| 48 | + b"\x00\x00\x00\x00", |
| 49 | + "Instantiate field from 32 bit integer value 0" |
| 50 | + ) |
| 51 | + |
| 52 | + self.assertEqual( |
| 53 | + tarantool.response.field(0x11223344), |
| 54 | + b"\x44\x33\x22\x11", |
| 55 | + "Instantiate field from 32 bit integer value 0x11223344" |
| 56 | + ) |
| 57 | + |
| 58 | + self.assertEqual( |
| 59 | + tarantool.response.field(0x7fffffff), |
| 60 | + b"\xff\xff\xff\x7f", |
| 61 | + "Instantiate field from 32 bit integer value 0x7fffffff" |
| 62 | + ) |
| 63 | + |
| 64 | + self.assertEqual( |
| 65 | + tarantool.response.field(0xffffffff), |
| 66 | + b"\xff\xff\xff\xff", |
| 67 | + "Instantiate field from 32 bit integer value 0xffffffff" |
| 68 | + ) |
| 69 | + |
| 70 | + self.assertEqual( |
| 71 | + tarantool.response.field(0xffffffffffffffff), |
| 72 | + b"\xff\xff\xff\xff\xff\xff\xff\xff", |
| 73 | + "Instantiate field from 64 bit integer value 0xffffffffffffffff" |
| 74 | + ) |
| 75 | + |
| 76 | + self.assertEqual( |
| 77 | + tarantool.response.field(0x0100000000000000), |
| 78 | + b"\x00\x00\x00\x00\x00\x00\x00\x01", |
| 79 | + "Instantiate field from 64 bit integer value 0x0100000000000000" |
| 80 | + ) |
| 81 | + |
| 82 | + self.assertEqual( |
| 83 | + tarantool.response.field(0x1122334455667788), |
| 84 | + b"\x88\x77\x66\x55\x44\x33\x22\x11", |
| 85 | + "Instantiate field from 64 bit integer value 0x1122334455667788" |
| 86 | + ) |
| 87 | + |
| 88 | + # Out of range |
| 89 | + with self.assertRaises(ValueError): |
| 90 | + tarantool.response.field(0xffffffffffffffff+1) |
| 91 | + tarantool.response.field(-1) |
| 92 | + |
| 93 | + # Unsupported argument type |
| 94 | + with self.assertRaises(TypeError): |
| 95 | + tarantool.response.field(None) |
| 96 | + tarantool.response.field([1,2,3]) |
| 97 | + |
| 98 | + |
| 99 | + def test__cast_to_int(self): |
| 100 | + ''' |
| 101 | + Test type casting from field to int |
| 102 | + ''' |
| 103 | + for i in (0, 0x11, 0x1122, 0x112233, 0xffffffff, 0xffffffffffffffff): |
| 104 | + f = tarantool.response.field(i) |
| 105 | + self.assertEqual( |
| 106 | + int(f), |
| 107 | + i, |
| 108 | + "Cast field instance to int, value = %d"%i |
| 109 | + ) |
| 110 | + |
| 111 | + # Can't cast string value to int |
| 112 | + f = tarantool.response.field(b"not an int value") |
| 113 | + with self.assertRaises(ValueError): |
| 114 | + int(f) |
| 115 | + |
| 116 | + |
| 117 | + def test__cast_to_str(self): |
| 118 | + ''' |
| 119 | + Test type casting from field to str or unicode |
| 120 | + ''' |
| 121 | + # Word "Test" in cyrillic utf-8 encoded |
| 122 | + if py3: |
| 123 | + self.assertEqual( |
| 124 | + str(tarantool.response.field(b"\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82")), |
| 125 | + str(b"\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82", "utf-8"), |
| 126 | + "Cast field instance to unicode") |
| 127 | + else: |
| 128 | + self.assertEqual( |
| 129 | + unicode(tarantool.response.field(b"\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82")), |
| 130 | + unicode(b"\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82", "utf-8"), |
| 131 | + "Cast field instance to unicode") |
| 132 | + |
| 133 | + |
| 134 | + |
0 commit comments