From 1425dc1fa00c3f25fa2957b4849d6cce1e63efbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 3 Apr 2019 11:35:14 +0200 Subject: [PATCH] Implement JSON send/recv test case --- tests.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tests.py b/tests.py index f4eea50..518adcf 100644 --- a/tests.py +++ b/tests.py @@ -3,9 +3,10 @@ from os import stat, urandom from os.path import exists, dirname, realpath, join from stat import S_ISFIFO from secrets import token_urlsafe -from random import randint +from random import randint, getrandbits, uniform from threading import Thread from contextlib import contextmanager +from json import dumps, loads import pytest @@ -157,3 +158,24 @@ def test_io_newlines(io_pipes): io_pipes.send(b'\n' * times) for _ in range(times + 1): # IOPipes.send appends +1 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