From 456fe2f0bde324fd67c0c001c68a694af72be459 Mon Sep 17 00:00:00 2001 From: Simon Li Date: Wed, 22 May 2024 14:36:22 +0100 Subject: [PATCH] Add basic test --- .github/workflows/test.yml | 41 +++++++++++++++++++++++++++++++++++++ tests/test_project_costs.py | 26 +++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 .github/workflows/test.yml create mode 100644 tests/test_project_costs.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..fe8cb80 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,41 @@ +name: Test + +on: + pull_request: + push: + workflow_dispatch: + +jobs: + pre-commit: + runs-on: ubuntu-22.04 + timeout-minutes: 2 + + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: "3.10" + cache: pip + + # ref: https://github.com/pre-commit/action + # Run "pre-commit run --all-files" + - uses: pre-commit/action@v3.0.1 + + test: + runs-on: ubuntu-22.04 + timeout-minutes: 2 + + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: "3.10" + cache: pip + + - run: | + pip install ".[test]" + + - run: | + python -mpytest -v diff --git a/tests/test_project_costs.py b/tests/test_project_costs.py new file mode 100644 index 0000000..014effd --- /dev/null +++ b/tests/test_project_costs.py @@ -0,0 +1,26 @@ +from difflib import unified_diff +from pathlib import Path + +import yaml + +from aws_project_costs.project_costs import analyse_costs_csv + +# from aws_project_costs.schema import validate + +EXAMPLE_DIR = Path(__file__).parent / ".." / "example" + + +def test_analyse_costs_csv(tmp_path): + config_yaml = EXAMPLE_DIR / "projects.yaml" + input_csv = EXAMPLE_DIR / "2024-01-01_2024-02-01.csv" + output_csv = tmp_path / "output.csv" + reference_csv = EXAMPLE_DIR / "output.csv" + + with (config_yaml).open() as f: + cfg = yaml.safe_load(f) + analyse_costs_csv(cfg, input_csv, output_csv) + + reference = reference_csv.read_text().splitlines() + output = output_csv.read_text().splitlines() + diff = list(unified_diff(reference, output)) + assert not diff, "\n".join(diff)