Skip to content

Commit 88371d1

Browse files
OsOperations::read_binary(self, filename, start_pos) is added
It is a specialized function to read binary data from files.
1 parent 85d2aa3 commit 88371d1

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

testgres/operations/local_ops.py

+11
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,17 @@ def readlines(self, filename, num_lines=0, binary=False, encoding=None):
308308
buffers * max(2, int(num_lines / max(cur_lines, 1)))
309309
) # Adjust buffer size
310310

311+
def read_binary(self, filename, start_pos):
312+
assert type(filename) == str # noqa: E721
313+
assert type(start_pos) == int # noqa: E721
314+
assert start_pos >= 0
315+
316+
with open(filename, 'rb') as file: # open in a binary mode
317+
file.seek(start_pos, os.SEEK_SET)
318+
r = file.read()
319+
assert type(r) == bytes # noqa: E721
320+
return r
321+
311322
def isfile(self, remote_file):
312323
return os.path.isfile(remote_file)
313324

testgres/operations/os_ops.py

+6
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ def read(self, filename, encoding, binary):
9898
def readlines(self, filename):
9999
raise NotImplementedError()
100100

101+
def read_binary(self, filename, start_pos):
102+
assert type(filename) == str # noqa: E721
103+
assert type(start_pos) == int # noqa: E721
104+
assert start_pos >= 0
105+
raise NotImplementedError()
106+
101107
def isfile(self, remote_file):
102108
raise NotImplementedError()
103109

testgres/operations/remote_ops.py

+19
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,16 @@ def readlines(self, filename, num_lines=0, binary=False, encoding=None):
340340

341341
return lines
342342

343+
def read_binary(self, filename, start_pos):
344+
assert type(filename) == str # noqa: E721
345+
assert type(start_pos) == int # noqa: E721
346+
assert start_pos >= 0
347+
348+
cmd = "tail -c +{} {}".format(start_pos + 1, __class__._escape_path(filename))
349+
r = self.exec_command(cmd)
350+
assert type(r) == bytes # noqa: E721
351+
return r
352+
343353
def isfile(self, remote_file):
344354
stdout = self.exec_command("test -f {}; echo $?".format(remote_file))
345355
result = int(stdout.strip())
@@ -386,6 +396,15 @@ def db_connect(self, dbname, user, password=None, host="localhost", port=5432):
386396
)
387397
return conn
388398

399+
def _escape_path(path):
400+
assert type(path) == str # noqa: E721
401+
assert path != "" # Ok?
402+
403+
r = "'"
404+
r += path
405+
r += "'"
406+
return r
407+
389408

390409
def normalize_error(error):
391410
if isinstance(error, bytes):

tests/test_local.py

+45
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import os
2+
13
import pytest
4+
import re
25

36
from testgres import ExecUtilException
47
from testgres import LocalOperations
@@ -52,3 +55,45 @@ def test_exec_command_failure__expect_error(self):
5255
assert error == b'/bin/sh: 1: nonexistent_command: not found\n'
5356
assert exit_status == 127
5457
assert result == b''
58+
59+
def test_read_binary__spec(self):
60+
"""
61+
Test LocalOperations::read_binary.
62+
"""
63+
filename = __file__ # current file
64+
65+
with open(filename, 'rb') as file: # open in a binary mode
66+
response0 = file.read()
67+
68+
assert type(response0) == bytes # noqa: E721
69+
70+
response1 = self.operations.read_binary(filename, 0)
71+
assert type(response1) == bytes # noqa: E721
72+
assert response1 == response0
73+
74+
response2 = self.operations.read_binary(filename, 1)
75+
assert type(response2) == bytes # noqa: E721
76+
assert len(response2) < len(response1)
77+
assert len(response2) + 1 == len(response1)
78+
assert response2 == response1[1:]
79+
80+
response3 = self.operations.read_binary(filename, len(response1))
81+
assert type(response3) == bytes # noqa: E721
82+
assert len(response3) == 0
83+
84+
response4 = self.operations.read_binary(filename, len(response2))
85+
assert type(response4) == bytes # noqa: E721
86+
assert len(response4) == 1
87+
assert response4[0] == response1[len(response1) - 1]
88+
89+
response5 = self.operations.read_binary(filename, len(response1) + 1)
90+
assert type(response5) == bytes # noqa: E721
91+
assert len(response5) == 0
92+
93+
def test_read_binary__spec__unk_file(self):
94+
"""
95+
Test LocalOperations::read_binary with unknown file.
96+
"""
97+
98+
with pytest.raises(FileNotFoundError, match=re.escape("[Errno 2] No such file or directory: '/dummy'")):
99+
self.operations.read_binary("/dummy", 0)

tests/test_remote.py

+43
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
import pytest
4+
import re
45

56
from testgres import ExecUtilException
67
from testgres import RemoteOperations
@@ -181,6 +182,48 @@ def test_read_binary_file(self):
181182

182183
assert isinstance(response, bytes)
183184

185+
def test_read_binary__spec(self):
186+
"""
187+
Test RemoteOperations::read_binary.
188+
"""
189+
filename = __file__ # currnt file
190+
191+
with open(filename, 'rb') as file: # open in a binary mode
192+
response0 = file.read()
193+
194+
assert type(response0) == bytes # noqa: E721
195+
196+
response1 = self.operations.read_binary(filename, 0)
197+
assert type(response1) == bytes # noqa: E721
198+
assert response1 == response0
199+
200+
response2 = self.operations.read_binary(filename, 1)
201+
assert type(response2) == bytes # noqa: E721
202+
assert len(response2) < len(response1)
203+
assert len(response2) + 1 == len(response1)
204+
assert response2 == response1[1:]
205+
206+
response3 = self.operations.read_binary(filename, len(response1))
207+
assert type(response3) == bytes # noqa: E721
208+
assert len(response3) == 0
209+
210+
response4 = self.operations.read_binary(filename, len(response2))
211+
assert type(response4) == bytes # noqa: E721
212+
assert len(response4) == 1
213+
assert response4[0] == response1[len(response1) - 1]
214+
215+
response5 = self.operations.read_binary(filename, len(response1) + 1)
216+
assert type(response5) == bytes # noqa: E721
217+
assert len(response5) == 0
218+
219+
def test_read_binary__spec__unk_file(self):
220+
"""
221+
Test RemoteOperations::read_binary with unknown file.
222+
"""
223+
224+
with pytest.raises(ExecUtilException, match=re.escape("tail: cannot open '/dummy' for reading: No such file or directory")):
225+
self.operations.read_binary("/dummy", 0)
226+
184227
def test_touch(self):
185228
"""
186229
Test touch for creating a new file or updating access and modification times of an existing file.

0 commit comments

Comments
 (0)