Skip to content

Commit

Permalink
add tests for OnGetValue override
Browse files Browse the repository at this point in the history
  • Loading branch information
David Coe committed Nov 21, 2024
1 parent 2c9108b commit a693d56
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
10 changes: 5 additions & 5 deletions csharp/src/Client/AdbcDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public sealed class AdbcDataReader : DbDataReader, IDbColumnSchemaGenerator
private int recordsAffected = -1;

/// <summary>
/// An event that is raised when a value is read from an Arrow array.
/// An event that is raised when a value is read from an IArrowArray.
/// </summary>
/// <remarks>
/// Callers may opt to provide overrides for parsing values.
Expand Down Expand Up @@ -86,7 +86,7 @@ internal AdbcDataReader(AdbcCommand adbcCommand, QueryResult adbcQueryResult, De

public override object this[int ordinal] => GetValue(ordinal);

public override object this[string name] => GetValue(this.RecordBatch.Column(name), GetOrdinal(name)) ?? DBNull.Value;
public override object this[string name] => GetValue(this.RecordBatch.Column(name)) ?? DBNull.Value;

public override int Depth => 0;

Expand Down Expand Up @@ -247,7 +247,7 @@ public override string GetString(int ordinal)

public override object GetValue(int ordinal)
{
object? value = GetValue(this.RecordBatch.Column(ordinal), ordinal);
object? value = GetValue(this.RecordBatch.Column(ordinal));

if (value == null)
return DBNull.Value;
Expand Down Expand Up @@ -362,10 +362,10 @@ public ReadOnlyCollection<AdbcColumn> GetAdbcColumnSchema()
/// </summary>
/// <param name="arrowArray"></param>
/// <returns></returns>
public object? GetValue(IArrowArray arrowArray, int ordinal)
private object? GetValue(IArrowArray arrowArray)
{
// if the OnGetValue event is set, call it
object? result = OnGetValue?.Invoke(arrowArray, ordinal);
object? result = OnGetValue?.Invoke(arrowArray, this.currentRowInRecordBatch);

// if the value is null, try to get the value from the ArrowArray
result = result ?? arrowArray.ValueAt(this.currentRowInRecordBatch);
Expand Down
86 changes: 86 additions & 0 deletions csharp/test/Apache.Arrow.Adbc.Tests/Client/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Apache.Arrow.Adbc.Client;
Expand All @@ -43,6 +44,56 @@ public void TestDecimalValues(DecimalBehavior decimalBehavior, string value, int
Assert.True(rdrValue.GetType().Equals(expectedType));
}

/// <summary>
/// Demonstrates the OnGetValue method of an AdbcDataReader.
/// </summary>
/// <param name="treatIntegersAsStrings">True/False to treat integers as strings.</param>
[Theory]
[InlineData(true)]
[InlineData(false)]
public void TestOnGetValue(bool treatIntegersAsStrings)
{
AdbcDataReader rdr = GetMoqDataReaderForIntegers();

if (treatIntegersAsStrings)
{
rdr.OnGetValue += (o, e) =>
{
if (o != null)
{
Int32Array? ints = o as Int32Array;
if (ints != null)
{
int? value = ints.GetValue(e);
if (value.HasValue)
return value.Value.ToString();
else
return string.Empty;
}
}
return string.Empty;
};
}

while (rdr.Read())
{
object? rdrValue = rdr.GetValue(0);

if (treatIntegersAsStrings)
{
Assert.True(rdrValue.GetType().Equals(typeof(string)));
}
else
{
if (rdrValue != DBNull.Value)
Assert.True(rdrValue.GetType().Equals(typeof(int)));
}
}
}

private AdbcDataReader GetMoqDataReader(DecimalBehavior decimalBehavior, string value, int precision, int scale)
{
SqlDecimal sqlDecimal = SqlDecimal.Parse(value);
Expand Down Expand Up @@ -80,6 +131,41 @@ private AdbcDataReader GetMoqDataReader(DecimalBehavior decimalBehavior, string
AdbcDataReader reader = cmd.ExecuteReader();
return reader;
}

private AdbcDataReader GetMoqDataReaderForIntegers()
{
List<KeyValuePair<string, string>> metadata = new List<KeyValuePair<string, string>>();
List<Field> fields = new List<Field>();
fields.Add(new Field("TestIntegers", new Int32Type(), true, metadata));

Schema schema = new Schema(fields, metadata);
Int32Array.Builder numbersBuilder = new Int32Array.Builder();
numbersBuilder.AppendRange(new List<int>() { 1, 2, 3 });
numbersBuilder.AppendNull(); //null for #4
numbersBuilder.Append(5);

Int32Array numbersArray = numbersBuilder.Build();

List<IArrowArray> values = new List<IArrowArray>() { numbersArray };

List<RecordBatch> records = new List<RecordBatch>()
{
new RecordBatch(schema, values, numbersArray.Count())
};

MockArrayStream mockArrayStream = new MockArrayStream(schema, records);
QueryResult queryResult = new QueryResult(1, mockArrayStream);

Mock<AdbcStatement> mockStatement = new Mock<AdbcStatement>();
mockStatement.Setup(x => x.ExecuteQuery()).Returns(queryResult); ;

Adbc.Client.AdbcConnection mockConnection = new Adbc.Client.AdbcConnection();

AdbcCommand cmd = new AdbcCommand(mockStatement.Object, mockConnection);

AdbcDataReader reader = cmd.ExecuteReader();
return reader;
}
}

class MockArrayStream : IArrowArrayStream
Expand Down

0 comments on commit a693d56

Please sign in to comment.