forked from testcontainers/testcontainers-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_basic.py
More file actions
198 lines (173 loc) · 6.57 KB
/
Copy pathexample_basic.py
File metadata and controls
198 lines (173 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import json
from neo4j import GraphDatabase
from testcontainers.neo4j import Neo4jContainer
def basic_example():
with Neo4jContainer() as neo4j:
# Get connection parameters
host = neo4j.get_container_host_ip()
port = neo4j.get_exposed_port(neo4j.port)
username = neo4j.username
password = neo4j.password
# Create Neo4j driver
driver = GraphDatabase.driver(f"bolt://{host}:{port}", auth=(username, password))
print("Connected to Neo4j")
# Create session
with driver.session() as session:
# Create nodes
create_nodes_query = """
CREATE (p1:Person {name: 'Alice', age: 30})
CREATE (p2:Person {name: 'Bob', age: 35})
CREATE (p3:Person {name: 'Charlie', age: 25})
CREATE (c1:Company {name: 'Tech Corp', founded: 2000})
CREATE (c2:Company {name: 'Data Inc', founded: 2010})
"""
session.run(create_nodes_query)
print("Created nodes")
# Create relationships
create_rels_query = """
MATCH (p1:Person {name: 'Alice'}), (c1:Company {name: 'Tech Corp'})
CREATE (p1)-[:WORKS_AT {since: 2015}]->(c1)
MATCH (p2:Person {name: 'Bob'}), (c1:Company {name: 'Tech Corp'})
CREATE (p2)-[:WORKS_AT {since: 2018}]->(c1)
MATCH (p3:Person {name: 'Charlie'}), (c2:Company {name: 'Data Inc'})
CREATE (p3)-[:WORKS_AT {since: 2020}]->(c2)
MATCH (p1:Person {name: 'Alice'}), (p2:Person {name: 'Bob'})
CREATE (p1)-[:KNOWS {since: 2016}]->(p2)
"""
session.run(create_rels_query)
print("Created relationships")
# Query nodes
query_nodes = """
MATCH (n)
RETURN n
"""
result = session.run(query_nodes)
print("\nAll nodes:")
for record in result:
node = record["n"]
print(json.dumps({"labels": list(node.labels), "properties": dict(node)}, indent=2))
# Query relationships
query_rels = """
MATCH (n)-[r]->(m)
RETURN n, r, m
"""
result = session.run(query_rels)
print("\nAll relationships:")
for record in result:
print(
json.dumps(
{
"from": {"labels": list(record["n"].labels), "properties": dict(record["n"])},
"relationship": {"type": record["r"].type, "properties": dict(record["r"])},
"to": {"labels": list(record["m"].labels), "properties": dict(record["m"])},
},
indent=2,
)
)
# Create index
create_index = """
CREATE INDEX person_name IF NOT EXISTS
FOR (p:Person)
ON (p.name)
"""
session.run(create_index)
print("\nCreated index on Person.name")
# Query using index
query_indexed = """
MATCH (p:Person)
WHERE p.name = 'Alice'
RETURN p
"""
result = session.run(query_indexed)
print("\nQuery using index:")
for record in result:
node = record["p"]
print(json.dumps({"labels": list(node.labels), "properties": dict(node)}, indent=2))
# Create constraint
create_constraint = """
CREATE CONSTRAINT company_name IF NOT EXISTS
FOR (c:Company)
REQUIRE c.name IS UNIQUE
"""
session.run(create_constraint)
print("\nCreated constraint on Company.name")
# Create full-text index
create_ft_index = """
CALL db.index.fulltext.createNodeIndex(
"personSearch",
["Person"],
["name"]
)
"""
session.run(create_ft_index)
print("Created full-text index")
# Query using full-text index
query_ft = """
CALL db.index.fulltext.queryNodes(
"personSearch",
"Alice"
)
YIELD node
RETURN node
"""
result = session.run(query_ft)
print("\nFull-text search results:")
for record in result:
node = record["node"]
print(json.dumps({"labels": list(node.labels), "properties": dict(node)}, indent=2))
# Create stored procedure
create_proc = """
CALL apoc.custom.asProcedure(
'getCompanyEmployees',
'MATCH (p:Person)-[:WORKS_AT]->(c:Company {name: $companyName})
RETURN p',
'READ',
[['p', 'NODE']],
[['companyName', 'STRING']]
)
"""
session.run(create_proc)
print("\nCreated stored procedure")
# Call stored procedure
call_proc = """
CALL custom.getCompanyEmployees('Tech Corp')
YIELD p
RETURN p
"""
result = session.run(call_proc)
print("\nStored procedure results:")
for record in result:
node = record["p"]
print(json.dumps({"labels": list(node.labels), "properties": dict(node)}, indent=2))
# Create trigger
create_trigger = """
CALL apoc.trigger.add(
'setTimestamp',
'UNWIND apoc.trigger.nodesByLabel($assignedLabels, "Person") AS n
SET n.updated_at = datetime()',
{phase: 'after'}
)
"""
session.run(create_trigger)
print("\nCreated trigger")
# Test trigger
test_trigger = """
MATCH (p:Person {name: 'Alice'})
SET p.age = 31
RETURN p
"""
result = session.run(test_trigger)
print("\nTrigger test results:")
for record in result:
node = record["p"]
print(json.dumps({"labels": list(node.labels), "properties": dict(node)}, indent=2))
# Clean up
cleanup = """
MATCH (n)
DETACH DELETE n
"""
session.run(cleanup)
print("\nCleaned up database")
driver.close()
if __name__ == "__main__":
basic_example()