Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Make manual test-ready #406

Merged
merged 5 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions python-manual/modules/ROOT/pages/bookmarks.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ To disable bookmark management and causal consistency, set `bookmark_manager_=No
[source, python]
----
driver.execute_query(
... ,
"<QUERY>",
bookmark_manager_=None,
)
----
Expand All @@ -45,7 +45,6 @@ with driver.session() as session:
session.execute_write(lambda tx: tx.run("<QUERY 1>"))
session.execute_write(lambda tx: tx.run("<QUERY 2>")) # can read QUERY 1
session.execute_write(lambda tx: tx.run("<QUERY 3>")) # can read QUERY 1,2
...
----


Expand Down
2 changes: 1 addition & 1 deletion python-manual/modules/ROOT/pages/concurrency.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ AUTH = ("<Username>", "<Password>")
async def main():
async with AsyncGraphDatabase.driver(URI, auth=AUTH) as driver:
async with driver.session(database="neo4j") as session:
records = await session.read_transaction(get_people)
records = await session.execute_read(get_people)
print(records)


Expand Down
19 changes: 11 additions & 8 deletions python-manual/modules/ROOT/pages/connect-advanced.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

= Advanced connection information

== Connection URI
Expand Down Expand Up @@ -59,35 +60,37 @@ If you are unsure, ask the database administrator.
The basic authentication scheme relies on traditional username and password.
These can either be the credentials for your local installation, or the ones provided with an Aura instance.

[source,python]
[source, python]
----
from neo4j import GraphDatabase

driver = GraphDatabase.driver(uri, auth=(user, password))
driver = GraphDatabase.driver(URI, auth=(USERNAME, PASSWORD))
----

[INFO]
The basic authentication scheme can also be used to authenticate against an LDAP server (Enterprise Edition only).

=== Kerberos authentication

The Kerberos authentication scheme requires a base64-encoded ticket.
It can only be used if the server has the link:{neo4j-docs-base-uri}/kerberos-add-on/current/deployment/[Kerberos Add-on installed].

[source,python]
[source, python, role=test-skip]
----
from neo4j import GraphDatabase, kerberos_auth

driver = GraphDatabase.driver(uri, auth=kerberos_auth(ticket))
driver = GraphDatabase.driver(URI, auth=kerberos_auth(ticket))
----

=== Bearer authentication

The bearer authentication scheme requires a base64-encoded token provided by an Identity Provider through Neo4j's link:{neo4j-docs-base-uri}/operations-manual/current/authentication-authorization/sso-integration[Single Sign-On feature].

[source,python]
[source, python, role=test-skip]
----
from neo4j import GraphDatabase, bearer_auth

driver = GraphDatabase.driver(uri, auth=bearer_auth(token))
driver = GraphDatabase.driver(URI, auth=bearer_auth(token))
----

[NOTE]
Expand Down Expand Up @@ -126,7 +129,7 @@ def custom_resolver(socket_address):
yield neo4j.Address.parse("localhost:7687")
yield neo4j.Address.parse("[::1]:7687")

# or any tuple that can be passed to neo4j.Address(...).
# or any tuple that can be passed to neo4j.Address().
# This will initially be interpreted as IPv4, but DNS resolution
# will turn it into IPv6 if appropriate.
yield "::1", 7687
Expand All @@ -137,7 +140,7 @@ def custom_resolver(socket_address):


driver = neo4j.GraphDatabase.driver("neo4j://example.com:9999",
auth=(user, password),
auth=(USERNAME, PASSWORD),
resolver=custom_resolver)
----

Expand Down
2 changes: 1 addition & 1 deletion python-manual/modules/ROOT/pages/connect.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ You connect to a database by creating a <<Driver>> object and providing a URL an
from neo4j import GraphDatabase

# URI examples: "neo4j://localhost", "neo4j+s://xxx.databases.neo4j.io"
URI = "<URI to Neo4j database>"
URI = "<URI for Neo4j database>"
AUTH = ("<Username>", "<Password>")

with GraphDatabase.driver(URI, auth=AUTH) as driver: # <1>
Expand Down
19 changes: 10 additions & 9 deletions python-manual/modules/ROOT/pages/data-types.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ from neo4j import GraphDatabase
from neo4j.time import DateTime


URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "secretgraph")
URI = "<URI for Neo4j database>"
AUTH = ("<Username>", "<Password>")


friends_since = DateTime(year=1999, month=11, day=23,
Expand Down Expand Up @@ -108,6 +108,7 @@ Represents an instant capturing the time, and the timezone offset in seconds, bu
[source, python]
----
from neo4j.time import Time
import pytz

t = Time(hour=7, minute=47, nanosecond=4123, tzinfo=pytz.FixedOffset(-240))
print(t) # '07:47:00.000004123-04:00'
Expand All @@ -120,7 +121,7 @@ For full documentation, see link:{neo4j-docs-base-uri}/api/python-driver/current

Represents an instant capturing the time of day, but not the date, nor the timezone.

[source, python, indent=0]
[source, python]
----
from neo4j.time import Time

Expand Down Expand Up @@ -270,8 +271,8 @@ This should be used with care, as no guarantees are given about the mapping betw
from neo4j import GraphDatabase


URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "secretgraph")
URI = "<URI for Neo4j database>"
AUTH = ("<Username>", "<Password>")

with GraphDatabase.driver(URI, auth=AUTH) as driver:
records, _, _ = driver.execute_query(
Expand Down Expand Up @@ -305,8 +306,8 @@ This should be used with care, as no guarantees are given about the mapping betw
from neo4j import GraphDatabase


URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "secretgraph")
URI = "<URI for Neo4j database>"
AUTH = ("<Username>", "<Password>")

with GraphDatabase.driver(URI, auth=AUTH) as driver:
records, _, _ = driver.execute_query("""
Expand Down Expand Up @@ -346,8 +347,8 @@ from neo4j import GraphDatabase
from neo4j.time import Date


URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "secretgraph")
URI = "<URI for Neo4j database>"
AUTH = ("<Username>", "<Password>")

def add_friend(driver, name, status, date, friend_name):
driver.execute_query("""
Expand Down
8 changes: 6 additions & 2 deletions python-manual/modules/ROOT/pages/performance.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,17 @@ with driver.session(database="<DB NAME>") as session:
[source, python]
----
driver.execute_query("MATCH (p:Person {name: 'Alice'}) RETURN p")
# or
name = "Alice"
driver.execute_query("MATCH (p:Person {name: '" + name + "'}) RETURN p")
----

[source, python]
----
with driver.session(database="<DB NAME>") as session:
session.run("MATCH (p:Person {name: 'Alice'}) RETURN p")
# or
name = "Alice"
session.run("MATCH (p:Person {name: '" + name + "'}) RETURN p")
----

Expand Down Expand Up @@ -123,7 +127,7 @@ with driver.session(database="<DB NAME>") as session:

[source, python]
----
numbers = [{"value": random()} for _ in range(10000)]
numbers = [{"value": random()} for _ in range(1000)]
driver.execute_query("""
WITH $numbers AS batch
UNWIND batch AS node
Expand All @@ -138,7 +142,7 @@ driver.execute_query("""

[source, python]
----
for _ in range(10000):
for _ in range(1000):
driver.execute_query("MERGE (:Number {value: $value})", value=random())
----

Expand Down
4 changes: 2 additions & 2 deletions python-manual/modules/ROOT/pages/query-advanced.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ It returns a `Result` object that needs to be xref:transactions#process-result[p
[source, python]
----
with driver.session(database="neo4j") as session:
session.run("CREATE (a:Person {name: $name})", name=name)
session.run("CREATE (a:Person {name: $name})", name="Licia")
----

An implicit transaction gets committed _at the latest_ when the session is destroyed, or before another transaction is executed within the same session.
Expand Down Expand Up @@ -64,7 +64,7 @@ with driver.session(database="neo4j") as session:
query = Query("CREATE (a:Person {name: $name})",
timeout=1.0,
metadata={"app_name": "people"})
result = session.run(query, name=name)
result = session.run(query, name="John")
----


Expand Down
5 changes: 2 additions & 3 deletions python-manual/modules/ROOT/pages/query-simple.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ print("Created {nodes_created} nodes in {time} ms.".format(
nodes_created=summary.counters.nodes_created,
time=summary.result_available_after
))

----

<1> specifies the Cypher query
Expand Down Expand Up @@ -224,7 +223,7 @@ You can execute a query under the security context of a different user with the
For this to work, the user under which the `Driver` was created needs to have the link:{neo4j-docs-base-uri}/cypher-manual/current/administration/access-control/dbms-administration#access-control-dbms-administration-impersonation[appropriate permissions].
Impersonating a user is cheaper than creating a new `Driver` object.

[source, python]
[source, python, role=test-skip]
----
driver.execute_query(
"MATCH (p:Person) RETURN p.name",
Expand All @@ -251,7 +250,7 @@ For more information, see xref:transformers.adoc[Manipulate query results].
from neo4j import GraphDatabase


URI = "<URI to Neo4j database>"
URI = "<URI for Neo4j database>"
AUTH = ("<Username>", "<Password>")

people = [{"name": "Alice", "age": 42, "friends": ["Bob", "Peter", "Anna"]},
Expand Down
2 changes: 1 addition & 1 deletion python-manual/modules/ROOT/pages/result-summary.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ There is a slight performance gain in restricting the amount of notifications th
[source, python]
----
driver = neo4j.GraphDatabase.driver(
url, auth=auth,
URI, auth=AUTH,
notifications_min_severity='WARNING', # or 'OFF' to disable
notifications_disabled_categories=['HINT', 'GENERIC']
)
Expand Down
30 changes: 27 additions & 3 deletions python-manual/modules/ROOT/pages/transactions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Both methods take a _transaction function_ callback, which is responsible for ac
[source, python]
----
def match_person_nodes(tx, name_filter): # <3>
result = tx.run(""" # <4>
result = tx.run(""" // <4>
MATCH (p:Person) WHERE p.name STARTS WITH $filter
RETURN p.name as name ORDER BY name
""", filter=name_filter)
Expand Down Expand Up @@ -74,7 +74,7 @@ Within a transaction function, a `return` statement results in the transaction b
from neo4j import GraphDatabase


URI = "<URI to Neo4j database>"
URI = "<URI for Neo4j database>"
AUTH = ("<Username>", "<Password>")
employee_threshold=10

Expand Down Expand Up @@ -187,6 +187,8 @@ You run queries inside an explicit transaction with the method link:{neo4j-docs-
with driver.session(database="neo4j") as session:
with session.begin_transaction() as tx:
# use tx.run() to run queries
tx.run("<QUERY 1>")
tx.run("<QUERY 2>")
----

Closing an explicit transaction can either happen automatically at the end of a `with` block, or can be explicitly controlled through the methods link:{neo4j-docs-base-uri}/api/python-driver/current/api.html#neo4j.Transaction.commit[`Transaction.commit()`], link:{neo4j-docs-base-uri}/api/python-driver/current/api.html#neo4j.Transaction.rollback[`Transaction.rollback()`], or link:{neo4j-docs-base-uri}/api/python-driver/current/api.html#neo4j.Transaction.close[`Transaction.close()`].
Expand All @@ -198,6 +200,25 @@ Explicit transactions are most useful for applications that need to distribute C
import neo4j


URI = "<URI for Neo4j database>"
AUTH = ("<Username>", "<Password>")


def main():
with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
customer_id = create_customer(driver)
other_bank_id = 42
transfer_to_other_bank(driver, customer_id, other_bank_id, 999)


def create_customer(driver):
result, _, _ = driver.execute_query("""
MERGE (c:Customer {id: rand()})
RETURN c.id AS id
""", database_ = "neo4j")
return result[0]["id"]


def transfer_to_other_bank(driver, customer_id, other_bank_id, amount):
with driver.session(database="neo4j") as session:
tx = session.begin_transaction()
Expand Down Expand Up @@ -232,8 +253,8 @@ def customer_balance_check(tx, customer_id, amount):


def other_bank_transfer_api(customer_id, other_bank_id, amount):
...
# make some API call to other bank
pass


def decrease_customer_balance(tx, customer_id, amount):
Expand All @@ -251,6 +272,9 @@ def request_inspection(customer_id, other_bank_id, amount, e):
print("customer_id:", customer_id, "other_bank_id:", other_bank_id,
"amount:", amount)


if __name__ == "__main__":
main()
----


Expand Down
Loading