From 512247163c9ff8ff1b5a8f1eb0ef3f56cfa03982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Sun, 16 Dec 2018 23:26:51 +0100 Subject: [PATCH] Refactor tests --- tests.py | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/tests.py b/tests.py index 7bec774..a0af73f 100644 --- a/tests.py +++ b/tests.py @@ -45,8 +45,21 @@ def test_pipes_isfifo(pipe_io): ] ) def test_io(pipe_io, test_data): - File(pipe_io.in_pipe, string=True).write(test_data) - assert File(pipe_io.out_pipe, string=True).read() == test_data + File(pipe_io.in_pipe).write(test_data.encode()) + assert File(pipe_io.out_pipe).read().decode() == test_data + + +class File: + def __init__(self, path): + self.path = path + + def write(self, what): + with open(self.path, 'wb') as ofile: + ofile.write(what) + + def read(self): + with open(self.path, 'rb') as ifile: + return ifile.read() @pytest.mark.parametrize( @@ -61,33 +74,19 @@ def test_io(pipe_io, test_data): ] ) def test_io_large_data(pipe_io, test_data_size): - random_data = urandom(test_data_size) - File(pipe_io.in_pipe).write(random_data) - assert File(pipe_io.out_pipe).read() == random_data + test_data = urandom(test_data_size) + File(pipe_io.in_pipe).write(test_data) + assert File(pipe_io.out_pipe).read() == test_data def test_io_stress(pipe_io): for _ in range(2222): - random_data = urandom(randint(1, 1024)) - File(pipe_io.in_pipe).write(random_data) - assert File(pipe_io.out_pipe).read() == random_data + test_data = urandom(randint(1, 1024)) + File(pipe_io.in_pipe).write(test_data) + assert File(pipe_io.out_pipe).read() == test_data def test_stop_removes_pipes(pipe_io): pipe_io.stop() for path in (pipe_io.in_pipe, pipe_io.out_pipe): assert not exists(path) - - -class File: - def __init__(self, path, string=False): - self.path = path - self._filemode = '' if string else 'b' - - def write(self, what): - with open(self.path, f'w{self._filemode}') as ofile: - ofile.write(what) - - def read(self): - with open(self.path, f'r{self._filemode}') as ifile: - return ifile.read()