Skip to content

Commit f7b8e63

Browse files
committed
Replace pendulum with whenever
1 parent 058e0c4 commit f7b8e63

File tree

14 files changed

+234
-152
lines changed

14 files changed

+234
-152
lines changed

backend/infrahub/core/diff/model/path.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from neo4j.graph import Node as Neo4jNode
2121
from neo4j.graph import Path as Neo4jPath
2222
from neo4j.graph import Relationship as Neo4jRelationship
23-
from pendulum import Interval
23+
from whenever import TimeDelta
2424

2525
from infrahub.graphql.initialization import GraphqlContext
2626

@@ -417,7 +417,7 @@ def __hash__(self) -> int:
417417
return hash(self.uuid)
418418

419419
@property
420-
def time_range(self) -> Interval:
420+
def time_range(self) -> TimeDelta:
421421
return self.to_time.obj - self.from_time.obj
422422

423423
def update_metadata(
@@ -447,7 +447,7 @@ def __hash__(self) -> int:
447447
return hash(self.uuid)
448448

449449
@property
450-
def time_range(self) -> Interval:
450+
def time_range(self) -> TimeDelta:
451451
return self.to_time.obj - self.from_time.obj
452452

453453
def get_nodes_without_parents(self) -> set[EnrichedDiffNode]:

backend/infrahub/core/timestamp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
from infrahub_sdk.timestamp import Timestamp as BaseTimestamp
66

77
if TYPE_CHECKING:
8-
from pendulum.datetime import DateTime
8+
from whenever import ZonedDateTime
99

1010

1111
class Timestamp(BaseTimestamp):
12-
async def to_graphql(self, *args: Any, **kwargs: Any) -> DateTime: # noqa: ARG002
12+
async def to_graphql(self, *args: Any, **kwargs: Any) -> ZonedDateTime: # noqa: ARG002
1313
return self.obj
1414

1515
def get_query_filter_path(self, rel_name: str = "r") -> tuple[str, dict]:

backend/tests/integration/user_workflows/test_user_worflow.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import pendulum
21
import pytest
32
from deepdiff import DeepDiff
3+
from whenever import Instant
44

55
from infrahub.database import InfrahubDatabase
66
from infrahub.graphql.manager import GraphQLSchemaManager
@@ -251,7 +251,7 @@ async def test_query_all_devices(self, test_client, integration_helper):
251251
if device["node"]["name"]["value"] == "spine1":
252252
state.data["spine1_id"] = device["node"]["id"]
253253
# Initialize the start time
254-
state.data["time_start"] = pendulum.now(tz="UTC")
254+
state.data["time_start"] = Instant.now()
255255

256256
async def test_query_spine1_loobpack0(self, test_client, integration_helper):
257257
"""
@@ -374,7 +374,7 @@ async def test_update_intf_description_branch1(
374374

375375
assert intfs[0]["node"]["description"]["value"] == new_description
376376

377-
state.data["time_after_intf_update_branch1"] = pendulum.now("UTC").to_iso8601_string()
377+
state.data["time_after_intf_update_branch1"] = Instant.now().format_common_iso()
378378

379379
async def test_update_intf_description_main(self, test_client, integration_helper):
380380
"""
@@ -781,7 +781,7 @@ async def test_query_spine1_lo0_at_start_time(self, test_client, integration_hel
781781
"intf_name": intf_name,
782782
},
783783
},
784-
params={"at": state.data["time_start"].to_iso8601_string()},
784+
params={"at": state.data["time_start"].format_common_iso()},
785785
headers=headers,
786786
)
787787
assert response.status_code == 200

backend/tests/unit/api/conftest.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import pendulum
21
import pytest
32
from fastapi.testclient import TestClient
43

