Skip to content

Commit

Permalink
Test schema errors
Browse files Browse the repository at this point in the history
  • Loading branch information
manics committed Jun 19, 2024
1 parent 3cd19ea commit 3f32519
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
2 changes: 1 addition & 1 deletion aws_project_costs/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def validate(projects, raise_on_error=False):
)

for group in acc.get("project-groups", []):
if set(p["costshare"] for p in group) == {0}:
if set(p["costshare"] for p in projects["project-groups"][group]) == {0}:
errors.append(f"All projects in project-group {group} have costshare=0")

if raise_on_error and errors:
Expand Down
9 changes: 0 additions & 9 deletions tests/test_project_costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@
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_schema_validate():
config_yaml = EXAMPLE_DIR / "projects.yaml"
with (config_yaml).open() as f:
cfg = yaml.safe_load(f)
errors = validate(cfg, raise_on_error=False)
assert len(errors) == 0


def test_analyse_costs_csv(tmp_path):
config_yaml = EXAMPLE_DIR / "projects.yaml"
input_csv = EXAMPLE_DIR / "2024-01-01_2024-02-01.csv"
Expand Down
72 changes: 72 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from pathlib import Path

import pytest
import yaml

from aws_project_costs.schema import validate

EXAMPLE_DIR = Path(__file__).parent / ".." / "example"


def test_schema_validate_example():
config_yaml = EXAMPLE_DIR / "projects.yaml"
with (config_yaml).open() as f:
cfg = yaml.safe_load(f)
errors = validate(cfg, raise_on_error=False)
assert len(errors) == 0


@pytest.mark.parametrize("raise_on_error", [True, False])
def test_schema_validate_errors(raise_on_error):
config_yaml = """
proj-tag-names:
project-001: Project A
accounts:
- name: aws-auth
billing-type: shared
project-tagname: Proj
project-groups:
- tre
- non-existent
- name: aws-AUTH
billing-type: project-specific
project: x
- name: invalid-costshare
billing-type: shared
project-groups:
- invalid
project-groups:
tre:
- name: Project A
costshare: 1
- name: Project B
costshare: 0
web:
- name: Project A
costshare: 2
- name: Project C
costshare: 1
invalid:
- name: Project D
costshare: 0
"""
cfg = yaml.safe_load(config_yaml)

if raise_on_error:
with pytest.raises(ValueError) as excinfo:
validate(cfg, raise_on_error=raise_on_error)
errors = excinfo.value.args[0]
else:
errors = validate(cfg, raise_on_error=raise_on_error)

assert len(errors) == 3
expected_errors = [
"project-group non-existent in account aws-auth does not exist!",
"Multiple entries for account name=aws-auth",
"All projects in project-group invalid have costshare=0",
]
assert errors == expected_errors

0 comments on commit 3f32519

Please sign in to comment.