Skip to content

Commit

Permalink
make diff responses consistent with NULL vs being empty (#4242)
Browse files Browse the repository at this point in the history
* make diff responses consistent with NULL vs being empty

* mypy fix
  • Loading branch information
ajtmccarty authored Sep 2, 2024
1 parent 56abac7 commit 37955f7
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
7 changes: 5 additions & 2 deletions backend/infrahub/core/diff/query/diff_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,13 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
"SUM(diff_node.num_conflicts) as num_conflicts",
"min(diff_root.from_time) as from_time",
"max(diff_root.to_time) as to_time",
"count(diff_root) as num_roots",
]

def get_summary(self) -> DiffSummaryCounters:
def get_summary(self) -> DiffSummaryCounters | None:
result = self.get_result()
if not result:
return DiffSummaryCounters(from_time=self.from_time, to_time=self.to_time)
return None
if result.get("num_roots") == 0:
return None
return DiffSummaryCounters.from_graph(result)
2 changes: 1 addition & 1 deletion backend/infrahub/core/diff/repository/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async def summary(
from_time: Timestamp,
to_time: Timestamp,
filters: dict | None = None,
) -> DiffSummaryCounters:
) -> DiffSummaryCounters | None:
query = await DiffSummaryQuery.init(
db=self.db,
base_branch_name=base_branch_name,
Expand Down
3 changes: 3 additions & 0 deletions backend/infrahub/graphql/queries/diff/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ async def resolve(
include_parents=include_parents,
limit=limit,
offset=offset,
include_empty=True,
)
if not enriched_diffs:
return None
Expand Down Expand Up @@ -454,6 +455,8 @@ async def summary(
to_time=to_timestamp,
filters=filters_dict,
)
if summary is None:
return None

diff_tree_summary = DiffTreeSummary(
base_branch=base_branch.name,
Expand Down
90 changes: 89 additions & 1 deletion backend/tests/unit/graphql/test_diff_tree_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,43 @@ async def diff_coordinator(db: InfrahubDatabase, diff_branch: Branch) -> DiffCoo
return coordinator


async def test_diff_tree_empty_diff(
async def test_diff_tree_no_changes(
db: InfrahubDatabase,
default_branch: Branch,
criticality_low,
diff_coordinator: DiffCoordinator,
diff_branch: Branch,
):
enriched_diff = await diff_coordinator.update_branch_diff(base_branch=default_branch, diff_branch=diff_branch)
from_time = datetime.fromisoformat(diff_branch.created_at)
to_time = datetime.fromisoformat(enriched_diff.to_time.to_string())

params = prepare_graphql_params(db=db, include_mutation=False, include_subscription=False, branch=default_branch)
result = await graphql(
schema=params.schema,
source=DIFF_TREE_QUERY,
context_value=params.context,
root_value=None,
variable_values={"branch": diff_branch.name},
)

assert result.errors is None
assert result.data["DiffTree"] == {
"base_branch": default_branch.name,
"diff_branch": diff_branch.name,
"from_time": from_time.isoformat(),
"to_time": to_time.isoformat(),
"num_added": 0,
"num_removed": 0,
"num_updated": 0,
"num_conflicts": 0,
"num_untracked_base_changes": 0,
"num_untracked_diff_changes": 0,
"nodes": [],
}


async def test_diff_tree_no_diffs(
db: InfrahubDatabase, default_branch: Branch, criticality_schema: NodeSchema, diff_branch: Branch
):
params = prepare_graphql_params(db=db, include_mutation=False, include_subscription=False, branch=default_branch)
Expand Down Expand Up @@ -636,6 +672,58 @@ async def test_diff_tree_hierarchy_change(
assert nodes_parent == expected_nodes_parent


async def test_diff_tree_summary_no_diffs(
db: InfrahubDatabase, default_branch: Branch, criticality_schema: NodeSchema, diff_branch: Branch
):
params = prepare_graphql_params(db=db, include_mutation=False, include_subscription=False, branch=default_branch)
result = await graphql(
schema=params.schema,
source=DIFF_TREE_QUERY_SUMMARY,
context_value=params.context,
root_value=None,
variable_values={"branch": diff_branch.name},
)

assert result.errors is None
assert result.data["DiffTreeSummary"] is None


async def test_diff_tree_summary_no_changes(
db: InfrahubDatabase,
default_branch: Branch,
criticality_low,
diff_coordinator: DiffCoordinator,
diff_branch: Branch,
):
enriched_diff = await diff_coordinator.update_branch_diff(base_branch=default_branch, diff_branch=diff_branch)
from_time = datetime.fromisoformat(diff_branch.created_at)
to_time = datetime.fromisoformat(enriched_diff.to_time.to_string())

params = prepare_graphql_params(db=db, include_mutation=False, include_subscription=False, branch=default_branch)
result = await graphql(
schema=params.schema,
source=DIFF_TREE_QUERY_SUMMARY,
context_value=params.context,
root_value=None,
variable_values={"branch": diff_branch.name},
)

assert result.errors is None
assert result.data["DiffTreeSummary"] == {
"base_branch": default_branch.name,
"diff_branch": diff_branch.name,
"from_time": from_time.isoformat(),
"to_time": to_time.isoformat(),
"num_added": 0,
"num_removed": 0,
"num_updated": 0,
"num_unchanged": 0,
"num_conflicts": 0,
"num_untracked_base_changes": 0,
"num_untracked_diff_changes": 0,
}


@pytest.mark.parametrize(
"filters,counters",
[
Expand Down

0 comments on commit 37955f7

Please sign in to comment.