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

Commit155f6c8

Browse files
Merge pull request#171 from dmitry-lipetsk/D20241225_002--os_ops
OsOps::read methods were corrected (text mode)
2 parents5b263f3 +6c514bf commit155f6c8

File tree

4 files changed

+191
-12
lines changed

4 files changed

+191
-12
lines changed

‎testgres/operations/local_ops.py

Lines changed: 29 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,35 @@ 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+
assert (Noneor"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+
withopen(filename,mode='r',encoding=encoding)asfile:# open in a text mode
290+
content=file.read()
291+
asserttype(content)==str# noqa: E721
292+
returncontent
293+
294+
def_read__binary(self,filename):
295+
asserttype(filename)==str# noqa: E721
296+
withopen(filename,'rb')asfile:# open in a binary mode
271297
content=file.read()
272-
ifbinary:
273-
returncontent
274-
ifisinstance(content,bytes):
275-
returncontent.decode(encodingorget_default_encoding())
298+
asserttype(content)==bytes# noqa: E721
276299
returncontent
277300

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

‎testgres/operations/remote_ops.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
importplatform
44
importsubprocess
55
importtempfile
6+
importio
67

78
# we support both pg8000 and psycopg2
89
try:
@@ -14,6 +15,7 @@
1415
raiseImportError("You must have psycopg2 or pg8000 modules installed")
1516

1617
from ..exceptionsimportExecUtilException
18+
from ..exceptionsimportInvalidOperationException
1719
from .os_opsimportOsOperations,ConnectionParams,get_default_encoding
1820
from .raise_errorimportRaiseError
1921
from .helpersimportHelpers
@@ -319,13 +321,39 @@ def touch(self, filename):
319321
self.exec_command("touch {}".format(filename))
320322

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

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

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

330358
defreadlines(self,filename,num_lines=0,binary=False,encoding=None):
331359
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