Skip to content

Commit

Permalink
reordered commands, added test for new command
Browse files Browse the repository at this point in the history
  • Loading branch information
charlottekostelic committed Aug 20, 2024
1 parent 53ec554 commit 1bdacfc
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 80 deletions.
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ def write_file(self, *args, **kwargs) -> FileInfo:

@pytest.fixture
def mock_Client(monkeypatch):
def mock_file_exists(*args, **kwargs):
def mock_check_file(*args, **kwargs):
return False

def mock_session(*args, **kwargs):
return MockClient()

monkeypatch.setattr(Client, "file_exists", mock_file_exists)
monkeypatch.setattr(Client, "check_file", mock_check_file)
monkeypatch.setattr(Client, "_Client__connect_to_server", mock_session)


Expand Down
60 changes: 39 additions & 21 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,37 @@ def test_vendor_file_cli():
assert runner.get_default_prog_name(vendor_file_cli) == "vendor-file-cli"


def test_vendor_file_cli_get_files(cli_runner, caplog):
cli_runner.invoke(
def test_vendor_file_cli_get_all_vendor_files(cli_runner, caplog):
result = cli_runner.invoke(
cli=vendor_file_cli,
args=["vendor-files", "-v", "all"],
args=["all-vendor-files"],
)
assert result.exit_code == 0
assert "(NSDROP) Connected to server" in caplog.text
assert "(FOO) Connected to server" in caplog.text
assert "(FOO) Retrieving list of files in " in caplog.text
assert "(FOO) Closing client session" in caplog.text
assert "(BAR) Connected to server" in caplog.text
assert "(BAR) Retrieving list of files in " in caplog.text
assert "(BAR) Closing client session" in caplog.text
assert "(BAZ) Connected to server" in caplog.text
assert "(BAZ) Retrieving list of files in " in caplog.text
assert "(BAZ) Closing client session" in caplog.text


def test_vendor_file_cli_get_files_none(cli_runner, caplog):
def test_vendor_file_cli_get_available_vendors(cli_runner, caplog):
result = cli_runner.invoke(
cli=vendor_file_cli,
args=["vendor-files"],
args=["available-vendors"],
)
assert result.runner.get_default_prog_name(vendor_file_cli) == "vendor-file-cli"
assert "(NSDROP) Connected to server" in caplog.text
assert result.exit_code == 0
assert "Available vendors: ['FOO', 'BAR', 'BAZ']" in result.stdout


def test_vendor_file_cli_get_files_multiple_vendors(cli_runner, caplog):
def test_vendor_file_cli_get_daily_vendor_files(cli_runner, caplog):
result = cli_runner.invoke(
cli=vendor_file_cli,
args=["vendor-files", "-v", "foo", "-v", "bar", "-v", "baz"],
args=["daily-vendor-files"],
)
assert result.exit_code == 0
assert "(NSDROP) Connected to server" in caplog.text
Expand All @@ -55,10 +62,30 @@ def test_vendor_file_cli_get_files_multiple_vendors(cli_runner, caplog):
assert "(BAZ) Closing client session" in caplog.text


def test_vendor_file_cli_get_files_today(cli_runner, caplog):
def test_vendor_file_cli_get_recent_vendor_files(cli_runner, caplog):
cli_runner.invoke(
cli=vendor_file_cli,
args=["recent-vendor-files", "-v", "all"],
)
assert "(NSDROP) Connected to server" in caplog.text
assert "(FOO) Connected to server" in caplog.text
assert "(FOO) Retrieving list of files in " in caplog.text
assert "(FOO) Closing client session" in caplog.text


def test_vendor_file_cli_get_recent_vendor_files_none(cli_runner, caplog):
result = cli_runner.invoke(
cli=vendor_file_cli,
args=["daily-vendor-files"],
args=["recent-vendor-files"],
)
assert result.runner.get_default_prog_name(vendor_file_cli) == "vendor-file-cli"
assert "(NSDROP) Connected to server" in caplog.text


def test_vendor_file_cli_get_recent_vendor_files_multiple_vendors(cli_runner, caplog):
result = cli_runner.invoke(
cli=vendor_file_cli,
args=["recent-vendor-files", "-v", "foo", "-v", "bar", "-v", "baz"],
)
assert result.exit_code == 0
assert "(NSDROP) Connected to server" in caplog.text
Expand All @@ -73,17 +100,8 @@ def test_vendor_file_cli_get_files_today(cli_runner, caplog):
assert "(BAZ) Closing client session" in caplog.text


def test_vendor_file_cli_list_vendors(cli_runner, caplog):
result = cli_runner.invoke(
cli=vendor_file_cli,
args=["available-vendors"],
)
assert result.exit_code == 0
assert "Available vendors: ['FOO', 'BAR', 'BAZ']" in result.stdout


@pytest.mark.livetest
def test_vendor_file_cli_live_available_vendors():
def test_vendor_file_cli_live_get_available_vendors():
runner = CliRunner()
result = runner.invoke(
cli=vendor_file_cli,
Expand Down
10 changes: 5 additions & 5 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import pytest
from file_retriever.connect import Client
from vendor_file_cli.commands import connect, get_recent_files, load_vendor_creds
from vendor_file_cli.commands import connect, get_vendor_files, load_vendor_creds
from file_retriever.utils import logger_config


Expand All @@ -29,7 +29,7 @@ def test_connect(mock_Client, mocker):
assert client.session is not None


def test_get_recent_files(mock_Client, caplog):
def test_get_vendor_files(mock_Client, caplog):
(
os.environ["NSDROP_HOST"],
os.environ["NSDROP_USER"],
Expand All @@ -38,14 +38,14 @@ def test_get_recent_files(mock_Client, caplog):
os.environ["NSDROP_SRC"],
) = ("sftp.foo.com", "foo", "bar", "22", "foo_src")
vendors = ["foo"]
get_recent_files(vendors=vendors, days=300)
get_vendor_files(vendors=vendors, days=300)
assert "(NSDROP) Connected to server" in caplog.text
assert "(FOO) Connected to server" in caplog.text
assert "(FOO) Retrieving list of files in " in caplog.text
assert "(FOO) Closing client session" in caplog.text


def test_get_recent_files_no_files(mock_Client, caplog):
def test_get_vendor_files_no_files(mock_Client, caplog):
(
os.environ["NSDROP_HOST"],
os.environ["NSDROP_USER"],
Expand All @@ -54,7 +54,7 @@ def test_get_recent_files_no_files(mock_Client, caplog):
os.environ["NSDROP_SRC"],
) = ("sftp.foo.com", "foo", "bar", "22", "foo_src")
vendors = ["foo"]
get_recent_files(vendors=vendors, days=1, hours=1, minutes=1)
get_vendor_files(vendors=vendors, days=1, hours=1, minutes=1)
assert "(NSDROP) Connected to server" in caplog.text
assert "(FOO) Connected to server" in caplog.text
assert "(FOO) Retrieving list of files in " in caplog.text
Expand Down
104 changes: 52 additions & 52 deletions vendor_file_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,57 @@ def vendor_file_cli() -> None:
pass


@vendor_file_cli.command(
"all-vendor-files",
short_help="Retrieve all vendor files that are not in NSDROP.",
)
def get_all_vendor_files() -> None:
"""
Retrieve all files from vendor server that are not in vendor's NSDROP directory.
Args:
None
"""
vendor_list = load_vendor_creds(
os.path.join(os.environ["USERPROFILE"], ".cred/.sftp/connections.yaml")
)
get_vendor_files(vendors=vendor_list)


@vendor_file_cli.command("available-vendors", short_help="List all configured vendors.")
def get_available_vendors() -> None:
"""
List all configured vendors.
Args:
ctx: click context object that contains a list of vendor names
"""
vendor_list = load_vendor_creds(
os.path.join(os.environ["USERPROFILE"], ".cred/.sftp/connections.yaml")
)
click.echo(f"Available vendors: {vendor_list}")


@vendor_file_cli.command(
"daily-vendor-files",
short_help="Retrieve files created in last day for all vendors",
)
def get_daily_vendor_files() -> None:
"""
Retrieve files updated within last day from remote server for all vendor(s).
Args:
ctx: click context object that contains a list of vendor names
"""
vendor_list = load_vendor_creds(
os.path.join(os.environ["USERPROFILE"], ".cred/.sftp/connections.yaml")
)
get_vendor_files(vendors=vendor_list, days=1)


@vendor_file_cli.command(
"recent-vendor-files",
short_help="Retrieve files from remote server based on timedelta.",
Expand Down Expand Up @@ -58,7 +109,7 @@ def vendor_file_cli() -> None:
type=int,
help="How many minutes back to retrieve files.",
)
def get_files(vendor: str, days: int, hours: int, minutes: int) -> None:
def get_recent_vendor_files(vendor: str, days: int, hours: int, minutes: int) -> None:
"""
Retrieve files from remote server for specified vendor(s).
Expand Down Expand Up @@ -86,56 +137,5 @@ def get_files(vendor: str, days: int, hours: int, minutes: int) -> None:
get_vendor_files(vendors=vendor_list, days=days, hours=hours, minutes=minutes)


@vendor_file_cli.command(
"daily-vendor-files",
short_help="Retrieve files created in last day for all vendors",
)
def get_files_today() -> None:
"""
Retrieve files updated within last day from remote server for all vendor(s).
Args:
ctx: click context object that contains a list of vendor names
"""
vendor_list = load_vendor_creds(
os.path.join(os.environ["USERPROFILE"], ".cred/.sftp/connections.yaml")
)
get_vendor_files(vendors=vendor_list, days=1)


@vendor_file_cli.command(
"all-vendor-files",
short_help="Retrieve all vendor files that are not in NSDROP.",
)
def get_all_vendor_files() -> None:
"""
Retrieve all files from vendor server that are not in vendor's NSDROP directory.
Args:
None
"""
vendor_list = load_vendor_creds(
os.path.join(os.environ["USERPROFILE"], ".cred/.sftp/connections.yaml")
)
get_vendor_files(vendors=vendor_list)


@vendor_file_cli.command("available-vendors", short_help="List all configured vendors.")
def list_vendors() -> None:
"""
List all configured vendors.
Args:
ctx: click context object that contains a list of vendor names
"""
vendor_list = load_vendor_creds(
os.path.join(os.environ["USERPROFILE"], ".cred/.sftp/connections.yaml")
)
click.echo(f"Available vendors: {vendor_list}")


def main():
vendor_file_cli()

0 comments on commit 1bdacfc

Please sign in to comment.