-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit-tests for DataReaderExtensions
- Loading branch information
Showing
2 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
test/Atc.Kusto.Tests/Extensions/DataReaderExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
namespace Atc.Kusto.Tests.Extensions; | ||
|
||
public sealed class DataReaderExtensionsTests | ||
{ | ||
public record TestObject(string Property1, string Property2, string Property3); | ||
|
||
[Theory, AutoNSubstituteData] | ||
public void ReadObjects_Will_Return_Objects_Read_From_DataReader( | ||
List<TestObject> data, | ||
IDataReader dataReader) | ||
{ | ||
// Arrange | ||
var properties = typeof(TestObject).GetProperties(); | ||
var fieldNames = properties.Select(p => p.Name).ToArray(); | ||
var values = data.Select(d => properties.Select(p => p.GetValue(d)!).ToArray()).ToArray(); | ||
var index = -1; | ||
|
||
dataReader.FieldCount | ||
.Returns(fieldNames.Length); | ||
|
||
dataReader | ||
.GetName(default) | ||
.ReturnsForAnyArgs(c => fieldNames[c.Arg<int>()]); | ||
|
||
dataReader | ||
.Read() | ||
.Returns(c => ++index < data.Count); | ||
|
||
dataReader | ||
.GetValues(default!) | ||
.ReturnsForAnyArgs(c => c.Arg<object[]>().CopyFrom(values[index], 0)); | ||
|
||
// Act | ||
var actual = dataReader.ReadObjects<TestObject>(); | ||
|
||
// Assert | ||
actual.Should().BeEquivalentTo(data); | ||
} | ||
|
||
[Theory, AutoNSubstituteData] | ||
public void CanReadObjects( | ||
List<TestObject> data, | ||
IDataReader dataReader) | ||
{ | ||
// Arrange | ||
var properties = typeof(TestObject).GetProperties(); | ||
var fieldNames = properties.Select(p => p.Name).ToArray(); | ||
var values = data.Select(d => properties.Select(p => p.GetValue(d)!).ToArray()).ToArray(); | ||
var index = -1; | ||
|
||
dataReader.FieldCount | ||
.Returns(fieldNames.Length); | ||
|
||
dataReader | ||
.GetName(default) | ||
.ReturnsForAnyArgs(c => fieldNames[c.Arg<int>()]); | ||
|
||
dataReader | ||
.Read() | ||
.Returns(c => ++index < data.Count); | ||
|
||
dataReader | ||
.GetValues(default!) | ||
.ReturnsForAnyArgs(c => c.Arg<object[]>().CopyFrom(values[index], 0)); | ||
|
||
dataReader | ||
.NextResult() | ||
.Returns(true); | ||
|
||
// Act | ||
var actual = dataReader.ReadObjectsFromNextResult<TestObject>(); | ||
|
||
// Assert | ||
actual.Should().BeEquivalentTo(data); | ||
dataReader.Received(1).NextResult(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
global using System.Data; | ||
global using Atc.Kusto.Extensions.Internal; | ||
global using Atc.Kusto.Providers.Internal; | ||
global using Atc.Kusto.Providers.Internal; | ||
global using Kusto.Cloud.Platform.Utils; |