Skip to content

Commit 91a70ba

Browse files
iddocohendummyuser
authored andcommitted
Tighten branch time helpers to require Timestamp inputs (#7608)
* Tighten branch query helpers to Timestamp inputs and is related to issue #63. API tightening on get_branches_and_times_to_query[_global], with tests adjusted accordingly. * refactor: require Timestamp inputs for branch range filters. This captures that both get_query_filter_range and get_query_filter_relationships_range now enforce Timestamp arguments only. * refactor: make Branch.get_query_filter_relationships Timestamp-only * Altered spacing and added towncrier notes as well
1 parent 2740532 commit 91a70ba

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

backend/infrahub/core/branch/models.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def get_branches_in_scope(self) -> list[str]:
206206

207207
return [default_branch, self.name]
208208

209-
def get_branches_and_times_to_query(self, at: Optional[Union[Timestamp, str]] = None) -> dict[frozenset, str]:
209+
def get_branches_and_times_to_query(self, at: Optional[Timestamp] = None) -> dict[frozenset, str]:
210210
"""Return all the names of the branches that are constituing this branch with the associated times excluding the global branch"""
211211

212212
at = Timestamp(at)
@@ -227,7 +227,7 @@ def get_branches_and_times_to_query(self, at: Optional[Union[Timestamp, str]] =
227227

228228
def get_branches_and_times_to_query_global(
229229
self,
230-
at: Optional[Union[Timestamp, str]] = None,
230+
at: Optional[Timestamp] = None,
231231
is_isolated: bool = True,
232232
) -> dict[frozenset, str]:
233233
"""Return all the names of the branches that are constituting this branch with the associated times."""
@@ -297,7 +297,7 @@ async def delete(self, db: InfrahubDatabase) -> None:
297297
await super().delete(db=db)
298298

299299
def get_query_filter_relationships(
300-
self, rel_labels: list, at: Optional[Union[Timestamp, str]] = None, include_outside_parentheses: bool = False
300+
self, rel_labels: list, at: Optional[Timestamp] = None, include_outside_parentheses: bool = False
301301
) -> tuple[list, dict]:
302302
"""
303303
Generate a CYPHER Query filter based on a list of relationships to query a part of the graph at a specific time and on a specific branch.
@@ -360,7 +360,7 @@ def get_query_filter_path(
360360
params[f"{pp}time1"] = at_str
361361
return filter_str, params
362362

363-
branches_times = self.get_branches_and_times_to_query_global(at=at_str, is_isolated=is_isolated)
363+
branches_times = self.get_branches_and_times_to_query_global(at=at, is_isolated=is_isolated)
364364

365365
for idx, (branch_name, time_to_query) in enumerate(branches_times.items()):
366366
params[f"{pp}branch{idx}"] = list(branch_name)
@@ -382,8 +382,8 @@ def get_query_filter_path(
382382
def get_query_filter_relationships_range(
383383
self,
384384
rel_labels: list,
385-
start_time: Union[Timestamp, str],
386-
end_time: Union[Timestamp, str],
385+
start_time: Timestamp,
386+
end_time: Timestamp,
387387
include_outside_parentheses: bool = False,
388388
include_global: bool = False,
389389
) -> tuple[list, dict]:
@@ -463,9 +463,7 @@ def get_query_filter_relationships_diff(
463463

464464
return filters, params
465465

466-
def get_query_filter_range(
467-
self, rel_label: list, start_time: Union[Timestamp, str], end_time: Union[Timestamp, str]
468-
) -> tuple[list, dict]:
466+
def get_query_filter_range(self, rel_label: list, start_time: Timestamp, end_time: Timestamp) -> tuple[list, dict]:
469467
"""
470468
Generate a CYPHER Query filter to query a range of values in the graph between start_time and end_time."""
471469

backend/infrahub/core/query/relationship.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs) -> None: # noqa: ARG
871871
self.params["branch"] = self.branch.name
872872

873873
rels_filter, rels_params = self.branch.get_query_filter_relationships(
874-
rel_labels=["r1", "r2"], at=self.at.to_string(), include_outside_parentheses=True
874+
rel_labels=["r1", "r2"], at=self.at, include_outside_parentheses=True
875875
)
876876

877877
self.params.update(rels_params)
@@ -954,7 +954,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs) -> None: # noqa: ARG
954954
self.params["at"] = self.at.to_string()
955955

956956
rels_filter, rels_params = self.branch.get_query_filter_relationships(
957-
rel_labels=["r1", "r2"], at=self.at.to_string(), include_outside_parentheses=True
957+
rel_labels=["r1", "r2"], at=self.at, include_outside_parentheses=True
958958
)
959959
self.params.update(rels_params)
960960

backend/tests/unit/core/test_branch.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ async def test_get_query_filter_relationships_main(db: InfrahubDatabase, base_da
120120
default_branch = await registry.get_branch(branch="main", db=db)
121121

122122
filters, params = default_branch.get_query_filter_relationships(
123-
rel_labels=["r1", "r2"], at=Timestamp().to_string(), include_outside_parentheses=False
123+
rel_labels=["r1", "r2"], at=Timestamp(), include_outside_parentheses=False
124124
)
125125

126126
expected_filters = [
@@ -139,7 +139,7 @@ async def test_get_query_filter_relationships_branch1(db: InfrahubDatabase, base
139139
branch1 = await registry.get_branch(branch="branch1", db=db)
140140

141141
filters, params = branch1.get_query_filter_relationships(
142-
rel_labels=["r1", "r2"], at=Timestamp().to_string(), include_outside_parentheses=False
142+
rel_labels=["r1", "r2"], at=Timestamp(), include_outside_parentheses=False
143143
)
144144

145145
assert isinstance(filters, list)
@@ -157,7 +157,7 @@ async def test_get_branches_and_times_to_query_main(db: InfrahubDatabase, base_d
157157
assert Timestamp(results[frozenset(["main"])]) > now
158158

159159
t1 = Timestamp("2s")
160-
results = main_branch.get_branches_and_times_to_query(at=t1.to_string())
160+
results = main_branch.get_branches_and_times_to_query(at=t1)
161161
assert results[frozenset(["main"])] == t1.to_string()
162162

163163

@@ -186,7 +186,7 @@ async def test_get_branches_and_times_to_query_global_main(db: InfrahubDatabase,
186186
assert Timestamp(results[frozenset((GLOBAL_BRANCH_NAME, "main"))]) > now
187187

188188
t1 = Timestamp("2s")
189-
results = main_branch.get_branches_and_times_to_query_global(at=t1.to_string())
189+
results = main_branch.get_branches_and_times_to_query_global(at=t1)
190190
assert results[frozenset((GLOBAL_BRANCH_NAME, "main"))] == t1.to_string()
191191

192192

@@ -201,7 +201,7 @@ async def test_get_branches_and_times_to_query_global_branch1(db: InfrahubDataba
201201
assert results[frozenset((GLOBAL_BRANCH_NAME, "main"))] == base_dataset_02["time_m45"]
202202

203203
t1 = Timestamp("2s")
204-
results = branch1.get_branches_and_times_to_query_global(at=t1.to_string())
204+
results = branch1.get_branches_and_times_to_query_global(at=t1)
205205
assert results[frozenset((GLOBAL_BRANCH_NAME, "branch1"))] == t1.to_string()
206206
assert results[frozenset((GLOBAL_BRANCH_NAME, "main"))] == base_dataset_02["time_m45"]
207207

changelog/63.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This refactors timestamp handling across query and relationship functions to accept only Timestamp types, updates all dependent code and tests accordingly, refreshes development environment files, and includes no user-facing changes.

0 commit comments

Comments
 (0)