Skip to content

Commit

Permalink
chore: unit testing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
0x6f677548 committed Oct 25, 2023
1 parent 8d8c3ed commit e7d4f04
Show file tree
Hide file tree
Showing 11 changed files with 644 additions and 319 deletions.
50 changes: 50 additions & 0 deletions tests/cleanup_entity_cmd_test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from click.testing import CliRunner
import json
from click.core import BaseCommand
from .utils import assert_valid_output_file


def _test_cleanup_entity(test_data: list[dict], cli: BaseCommand):
runner = CliRunner()

# we might receive a list of policies or a single policy
if isinstance(test_data, list):
first_entity = test_data[0]
else:
first_entity = test_data

# check that the test data has id, createdDateTime and modifiedDateTime
assert "id" in first_entity
assert "createdDateTime" in first_entity
assert "modifiedDateTime" in first_entity

with runner.isolated_filesystem():
# write the test data to a file
test_data_file = "test_data.json"
with open(test_data_file, "w") as f:
# convert the test data to a string
f.write(json.dumps(test_data, indent=4))

assert_valid_output_file(test_data_file)

result = runner.invoke(
cli,
[
"--input_file",
test_data_file,
"--output_file",
test_data_file,
],
)
assert result is not None
assert result.exit_code == 0
assert_valid_output_file(test_data_file)

# check if the file was cleaned up, by checking if the id, createdDateTime and modifiedDateTime are gone
with open(test_data_file) as f:
data = json.load(f)

# check if the expected node is present
assert "id" not in data[0]
assert "createdDateTime" not in data[0]
assert "modifiedDateTime" not in data[0]
10 changes: 10 additions & 0 deletions tests/cleanup_groups_cmd_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from src.ca_pwt.commands import (
cleanup_groups_cmd,
)
from .utils import VALID_GROUPS
from .cleanup_entity_cmd_test_utils import _test_cleanup_entity


def test_cleanup_groups():
"""Tests if the cleanup-groups command works as expected"""
_test_cleanup_entity(VALID_GROUPS, cleanup_groups_cmd)
10 changes: 10 additions & 0 deletions tests/cleanup_policies_cmd_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from src.ca_pwt.commands import (
cleanup_policies_cmd,
)
from .utils import VALID_POLICIES
from .cleanup_entity_cmd_test_utils import _test_cleanup_entity


def test_cleanup_policies():
"""Tests if the cleanup-policies command works as expected"""
_test_cleanup_entity(VALID_POLICIES, cleanup_policies_cmd)
316 changes: 0 additions & 316 deletions tests/commands_test.py

This file was deleted.

Loading

0 comments on commit e7d4f04

Please sign in to comment.