neo4j / neo4j-dotnet-driver

Neo4j Bolt driver for .NET
Apache License 2.0
226 stars 69 forks source link

SessionConfig / SessionConfigBuilder not designed for testability #721

Closed 59977 closed 1 year ago

59977 commented 1 year ago

Is your feature request related to a problem? Please describe. Consider the following code:

using var session = this.Driver.AsyncSession(o =>
            {
                o.WithDatabase(this.currentDatabase);
                o.WithDefaultAccessMode(AccessMode.Read);
            });

How can I test that the correct database and access mode is set in a unit test in a reasonable way?

Describe the solution you'd like Public constructors for SessionConfig and SessionConfigBuilder would be one approach.

Describe alternatives you've considered I know it's possible to do this using reflection but that is not a reasonable solution in my opinion.

thelonelyvulpes commented 1 year ago

Hi @59977, Thanks for highlighting an issue, I appreciate this doesn't entirely fit the constraints of unit testing, but for testing your configuration is set, your config is accessible in resulting session's SessionConfig property.

so you can have a test like this

[Fact]
public void ShouldConfigureSessionCorrectly()
{
    // Because no connection is made to the database until a connection is actually needed you can do this.
    using var driver = GraphDatabase.Driver("bolt://localhost:7687", AuthTokens.None);
    using var session = driver.AsyncSession(x => x.WithDatabase("testDb").WithDefaultAccessMode(AccessMode.Read));

    Assert.Equal("testDb", session.SessionConfig.Database);
    Assert.Equal(AccessMode.Read, session.SessionConfig.DefaultAccessMode);
}