Skip to content

Commit 7d0d9d8

Browse files
committed
Made the e2e tests for the ttls
1 parent cc436a2 commit 7d0d9d8

File tree

4 files changed

+161
-50
lines changed

4 files changed

+161
-50
lines changed

tests/e2e/project/node-ttls.yaml

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,7 @@
22
arguments:
33
graph_object_type: NODE
44
configurations:
5-
- object_type: Airport
6-
expiry_in_hours: 0
7-
- object_type: Country
8-
expiry_in_hours: 0
9-
- object_type: Region
10-
expiry_in_hours: 0
11-
- object_type: Player
12-
expiry_in_hours: 0
13-
- object_type: PlaceOfOrigin
14-
expiry_in_hours: 0
15-
- object_type: Position
16-
expiry_in_hours: 0
17-
- object_type: Team
5+
- object_type: ObjectA
6+
expiry_in_hours: 48
7+
- object_type: ObjectB
8+
expiry_in_hours: 24

tests/e2e/project/nodestream.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ scopes:
77
name: fifa
88
- path: tests/e2e/project/node-ttls.yaml
99
name: node-ttls
10-
- path: tests/e2e/project/node-ttls.yaml
10+
- path: tests/e2e/project/relationship-ttls.yaml
1111
name: relationship-ttls
1212

1313
targets:

tests/e2e/project/relationship-ttls.yaml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
arguments:
33
graph_object_type: RELATIONSHIP
44
configurations:
5-
- object_type: WITHIN
6-
expiry_in_hours: 0
7-
- object_type: ORIGINATES_FROM
8-
expiry_in_hours: 0
9-
- object_type: LINES_UP_AT
10-
expiry_in_hours: 0
11-
- object_type: PLAYS_FOR
12-
expiry_in_hours: 0
5+
- object_type: CONNECTED_TO
6+
expiry_in_hours: 48
7+
- object_type: ADJACENT_TO
8+
expiry_in_hours: 24

tests/e2e/test_neo4j_pipelines.py

Lines changed: 152 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from pathlib import Path
2-
from time import sleep
32

43
import pytest
4+
from neo4j import Session
55
from nodestream.pipeline import (
66
PipelineInitializationArguments,
77
PipelineProgressReporter,
88
)
99
from nodestream.project import Project, RunRequest
10+
from pandas import Timedelta, Timestamp
11+
12+
from nodestream_plugin_neo4j.query import Query
1013

1114
from .conftest import TESTED_NEO4J_VERSIONS
1215

@@ -60,31 +63,55 @@ def validate_fifa_mo_club(session):
6063
assert result.single()["club"] == "Liverpool"
6164

6265

63-
def validate_relationship_ttls(session):
66+
def validate_consistency_in_node_counts(session):
67+
result = session.run(
68+
"""
69+
MATCH (n:ObjectA)
70+
RETURN count(n) as node_count
71+
"""
72+
)
73+
assert result.single()["node_count"] == 30
6474
result = session.run(
6575
"""
66-
MATCH ()-[r]-()
76+
MATCH (n:ObjectB)
77+
RETURN count(n) as node_count
78+
"""
79+
)
80+
assert result.single()["node_count"] == 30
81+
82+
83+
def validate_ttl_seperation_between_relationship_object_types(session):
84+
result = session.run(
85+
"""
86+
MATCH ()-[r:CONNECTED_TO]->()
6787
RETURN count(r) as relationship_count
6888
"""
6989
)
70-
assert result.single()["relationship_count"] == 0
90+
assert result.single()["relationship_count"] == 20
7191
result = session.run(
7292
"""
73-
MATCH (n)
74-
RETURN count(n) as relationship_count
93+
MATCH ()-[r:ADJACENT_TO]->()
94+
RETURN count(r) as relationship_count
7595
"""
7696
)
77-
assert result.single()["relationship_count"] != 0
97+
assert result.single()["relationship_count"] == 10
7898

7999

80-
def validate_node_ttls(session):
100+
def validate_ttl_seperation_between_node_object_types(session):
81101
result = session.run(
82102
"""
83-
MATCH (n)
103+
MATCH (n:ObjectA)
84104
RETURN count(n) as node_count
85105
"""
86106
)
87-
assert result.single()["node_count"] == 0
107+
assert result.single()["node_count"] == 20
108+
result = session.run(
109+
"""
110+
MATCH (n:ObjectB)
111+
RETURN count(n) as node_count
112+
"""
113+
)
114+
assert result.single()["node_count"] == 10
88115

89116

90117
PIPELINE_TESTS = [
@@ -93,10 +120,122 @@ def validate_node_ttls(session):
93120
]
94121

95122
TTL_TESTS = [
96-
("relationship-ttlss", [validate_relationship_ttls]),
97-
("node-ttls", [validate_node_ttls]),
123+
(
124+
"relationship-ttls",
125+
[
126+
validate_consistency_in_node_counts,
127+
validate_ttl_seperation_between_relationship_object_types,
128+
],
129+
),
130+
("node-ttls", [validate_ttl_seperation_between_node_object_types]),
98131
]
99132

