diff --git a/tests/drivers/.gitignore b/tests/drivers/.gitignore index 8165574a9..5f3bedada 100644 --- a/tests/drivers/.gitignore +++ b/tests/drivers/.gitignore @@ -1,3 +1,7 @@ +# csharp driver +csharp/*.dll +csharp/*.exe + # java driver java/*.jar java/*.class diff --git a/tests/drivers/csharp/Basic.cs b/tests/drivers/csharp/Basic.cs new file mode 100644 index 000000000..865eeed02 --- /dev/null +++ b/tests/drivers/csharp/Basic.cs @@ -0,0 +1,22 @@ +using System; +using System.Linq; +using Neo4j.Driver.V1; + +public class Basic { + public static void Main(string[] args) { + var config = Config.DefaultConfig; + config.EncryptionLevel = EncryptionLevel.None; + using(var driver = GraphDatabase.Driver("bolt://localhost:7687", AuthTokens.None, config)) + using(var session = driver.Session()) + { + session.Run("MATCH (n) DETACH DELETE n").Consume(); + session.Run("CREATE (alice:Person {name: \"Alice\", age: 22})").Consume(); + var result = session.Run("MATCH (n) RETURN n").First(); + var alice = (INode) result["n"]; + Console.WriteLine(alice["name"]); + Console.WriteLine(string.Join(", ", alice.Labels)); + Console.WriteLine(alice["age"]); + } + Console.WriteLine("All ok!"); + } +} diff --git a/tests/drivers/csharp/Transactions.cs b/tests/drivers/csharp/Transactions.cs new file mode 100644 index 000000000..b15ce764e --- /dev/null +++ b/tests/drivers/csharp/Transactions.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using Neo4j.Driver.V1; + +public class Transactions +{ + public static void Main(string[] args) { + var config = Config.DefaultConfig; + config.EncryptionLevel = EncryptionLevel.None; + using(var driver = GraphDatabase.Driver("bolt://localhost:7687", AuthTokens.None, config)) + { + ClearDatabase(driver); + // Wrong query. + try + { + using(var session = driver.Session()) + using(var tx = session.BeginTransaction()) + { + CreatePerson(tx, "mirko"); + // Incorrectly start CREATE + tx.Run("CREATE (").Consume(); + CreatePerson(tx, "slavko"); + tx.Success(); + } + } + catch (ClientException) + { + Console.WriteLine("Rolled back transaction"); + } + Trace.Assert(CountNodes(driver) == 0, "Expected transaction was rolled back."); + // Correct query. + using(var session = driver.Session()) + using(var tx = session.BeginTransaction()) + { + CreatePerson(tx, "mirka"); + CreatePerson(tx, "slavka"); + tx.Success(); + } + Trace.Assert(CountNodes(driver) == 2, "Expected 2 created nodes."); + ClearDatabase(driver); + using(var session = driver.Session()) + { + // Create a lot of nodes so that the next read takes a long time. + session.Run("UNWIND range(1, 100000) AS i CREATE ()").Consume(); + try + { + Console.WriteLine("Running a long read..."); + session.Run("MATCH (a), (b), (c), (d), (e), (f) RETURN COUNT(*) AS cnt").Consume(); + } + catch (TransientException) + { + Console.WriteLine("Transaction timed out"); + } + } + } + Console.WriteLine("All ok!"); + } + + private static void CreatePerson(ITransaction tx, string name) + { + var parameters = new Dictionary{{"name", name}}; + var result = tx.Run("CREATE (person:Person {name: $name}) RETURN person", parameters); + Console.WriteLine("Created: " + ((INode) result.First()["person"])["name"]); + } + + private static void ClearDatabase(IDriver driver) + { + using(var session = driver.Session()) + session.Run("MATCH (n) DETACH DELETE n").Consume(); + } + + private static int CountNodes(IDriver driver) + { + using(var session = driver.Session()) + { + var result = session.Run("MATCH (n) RETURN COUNT(*) AS cnt"); + return Convert.ToInt32(result.First()["cnt"]); + } + } +} diff --git a/tests/drivers/csharp/run.sh b/tests/drivers/csharp/run.sh new file mode 100755 index 000000000..871ed48a3 --- /dev/null +++ b/tests/drivers/csharp/run.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +set -e + +script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +cd $script_dir + +if ! which nuget >/dev/null; then + echo "Please install nuget!" + exit 1 +fi + +if ! which mono >/dev/null; then + echo "Please install mono!" + exit 1 +fi + +if ! which mcs >/dev/null; then + echo "Please install mcs!" + exit 1 +fi + +driver=Neo4j.Driver +version=1.5.0 + +if [ ! -f $driver.dll ]; then + nuget_dir=`mktemp -d nuget.XXXXXXXX` || exit 1 + nuget install $driver -Version $version -OutputDirectory $nuget_dir || exit 1 + cp $nuget_dir/$driver.$version/lib/net452/$driver.dll ./ || exit 1 + rm -rf $nuget_dir || exit 1 +fi + +mcs -reference:$driver.dll Basic.cs +mono Basic.exe + +mcs -reference:$driver.dll Transactions.cs +mono Transactions.exe diff --git a/tests/drivers/python/run.sh b/tests/drivers/python/run.sh index 37be4c679..25a011f2d 100755 --- a/tests/drivers/python/run.sh +++ b/tests/drivers/python/run.sh @@ -21,7 +21,7 @@ if [ ! -d "ve3" ]; then fi source ve3/bin/activate $PIP install --upgrade pip -$PIP install neo4j-driver +$PIP install neo4j-driver==1.5.0 # execute test $PYTHON basic.py diff --git a/tests/drivers/python/transactions.py b/tests/drivers/python/transactions.py index 3137a5a6f..43dc039da 100644 --- a/tests/drivers/python/transactions.py +++ b/tests/drivers/python/transactions.py @@ -1,11 +1,8 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from neo4j.v1 import GraphDatabase, basic_auth, ClientError, TransientError - -driver = GraphDatabase.driver("bolt://localhost:7687", - auth=basic_auth("", ""), - encrypted=False) +from neo4j.v1 import GraphDatabase, basic_auth +from neo4j.exceptions import ClientError, TransientError def tx_error(tx, name, name2): a = tx.run("CREATE (a:Person {name: $name}) RETURN a", name=name).data() @@ -21,38 +18,33 @@ def tx_good(tx, name, name2): print(a[0]['a']) def tx_too_long(tx): - a = tx.run("MATCH (a), (b), (c), (d), (e), (f) RETURN COUNT(*) AS cnt") - print(a[0]['cnt']) + tx.run("MATCH (a), (b), (c), (d), (e), (f) RETURN COUNT(*) AS cnt") -def add_person(f, name, name2): +with GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("", ""), + encrypted=False) as driver: + + def add_person(f, name, name2): + with driver.session() as session: + session.write_transaction(f, name, name2) + + # Wrong query. + try: + add_person(tx_error, "mirko", "slavko") + except ClientError: + pass + + # Correct query. + add_person(tx_good, "mirka", "slavka") + + # Setup for next query. with driver.session() as session: - session.write_transaction(f, name, name2) + session.run("UNWIND range(1, 100000) AS x CREATE ()") -# Wrong query. -try: - add_person(tx_error, "mirko", "slavko") -except ClientError: - pass + # Query that will run for a very long time, transient error expected. + with driver.session() as session: + try: + session.run("MATCH (a), (b), (c), (d), (e), (f) RETURN COUNT(*) AS cnt") + except TransientError: + print("transient error") -# Correct query. -add_person(tx_good, "mirka", "slavka") - -# Setup for next query. -with driver.session() as session: - session.write_transaction( - lambda tx: tx.run("UNWIND range(1, 100000) AS x CREATE ()")) - -# Query that will run for a very long time, transient error expected. -try: - session = driver.session() - session.read_transaction(tx_too_long) -except TransientError: - print("transient error") -except: - assert False, "TransientError should have been thrown" -finally: - session.close() - -driver.close() - -print("All ok!") + print("All ok!") diff --git a/tests/drivers/run.sh b/tests/drivers/run.sh index 0df212190..e8de35921 100755 --- a/tests/drivers/run.sh +++ b/tests/drivers/run.sh @@ -15,7 +15,7 @@ for i in *; do if [ ! -d $i ]; then continue; fi pushd $i echo "Running: $i" - ./run.sh || exit 1 + ./run.sh || exit 1 echo popd done