@@ -7,6 +6,7 @@
76
from infrahub.core.initialization import create_branch
87
from infrahub.core.manager import NodeManager
98
from infrahub.core.node import Node
9+
from infrahub.core.timestamp import Timestamp
1010
from infrahub.database import InfrahubDatabase
1111
from infrahub.services.adapters.workflow.local import WorkflowLocalExecution
1212
from infrahub.workflows.initialization import setup_task_manager
@@ -140,7 +140,7 @@ async def car_person_data_diff(db: InfrahubDatabase, default_branch, car_person_
140140
branch2 = await create_branch(branch_name="branch2", db=db)
141141

142142
# Time post Branch Creation
143-
time0 = pendulum.now(tz="UTC")
143+
time0 = Timestamp()
144144

145145
persons_list = await NodeManager.query(db=db, schema="Person", branch=branch2)
146146
persons = {item.name.value: item for item in persons_list}
@@ -166,7 +166,7 @@ async def car_person_data_diff(db: InfrahubDatabase, default_branch, car_person_
166166
await p1.save(db=db)
167167

168168
# Time in-between the 2 batch of changes
169-
time1 = pendulum.now(tz="UTC")
169+
time1 = Timestamp()
170170

171171
# Update Repo 01 in Branch2
172172
repo01 = repos["repo01"]
@@ -181,7 +181,7 @@ async def car_person_data_diff(db: InfrahubDatabase, default_branch, car_person_
181181
await cars_main["bolt"].save(db=db)
182182

183183
# Time After the changes
184-
time2 = pendulum.now(tz="UTC")
184+
time2 = Timestamp()
185185

186186
params = {
187187
"branch": branch2,
@@ -198,7 +198,7 @@ async def car_person_data_generic_diff(db: InfrahubDatabase, default_branch, car
198198
branch2 = await create_branch(branch_name="branch2", db=db)
199199

200200
# Time After Creation of branch2
201-
time0 = pendulum.now(tz="UTC")
201+
time0 = Timestamp()
202202

203203
persons_list = await NodeManager.query(db=db, schema="TestPerson", branch=branch2)
204204
persons = {item.name.value: item for item in persons_list}
@@ -213,34 +213,34 @@ async def car_person_data_generic_diff(db: InfrahubDatabase, default_branch, car
213213
gcars = {item.name.value: item for item in gcars_list}
214214

215215
# Add a new Person P3 in Branch2 and assign him as the owner of C1
216-
time10 = pendulum.now(tz="UTC")
216+
time10 = Timestamp()
217217
p3 = await Node.init(db=db, schema="TestPerson", branch=branch2)
218218
await p3.new(db=db, name="Bill", height=160)
219219
await p3.save(db=db, at=time10)
220220
persons["Bill"] = p3
221221

222-
time11 = pendulum.now(tz="UTC")
222+
time11 = Timestamp()
223223
await ecars["volt"].owner.update(data=p3, db=db)
224224
await ecars["volt"].save(db=db, at=time11)
225225

226226
# Update Repo 01 in Branch2 a first time
227-
time12 = pendulum.now(tz="UTC")
227+
time12 = Timestamp()
228228
repo01 = repos["repo01"]
229229
repo01.commit.value = "bbbbbbbbbbbbbbb"
230230
repo01.description.value = "First change in branch"
231231
await repo01.save(db=db, at=time12)
232232

233233
# Update P1 height in main
234-
time13 = pendulum.now(tz="UTC")
234+
time13 = Timestamp()
235235
p1 = await NodeManager.get_one(id=persons["John"].id, db=db)
236236
p1.height.value = 120
237237
await p1.save(db=db, at=time13)
238238

239239
# Time in-between the 2 batch of changes
240-
time20 = pendulum.now(tz="UTC")
240+
time20 = Timestamp()
241241

242242
# Update Repo 01 in Branch2 a second time
243-
time21 = pendulum.now(tz="UTC")
243+
time21 = Timestamp()
244244
repo01 = repos["repo01"]
245245
repo01.commit.value = "dddddddddd"
246246
repo01.description.value = "Second change in branch"
@@ -257,7 +257,7 @@ async def car_person_data_generic_diff(db: InfrahubDatabase, default_branch, car
257257
await ecars_main["bolt"].save(db=db)
258258

259259
# Time After the changes
260-
time30 = pendulum.now(tz="UTC")
260+
time30 = Timestamp()
261261

262262
params = {
263263
"branch": branch2,
@@ -414,7 +414,7 @@ async def data_diff_attribute(db: InfrahubDatabase, default_branch, car_person_d
414414
branch2 = await create_branch(branch_name="branch2", db=db)
415415

416416
# Time After Creation of branch2
417-
time0 = pendulum.now(tz="UTC")
417+
time0 = Timestamp()
418418

419419
persons_list = await NodeManager.query(db=db, schema="TestPerson", branch=branch2)
420420
persons = {item.name.value: item for item in persons_list}
@@ -429,23 +429,23 @@ async def data_diff_attribute(db: InfrahubDatabase, default_branch, car_person_d
429429
gcars = {item.name.value: item for item in gcars_list}
430430

431431
# Update Repo 01 in Branch2 a first time
432-
time12 = pendulum.now(tz="UTC")
432+
time12 = Timestamp()
433433
repo01 = repos["repo01"]
434434
repo01.commit.value = "bbbbbbbbbbbbbbb"
435435
repo01.description.value = "First update in Branch"
436436
await repo01.save(db=db, at=time12)
437437

438438
# Update P1 height in main
439-
time13 = pendulum.now(tz="UTC")
439+
time13 = Timestamp()
440440
p1 = await NodeManager.get_one(id=persons["John"].id, db=db)
441441
p1.height.value = 120
442442
await p1.save(db=db, at=time13)
443443

444444
# Time in-between the 2 batch of changes
445-
time20 = pendulum.now(tz="UTC")
445+
time20 = Timestamp()
446446

447447
# Update Repo 01 in Branch2 a second time
448-
time21 = pendulum.now(tz="UTC")
448+
time21 = Timestamp()
449449
repo01 = repos["repo01"]
450450
repo01.commit.value = "dddddddddd"
451451
repo01.description.value = "Second update in Branch"
@@ -459,7 +459,7 @@ async def data_diff_attribute(db: InfrahubDatabase, default_branch, car_person_d
459459
await ecars_main["bolt"].save(db=db)
460460

461461
# Time After the changes
462-
time30 = pendulum.now(tz="UTC")
462+
time30 = Timestamp()
463463

464464
params = {
465465
"branch": branch2,
@@ -486,7 +486,7 @@ async def data_conflict_attribute(db: InfrahubDatabase, default_branch, car_pers
486486
branch2 = await create_branch(branch_name="branch2", db=db)
487487

488488
# Time After Creation of branch2
489-
time0 = pendulum.now(tz="UTC")
489+
time0 = Timestamp()
490490

491491
persons_list_branch = await NodeManager.query(db=db, schema="TestPerson", branch=branch2)
492492
persons_branch = {item.name.value: item for item in persons_list_branch}
@@ -501,13 +501,13 @@ async def data_conflict_attribute(db: InfrahubDatabase, default_branch, car_pers
501501
repos_main = {item.name.value: item for item in repos_list_main}
502502

503503
# Update Repo 01 in Branch2 a first time
504-
time12 = pendulum.now(tz="UTC")
504+
time12 = Timestamp()
505505
repos_branch["repo01"].commit.value = "bbbbbbbbbbbbbbb"
506506
repos_branch["repo01"].description.value = "First update in Branch"
507507
await repos_branch["repo01"].save(db=db, at=time12)
508508

509509
# Update P1 height in branch2
510-
time13 = pendulum.now(tz="UTC")
510+
time13 = Timestamp()
511511
persons_branch["John"].height.value = 666
512512
await persons_branch["John"].save(db=db, at=time13)
513513

@@ -516,22 +516,22 @@ async def data_conflict_attribute(db: InfrahubDatabase, default_branch, car_pers
516516
await persons_main["John"].save(db=db, at=time13)
517517

518518
# Time in-between the 2 batch of changes
519-
time20 = pendulum.now(tz="UTC")
519+
time20 = Timestamp()
520520

521521
# Update Repo 01 in Branch2 a second time
522-
time21 = pendulum.now(tz="UTC")
522+
time21 = Timestamp()
523523
repos_branch["repo01"].commit.value = "dddddddddd"
524524
repos_branch["repo01"].description.value = "Second update in Branch"
525525
await repos_branch["repo01"].save(db=db, at=time21)
526526

527527
# Update Repo 01 in main
528-
time22 = pendulum.now(tz="UTC")
528+
time22 = Timestamp()
529529
repos_main["repo01"].commit.value = "mmmmmmmmmmmmm"
530530
repos_main["repo01"].description.value = "update in main"
531531
await repos_main["repo01"].save(db=db, at=time12)
532532

533533
# Time After the changes
534-
time30 = pendulum.now(tz="UTC")
534+
time30 = Timestamp()
535535

536536
params = {
537537
"branch": branch2,
@@ -553,7 +553,7 @@ async def data_conflict_attribute(db: InfrahubDatabase, default_branch, car_pers
553553
@pytest.fixture
554554
async def data_diff_relationship_one(db: InfrahubDatabase, default_branch, car_person_data_generic, first_account):
555555
# Set some values in C1 in Main before creating the branch
556-
time_minus1 = pendulum.now(tz="UTC")
556+
time_minus1 = Timestamp()
557557

558558
c1_main = await NodeManager.get_one_by_id_or_default_filter(
559559
db=db, id="volt", kind="TestElectricCar", branch=default_branch
@@ -569,7 +569,7 @@ async def data_diff_relationship_one(db: InfrahubDatabase, default_branch, car_p
569569
branch2 = await create_branch(branch_name="branch2", db=db)
570570

571571
# Time After Creation of branch2
572-
time0 = pendulum.now(tz="UTC")
572+
time0 = Timestamp()
573573

574574
persons_list = await NodeManager.query(db=db, schema="TestPerson", branch=branch2)
575575
persons = {item.name.value: item for item in persons_list}
@@ -578,19 +578,19 @@ async def data_diff_relationship_one(db: InfrahubDatabase, default_branch, car_p
578578
ecars = {item.name.value: item for item in ecars_list}
579579

580580
# Change previous owner of C1 from P1 to P2 in branch
581-
time11 = pendulum.now(tz="UTC")
581+
time11 = Timestamp()
582582
await ecars["volt"].previous_owner.update(data=persons["John"], db=db)
583583
await ecars["volt"].save(db=db, at=time11)
584584

585585
# Time in-between the 2 batch of changes
586-
time20 = pendulum.now(tz="UTC")
586+
time20 = Timestamp()
587587

588588
# Set previous owner for C2 in branch
589589
await ecars["bolt"].previous_owner.update(data=persons["Jane"], db=db)
590590
await ecars["bolt"].save(db=db, at=time20)
591591

592592
# Time After the changes
593-
time30 = pendulum.now(tz="UTC")
593+
time30 = Timestamp()
594594

595595
params = {
596596
"branch": branch2,
@@ -610,7 +610,7 @@ async def data_diff_relationship_one(db: InfrahubDatabase, default_branch, car_p
610610
@pytest.fixture
611611
async def data_conflict_relationship_one(db: InfrahubDatabase, default_branch, car_person_data_generic, first_account):
612612
# Set some values in C1 in Main before creating the branch
613-
time_minus1 = pendulum.now(tz="UTC")
613+
time_minus1 = Timestamp()
614614

615615
ecars_list_main = await NodeManager.query(db=db, schema="TestElectricCar", branch=default_branch)
616616
ecars_main = {item.name.value: item for item in ecars_list_main}
@@ -624,7 +624,7 @@ async def data_conflict_relationship_one(db: InfrahubDatabase, default_branch, c
624624
branch2 = await create_branch(branch_name="branch2", db=db)
625625

626626
# Time After Creation of branch2
627-
time0 = pendulum.now(tz="UTC")
627+
time0 = Timestamp()
628628

629629
persons_list_branch = await NodeManager.query(db=db, schema="TestPerson", branch=branch2)
630630
persons_branch = {item.name.value: item for item in persons_list_branch}
@@ -633,30 +633,30 @@ async def data_conflict_relationship_one(db: InfrahubDatabase, default_branch, c
633633
ecars_branch = {item.name.value: item for item in ecars_list_branch}
634634

635635
# Change previous owner of C1 from P1 to P2 in branch
636-
time11 = pendulum.now(tz="UTC")
636+
time11 = Timestamp()
637637
await ecars_branch["volt"].previous_owner.update(data=persons_branch["John"], db=db)
638638
await ecars_branch["volt"].save(db=db, at=time11)
639639

640640
# Change previous owner of C1 from P1 to Null in main
641-
time12 = pendulum.now(tz="UTC")
641+
time12 = Timestamp()
642642
await ecars_main["volt"].previous_owner.update(data=None, db=db)
643643
await ecars_main["volt"].save(db=db, at=time12)
644644

645645
# Time in-between the 2 batch of changes
646-
time20 = pendulum.now(tz="UTC")
646+
time20 = Timestamp()
647647

648648
# Set previous owner for C2 in branch
649649
await ecars_branch["bolt"].previous_owner.update(data=persons_branch["Jane"], db=db)
650650
await ecars_branch["bolt"].save(db=db, at=time20)
651651

652652
# Set previous owner for C2 in main
653-
time21 = pendulum.now(tz="UTC")
653+
time21 = Timestamp()
654654

655655
await ecars_main["bolt"].previous_owner.update(data=persons_branch["John"], db=db)
656656
await ecars_main["bolt"].save(db=db, at=time21)
657657

658658
# Time After the changes
659-
time30 = pendulum.now(tz="UTC")
659+
time30 = Timestamp()
660660

661661
params = {
662662
"branch": branch2,

0 commit comments

Comments
 (0)