|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | + |
| 4 | +# Copyright (c) "Neo4j" |
| 5 | +# Neo4j Sweden AB [http://neo4j.com] |
| 6 | +# |
| 7 | +# This file is part of Neo4j. |
| 8 | +# |
| 9 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +# you may not use this file except in compliance with the License. |
| 11 | +# You may obtain a copy of the License at |
| 12 | +# |
| 13 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +# |
| 15 | +# Unless required by applicable law or agreed to in writing, software |
| 16 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +# See the License for the specific language governing permissions and |
| 19 | +# limitations under the License. |
| 20 | + |
| 21 | +from neo4j import unit_of_work, Query |
| 22 | + |
| 23 | + |
| 24 | +# python -m pytest tests/integration/examples/test_transaction_metadata_config_example.py -s -v |
| 25 | + |
| 26 | +# tag::transaction-metadata-config[] |
| 27 | +@unit_of_work(timeout=5, metadata={"applicationId": "123"}) |
| 28 | +def create_person(tx, name): |
| 29 | + return tx.run( |
| 30 | + "CREATE (a:Person {name: $name}) RETURN id(a)", name=name |
| 31 | + ).single().value() |
| 32 | + |
| 33 | + |
| 34 | +def add_person(driver, name): |
| 35 | + with driver.session() as session: |
| 36 | + return session.write_transaction(create_person, name) |
| 37 | +# end::transaction-metadata-config[] |
| 38 | + |
| 39 | + |
| 40 | +class TransactionMetadataConfigExample: |
| 41 | + |
| 42 | + def __init__(self, driver): |
| 43 | + self.driver = driver |
| 44 | + |
| 45 | + def add_person(self, name): |
| 46 | + return add_person(self.driver, name) |
| 47 | + |
| 48 | + |
| 49 | +def test_example(bolt_driver): |
| 50 | + eg = TransactionMetadataConfigExample(bolt_driver) |
| 51 | + with eg.driver.session() as session: |
| 52 | + session.run("MATCH (_) DETACH DELETE _") |
| 53 | + eg.add_person("Alice") |
| 54 | + n = session.run("MATCH (a:Person) RETURN count(a)").single().value() |
| 55 | + assert n == 1 |
0 commit comments