Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4e23e03

Browse files
OsOps::read methods were corrected
Now they always read a file as binary.When 'binary' parameter is False we will use 'encoding' parameter to decode bytes into string.Binary read does not allow an usage of 'encoding' parameter (InvalidOperationException is raised).New tests are added.
1 parent5b263f3 commit4e23e03

File tree

4 files changed

+189
-12
lines changed

4 files changed

+189
-12
lines changed

‎testgres/operations/local_ops.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
importpsutil
1111

1212
from ..exceptionsimportExecUtilException
13+
from ..exceptionsimportInvalidOperationException
1314
from .os_opsimportConnectionParams,OsOperations,pglib,get_default_encoding
1415
from .raise_errorimportRaiseError
1516
from .helpersimportHelpers
@@ -266,13 +267,36 @@ def touch(self, filename):
266267
os.utime(filename,None)
267268

268269
defread(self,filename,encoding=None,binary=False):
269-
mode="rb"ifbinaryelse"r"
270-
withopen(filename,mode)asfile:
270+
asserttype(filename)==str# noqa: E721
271+
assertencodingisNoneortype(encoding)==str# noqa: E721
272+
asserttype(binary)==bool# noqa: E721
273+
274+
ifbinary:
275+
ifencodingisnotNone:
276+
raiseInvalidOperationException("Enconding is not allowed for read binary operation")
277+
278+
returnself._read__binary(filename)
279+
280+
# python behavior
281+
assertNoneor"abc"=="abc"
282+
assert""or"abc"=="abc"
283+
284+
returnself._read__text_with_encoding(filename,encodingorget_default_encoding())
285+
286+
def_read__text_with_encoding(self,filename,encoding):
287+
asserttype(filename)==str# noqa: E721
288+
asserttype(encoding)==str# noqa: E721
289+
content=self._read__binary(filename)
290+
asserttype(content)==bytes# noqa: E721
291+
content_s=content.decode(encoding)
292+
asserttype(content_s)==str# noqa: E721
293+
returncontent_s
294+
295+
def_read__binary(self,filename):
296+
asserttype(filename)==str# noqa: E721
297+
withopen(filename,'rb')asfile:# open in a binary mode
271298
content=file.read()
272-
ifbinary:
273-
returncontent
274-
ifisinstance(content,bytes):
275-
returncontent.decode(encodingorget_default_encoding())
299+
asserttype(content)==bytes# noqa: E721
276300
returncontent
277301

278302
defreadlines(self,filename,num_lines=0,binary=False,encoding=None):

‎testgres/operations/remote_ops.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
raiseImportError("You must have psycopg2 or pg8000 modules installed")
1515

1616
from ..exceptionsimportExecUtilException
17+
from ..exceptionsimportInvalidOperationException
1718
from .os_opsimportOsOperations,ConnectionParams,get_default_encoding
1819
from .raise_errorimportRaiseError
1920
from .helpersimportHelpers
@@ -319,13 +320,37 @@ def touch(self, filename):
319320
self.exec_command("touch {}".format(filename))
320321

321322
defread(self,filename,binary=False,encoding=None):
322-
cmd="cat {}".format(filename)
323-
result=self.exec_command(cmd,encoding=encoding)
323+
asserttype(filename)==str# noqa: E721
324+
assertencodingisNoneortype(encoding)==str# noqa: E721
325+
asserttype(binary)==bool# noqa: E721
324326

325-
ifnotbinaryandresult:
326-
result=result.decode(encodingorget_default_encoding())
327+
ifbinary:
328+
ifencodingisnotNone:
329+
raiseInvalidOperationException("Enconding is not allowed for read binary operation")
327330

328-
returnresult
331+
returnself._read__binary(filename)
332+
333+
# python behavior
334+
assertNoneor"abc"=="abc"
335+
assert""or"abc"=="abc"
336+
337+
returnself._read__text_with_encoding(filename,encodingorget_default_encoding())
338+
339+
def_read__text_with_encoding(self,filename,encoding):
340+
asserttype(filename)==str# noqa: E721
341+
asserttype(encoding)==str# noqa: E721
342+
content=self._read__binary(filename)
343+
asserttype(content)==bytes# noqa: E721
344+
content_s=content.decode(encoding)
345+
asserttype(content_s)==str# noqa: E721
346+
returncontent_s
347+
348+
def_read__binary(self,filename):
349+
asserttype(filename)==str# noqa: E721
350+
cmd= ["cat",filename]
351+
content=self.exec_command(cmd)
352+
asserttype(content)==bytes# noqa: E721
353+
returncontent
329354

330355
defreadlines(self,filename,num_lines=0,binary=False,encoding=None):
331356
ifnum_lines>0:

‎tests/test_local.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
importre
55

66
fromtestgresimportExecUtilException
7+
fromtestgresimportInvalidOperationException
78
fromtestgresimportLocalOperations
89

910
from .helpers.run_conditionsimportRunConditions
@@ -56,6 +57,67 @@ def test_exec_command_failure__expect_error(self):
5657
assertexit_status==127
5758
assertresult==b''
5859

