Skip to content

Commit 7e1ed3d

Browse files
committed
test: add unit tests
Tests written using unittest [1] and hypothesis [2] modules. We use hypothesis v4.57.0 because it is a latest version with Python 2.7 support. How-to run: $ make test_unittest Unit test that pass tarantoolctl output to JSON serializer failed [4] with Python 3.5 because it expects argument with type str while we pass bytes. Later Python versions (since 3.6, see [5]) starts to accept bytes and bytearray too. To fix test for Python 3.5 added explicit conversion of bytes to str before JSON serialization. ====================================================================== ERROR: test_extract_schema_from_snapshot (__main__.TestUtils) ---------------------------------------------------------------------- Traceback (most recent call last): File "test/unittest/test_lib_utils.py", line 14, in test_extract_schema_from_snapshot v = utils.extract_schema_from_snapshot(snapshot_path) File "/home/runner/work/test-run/test-run/lib/utils.py", line 309, in extract_schema_from_snapshot for row in xlog_rows(snapshot_path): File "/home/runner/work/test-run/test-run/lib/utils.py", line 290, in xlog_rows yield json.loads(line) File "/opt/hostedtoolcache/Python/3.5.10/x64/lib/python3.5/json/__init__.py", line 312, in loads s.__class__.__name__)) TypeError: the JSON object must be str, not 'bytes' 1. https://docs.python.org/3/library/unittest.html 2. https://hypothesis.readthedocs.io/en/latest/ 3. https://hypothesis.readthedocs.io/en/latest/changes.html#v4-57-0 4. https://github.com/tarantool/test-run/runs/2199956452 5. https://docs.python.org/3/library/json.html#json.loads
1 parent 6e3ecd5 commit 7e1ed3d

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ htmlcov/
4141
.coverage
4242
.coverage.*
4343
.cache
44+
.hypothesis/
4445
nosetests.xml
4546
coverage.xml
4647
*,cover

lib/utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ def xlog_rows(xlog_path):
286286
cmd = ['tarantoolctl', 'cat', xlog_path, '--format=json', '--show-system']
287287
with open(os.devnull, 'w') as devnull:
288288
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=devnull)
289-
for line in process.stdout.readlines():
290-
yield json.loads(line)
289+
for line in process.stdout.readlines():
290+
yield json.loads(bytes_to_str(line))
291291

292292

293293
def extract_schema_from_snapshot(snapshot_path):

requirements-test.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
flake8==3.7.9
2+
hypothesis==4.*
5.93 KB
Binary file not shown.

test/unittest/test_lib_utils.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import unittest
2+
3+
import socket
4+
5+
from hypothesis import given, settings
6+
from hypothesis.strategies import integers
7+
8+
import lib.utils as utils
9+
10+
11+
class TestUtils(unittest.TestCase):
12+
def test_extract_schema_from_snapshot(self):
13+
snapshot_path = '00000000000000000003.snap'
14+
v = utils.extract_schema_from_snapshot(snapshot_path)
15+
self.assertEqual(v, (2, 3, 1))
16+
17+
@settings(max_examples=5)
18+
@given(port=integers(65000, 65535))
19+
def test_check_port(self, port):
20+
def open_socket(p):
21+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
22+
s.bind(('localhost', p))
23+
s.listen(0)
24+
return s
25+
status = utils.check_port(port, ipv4=True, ipv6=False)
26+
self.assertEqual(status, True)
27+
s = open_socket(port)
28+
status = utils.check_port(port, ipv4=True, ipv6=False)
29+
s.close()
30+
self.assertEqual(status, False)
31+
32+
33+
if __name__ == "__main__":
34+
unittest.main()

0 commit comments

Comments
 (0)