133+
NODE_CREATION_QUERY = """
134+
CREATE (n:{node_label})
135+
SET n.last_ingested_at = $timestamp
136+
SET n.identifier = $node_id
137+
"""
138+
139+
RELATIONSHIP_CREATION_QUERY = """
140+
MATCH (from_node:{from_node_label}) MATCH (to_node:{to_node_label})
141+
WHERE to_node.identifier=$to_node_identifier AND from_node.identifier=$from_node_identifier
142+
CREATE (from_node)-[rel:{relationship_label}]->(to_node)
143+
SET rel.last_ingested_at = $timestamp
144+
"""
145+
146+
REALLY_OLD_TIMESTAMP = Timestamp.utcnow() - Timedelta(hours=60)
147+
OLD_TIMESTAMP = Timestamp.utcnow() - Timedelta(hours=36)
148+
NEW_TIMESTAMP = Timestamp.utcnow() - Timedelta(hours=12)
149+
150+
151+
def create_node_query_from_params(node_label, node_id, timestamp):
152+
return Query(
153+
NODE_CREATION_QUERY.format(node_label=node_label),
154+
{"timestamp": timestamp, "node_id": node_id},
155+
)
156+
157+
158+
def create_relationship_query_from_params(
159+
from_node_label,
160+
from_node_identifier,
161+
to_node_label,
162+
to_node_identifier,
163+
relationship_label,
164+
timestamp,
165+
):
166+
return Query(
167+
RELATIONSHIP_CREATION_QUERY.format(
168+
from_node_label=from_node_label,
169+
to_node_label=to_node_label,
170+
relationship_label=relationship_label,
171+
),
172+
{
173+
"timestamp": timestamp,
174+
"from_node_identifier": from_node_identifier,
175+
"to_node_identifier": to_node_identifier,
176+
},
177+
)
178+
179+
180+
def create_test_objects(session: Session):
181+
def create_node(node_label, node_id, timestamp):
182+
query = create_node_query_from_params(node_label, node_id, timestamp)
183+
session.run(query.query_statement, query.parameters)
184+
185+
def create_relationship(
186+
from_node_label,
187+
from_node_identifier,
188+
to_node_label,
189+
to_node_identifier,
190+
relationship_label,
191+
timestamp,
192+
):
193+
query = create_relationship_query_from_params(
194+
from_node_label,
195+
from_node_identifier,
196+
to_node_label,
197+
to_node_identifier,
198+
relationship_label,
199+
timestamp,
200+
)
201+
session.run(query.query_statement, query.parameters)
202+
203+
for i in range(0, 10):
204+
create_node("ObjectA", str(i), REALLY_OLD_TIMESTAMP)
205+
create_node("ObjectB", str(i), REALLY_OLD_TIMESTAMP)
206+
207+
for i in range(10, 20):
208+
create_node("ObjectA", str(i), OLD_TIMESTAMP)
209+
create_node("ObjectB", str(i), OLD_TIMESTAMP)
210+
211+
for i in range(20, 30):
212+
create_node("ObjectA", str(i), NEW_TIMESTAMP)
213+
create_node("ObjectB", str(i), NEW_TIMESTAMP)
214+
215+
for i in range(0, 10):
216+
create_relationship(
217+
"ObjectA", str(i), "ObjectB", str(i), "CONNECTED_TO", NEW_TIMESTAMP
218+
)
219+
create_relationship(
220+
"ObjectA", str(i), "ObjectB", str(i), "ADJACENT_TO", NEW_TIMESTAMP
221+
)
222+
223+
for i in range(10, 20):
224+
create_relationship(
225+
"ObjectA", str(i), "ObjectB", str(i), "CONNECTED_TO", OLD_TIMESTAMP
226+
)
227+
create_relationship(
228+
"ObjectA", str(i), "ObjectB", str(i), "ADJACENT_TO", OLD_TIMESTAMP
229+
)
230+
231+
for i in range(20, 30):
232+
create_relationship(
233+
"ObjectA", str(i), "ObjectB", str(i), "CONNECTED_TO", REALLY_OLD_TIMESTAMP
234+
)
235+
create_relationship(
236+
"ObjectA", str(i), "ObjectB", str(i), "ADJACENT_TO", REALLY_OLD_TIMESTAMP
237+
)
238+
100239

101240
@pytest.mark.asyncio
102241
@pytest.mark.e2e
@@ -132,22 +271,8 @@ async def test_neo4j_ttls(project, neo4j_container, neo4j_version):
132271
with neo4j_container(
133272
neo4j_version
134273
) as neo4j_container, neo4j_container.get_driver() as driver, driver.session() as session:
274+
create_test_objects(session)
135275
target = project.get_target_by_name("my-neo4j-db")
136-
137-
for pipeline_name, validations in PIPELINE_TESTS:
138-
await project.run(
139-
RunRequest(
140-
pipeline_name,
141-
PipelineInitializationArguments(extra_steps=[target.make_writer()]),
142-
PipelineProgressReporter(),
143-
)
144-
)
145-
146-
for validator in validations:
147-
validator(session)
148-
149-
sleep(30)
150-
151276
for pipeline_name, validations in TTL_TESTS:
152277
await project.run(
153278
RunRequest(
@@ -156,6 +281,5 @@ async def test_neo4j_ttls(project, neo4j_container, neo4j_version):
156281
PipelineProgressReporter(),
157282
)
158283
)
159-
160284
for validator in validations:
161285
validator(session)

0 commit comments

Comments
 (0)