Skip to content

Commit

Permalink
Merge pull request #193 from ali-ince/1.5-async
Browse files Browse the repository at this point in the history
Improve tests & result builder classes
  • Loading branch information
zhenlineo committed Jul 4, 2017
2 parents a2f6e2c + e88daf6 commit 71bce90
Show file tree
Hide file tree
Showing 10 changed files with 1,137 additions and 442 deletions.
2 changes: 2 additions & 0 deletions Neo4j.Driver/Neo4j.Driver.Tests/Neo4j.Driver.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@
<Compile Include="AuthTokenTests.cs" />
<Compile Include="BookmarkTests.cs" />
<Compile Include="Connector\PooledConnectionTests.cs" />
<Compile Include="Result\ResultReaderBuilderTests.cs" />
<Compile Include="Result\StatementResultReaderTests.cs" />
<Compile Include="TestUtil\NetworkExtensionsTests.cs" />
<Compile Include="Result\SummaryBuilderTests.cs" />
<Compile Include="RetryLogicTests.cs" />
Expand Down
153 changes: 153 additions & 0 deletions Neo4j.Driver/Neo4j.Driver.Tests/Result/ResultReaderBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Copyright (c) 2002-2017 "Neo Technology,"
// Network Engine for Objects in Lund AB [http://neotechnology.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;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Neo4j.Driver.Internal.Result;
using Neo4j.Driver.V1;
using Xunit;

namespace Neo4j.Driver.Tests
{
public class ResultReaderBuilderTests
{
private static ResultReaderBuilder GenerateBuilder(IDictionary<string, object> meta = null)
{
var builder = new ResultReaderBuilder();
builder.CollectFields(meta ?? new Dictionary<string, object> { { "fields", new List<object> { "x" } } });
return builder;
}

private static Task AssertGetExpectResults(IStatementResultReader reader, int numberExpected, List<object> exspectedRecordsValues = null)
{
int count = 0;
var t = Task.Factory.StartNew(async () =>
{
// ReSharper disable once LoopCanBeConvertedToQuery
while (await reader.ReadAsync())
{
if (exspectedRecordsValues != null)
{
reader.Current().Values.First().Value.Should().Be(exspectedRecordsValues[count]);
}
count++;
}
count.Should().Be(numberExpected);
});
return t;
}

public class CollectRecordMethod
{
[Fact]
public void ShouldStreamResults()
{
var builder = GenerateBuilder();
var i = 0;
builder.SetReceiveOneFunc(() =>
{
if (i++ >= 3)
{
builder.CollectSummary(null);
}
else
{
builder.CollectRecord(new object[] {123 + i});
}
return Task.CompletedTask;
});
var result = builder.PreBuild();

var t = AssertGetExpectResults(result, 3, new List<object> {124, 125, 126});

t.Wait();
}

[Fact]
public void ShouldReturnNoResultsWhenNoneRecieved()
{
var builder = GenerateBuilder();
builder.SetReceiveOneFunc(() =>
{
builder.CollectSummary(null);
return Task.CompletedTask;
});
var result = builder.PreBuild();

var t = AssertGetExpectResults(result, 0);

t.Wait();
}

[Fact]
public void ShouldReturnQueuedResultsWithExspectedValue()
{
var builder = GenerateBuilder();
List<object> recordValues = new List<object>
{
1,
"Hello",
false,
10
};
for (int i = 0; i < recordValues.Count; i++)
{
builder.CollectRecord(new[] { recordValues[i] });
}
builder.CollectSummary(null);

var result = builder.PreBuild();

var task = AssertGetExpectResults(result, recordValues.Count, recordValues);
task.Wait();
}
}

public class InvalidateResultMethod
{
[Fact]
public void ShouldStopStreamingWhenResultIsInvalid()
{
var builder = GenerateBuilder();
var i = 0;
builder.SetReceiveOneFunc(() =>
{
if (i++ >= 3)
{
builder.DoneFailure();
}
else
{
builder.CollectRecord(new object[] {123 + i});
}
return Task.CompletedTask;
});
var result = builder.PreBuild();

var t = AssertGetExpectResults(result, 3, new List<object> { 124, 125, 126 });
t.Wait();
}
}
}
}
Loading

0 comments on commit 71bce90

Please sign in to comment.