1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 Radim Rehurek <me@radimrehurek.com>
#
# This code is distributed under the terms and conditions
# from the MIT License (MIT).
#
import gzip
import os
import os.path as P
import subprocess
from unittest import mock
import sys
import pytest
import smart_open.hdfs
CURR_DIR = P.dirname(P.abspath(__file__))
if sys.platform.startswith("win"):
pytest.skip("these tests don't work under Windows", allow_module_level=True)
#
# We want our mocks to emulate the real implementation as close as possible,
# so we use a Popen call during each test. If we mocked using io.BytesIO, then
# it is possible the mocks would behave differently to what we expect in real
# use.
#
# Since these tests use cat, they will not work in an environment without cat,
# such as Windows. The main line of this test submodule contains a simple
# cat implementation. We need this because Windows' analog, type, does
# weird stuff with line endings (inserts CRLF). Also, I don't know of a way
# to get type to echo standard input.
#
def cat(path=None):
command = [sys.executable, P.abspath(__file__)]
if path:
command.append(path)
return subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
CAP_PATH = P.join(CURR_DIR, 'test_data', 'crime-and-punishment.txt')
with open(CAP_PATH, encoding='utf-8') as fin:
CRIME_AND_PUNISHMENT = fin.read()
def test_sanity_read_bytes():
with open(CAP_PATH, 'rb') as fin:
lines = [line for line in fin]
assert len(lines) == 3
def test_sanity_read_text():
with open(CAP_PATH, 'r', encoding='utf-8') as fin:
text = fin.read()
expected = 'В начале июля, в чрезвычайно жаркое время'
assert text[:len(expected)] == expected
@pytest.mark.parametrize('schema', [('hdfs', ), ('viewfs', )])
def test_read(schema):
with mock.patch('subprocess.Popen', return_value=cat(CAP_PATH)):
reader = smart_open.hdfs.CliRawInputBase(f'{schema}://dummy/url')
as_bytes = reader.read()
#
# Not 100% sure why this is necessary on Windows platforms, but the
# tests fail without it. It may be a bug, but I don't have time to
# investigate right now.
#
as_text = as_bytes.decode('utf-8').replace(os.linesep, '\n')
assert as_text == CRIME_AND_PUNISHMENT
@pytest.mark.parametrize('schema', [('hdfs', ), ('viewfs', )])
def test_read_75(schema):
with mock.patch('subprocess.Popen', return_value=cat(CAP_PATH)):
reader = smart_open.hdfs.CliRawInputBase(f'{schema}://dummy/url')
as_bytes = reader.read(75)
as_text = as_bytes.decode('utf-8').replace(os.linesep, '\n')
assert as_text == CRIME_AND_PUNISHMENT[:len(as_text)]
@pytest.mark.parametrize('schema', [('hdfs', ), ('viewfs', )])
def test_unzip(schema):
with mock.patch('subprocess.Popen', return_value=cat(CAP_PATH + '.gz')):
with gzip.GzipFile(fileobj=smart_open.hdfs.CliRawInputBase(f'{schema}://dummy/url')) as fin:
as_bytes = fin.read()
as_text = as_bytes.decode('utf-8')
assert as_text == CRIME_AND_PUNISHMENT
@pytest.mark.parametrize('schema', [('hdfs', ), ('viewfs', )])
def test_context_manager(schema):
with mock.patch('subprocess.Popen', return_value=cat(CAP_PATH)):
with smart_open.hdfs.CliRawInputBase(f'{schema}://dummy/url') as fin:
as_bytes = fin.read()
as_text = as_bytes.decode('utf-8').replace('\r\n', '\n')
assert as_text == CRIME_AND_PUNISHMENT
@pytest.mark.parametrize('schema', [('hdfs', ), ('viewfs', )])
def test_write(schema):
expected = 'мы в ответе за тех, кого приручили'
mocked_cat = cat()
with mock.patch('subprocess.Popen', return_value=mocked_cat):
with smart_open.hdfs.CliRawOutputBase(f'{schema}://dummy/url') as fout:
fout.write(expected.encode('utf-8'))
actual = mocked_cat.stdout.read().decode('utf-8')
assert actual == expected
@pytest.mark.parametrize('schema', [('hdfs', ), ('viewfs', )])
def test_write_zip(schema):
expected = 'мы в ответе за тех, кого приручили'
mocked_cat = cat()
with mock.patch('subprocess.Popen', return_value=mocked_cat):
with smart_open.hdfs.CliRawOutputBase(f'{schema}://dummy/url') as fout:
with gzip.GzipFile(fileobj=fout, mode='wb') as gz_fout:
gz_fout.write(expected.encode('utf-8'))
with gzip.GzipFile(fileobj=mocked_cat.stdout) as fin:
actual = fin.read().decode('utf-8')
assert actual == expected
def main():
try:
path = sys.argv[1]
except IndexError:
bytez = sys.stdin.buffer.read()
else:
with open(path, 'rb') as fin:
bytez = fin.read()
sys.stdout.buffer.write(bytez)
sys.stdout.flush()
if __name__ == '__main__':
main()
|