Skip to content

Commit

Permalink
Add click and rich
Browse files Browse the repository at this point in the history
  • Loading branch information
vmagueta committed Jun 27, 2024
1 parent 7aa2428 commit e598115
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 35 deletions.
1 change: 1 addition & 0 deletions assets/people.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Jim Halpert, Sales, Salesman, [email protected]
Dwight Schrute, Sales, Manager, [email protected]
Gabe Lewis, Directory, Manager, [email protected]
62 changes: 43 additions & 19 deletions dundie/cli.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
import argparse
from importlib import metadata

from dundie.core import load # noqa
import rich_click as click
from rich.console import Console
from rich.table import Table

from dundie import core

click.rich_click.USE_RICH_MARKUP = True
click.rich_click.USE_MARKDOWN = True
click.rich_click.SHOW_ARGUMENTS = True
click.rich_click.GRUOP_ARGUMENTS_OPTIONS = True
click.rich_click.SHOW_METAVARS_COLUMN = False
click.rich_click.APPEND_METAVARS_HELP = True


@click.group()
@click.version_option(metadata.version("dundie"))
def main():
parser = argparse.ArgumentParser(
description="Dunder Mifflin Rewards CLI",
epilog="Enjoy and use with cautious.",
)
parser.add_argument(
"subcommand",
type=str,
help="The subcommand to run",
choices=("load", "show", "send"),
default="help",
)
parser.add_argument(
"filepath", type=str, help="Filepath to load", default=None
)
args = parser.parse_args()

print(*globals()[args.subcommand](args.filepath))
"""Dunder Mifflin Rewards System.
This CLI application controls Dunder Mifflin Rewards.
"""


@main.command()
@click.argument("filepath", type=click.Path(exists=True))
def load(filepath):
"""Loads the file to the database.
## Features
- Validates data
- Parses the file
- Loads to database
"""
table = Table(title="Dunder Mifflin Associates")
headers = ["name", "dept", "role", "e-mail"]
for header in headers:
table.add_column(header, style="italic cyan1")

result = core.load(filepath)
for person in result:
table.add_row(*[field.strip() for field in person.split(",")])

console = Console()
console.print(table)
1 change: 1 addition & 0 deletions integration/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PEOPLE_FILE = "tests/assets/people.csv"
26 changes: 12 additions & 14 deletions integration/test_load.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
from subprocess import CalledProcessError, check_output

import pytest
from click.testing import CliRunner

from dundie.cli import load, main

from .constants import PEOPLE_FILE

cmd = CliRunner()


@pytest.mark.integration
@pytest.mark.medium
def test_load_positive_call_load_command():
"""Test command load"""
out = (
check_output(["dundie", "load", "assets/people.csv"])
.decode("utf-8")
.split("\n")
)
assert len(out) == 2
out = cmd.invoke(load, PEOPLE_FILE)
assert "Dunder Mifflin Associates" in out.output


@pytest.mark.integration
@pytest.mark.medium
@pytest.mark.parametrize("wrong_command", ["loady", "carrega", "start"])
def test_load_negative_call_load_command_with_wrong_params(wrong_command):
"""Test command load"""
with pytest.raises(CalledProcessError) as error:
check_output(["dundie", wrong_command, "assets/people.csv"]).decode(
"utf-8"
).split("\n")

assert "status 2" in str(error.getrepr())
out = cmd.invoke(main, wrong_command, PEOPLE_FILE)
assert out.exit_code != 0
assert f"No such command '{wrong_command}'" in out.output
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
click
rich
rich-click
4 changes: 2 additions & 2 deletions tests/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

@pytest.mark.unit
@pytest.mark.high
def test_load_positive_has_2_people(request):
"""Test load function has 2 people."""
def test_load_positive_has_3_people(request):
"""Test load function has 3 people."""
assert len(load(PEOPLE_FILE)) == 3


Expand Down

0 comments on commit e598115

Please sign in to comment.