Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75b0804

Browse files
TestOsOpsCommon is added [generic os_ops tests]
1 parent a9137df commit 75b0804

File tree

3 files changed

+278
-452
lines changed

3 files changed

+278
-452
lines changed

tests/test_local.py

Lines changed: 0 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33

44
import pytest
55
import re
6-
import tempfile
76
import logging
87

98
from ..testgres import ExecUtilException
10-
from ..testgres import InvalidOperationException
119
from ..testgres import LocalOperations
1210

1311
from .helpers.run_conditions import RunConditions
@@ -98,59 +96,6 @@ def test_listdir(self):
9896
assert f is not None
9997
assert type(f) == str # noqa: E721
10098

101-
def test_read__text(self):
102-
"""
103-
Test LocalOperations::read for text data.
104-
"""
105-
filename = __file__ # current file
106-
107-
with open(filename, 'r') as file: # open in a text mode
108-
response0 = file.read()
109-
110-
assert type(response0) == str # noqa: E721
111-
112-
response1 = self.operations.read(filename)
113-
assert type(response1) == str # noqa: E721
114-
assert response1 == response0
115-
116-
response2 = self.operations.read(filename, encoding=None, binary=False)
117-
assert type(response2) == str # noqa: E721
118-
assert response2 == response0
119-
120-
response3 = self.operations.read(filename, encoding="")
121-
assert type(response3) == str # noqa: E721
122-
assert response3 == response0
123-
124-
response4 = self.operations.read(filename, encoding="UTF-8")
125-
assert type(response4) == str # noqa: E721
126-
assert response4 == response0
127-
128-
def test_read__binary(self):
129-
"""
130-
Test LocalOperations::read for binary data.
131-
"""
132-
filename = __file__ # current file
133-
134-
with open(filename, 'rb') as file: # open in a binary mode
135-
response0 = file.read()
136-
137-
assert type(response0) == bytes # noqa: E721
138-
139-
response1 = self.operations.read(filename, binary=True)
140-
assert type(response1) == bytes # noqa: E721
141-
assert response1 == response0
142-
143-
def test_read__binary_and_encoding(self):
144-
"""
145-
Test LocalOperations::read for binary data and encoding.
146-
"""
147-
filename = __file__ # current file
148-
149-
with pytest.raises(
150-
InvalidOperationException,
151-
match=re.escape("Enconding is not allowed for read binary operation")):
152-
self.operations.read(filename, encoding="", binary=True)
153-
15499
def test_read__unknown_file(self):
155100
"""
156101
Test LocalOperations::read with unknown file.
@@ -159,40 +104,6 @@ def test_read__unknown_file(self):
159104
with pytest.raises(FileNotFoundError, match=re.escape("[Errno 2] No such file or directory: '/dummy'")):
160105
self.operations.read("/dummy")
161106

162-
def test_read_binary__spec(self):
163-
"""
164-
Test LocalOperations::read_binary.
165-
"""
166-
filename = __file__ # current file
167-
168-
with open(filename, 'rb') as file: # open in a binary mode
169-
response0 = file.read()
170-
171-
assert type(response0) == bytes # noqa: E721
172-
173-
response1 = self.operations.read_binary(filename, 0)
174-
assert type(response1) == bytes # noqa: E721
175-
assert response1 == response0
176-
177-
response2 = self.operations.read_binary(filename, 1)
178-
assert type(response2) == bytes # noqa: E721
179-
assert len(response2) < len(response1)
180-
assert len(response2) + 1 == len(response1)
181-
assert response2 == response1[1:]
182-
183-
response3 = self.operations.read_binary(filename, len(response1))
184-
assert type(response3) == bytes # noqa: E721
185-
assert len(response3) == 0
186-
187-
response4 = self.operations.read_binary(filename, len(response2))
188-
assert type(response4) == bytes # noqa: E721
189-
assert len(response4) == 1
190-
assert response4[0] == response1[len(response1) - 1]
191-
192-
response5 = self.operations.read_binary(filename, len(response1) + 1)
193-
assert type(response5) == bytes # noqa: E721
194-
assert len(response5) == 0
195-
196107
def test_read_binary__spec__unk_file(self):
197108
"""
198109
Test LocalOperations::read_binary with unknown file.
@@ -234,70 +145,6 @@ def test_get_file_size__unk_file(self):
234145
with pytest.raises(FileNotFoundError, match=re.escape("[Errno 2] No such file or directory: '/dummy'")):
235146
self.operations.get_file_size("/dummy")
236147

