Skip to content

Commit de9775a

Browse files
committed
Parse ttl and make host non optional
host is already known in the gds-api now on the first call -> always present. ttl is a recently added field
1 parent 714b685 commit de9775a

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

graphdatascience/session/aura_api_responses.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import sys
55
from collections import defaultdict
66
from dataclasses import dataclass
7-
from datetime import datetime, timezone
87
from typing import Any, Dict, NamedTuple, Optional, Set
8+
from datetime import datetime, timedelta, timezone
9+
10+
from pandas import Timedelta
911

1012

1113
@dataclass(repr=True, frozen=True)
@@ -15,28 +17,34 @@ class SessionDetails:
1517
instance_id: str
1618
memory: str
1719
status: str
18-
host: Optional[str]
19-
expiry_date: Optional[datetime]
20+
host: str
2021
created_at: datetime
22+
expiry_date: Optional[datetime]
23+
ttl: Optional[timedelta]
2124

2225
@classmethod
2326
def fromJson(cls, json: Dict[str, Any]) -> SessionDetails:
2427
expiry_date = json.get("expiry_date")
28+
ttl = json.get("ttl")
2529

2630
return cls(
2731
id=json["id"],
2832
name=json["name"],
2933
instance_id=json["instance_id"],
3034
memory=json["memory"],
3135
status=json["status"],
32-
host=json.get("host"),
36+
host=json["host"],
3337
expiry_date=TimeParser.fromisoformat(expiry_date) if expiry_date else None,
3438
created_at=TimeParser.fromisoformat(json["created_at"]),
39+
ttl=Timedelta(ttl).to_pytimedelta() if ttl else None, # datetime has no support for parsing timedetla
3540
)
3641

3742
def bolt_connection_url(self) -> str:
3843
return f"neo4j+ssc://{self.host}" # TODO use neo4j+s
3944

45+
def is_expired(self) -> bool:
46+
return self.status == "Expired"
47+
4048

4149
@dataclass(repr=True, frozen=True)
4250
class InstanceDetails:

graphdatascience/tests/unit/test_aura_api.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from datetime import datetime, timezone
2+
from datetime import datetime, timedelta, timezone
33

44
import pytest
55
from _pytest.logging import LogCaptureFixture
@@ -48,6 +48,7 @@ def test_create_session(requests_mock: Mocker) -> None:
4848
host="1.2.3.4",
4949
memory="4G",
5050
expiry_date=None,
51+
ttl=None,
5152
)
5253

5354

@@ -82,6 +83,7 @@ def test_list_session(requests_mock: Mocker) -> None:
8283
host="1.2.3.4",
8384
memory="4G",
8485
expiry_date=TimeParser.fromisoformat("1977-01-01T00:00:00Z"),
86+
ttl=None,
8587
)
8688

8789

@@ -124,6 +126,7 @@ def test_list_sessions(requests_mock: Mocker) -> None:
124126
host="1.2.3.4",
125127
memory="4G",
126128
expiry_date=TimeParser.fromisoformat("1977-01-01T00:00:00Z"),
129+
ttl=None,
127130
)
128131

129132
expected2 = SessionDetails(
@@ -133,8 +136,9 @@ def test_list_sessions(requests_mock: Mocker) -> None:
133136
instance_id="dbid-3",
134137
created_at=TimeParser.fromisoformat("2012-01-01T00:00:00Z"),
135138
memory="8G",
136-
host=None,
139+
host="foo.bar",
137140
expiry_date=None,
141+
ttl=None,
138142
)
139143

140144
assert result == [expected1, expected2]
@@ -635,6 +639,7 @@ def test_parse_session_info() -> None:
635639
"expiry_date": "2022-01-01T00:00:00Z",
636640
"created_at": "2021-01-01T00:00:00Z",
637641
"host": "a.b",
642+
"ttl": "1d8h1m2s",
638643
}
639644
session_info = SessionDetails.fromJson(session_details)
640645

@@ -647,10 +652,11 @@ def test_parse_session_info() -> None:
647652
host="a.b",
648653
expiry_date=datetime(2022, 1, 1, 0, 0, 0, tzinfo=timezone.utc),
649654
created_at=datetime(2021, 1, 1, 0, 0, 0, tzinfo=timezone.utc),
655+
ttl=timedelta(days=1, hours=8, minutes=1, seconds=2),
650656
)
651657

652658

653-
def test_parse_session_info_without_expiry() -> None:
659+
def test_parse_session_info_without_optionals() -> None:
654660
session_details = {
655661
"id": "test_id",
656662
"name": "test_session",
@@ -670,5 +676,6 @@ def test_parse_session_info_without_expiry() -> None:
670676
host="a.b",
671677
status="running",
672678
expiry_date=None,
679+
ttl=None,
673680
created_at=datetime(2021, 1, 1, 0, 0, 0, tzinfo=timezone.utc),
674681
)

0 commit comments

Comments
 (0)