diff --git a/test/Atc.Kusto.Tests/Extensions/DataReaderExtensionsTests.cs b/test/Atc.Kusto.Tests/Extensions/DataReaderExtensionsTests.cs new file mode 100644 index 0000000..8ae7127 --- /dev/null +++ b/test/Atc.Kusto.Tests/Extensions/DataReaderExtensionsTests.cs @@ -0,0 +1,80 @@ +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 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()]); + + dataReader + .Read() + .Returns(c => ++index < data.Count); + + dataReader + .GetValues(default!) + .ReturnsForAnyArgs(c => c.Arg().CopyFrom(values[index], 0)); + + // Act + var actual = dataReader.ReadObjects(); + + // Assert + actual.Should().BeEquivalentTo(data); + } + + [Theory, AutoNSubstituteData] + public void CanReadObjects( + List 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()]); + + dataReader + .Read() + .Returns(c => ++index < data.Count); + + dataReader + .GetValues(default!) + .ReturnsForAnyArgs(c => c.Arg().CopyFrom(values[index], 0)); + + dataReader + .NextResult() + .Returns(true); + + // Act + var actual = dataReader.ReadObjectsFromNextResult(); + + // Assert + actual.Should().BeEquivalentTo(data); + dataReader.Received(1).NextResult(); + } +} \ No newline at end of file diff --git a/test/Atc.Kusto.Tests/GlobalUsings.cs b/test/Atc.Kusto.Tests/GlobalUsings.cs index 18c9f51..4ec4f1f 100644 --- a/test/Atc.Kusto.Tests/GlobalUsings.cs +++ b/test/Atc.Kusto.Tests/GlobalUsings.cs @@ -1,2 +1,4 @@ +global using System.Data; global using Atc.Kusto.Extensions.Internal; -global using Atc.Kusto.Providers.Internal; \ No newline at end of file +global using Atc.Kusto.Providers.Internal; +global using Kusto.Cloud.Platform.Utils; \ No newline at end of file