-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4e8053
commit a61edc6
Showing
12 changed files
with
116 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from pathlib import Path | ||
from typing import Collection, Iterable | ||
|
||
import click | ||
|
||
from monopoly.banks import auto_detect_bank | ||
|
||
|
||
def run(files: Collection[Path]): | ||
for file in files: | ||
bank = auto_detect_bank(file) | ||
statement = bank.extract() | ||
transformed_df = bank.transform(statement) | ||
bank.load(transformed_df, statement) | ||
|
||
|
||
def get_statement_paths(files: Iterable[Path]) -> set[Path]: | ||
matched_files = set() | ||
for path in files: | ||
if path.is_file() and str(path).endswith(".pdf"): | ||
matched_files.add(path) | ||
|
||
if path.is_dir(): | ||
matched_files |= get_statement_paths(path.iterdir()) | ||
|
||
return matched_files | ||
|
||
|
||
@click.command() | ||
@click.argument( | ||
"files", | ||
nargs=-1, | ||
type=click.Path(exists=True, allow_dash=True, resolve_path=True, path_type=Path), | ||
) | ||
def monopoly(files: list[Path]): | ||
""" | ||
Monopoly helps convert your bank statements from PDF to CSV. | ||
A file or directory can be passed in via the FILES argument | ||
""" | ||
if files: | ||
matched_files = get_statement_paths(files) | ||
run(matched_files) | ||
|
||
else: | ||
print("No command received") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from pathlib import Path | ||
from unittest.mock import DEFAULT, MagicMock, patch | ||
|
||
import pytest | ||
|
||
from monopoly.cli import get_statement_paths, run | ||
|
||
|
||
@pytest.fixture | ||
def test_directory() -> Path: | ||
return Path("tests/unit/test_cli").resolve() | ||
|
||
|
||
class MockBank(MagicMock): | ||
def extract(self): | ||
pass | ||
|
||
def transform(self): | ||
pass | ||
|
||
def load(self): | ||
pass | ||
|
||
|
||
def test_run(monkeypatch): | ||
def mock_auto_detect_bank(file_path: Path): | ||
assert "input.pdf" in str(file_path) | ||
return MockBank() | ||
|
||
monkeypatch.setattr("monopoly.cli.auto_detect_bank", mock_auto_detect_bank) | ||
|
||
# Mock paths | ||
files = [Path("tests/integration/banks/example/input.pdf").resolve()] | ||
|
||
with patch.multiple(MockBank, extract=DEFAULT, transform=DEFAULT, load=DEFAULT): | ||
run(files) | ||
|
||
assert isinstance(MockBank.extract, MagicMock) | ||
assert isinstance(MockBank.transform, MagicMock) | ||
assert isinstance(MockBank.load, MagicMock) | ||
|
||
# Assertions | ||
MockBank.extract.assert_called_once() | ||
MockBank.transform.assert_called_once() | ||
MockBank.load.assert_called_once() | ||
|
||
|
||
def test_get_statement_paths(test_directory: Path) -> None: | ||
path = test_directory | ||
expected = { | ||
path / "top_level.pdf", | ||
path / "top_level_2.pdf", | ||
path / "nested_directory/nested.pdf", | ||
} | ||
res = get_statement_paths(test_directory.iterdir()) | ||
assert res == expected |
Empty file.
Empty file.
Empty file.