Skip to content

Commit

Permalink
Merge pull request #397 from zhenlineo/michael-simons/feature/databas…
Browse files Browse the repository at this point in the history
…e_example

Create a database selection example.
  • Loading branch information
zhenlineo authored Mar 12, 2020
2 parents 5f133ae + 1b6f2c3 commit 59956a6
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 31 deletions.
31 changes: 1 addition & 30 deletions Neo4j.Driver/Neo4j.Driver.Tests.Integration/Direct/BoltV4IT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit.Abstractions;
using static Neo4j.Driver.IntegrationTests.VersionComparison;
using static Neo4j.Driver.SessionConfigBuilder;
using static Neo4j.Driver.IntegrationTests.DatabaseExtensions;

namespace Neo4j.Driver.IntegrationTests.Direct
{
Expand Down Expand Up @@ -228,33 +227,5 @@ private async Task VerifyDatabaseNameOnSummaryTxFunc(string name, string expecte
await session.CloseAsync();
}
}

private static async Task CreateDatabase(IDriver driver, string name)
{
var session = driver.AsyncSession(ForDatabase("system"));
try
{
var cursor = await session.RunAsync($"CREATE DATABASE {name}");
await cursor.ConsumeAsync();
}
finally
{
await session.CloseAsync();
}
}

private static async Task DropDatabase(IDriver driver, string name)
{
var session = driver.AsyncSession(ForDatabase("system"));
try
{
var cursor = await session.RunAsync($"DROP DATABASE {name}");
await cursor.ConsumeAsync();
}
finally
{
await session.CloseAsync();
}
}
}
}
80 changes: 80 additions & 0 deletions Neo4j.Driver/Neo4j.Driver.Tests.Integration/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Neo4j.Driver;
using Neo4j.Driver.IntegrationTests;
using Neo4j.Driver.IntegrationTests.Internals;
using static Neo4j.Driver.IntegrationTests.DatabaseExtensions;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -706,6 +707,85 @@ public void TestTransactionFunctionExample()
CountPerson("Alice").Should().Be(1);
}
}

public class DatabaseSelectionExampleTest : BaseExample
{
public DatabaseSelectionExampleTest(ITestOutputHelper output, StandAloneIntegrationTestFixture fixture)
: base(output, fixture)
{
}

[RequireEnterpriseEdition("4.0.0", VersionComparison.GreaterThanOrEqualTo)]
public async void TestUseAnotherDatabaseExample()
{
try
{
await DropDatabase(Driver, "examples");
}
catch (FatalDiscoveryException ex)
{
// Its a new server instance, the database didn't exist yet
}

await CreateDatabase(Driver, "examples");

// Given
using (var example = new DatabaseSelectionExample(Uri, User, Password))
{
// When
example.UseAnotherDatabaseExample();

// Then
var greetingCount = ReadInt("examples", "MATCH (a:Greeting) RETURN count(a)");
greetingCount.Should().Be(1);
}
}

private int ReadInt(string database, string query)
{
using (var session = Driver.Session(SessionConfigBuilder.ForDatabase(database)))
{
return session.Run(query).Single()[0].As<int>();
}
}

private class DatabaseSelectionExample : IDisposable
{
private readonly IDriver _driver;

public DatabaseSelectionExample(string uri, string user, string password)
{
_driver = GraphDatabase.Driver(uri, AuthTokens.Basic(user, password));
}

public void UseAnotherDatabaseExample()
{
// tag::database-selection[]
using (var session = _driver.Session(SessionConfigBuilder.ForDatabase("examples")))
{
session.Run("CREATE (a:Greeting {message: 'Hello, Example-Database'}) RETURN a").Consume();
}

void SessionConfig(SessionConfigBuilder configBuilder) =>
configBuilder.WithDatabase("examples")
.WithDefaultAccessMode(AccessMode.Read)
.Build();

using (var session = _driver.Session(SessionConfig))
{
var result = session.Run("MATCH (a:Greeting) RETURN a.message as msg");
var msg = result.Single()[0].As<string>();
Console.WriteLine(msg);
}
// end::database-selection[]
}

public void Dispose()
{
_driver?.Dispose();
}
}
}

[SuppressMessage("ReSharper", "xUnit1013")]
public class PassBookmarksExample : BaseExample
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2002-2020 "Neo4j,"
// Neo4j Sweden AB [http://neo4j.com]
//
// This file is part of Neo4j.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Threading.Tasks;
using static Neo4j.Driver.SessionConfigBuilder;

namespace Neo4j.Driver.IntegrationTests
{
public class DatabaseExtensions
{
public static async Task CreateDatabase(IDriver driver, string name)
{
var session = driver.AsyncSession(ForDatabase("system"));
try
{
var cursor = await session.RunAsync($"CREATE DATABASE {name}");
await cursor.ConsumeAsync();
}
finally
{
await session.CloseAsync();
}
}

public static async Task DropDatabase(IDriver driver, string name)
{
var session = driver.AsyncSession(ForDatabase("system"));
try
{
var cursor = await session.RunAsync($"DROP DATABASE {name}");
await cursor.ConsumeAsync();
}
finally
{
await session.CloseAsync();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private static BoltkitStatus TestBoltkitAvailability()
return BoltkitStatus.Installed;
}

private static bool IsEnterprise()
public static bool IsEnterprise()
{
var strings = BoltkitArgs.Split(null);
return strings.Contains("-e");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ public RequireServerFactAttribute(string versionText = null,
Skip = skipText.ToString();
}
}

public class RequireEnterpriseEdition : RequireServerFactAttribute
{
public RequireEnterpriseEdition(string versionText = null,
VersionComparison versionCompare = VersionComparison.EqualTo)
: base(versionText, versionCompare)
{
if (string.IsNullOrEmpty(Skip))
{

if (!BoltkitHelper.IsEnterprise())
{
Skip = "Test requires Neo4j enterprise edition.";
}
}
}
}

public class RequireServerWithIPv6FactAttribute : RequireServerFactAttribute
{
Expand Down

0 comments on commit 59956a6

Please sign in to comment.