From 857a6f89bc500a4eae52e859ab6196602350a3eb Mon Sep 17 00:00:00 2001 From: Charlotte Kostelic Date: Tue, 6 Aug 2024 16:08:42 -0400 Subject: [PATCH] Simplify clients (#4) * removed tmp_path tests * added _BaseClient, refactored classes, tests * fixes to Client class * simplified conftest * added tests for _BaseClient * renamed __create_client to __connect_to_server --- .github/workflows/unit-tests.yaml | 2 +- file_retriever/_clients.py | 67 +++++++++-- file_retriever/connect.py | 22 ++-- pyproject.toml | 1 - tests/conftest.py | 188 ++++++++---------------------- tests/test_clients.py | 98 +++++++++------- tests/test_connect.py | 36 +++--- 7 files changed, 191 insertions(+), 223 deletions(-) diff --git a/.github/workflows/unit-tests.yaml b/.github/workflows/unit-tests.yaml index 00dd026..61bc53e 100644 --- a/.github/workflows/unit-tests.yaml +++ b/.github/workflows/unit-tests.yaml @@ -23,7 +23,7 @@ jobs: python -m pip install --upgrade pip python -m pip install -r dev-requirements.txt - name: Run tests - run: pytest -m "not livetest and not tmpdir" --cov=file_retriever/ + run: pytest -m "not livetest" --cov=file_retriever/ - name: Send report to Coveralls uses: AndreMiras/coveralls-python-action@develop with: diff --git a/file_retriever/_clients.py b/file_retriever/_clients.py index c1769b9..a61bbb3 100644 --- a/file_retriever/_clients.py +++ b/file_retriever/_clients.py @@ -5,6 +5,7 @@ """ +from abc import ABC, abstractmethod import ftplib import os import paramiko @@ -14,7 +15,53 @@ from file_retriever.file import File -class _ftpClient: +class _BaseClient(ABC): + """""" + + @abstractmethod + def __init__(self, username: str, password: str, host: str, port: Union[str, int]): + self.connection: Union[ftplib.FTP, paramiko.SFTPClient] = ( + self._connect_to_server( + username=username, password=password, host=host, port=int(port) + ) + ) + + @abstractmethod + def __enter__(self, *args): + return self + + @abstractmethod + def __exit__(self, *args): + self.connection.close() + + @abstractmethod + def _connect_to_server( + self, + username: str, + password: str, + host: str, + port: int, + ) -> Union[ftplib.FTP, paramiko.SFTPClient]: + pass + + @abstractmethod + def get_remote_file_data(self, file: str, remote_dir: str) -> File: + pass + + @abstractmethod + def list_remote_file_data(self, remote_dir: str) -> List[File]: + pass + + @abstractmethod + def download_file(self, file: str, remote_dir: str, local_dir: str) -> None: + pass + + @abstractmethod + def upload_file(self, file: str, remote_dir: str, local_dir: str) -> File: + pass + + +class _ftpClient(_BaseClient): """ An FTP client to use when interacting with remote storage. Supports interactions with servers via the `ftplib` library. @@ -36,7 +83,9 @@ def __init__( port: port number for server """ - self.connection = self._create_ftp_connection( + if port not in [21, "21"]: + raise ValueError("Invalid port number for FTP connection.") + self.connection: ftplib.FTP = self._connect_to_server( username=username, password=password, host=host, port=int(port) ) @@ -56,7 +105,7 @@ def __exit__(self, *args): """ self.connection.close() - def _create_ftp_connection( + def _connect_to_server( self, username: str, password: str, host: str, port: int ) -> ftplib.FTP: """ @@ -135,7 +184,7 @@ def get_file_permissions(data): f"Unable to retrieve file data: {sys.exc_info()[1]}" ) - def list_file_data(self, remote_dir: str) -> List[File]: + def list_remote_file_data(self, remote_dir: str) -> List[File]: """ Retrieves metadata for each file in `remote_dir` on server. @@ -217,7 +266,7 @@ def upload_file(self, file: str, remote_dir: str, local_dir: str) -> File: raise -class _sftpClient: +class _sftpClient(_BaseClient): """ An SFTP client to use when interacting with remote storage. Supports interactions with servers via the `paramiko` library. @@ -239,7 +288,9 @@ def __init__( port: port number for server """ - self.connection = self._create_sftp_connection( + if port not in [22, "22"]: + raise ValueError("Invalid port number for SFTP connection.") + self.connection: paramiko.SFTPClient = self._connect_to_server( username=username, password=password, host=host, port=int(port) ) @@ -259,7 +310,7 @@ def __exit__(self, *args): """ self.connection.close() - def _create_sftp_connection( + def _connect_to_server( self, username: str, password: str, host: str, port: int ) -> paramiko.SFTPClient: """ @@ -311,7 +362,7 @@ def get_remote_file_data(self, file: str, remote_dir: str) -> File: except OSError: raise - def list_file_data(self, remote_dir: str) -> List[File]: + def list_remote_file_data(self, remote_dir: str) -> List[File]: """ Lists metadata for each file in `remote_dir` on server. diff --git a/file_retriever/connect.py b/file_retriever/connect.py index c9eb4da..97d97cb 100644 --- a/file_retriever/connect.py +++ b/file_retriever/connect.py @@ -38,23 +38,23 @@ def __init__( self.vendor = vendor self.host = host - self.port = int(port) + self.port = port self.remote_dir = remote_dir - self.session = self._create_client(username=username, password=password) + self.session = self.__connect_to_server(username=username, password=password) - def _create_client( + def __connect_to_server( self, username: str, password: str ) -> Union[_ftpClient, _sftpClient]: match self.port: - case 21: + case 21 | "21": return _ftpClient( username=username, password=password, host=self.host, port=self.port, ) - case 22: + case 22 | "22": return _sftpClient( username=username, password=password, @@ -84,24 +84,24 @@ def check_file(self, file: str, check_dir: str, remote: bool) -> bool: else: return os.path.exists(os.path.join(check_dir, file)) - def get_file_data(self, file: str, remote_dir: Optional[str] = None) -> List[File]: + def get_file_data(self, file: str, remote_dir: Optional[str] = None) -> File: """ - Retrieve metadata for file in `remote_dir` on server. If `remote_dir` is not - provided then data for file in `self.remote_dir` will be retrieved. + Retrieve metadata for `file` in `remote_dir` on server. If `remote_dir` is not + provided then data for `file` in `self.remote_dir` will be retrieved. Args: file: name of file to retrieve metadata for remote_dir: directory on server to interact with Returns: - files in `remote_dir` represented as `File` object + file in `remote_dir` represented as `File` object """ if not remote_dir or remote_dir is None: remote_dir = self.remote_dir with self.session as session: return session.get_remote_file_data(file, remote_dir) - def list_files( + def list_files_in_dir( self, time_delta: int = 0, remote_dir: Optional[str] = None ) -> List[File]: """ @@ -122,7 +122,7 @@ def list_files( if not remote_dir or remote_dir is None: remote_dir = self.remote_dir with self.session as session: - files = session.list_file_data(remote_dir) + files = session.list_remote_file_data(remote_dir) if time_delta > 0: return [ i diff --git a/pyproject.toml b/pyproject.toml index 8d536cd..8121a63 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,7 +24,6 @@ pytest-mock = "^3.14.0" testpaths = ["tests"] markers = [ "livetest: mark a test as hitting a live ftp/sftp server", - "tmpdir: mark a test as using a temporary directory", ] diff --git a/tests/conftest.py b/tests/conftest.py index c79168f..0e6342d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,7 +5,7 @@ from typing import Dict, List import yaml import pytest -from file_retriever._clients import _ftpClient, _sftpClient +from file_retriever._clients import _ftpClient, _sftpClient, _BaseClient from file_retriever.connect import Client @@ -27,6 +27,12 @@ def __init__(self): self.st_uid = 0 self.st_size = 140401 + def create_SFTPAttributes(self): + sftp_attr = paramiko.SFTPAttributes() + sftp_attr.__dict__ = self.__dict__ + sftp_attr.filename = self.file_name + return sftp_attr + @pytest.fixture def mock_file_data(monkeypatch): @@ -36,36 +42,19 @@ def mock_stat(*args, **kwargs): monkeypatch.setattr(os, "stat", mock_stat) -class Mock_SFTPAttributes(paramiko.SFTPAttributes): - - def __init__(self): - self.filename = "foo.mrc" - self.st_mtime = 1704070800 - self.st_size = 140401 - self.st_uid = 0 - self.st_gid = 0 - self.st_mode = 33188 - self.st_atime = None +@pytest.fixture +def mock_open_file(mocker): + m = mocker.mock_open() + mocker.patch("builtins.open", m) + return m class MockFTP: """Mock response from FTP server for a successful login""" - def __init__(self, *args, **kwargs): - pass - - def __enter__(self): - return self - - def __exit__(self, *args) -> None: - self.close() - def close(self, *args, **kwargs) -> None: pass - def getwelcome(self, *args, **kwargs) -> str: - return "220 (vsFTPd 3.0.5)" - def nlst(self, *args, **kwargs) -> List[str]: return ["foo.mrc"] @@ -89,15 +78,6 @@ def voidcmd(self, *args, **kwargs) -> str: class MockSFTPClient: """Mock response from SFTP for a successful login""" - def __init__(self): - pass - - def __enter__(self): - return self - - def __exit__(self, *args) -> None: - self.close() - def close(self, *args, **kwargs) -> None: pass @@ -105,44 +85,35 @@ def get(self, *args, **kwargs) -> None: open(args[1], "x+") def listdir_attr(self, *args, **kwargs) -> List[paramiko.SFTPAttributes]: - return [Mock_SFTPAttributes()] + return [MockFileData().create_SFTPAttributes()] def put(self, *args, **kwargs) -> paramiko.SFTPAttributes: - return Mock_SFTPAttributes() + return MockFileData().create_SFTPAttributes() def stat(self, *args, **kwargs) -> paramiko.SFTPAttributes: - return Mock_SFTPAttributes() + return MockFileData().create_SFTPAttributes() -class MockSSHClient: - """Mock response from SSH for a successful login""" +class MockABCClient: + """Mock response from SFTP for a successful login""" - def __init__(self): + def close(self, *args, **kwargs) -> None: pass - def connect(self, *args, **kwargs) -> None: - pass - def open_sftp(self, *args, **kwargs) -> MockSFTPClient: - return MockSFTPClient() +@pytest.fixture +def mock_BaseClient(monkeypatch): + def mock_bc(*args, **kwargs): + return MockABCClient() - def set_missing_host_key_policy(self, *args, **kwargs) -> None: - pass + monkeypatch.setattr(_BaseClient, "_connect_to_server", mock_bc) @pytest.fixture -def mock_client(monkeypatch): +def stub_client(monkeypatch): def mock_login(*args, **kwargs): pass - def mock_file_data(*args, **kwargs): - return MockFileData() - - def mock_file_exists(*args, **kwargs): - return False - - monkeypatch.setattr(os.path, "exists", mock_file_exists) - monkeypatch.setattr(os, "stat", mock_file_data) monkeypatch.setattr(ftplib.FTP, "login", mock_login) monkeypatch.setattr(ftplib.FTP, "connect", mock_login) monkeypatch.setattr(paramiko.SSHClient, "connect", mock_login) @@ -151,125 +122,60 @@ def mock_file_exists(*args, **kwargs): @pytest.fixture -def mock_open_file(mocker): - m = mocker.mock_open() - mocker.patch("builtins.open", m) - return m - - -@pytest.fixture -def stub_client(monkeypatch, mock_open_file, mock_client): +def mock_ftpClient_sftpClient(monkeypatch, mock_open_file, stub_client): def mock_ftp_client(*args, **kwargs): return MockFTP() def mock_sftp_client(*args, **kwargs): return MockSFTPClient() - def mock_file_not_found(*args, **kwargs): - return False - - monkeypatch.setattr(_ftpClient, "_create_ftp_connection", mock_ftp_client) - monkeypatch.setattr(_sftpClient, "_create_sftp_connection", mock_sftp_client) - monkeypatch.setattr(Client, "check_file", mock_file_not_found) + monkeypatch.setattr(_ftpClient, "_connect_to_server", mock_ftp_client) + monkeypatch.setattr(_sftpClient, "_connect_to_server", mock_sftp_client) @pytest.fixture -def stub_client_tmp_path(monkeypatch, mock_client): - def mock_ftp_client(*args, **kwargs): - return MockFTP() - - def mock_sftp_client(*args, **kwargs): - return MockSFTPClient() +def mock_Client(monkeypatch, mock_ftpClient_sftpClient, mock_file_data): + def mock_file_exists(*args, **kwargs): + return False - monkeypatch.setattr(_ftpClient, "_create_ftp_connection", mock_ftp_client) - monkeypatch.setattr(_sftpClient, "_create_sftp_connection", mock_sftp_client) + monkeypatch.setattr(os.path, "exists", mock_file_exists) + monkeypatch.setattr(Client, "check_file", mock_file_exists) @pytest.fixture -def stub_client_file_exists(monkeypatch, mock_open_file, mock_client): - def mock_ftp_client(*args, **kwargs): - return MockFTP() - - def mock_sftp_client(*args, **kwargs): - return MockSFTPClient() - - def mock_file_exists(*args, **kwargs): - file = MockFileData() - file.file_name = "bar.mrc" - return file - +def mock_Client_file_exists(monkeypatch, mock_ftpClient_sftpClient): def path_exists(*args, **kwargs): return True monkeypatch.setattr(os.path, "exists", path_exists) - monkeypatch.setattr(_ftpClient, "_create_ftp_connection", mock_ftp_client) - monkeypatch.setattr(_sftpClient, "_create_sftp_connection", mock_sftp_client) - # monkeypatch.setattr(_ftpClient, "get_remote_file_data", mock_file_exists) - # monkeypatch.setattr(_sftpClient, "get_remote_file_data", mock_file_exists) - - -class MockOSError: - - def __init__(self): - raise OSError - - -class MockAuthenticationException: - - def __init__(self): - raise paramiko.AuthenticationException - - -class MockErrorPerm: - - def __init__(self): - raise ftplib.error_perm - - -class MockSSHException: - - def __init__(self): - raise paramiko.SSHException - - -class MockErrorTemp: - - def __init__(self): - raise ftplib.error_temp - - -class MockErrorReply: - - def __init__(self): - raise ftplib.error_reply @pytest.fixture -def mock_auth_error(monkeypatch, mock_client): +def mock_auth_error(monkeypatch, stub_client): def mock_ssh_auth_error(*args, **kwargs): - return MockAuthenticationException() + raise paramiko.AuthenticationException def mock_ftp_error_perm(*args, **kwargs): - return MockErrorPerm() + raise ftplib.error_perm monkeypatch.setattr(ftplib.FTP, "login", mock_ftp_error_perm) monkeypatch.setattr(paramiko.SSHClient, "connect", mock_ssh_auth_error) @pytest.fixture -def mock_login_connection_error(monkeypatch, mock_client): - def mock_ftp_error_perm(*args, **kwargs): - return MockErrorTemp() +def mock_login_connection_error(monkeypatch, stub_client): + def mock_ftp_error_temp(*args, **kwargs): + raise ftplib.error_temp def mock_ssh_error(*args, **kwargs): - return MockSSHException() + raise paramiko.SSHException monkeypatch.setattr(paramiko.SSHClient, "connect", mock_ssh_error) - monkeypatch.setattr(ftplib.FTP, "login", mock_ftp_error_perm) + monkeypatch.setattr(ftplib.FTP, "login", mock_ftp_error_temp) @pytest.fixture -def mock_permissions_error(monkeypatch, mock_open_file, mock_client): +def mock_permissions_error(monkeypatch, mock_open_file, stub_client): def mock_retrlines(*args, **kwargs): return None @@ -277,12 +183,12 @@ def mock_retrlines(*args, **kwargs): @pytest.fixture -def mock_file_error(monkeypatch, mock_open_file, stub_client): +def mock_file_error(monkeypatch, mock_open_file, mock_ftpClient_sftpClient): def mock_os_error(*args, **kwargs): - return MockOSError() + raise OSError def mock_ftp_error_perm(*args, **kwargs): - return MockErrorPerm() + raise ftplib.error_perm def mock_retrlines(*args, **kwargs): return None @@ -299,9 +205,9 @@ def mock_retrlines(*args, **kwargs): @pytest.fixture -def mock_connection_error_reply(monkeypatch, mock_open_file, mock_client): +def mock_connection_error_reply(monkeypatch, mock_open_file, stub_client): def mock_ftp_error_reply(*args, **kwargs): - return MockErrorReply() + raise ftplib.error_reply monkeypatch.setattr(ftplib.FTP, "storbinary", mock_ftp_error_reply) monkeypatch.setattr(ftplib.FTP, "retrbinary", mock_ftp_error_reply) diff --git a/tests/test_clients.py b/tests/test_clients.py index adacf37..a4c9c57 100644 --- a/tests/test_clients.py +++ b/tests/test_clients.py @@ -4,10 +4,30 @@ import os import paramiko import pytest -from file_retriever._clients import _ftpClient, _sftpClient +from file_retriever._clients import _ftpClient, _sftpClient, _BaseClient from file_retriever.file import File +def test_BaseClient(): + _BaseClient.__abstractmethods__ = set() + ftp_bc = _BaseClient(username="foo", password="bar", host="baz", port=21) + assert ftp_bc.__dict__ == {"connection": None} + assert ftp_bc.get_remote_file_data("foo.mrc", "bar") is None + assert ftp_bc.list_remote_file_data("foo") is None + assert ftp_bc.download_file("foo.mrc", "bar", "baz") is None + assert ftp_bc.upload_file("foo.mrc", "bar", "baz") is None + + +def test_BaseClient_context_manager(mock_BaseClient): + _BaseClient.__abstractmethods__ = set() + with _BaseClient(username="foo", password="bar", host="baz", port=22) as sftp_bc: + assert sftp_bc.connection is not None + assert sftp_bc.get_remote_file_data("foo.mrc", "bar") is None + assert sftp_bc.list_remote_file_data("foo") is None + assert sftp_bc.download_file("foo.mrc", "bar", "baz") is None + assert sftp_bc.upload_file("foo.mrc", "bar", "baz") is None + + class TestMock_ftpClient: """Test the _ftpClient class with mock responses.""" @@ -22,9 +42,9 @@ def test_ftpClient_context_manager(self, stub_client, stub_creds): assert client.connection is not None @pytest.mark.parametrize("port", [None, [], {}]) - def test_ftpClient_port_TypeError(self, stub_client, stub_creds, port): + def test_ftpClient_port_ValueError(self, stub_client, stub_creds, port): stub_creds["port"] = port - with pytest.raises(TypeError): + with pytest.raises(ValueError): _ftpClient(**stub_creds) def test_ftpClient_no_creds(self, stub_client): @@ -42,7 +62,9 @@ def test_ftpClient_error_temp(self, mock_login_connection_error, stub_creds): with pytest.raises(ftplib.error_temp): _ftpClient(**stub_creds) - def test_ftpClient_get_remote_file_data(self, stub_client, stub_creds): + def test_ftpClient_get_remote_file_data( + self, mock_ftpClient_sftpClient, stub_creds + ): stub_creds["port"] = "21" ftp = _ftpClient(**stub_creds) files = ftp.get_remote_file_data("foo.mrc", "testdir") @@ -80,10 +102,12 @@ def test_ftpClient_get_remote_file_data_non_permissions( with pytest.raises(ftplib.error_perm): ftp.get_remote_file_data("foo.mrc", "testdir") - def test_ftpClient_list_file_data(self, stub_client, stub_creds): + def test_ftpClient_list_remote_file_data( + self, mock_ftpClient_sftpClient, stub_creds + ): stub_creds["port"] = "21" ftp = _ftpClient(**stub_creds) - files = ftp.list_file_data("testdir") + files = ftp.list_remote_file_data("testdir") assert files == [ File( file_name="foo.mrc", @@ -96,24 +120,15 @@ def test_ftpClient_list_file_data(self, stub_client, stub_creds): ) ] - def test_ftpClient_list_file_data_connection_error( + def test_ftpClient_list_remote_file_data_connection_error( self, mock_connection_error_reply, stub_creds ): stub_creds["port"] = "21" ftp = _ftpClient(**stub_creds) with pytest.raises(ftplib.error_reply): - ftp.list_file_data("testdir") - - @pytest.mark.tmpdir - def test_ftpClient_download_file(self, tmp_path, stub_client_tmp_path, stub_creds): - stub_creds["port"] = "21" - path = tmp_path / "test" - path.mkdir() - ftp = _ftpClient(**stub_creds) - ftp.download_file(file="foo.mrc", remote_dir="bar", local_dir=str(path)) - assert "foo.mrc" in os.listdir(path) + ftp.list_remote_file_data("testdir") - def test_ftpClient_download_mock_file(self, stub_client, stub_creds): + def test_ftpClient_download_file(self, mock_ftpClient_sftpClient, stub_creds): stub_creds["port"] = "21" with does_not_raise(): ftp = _ftpClient(**stub_creds) @@ -133,7 +148,7 @@ def test_ftpClient_download_connection_error( ftp = _ftpClient(**stub_creds) ftp.download_file(file="foo.mrc", remote_dir="bar", local_dir="test") - def test_ftpClient_upload_file(self, stub_client, stub_creds): + def test_ftpClient_upload_file(self, mock_ftpClient_sftpClient, stub_creds): stub_creds["port"] = "21" ftp = _ftpClient(**stub_creds) file = ftp.upload_file(file="foo.mrc", local_dir="foo", remote_dir="bar") @@ -157,18 +172,18 @@ def test_ftpClient_upload_connection_error( class TestMock_sftpClient: """Test the _sftpClient class with mock responses.""" - def test_sftpClient(self, mock_client, stub_creds): + def test_sftpClient(self, stub_client, stub_creds): stub_creds["port"] = "22" sftp = _sftpClient(**stub_creds) assert sftp.connection is not None @pytest.mark.parametrize("port", [None, [], {}]) - def test_sftpClient_port_TypeError(self, mock_client, stub_creds, port): + def test_sftpClient_port_ValueError(self, stub_client, stub_creds, port): stub_creds["port"] = port - with pytest.raises(TypeError): + with pytest.raises(ValueError): _sftpClient(**stub_creds) - def test_sftpClient_no_creds(self, mock_client): + def test_sftpClient_no_creds(self, stub_client): creds = {} with pytest.raises(TypeError): _sftpClient(**creds) @@ -183,12 +198,14 @@ def test_sftpclient_error_reply(self, mock_login_connection_error, stub_creds): with pytest.raises(paramiko.SSHException): _sftpClient(**stub_creds) - def test_sftpClient_context_manager(self, mock_client, stub_creds): + def test_sftpClient_context_manager(self, stub_client, stub_creds): stub_creds["port"] = "22" with _sftpClient(**stub_creds) as client: assert client.connection is not None - def test_sftpClient_get_remote_file_data(self, stub_client, stub_creds): + def test_sftpClient_get_remote_file_data( + self, mock_ftpClient_sftpClient, stub_creds + ): stub_creds["port"] = "22" ftp = _sftpClient(**stub_creds) file = ftp.get_remote_file_data("foo.mrc", "testdir") @@ -210,10 +227,12 @@ def test_sftpClient_get_remote_file_data_not_found( with pytest.raises(OSError): sftp.get_remote_file_data("foo.mrc", "testdir") - def test_sftpClient_list_file_data(self, stub_client, stub_creds): + def test_sftpClient_list_remote_file_data( + self, mock_ftpClient_sftpClient, stub_creds + ): stub_creds["port"] = "22" ftp = _sftpClient(**stub_creds) - files = ftp.list_file_data("testdir") + files = ftp.list_remote_file_data("testdir") assert files == [ File( file_name="foo.mrc", @@ -226,22 +245,15 @@ def test_sftpClient_list_file_data(self, stub_client, stub_creds): ) ] - def test_sftpClient_list_file_data_not_found(self, mock_file_error, stub_creds): + def test_sftpClient_list_remote_file_data_not_found( + self, mock_file_error, stub_creds + ): stub_creds["port"] = "22" sftp = _sftpClient(**stub_creds) with pytest.raises(OSError): - sftp.list_file_data("testdir") - - @pytest.mark.tmpdir - def test_sftpClient_download_file(self, tmp_path, stub_client_tmp_path, stub_creds): - stub_creds["port"] = "22" - path = tmp_path / "test" - path.mkdir() - sftp = _sftpClient(**stub_creds) - sftp.download_file(file="foo.mrc", remote_dir="bar", local_dir=str(path)) - assert "foo.mrc" in os.listdir(path) + sftp.list_remote_file_data("testdir") - def test_sftpClient_download_mock_file(self, stub_client, stub_creds): + def test_sftpClient_download_file(self, mock_ftpClient_sftpClient, stub_creds): stub_creds["port"] = "22" with does_not_raise(): sftp = _sftpClient(**stub_creds) @@ -253,7 +265,7 @@ def test_sftpClient_download_file_not_found(self, mock_file_error, stub_creds): with pytest.raises(OSError): sftp.download_file(file="foo.mrc", remote_dir="bar", local_dir="test") - def test_sftpClient_upload_file(self, stub_client, stub_creds): + def test_sftpClient_upload_file(self, mock_ftpClient_sftpClient, stub_creds): stub_creds["port"] = "22" sftp = _sftpClient(**stub_creds) file = sftp.upload_file(file="foo.mrc", local_dir="foo", remote_dir="bar") @@ -272,7 +284,7 @@ def test_ftpClient_live_test(self, live_ftp_creds): remote_dir = live_ftp_creds["remote_dir"] del live_ftp_creds["remote_dir"], live_ftp_creds["vendor"] live_ftp = _ftpClient(**live_ftp_creds) - files = live_ftp.list_file_data(remote_dir) + files = live_ftp.list_remote_file_data(remote_dir) file_names = [file.file_name for file in files] file_data = live_ftp.get_remote_file_data("Sample_Full_RDA.mrc", remote_dir) assert "Sample_Full_RDA.mrc" in file_names @@ -299,7 +311,7 @@ def test_sftpClient_live_test(self, live_sftp_creds): remote_dir = live_sftp_creds["remote_dir"] del live_sftp_creds["remote_dir"], live_sftp_creds["vendor"] live_sftp = _sftpClient(**live_sftp_creds) - files = live_sftp.list_file_data(remote_dir) + files = live_sftp.list_remote_file_data(remote_dir) file_data = live_sftp.get_remote_file_data("20049552_NYPL.mrc", remote_dir) assert datetime.datetime.fromtimestamp( files[0].file_mtime @@ -326,7 +338,7 @@ def test_sftpClient_NSDROP(self, NSDROP_creds, live_sftp_creds): if k != "remote_dir" and k != "vendor" } ev_sftp = _sftpClient(**ev_creds) - ev_files = ev_sftp.list_file_data(ev_remote_dir) + ev_files = ev_sftp.list_remote_file_data(ev_remote_dir) ev_sftp.download_file(ev_files[0].file_name, ev_remote_dir, local_test_dir) nsdrop_sftp = _sftpClient(**NSDROP_creds) nsdrop_file = nsdrop_sftp.upload_file( diff --git a/tests/test_connect.py b/tests/test_connect.py index 7255dc7..8b81606 100644 --- a/tests/test_connect.py +++ b/tests/test_connect.py @@ -20,7 +20,7 @@ class TestMockClient: ), ], ) - def test_Client(self, stub_client, stub_creds, port, client_type): + def test_Client(self, mock_Client, stub_creds, port, client_type): ( stub_creds["port"], stub_creds["remote_dir"], @@ -33,7 +33,7 @@ def test_Client(self, stub_client, stub_creds, port, client_type): assert connect.remote_dir == "testdir" assert isinstance(connect.session, client_type) - def test_Client_invalid_port(self, stub_client, stub_creds): + def test_Client_invalid_port(self, mock_Client, stub_creds): ( stub_creds["port"], stub_creds["remote_dir"], @@ -65,7 +65,7 @@ def test_Client_sftp_auth_error(self, mock_auth_error, stub_creds): "port", [21, 22], ) - def test_Client_check_file_local(self, stub_client_file_exists, stub_creds, port): + def test_Client_check_file_local(self, mock_Client_file_exists, stub_creds, port): ( stub_creds["port"], stub_creds["remote_dir"], @@ -79,7 +79,7 @@ def test_Client_check_file_local(self, stub_client_file_exists, stub_creds, port "port", [21, 22], ) - def test_Client_check_file_remote(self, stub_client_file_exists, stub_creds, port): + def test_Client_check_file_remote(self, mock_Client_file_exists, stub_creds, port): ( stub_creds["port"], stub_creds["remote_dir"], @@ -93,7 +93,7 @@ def test_Client_check_file_remote(self, stub_client_file_exists, stub_creds, por "port, dir, uid_gid", [(21, "testdir", None), (21, None, None), (22, "testdir", 0), (22, None, 0)], ) - def test_Client_get_file_data(self, stub_client, stub_creds, port, dir, uid_gid): + def test_Client_get_file_data(self, mock_Client, stub_creds, port, dir, uid_gid): ( stub_creds["port"], stub_creds["remote_dir"], @@ -134,15 +134,15 @@ def test_Client_sftp_get_file_data_not_found(self, mock_file_error, stub_creds): connect.get_file_data("foo.mrc", "testdir") @pytest.mark.parametrize("port, uid_gid", [(21, None), (22, 0)]) - def test_Client_list_files(self, stub_client, stub_creds, port, uid_gid): + def test_Client_list_files_in_dir(self, mock_Client, stub_creds, port, uid_gid): ( stub_creds["port"], stub_creds["remote_dir"], stub_creds["vendor"], ) = (port, "testdir", "test") connect = Client(**stub_creds) - all_files = connect.list_files() - recent_files = connect.list_files(time_delta=5, remote_dir="testdir") + all_files = connect.list_files_in_dir() + recent_files = connect.list_files_in_dir(time_delta=5, remote_dir="testdir") assert all_files == [ File( file_name="foo.mrc", @@ -166,7 +166,7 @@ def test_Client_list_ftp_file_not_found( ) = (21, "testdir", "test") connect = Client(**stub_creds) with pytest.raises(ftplib.error_reply): - connect.list_files() + connect.list_files_in_dir() def test_Client_list_sftp_file_not_found(self, mock_file_error, stub_creds): ( @@ -176,13 +176,13 @@ def test_Client_list_sftp_file_not_found(self, mock_file_error, stub_creds): ) = (22, "testdir", "test") connect = Client(**stub_creds) with pytest.raises(OSError): - connect.list_files() + connect.list_files_in_dir() @pytest.mark.parametrize( "port, dir", [(21, "testdir"), (21, None), (22, "testdir"), (22, None)], ) - def test_Client_get_file(self, stub_client, stub_creds, port, dir): + def test_Client_get_file(self, mock_Client, stub_creds, port, dir): ( stub_creds["port"], stub_creds["remote_dir"], @@ -220,7 +220,7 @@ def test_Client_get_file_not_found(self, mock_file_error, stub_creds, port): [21, 22], ) def test_Client_get_check_file_exists_true( - self, stub_client_file_exists, stub_creds, port + self, mock_Client_file_exists, stub_creds, port ): ( stub_creds["port"], @@ -236,7 +236,7 @@ def test_Client_get_check_file_exists_true( [(21, "testdir"), (21, None), (22, "testdir"), (22, None)], ) def test_Client_get_check_file_exists_false( - self, stub_client, stub_creds, port, dir + self, mock_Client, stub_creds, port, dir ): ( stub_creds["port"], @@ -261,7 +261,7 @@ def test_Client_get_check_file_exists_false( "port, dir, uid_gid", [(21, None, None), (21, "test", None), (22, None, 0), (22, "test", 0)], ) - def test_Client_put_file(self, stub_client, stub_creds, port, dir, uid_gid): + def test_Client_put_file(self, mock_Client, stub_creds, port, dir, uid_gid): ( stub_creds["port"], stub_creds["remote_dir"], @@ -313,7 +313,7 @@ def test_Client_put_client_error_reply( [21, 22], ) def test_Client_put_check_file_exists_true( - self, stub_client_file_exists, stub_creds, port + self, mock_Client_file_exists, stub_creds, port ): ( stub_creds["port"], @@ -331,7 +331,7 @@ def test_Client_put_check_file_exists_true( [(21, None), (22, 0)], ) def test_Client_put_check_file_exists_false( - self, stub_client, stub_creds, port, uid_gid + self, mock_Client, stub_creds, port, uid_gid ): ( stub_creds["port"], @@ -356,7 +356,7 @@ def test_Client_put_check_file_exists_false( @pytest.mark.livetest def test_Client_ftp_live_test(live_ftp_creds): live_ftp = Client(**live_ftp_creds) - files = live_ftp.list_files() + files = live_ftp.list_files_in_dir() assert len(files) > 1 assert "220" in live_ftp.session.connection.getwelcome() @@ -364,6 +364,6 @@ def test_Client_ftp_live_test(live_ftp_creds): @pytest.mark.livetest def test_Client_sftp_live_test(live_sftp_creds): live_sftp = Client(**live_sftp_creds) - files = live_sftp.list_files() + files = live_sftp.list_files_in_dir() assert len(files) > 1 assert live_sftp.session.connection.get_channel().active == 1