Skip to content

Commit

Permalink
test: add unit-tests for DataReaderExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
perkops committed Aug 20, 2024
1 parent a6206b2 commit cfea841
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
77 changes: 77 additions & 0 deletions test/Atc.Kusto.Tests/Extensions/DataReaderExtensionsTests.cs
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();
}
}
4 changes: 3 additions & 1 deletion test/Atc.Kusto.Tests/GlobalUsings.cs
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;

0 comments on commit cfea841

Please sign in to comment.