Skip to content

Commit

Permalink
IFC-135 diff summary update (#3971)
Browse files Browse the repository at this point in the history
* better validation for DiffSummary query

* remove DiffSummaryOld
  • Loading branch information
ajtmccarty authored Jul 31, 2024
1 parent c3dd18c commit 01a3353
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 195 deletions.
2 changes: 0 additions & 2 deletions backend/infrahub/graphql/queries/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from .account import AccountToken
from .branch import BranchQueryList
from .diff.diff import DiffSummary
from .diff.old import DiffSummaryOld
from .internal import InfrahubInfo
from .ipam import InfrahubIPAddressGetNextAvailable, InfrahubIPPrefixGetNextAvailable
from .relationship import Relationship
Expand All @@ -14,7 +13,6 @@
"AccountToken",
"BranchQueryList",
"DiffSummary",
"DiffSummaryOld",
"InfrahubInfo",
"InfrahubSearchAnywhere",
"InfrahubStatus",
Expand Down
5 changes: 5 additions & 0 deletions backend/infrahub/graphql/queries/diff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from graphene import Enum as GrapheneEnum
from graphene import Interface as GrapheneInterface

from infrahub.core import registry
from infrahub.core.constants import DiffAction
from infrahub.core.diff.branch_differ import BranchDiffer
from infrahub.core.diff.model.diff import BranchDiffRelationshipMany, DiffElementType
from infrahub.core.diff.payload_builder import DiffPayloadBuilder
from infrahub.exceptions import QueryValidationError

if TYPE_CHECKING:
from graphql import GraphQLResolveInfo
Expand Down Expand Up @@ -81,6 +83,9 @@ async def resolve(
time_from: Optional[str] = None,
time_to: Optional[str] = None,
) -> list[dict[str, Union[str, list[dict[str, str]]]]]:
context: GraphqlContext = info.context
if context.branch.name == registry.default_branch and time_from is None:
raise QueryValidationError("time_from is required on default branch")
return await DiffSummaryEntry.get_summary(
info=info,
branch_only=branch_only,
Expand Down
59 changes: 0 additions & 59 deletions backend/infrahub/graphql/queries/diff/old.py

This file was deleted.

2 changes: 0 additions & 2 deletions backend/infrahub/graphql/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
AccountToken,
BranchQueryList,
DiffSummary,
DiffSummaryOld,
InfrahubInfo,
InfrahubIPAddressGetNextAvailable,
InfrahubIPPrefixGetNextAvailable,
Expand Down Expand Up @@ -90,7 +89,6 @@ class InfrahubBaseQuery(ObjectType):

DiffTree = DiffTreeQuery
DiffSummary = DiffSummary
DiffSummaryOld = DiffSummaryOld

Relationship = Relationship

Expand Down
185 changes: 53 additions & 132 deletions backend/tests/unit/graphql/test_graphql_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,138 +970,6 @@ def _check_diff_for_branch_and_id(all_dicts: list[dict], branch_name: str, id: s
return this_dict


async def test_query_diffsummary_old(db: InfrahubDatabase, default_branch: Branch, car_person_schema):
car = registry.schema.get(name="TestCar")
person = registry.schema.get(name="TestPerson")

p1_main = await Node.init(db=db, schema=person)
await p1_main.new(db=db, name="John", height=180)
await p1_main.save(db=db)
p2_main = await Node.init(db=db, schema=person)
await p2_main.new(db=db, name="Jane", height=170)
await p2_main.save(db=db)

c1_main = await Node.init(db=db, schema=car)
await c1_main.new(db=db, name="volt", nbr_seats=4, is_electric=True, owner=p1_main)
await c1_main.save(db=db)
c2_main = await Node.init(db=db, schema=car)
await c2_main.new(db=db, name="bolt", nbr_seats=4, is_electric=True, owner=p1_main)
await c2_main.save(db=db)
c3_main = await Node.init(db=db, schema=car)
await c3_main.new(db=db, name="nolt", nbr_seats=4, is_electric=True, owner=p2_main)
await c3_main.save(db=db)

branch2 = await create_branch(branch_name="branch2", db=db)
await c1_main.delete(db=db)
p1_branch2 = await NodeManager.get_one_by_id_or_default_filter(
id=p1_main.id, db=db, kind="TestPerson", branch=branch2
)
p1_branch2.name.value = "Jonathan"
await p1_branch2.save(db=db)
p2_main.name.value = "Jeanette"
await p2_main.save(db=db)
c2_main.name.value = "bolting"
await c2_main.save(db=db)
c3_branch2 = await NodeManager.get_one_by_id_or_default_filter(id=c3_main.id, db=db, kind="TestCar", branch=branch2)
await c3_branch2.owner.update(data=p1_branch2.id, db=db)
await c3_branch2.save(db=db)

query = """
query {
DiffSummaryOld {
branch
node
kind
actions
}
}
"""
gql_params = prepare_graphql_params(db=db, include_mutation=False, include_subscription=False, branch=branch2)
result = await graphql(
schema=gql_params.schema,
source=query,
context_value=gql_params.context,
root_value=None,
variable_values={},
)
assert result.errors is None
assert result.data
diff_summary = result.data["DiffSummaryOld"]
assert len(diff_summary) == 7

assert {"branch": "main", "node": c1_main.id, "kind": "TestCar", "actions": ["removed"]} in diff_summary
assert {"branch": "main", "node": c2_main.id, "kind": "TestCar", "actions": ["updated"]} in diff_summary
assert {"branch": "branch2", "node": c3_branch2.id, "kind": "TestCar", "actions": ["updated"]} in diff_summary
assert {"branch": "main", "node": p2_main.id, "kind": "TestPerson", "actions": ["updated"]} in diff_summary
assert {"branch": "branch2", "node": p1_branch2.id, "kind": "TestPerson", "actions": ["updated"]} in diff_summary


async def test_query_diffsummaryold(db: InfrahubDatabase, default_branch: Branch, car_person_schema):
car = registry.schema.get(name="TestCar")
person = registry.schema.get(name="TestPerson")

p1_main = await Node.init(db=db, schema=person)
await p1_main.new(db=db, name="John", height=180)
await p1_main.save(db=db)
p2_main = await Node.init(db=db, schema=person)
await p2_main.new(db=db, name="Jane", height=170)
await p2_main.save(db=db)

c1_main = await Node.init(db=db, schema=car)
await c1_main.new(db=db, name="volt", nbr_seats=4, is_electric=True, owner=p1_main)
await c1_main.save(db=db)
c2_main = await Node.init(db=db, schema=car)
await c2_main.new(db=db, name="bolt", nbr_seats=4, is_electric=True, owner=p1_main)
await c2_main.save(db=db)
c3_main = await Node.init(db=db, schema=car)
await c3_main.new(db=db, name="nolt", nbr_seats=4, is_electric=True, owner=p2_main)
await c3_main.save(db=db)

branch2 = await create_branch(branch_name="branch2", db=db)
await c1_main.delete(db=db)
p1_branch2 = await NodeManager.get_one_by_id_or_default_filter(
id=p1_main.id, db=db, kind="TestPerson", branch=branch2
)
p1_branch2.name.value = "Jonathan"
await p1_branch2.save(db=db)
p2_main.name.value = "Jeanette"
await p2_main.save(db=db)
c2_main.name.value = "bolting"
await c2_main.save(db=db)
c3_branch2 = await NodeManager.get_one_by_id_or_default_filter(id=c3_main.id, db=db, kind="TestCar", branch=branch2)
await c3_branch2.owner.update(data=p1_branch2.id, db=db)
await c3_branch2.save(db=db)

query = """
query {
DiffSummaryOld {
branch
node
kind
actions
}
}
"""
gql_params = prepare_graphql_params(db=db, include_mutation=False, include_subscription=False, branch=branch2)
result = await graphql(
schema=gql_params.schema,
source=query,
context_value=gql_params.context,
root_value=None,
variable_values={},
)
assert result.errors is None
assert result.data
diff_summary = result.data["DiffSummaryOld"]
assert len(diff_summary) == 7

assert {"branch": "main", "node": c1_main.id, "kind": "TestCar", "actions": ["removed"]} in diff_summary
assert {"branch": "main", "node": c2_main.id, "kind": "TestCar", "actions": ["updated"]} in diff_summary
assert {"branch": "branch2", "node": c3_branch2.id, "kind": "TestCar", "actions": ["updated"]} in diff_summary
assert {"branch": "main", "node": p2_main.id, "kind": "TestPerson", "actions": ["updated"]} in diff_summary
assert {"branch": "branch2", "node": p1_branch2.id, "kind": "TestPerson", "actions": ["updated"]} in diff_summary


async def test_query_diffsummary(db: InfrahubDatabase, default_branch: Branch, car_person_schema):
car = registry.schema.get(name="TestCar")
person = registry.schema.get(name="TestPerson")
Expand Down Expand Up @@ -1279,6 +1147,59 @@ async def test_query_diffsummary(db: InfrahubDatabase, default_branch: Branch, c
assert cars_element["peers"][0]["action"] == "ADDED"


async def test_diffsummary_on_default_branch(db: InfrahubDatabase, default_branch: Branch, car_person_schema):
person = registry.schema.get(name="TestPerson")

before_create = Timestamp()
p1 = await Node.init(db=db, schema=person)
await p1.new(db=db, name="John", height=180)
await p1.save(db=db)
p2 = await Node.init(db=db, schema=person)
await p2.new(db=db, name="Jane", height=170)
await p2.save(db=db)

query = """
query DiffSummaries($time_from: String) {
DiffSummary(time_from: $time_from) {
branch
id
kind
action
}
}
"""
gql_params = prepare_graphql_params(
db=db, include_mutation=False, include_subscription=False, branch=default_branch
)
result = await graphql(
schema=gql_params.schema,
source=query,
context_value=gql_params.context,
root_value=None,
variable_values={},
)
assert result.errors
assert len(result.errors) == 1
assert result.errors[0].message == "time_from is required on default branch"

gql_params = prepare_graphql_params(
db=db, include_mutation=False, include_subscription=False, branch=default_branch
)
result = await graphql(
schema=gql_params.schema,
source=query,
context_value=gql_params.context,
root_value=None,
variable_values={"time_from": before_create.to_string()},
)
assert result.errors is None
assert result.data
summaries = result.data["DiffSummary"]
assert len(summaries) == 2
assert {"branch": default_branch.name, "kind": "TestPerson", "id": p1.get_id(), "action": "ADDED"} in summaries
assert {"branch": default_branch.name, "kind": "TestPerson", "id": p2.get_id(), "action": "ADDED"} in summaries


async def test_query_typename(db: InfrahubDatabase, default_branch: Branch, car_person_schema):
car = registry.schema.get(name="TestCar")
person = registry.schema.get(name="TestPerson")
Expand Down

0 comments on commit 01a3353

Please sign in to comment.