Implement JSON send/recv test case

This commit is contained in:
Kristóf Tóth 2019-04-03 11:35:14 +02:00
parent ddc304ae82
commit 1425dc1fa0

View File

@ -3,9 +3,10 @@ from os import stat, urandom
from os.path import exists, dirname, realpath, join from os.path import exists, dirname, realpath, join
from stat import S_ISFIFO from stat import S_ISFIFO
from secrets import token_urlsafe from secrets import token_urlsafe
from random import randint from random import randint, getrandbits, uniform
from threading import Thread from threading import Thread
from contextlib import contextmanager from contextlib import contextmanager
from json import dumps, loads
import pytest import pytest
@ -157,3 +158,24 @@ def test_io_newlines(io_pipes):
io_pipes.send(b'\n' * times) io_pipes.send(b'\n' * times)
for _ in range(times + 1): # IOPipes.send appends +1 for _ in range(times + 1): # IOPipes.send appends +1
assert io_pipes.recv() == b'' assert io_pipes.recv() == b''
def test_json_io(io_pipes):
for _ in range(10):
test_data = {
f'{token_urlsafe(8)}': randint(1, 2 ** 20),
f'{token_urlsafe(9)}': [randint(1, 2 **10) for i in range(10)],
f'{token_urlsafe(4)}': f'{token_urlsafe(8)}\\\n{token_urlsafe(8)}\n{randint(1, 2 ** 10)}',
f'{token_urlsafe(11)}': {
f'{token_urlsafe(8)}': '',
f'{token_urlsafe(3)}': f'{token_urlsafe(8)}\n{token_urlsafe(8)}\n\\n{token_urlsafe(8)}',
f'{token_urlsafe(44)}': f'{token_urlsafe(8)}\n{token_urlsafe(8)} {token_urlsafe(8)}',
f'{token_urlsafe(6)}\n{token_urlsafe(4)}': bool(getrandbits(1)),
f'{token_urlsafe(8)}': None,
f'{token_urlsafe(21)} {token_urlsafe(4)}': None,
f'{token_urlsafe(3)}': uniform(randint(1, 100), randint(1, 100)),
f'{token_urlsafe(8)}': [token_urlsafe(4) for i in range(10)],
}
}
io_pipes.send(dumps(test_data).encode())
assert loads(io_pipes.recv()) == test_data