237-
def test_isfile_true(self):
238-
"""
239-
Test isfile for an existing file.
240-
"""
241-
filename = __file__
242-
243-
response = self.operations.isfile(filename)
244-
245-
assert response is True
246-
247-
def test_isfile_false__not_exist(self):
248-
"""
249-
Test isfile for a non-existing file.
250-
"""
251-
filename = os.path.join(os.path.dirname(__file__), "nonexistent_file.txt")
252-
253-
response = self.operations.isfile(filename)
254-
255-
assert response is False
256-
257-
def test_isfile_false__directory(self):
258-
"""
259-
Test isfile for a firectory.
260-
"""
261-
name = os.path.dirname(__file__)
262-
263-
assert self.operations.isdir(name)
264-
265-
response = self.operations.isfile(name)
266-
267-
assert response is False
268-
269-
def test_isdir_true(self):
270-
"""
271-
Test isdir for an existing directory.
272-
"""
273-
name = os.path.dirname(__file__)
274-
275-
response = self.operations.isdir(name)
276-
277-
assert response is True
278-
279-
def test_isdir_false__not_exist(self):
280-
"""
281-
Test isdir for a non-existing directory.
282-
"""
283-
name = os.path.join(os.path.dirname(__file__), "it_is_nonexistent_directory")
284-
285-
response = self.operations.isdir(name)
286-
287-
assert response is False
288-
289-
def test_isdir_false__file(self):
290-
"""
291-
Test isdir for a file.
292-
"""
293-
name = __file__
294-
295-
assert self.operations.isfile(name)
296-
297-
response = self.operations.isdir(name)
298-
299-
assert response is False
300-
301148
def test_cwd(self):
302149
"""
303150
Test cwd.
@@ -314,71 +161,3 @@ def test_cwd(self):
314161

315162
# Comp result
316163
assert v == expectedValue
317-
318-
class tagWriteData001:
319-
def __init__(self, sign, source, cp_rw, cp_truncate, cp_binary, cp_data, result):
320-
self.sign = sign
321-
self.source = source
322-
self.call_param__rw = cp_rw
323-
self.call_param__truncate = cp_truncate
324-
self.call_param__binary = cp_binary
325-
self.call_param__data = cp_data
326-
self.result = result
327-
328-
sm_write_data001 = [
329-
tagWriteData001("A001", "1234567890", False, False, False, "ABC", "1234567890ABC"),
330-
tagWriteData001("A002", b"1234567890", False, False, True, b"ABC", b"1234567890ABC"),
331-
332-
tagWriteData001("B001", "1234567890", False, True, False, "ABC", "ABC"),
333-
tagWriteData001("B002", "1234567890", False, True, False, "ABC1234567890", "ABC1234567890"),
334-
tagWriteData001("B003", b"1234567890", False, True, True, b"ABC", b"ABC"),
335-
tagWriteData001("B004", b"1234567890", False, True, True, b"ABC1234567890", b"ABC1234567890"),
336-
337-
tagWriteData001("C001", "1234567890", True, False, False, "ABC", "1234567890ABC"),
338-
tagWriteData001("C002", b"1234567890", True, False, True, b"ABC", b"1234567890ABC"),
339-
340-
tagWriteData001("D001", "1234567890", True, True, False, "ABC", "ABC"),
341-
tagWriteData001("D002", "1234567890", True, True, False, "ABC1234567890", "ABC1234567890"),
342-
tagWriteData001("D003", b"1234567890", True, True, True, b"ABC", b"ABC"),
343-
tagWriteData001("D004", b"1234567890", True, True, True, b"ABC1234567890", b"ABC1234567890"),
344-
345-
tagWriteData001("E001", "\0001234567890\000", False, False, False, "\000ABC\000", "\0001234567890\000\000ABC\000"),
346-
tagWriteData001("E002", b"\0001234567890\000", False, False, True, b"\000ABC\000", b"\0001234567890\000\000ABC\000"),
347-
348-
tagWriteData001("F001", "a\nb\n", False, False, False, ["c", "d"], "a\nb\nc\nd\n"),
349-
tagWriteData001("F002", b"a\nb\n", False, False, True, [b"c", b"d"], b"a\nb\nc\nd\n"),
350-
351-
tagWriteData001("G001", "a\nb\n", False, False, False, ["c\n\n", "d\n"], "a\nb\nc\nd\n"),
352-
tagWriteData001("G002", b"a\nb\n", False, False, True, [b"c\n\n", b"d\n"], b"a\nb\nc\nd\n"),
353-
]
354-
355-
@pytest.fixture(
356-
params=sm_write_data001,
357-
ids=[x.sign for x in sm_write_data001],
358-
)
359-
def write_data001(self, request):
360-
assert isinstance(request, pytest.FixtureRequest)
361-
assert type(request.param) == __class__.tagWriteData001 # noqa: E721
362-
return request.param
363-
364-
def test_write(self, write_data001):
365-
assert type(write_data001) == __class__.tagWriteData001 # noqa: E721
366-
367-
mode = "w+b" if write_data001.call_param__binary else "w+"
368-
369-
with tempfile.NamedTemporaryFile(mode=mode, delete=True) as tmp_file:
370-
tmp_file.write(write_data001.source)
371-
tmp_file.flush()
372-
373-
self.operations.write(
374-
tmp_file.name,
375-
write_data001.call_param__data,
376-
read_and_write=write_data001.call_param__rw,
377-
truncate=write_data001.call_param__truncate,
378-
binary=write_data001.call_param__binary)
379-
380-
tmp_file.seek(0)
381-
382-
s = tmp_file.read()
383-
384-
assert s == write_data001.result

0 commit comments

Comments
 (0)