60+
deftest_read__text(self):
61+
"""
62+
Test LocalOperations::read for text data.
63+
"""
64+
filename=__file__# current file
65+
66+
withopen(filename,'r')asfile:# open in a text mode
67+
response0=file.read()
68+
69+
asserttype(response0)==str# noqa: E721
70+
71+
response1=self.operations.read(filename)
72+
asserttype(response1)==str# noqa: E721
73+
assertresponse1==response0
74+
75+
response2=self.operations.read(filename,encoding=None,binary=False)
76+
asserttype(response2)==str# noqa: E721
77+
assertresponse2==response0
78+
79+
response3=self.operations.read(filename,encoding="")
80+
asserttype(response3)==str# noqa: E721
81+
assertresponse3==response0
82+
83+
response4=self.operations.read(filename,encoding="UTF-8")
84+
asserttype(response4)==str# noqa: E721
85+
assertresponse4==response0
86+
87+
deftest_read__binary(self):
88+
"""
89+
Test LocalOperations::read for binary data.
90+
"""
91+
filename=__file__# current file
92+
93+
withopen(filename,'rb')asfile:# open in a binary mode
94+
response0=file.read()
95+
96+
asserttype(response0)==bytes# noqa: E721
97+
98+
response1=self.operations.read(filename,binary=True)
99+
asserttype(response1)==bytes# noqa: E721
100+
assertresponse1==response0
101+
102+
deftest_read__binary_and_encoding(self):
103+
"""
104+
Test LocalOperations::read for binary data and encoding.
105+
"""
106+
filename=__file__# current file
107+
108+
withpytest.raises(
109+
InvalidOperationException,
110+
match=re.escape("Enconding is not allowed for read binary operation")):
111+
self.operations.read(filename,encoding="",binary=True)
112+
113+
deftest_read__unknown_file(self):
114+
"""
115+
Test LocalOperations::read with unknown file.
116+
"""
117+
118+
withpytest.raises(FileNotFoundError,match=re.escape("[Errno 2] No such file or directory: '/dummy'")):
119+
self.operations.read("/dummy")
120+
59121
deftest_read_binary__spec(self):
60122
"""
61123
Test LocalOperations::read_binary.
@@ -95,7 +157,9 @@ def test_read_binary__spec__unk_file(self):
95157
Test LocalOperations::read_binary with unknown file.
96158
"""
97159

98-
withpytest.raises(FileNotFoundError,match=re.escape("[Errno 2] No such file or directory: '/dummy'")):
160+
withpytest.raises(
161+
FileNotFoundError,
162+
match=re.escape("[Errno 2] No such file or directory: '/dummy'")):
99163
self.operations.read_binary("/dummy",0)
100164

101165
deftest_get_file_size(self):

‎tests/test_remote.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
importre
55

66
fromtestgresimportExecUtilException
7+
fromtestgresimportInvalidOperationException
78
fromtestgresimportRemoteOperations
89
fromtestgresimportConnectionParams
910

@@ -182,6 +183,69 @@ def test_read_binary_file(self):
182183

183184
assertisinstance(response,bytes)
184185

186+
deftest_read__text(self):
187+
"""
188+
Test RemoteOperations::read for text data.
189+
"""
190+
filename=__file__# current file
191+
192+
withopen(filename,'r')asfile:# open in a text mode
193+
response0=file.read()
194+
195+
asserttype(response0)==str# noqa: E721
196+
197+
response1=self.operations.read(filename)
198+
asserttype(response1)==str# noqa: E721
199+
assertresponse1==response0
200+
201+
response2=self.operations.read(filename,encoding=None,binary=False)
202+
asserttype(response2)==str# noqa: E721
203+
assertresponse2==response0
204+
205+
response3=self.operations.read(filename,encoding="")
206+
asserttype(response3)==str# noqa: E721
207+
assertresponse3==response0
208+
209+
response4=self.operations.read(filename,encoding="UTF-8")
210+
asserttype(response4)==str# noqa: E721
211+
assertresponse4==response0
212+
213+
deftest_read__binary(self):
214+
"""
215+
Test RemoteOperations::read for binary data.
216+
"""
217+
filename=__file__# current file
218+
219+
withopen(filename,'rb')asfile:# open in a binary mode
220+
response0=file.read()
221+
222+
asserttype(response0)==bytes# noqa: E721
223+
224+
response1=self.operations.read(filename,binary=True)
225+
asserttype(response1)==bytes# noqa: E721
226+
assertresponse1==response0
227+
228+
deftest_read__binary_and_encoding(self):
229+
"""
230+
Test RemoteOperations::read for binary data and encoding.
231+
"""
232+
filename=__file__# current file
233+
234+
withpytest.raises(
235+
InvalidOperationException,
236+
match=re.escape("Enconding is not allowed for read binary operation")):
237+
self.operations.read(filename,encoding="",binary=True)
238+
239+
deftest_read__unknown_file(self):
240+
"""
241+
Test RemoteOperations::read with unknown file.
242+
"""
243+
244+
withpytest.raises(
245+
ExecUtilException,
246+
match=re.escape("cat: /dummy: No such file or directory")):
247+
self.operations.read("/dummy")
248+
185249
deftest_read_binary__spec(self):
186250
"""
187251
Test RemoteOperations::read_binary.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp