Skip to content

Commit

Permalink
Merge pull request #62 from IvaYan/datetime-tests
Browse files Browse the repository at this point in the history
Add tests with table for DateTime types
  • Loading branch information
Giorgi committed Aug 27, 2022
2 parents f86ec04 + 997334a commit 29959ef
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
46 changes: 46 additions & 0 deletions DuckDB.NET.Test/Parameters/DateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,50 @@ public void BindWithCastTest(int year, int mon, int day)
var convertedValue = (DateTime) dateOnly;
convertedValue.Should().Be(dateTime);
}

[Theory]
[InlineData(1992, 09, 20)]
[InlineData(2022, 05, 04)]
[InlineData(2022, 04, 05)]
public void InsertAndQueryTest(int year, byte mon, byte day)
{
using var connection = new DuckDBConnection(DuckDBConnectionStringBuilder.InMemoryConnectionString);
connection.Open();

using var cmd = connection.CreateCommand();
cmd.CommandText = "CREATE TABLE DateOnlyTestTable (a INTEGER, b DATE);";
cmd.ExecuteNonQuery();

cmd.CommandText = "INSERT INTO DateOnlyTestTable (a, b) VALUES (42, ?);";
cmd.Parameters.Add(new DuckDBParameter(new DuckDBDateOnly {Year = year, Month = mon, Day = day}));
cmd.ExecuteNonQuery();

cmd.Parameters.Clear();
cmd.CommandText = "SELECT * FROM DateOnlyTestTable LIMIT 1;";

var reader = cmd.ExecuteReader();
reader.Read();

reader.GetFieldType(1).Should().Be(typeof(DuckDBDateOnly));

var dateOnly = reader.GetDateOnly(1);

dateOnly.Year.Should().Be(year);
dateOnly.Month.Should().Be(mon);
dateOnly.Day.Should().Be(day);

var dateTime = dateOnly.ToDateTime();
dateTime.Year.Should().Be(year);
dateTime.Month.Should().Be(mon);
dateTime.Day.Should().Be(day);
dateTime.Hour.Should().Be(0);
dateTime.Minute.Should().Be(0);
dateTime.Second.Should().Be(0);

var convertedValue = (DateTime) dateOnly;
convertedValue.Should().Be(dateTime);

cmd.CommandText = "DROP TABLE DateOnlyTestTable;";
cmd.ExecuteNonQuery();
}
}
51 changes: 51 additions & 0 deletions DuckDB.NET.Test/Parameters/TimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,55 @@ public void BindWithCastTest(int hour, int minute, int second, int millisecond)
var convertedValue = (DateTime) timeOnly;
convertedValue.Should().Be(dateTime);
}

[Theory]
[InlineData(12, 15, 17, 350)]
[InlineData(12, 17, 15, 450)]
[InlineData(18, 15, 17, 125)]
public void InsertAndQueryTest(byte hour, byte minute, byte second, int millisecond)
{
using var connection = new DuckDBConnection(DuckDBConnectionStringBuilder.InMemoryConnectionString);
connection.Open();

var expectedValue = new DateTime(DateTime.MinValue.Year, DateTime.MinValue.Month, DateTime.MinValue.Day,
hour, minute, second, millisecond);

using var cmd = connection.CreateCommand();
cmd.CommandText = "CREATE TABLE TimeOnlyTestTable (a INTEGER, b TIME);";
cmd.ExecuteNonQuery();

cmd.CommandText = "INSERT INTO TimeOnlyTestTable (a, b) VALUES (42, ?);";
cmd.Parameters.Add(new DuckDBParameter((DuckDBTimeOnly)expectedValue));
cmd.ExecuteNonQuery();

cmd.Parameters.Clear();
cmd.CommandText = "SELECT * FROM TimeOnlyTestTable LIMIT 1;";

var reader = cmd.ExecuteReader();
reader.Read();

reader.GetFieldType(1).Should().Be(typeof(DuckDBTimeOnly));

var timeOnly = reader.GetTimeOnly(1);

timeOnly.Hour.Should().Be(hour);
timeOnly.Min.Should().Be(minute);
timeOnly.Sec.Should().Be(second);
timeOnly.Msec.Should().Be(millisecond);

var dateTime = timeOnly.ToDateTime();
dateTime.Year.Should().Be(DateTime.MinValue.Year);
dateTime.Month.Should().Be(DateTime.MinValue.Month);
dateTime.Day.Should().Be(DateTime.MinValue.Day);
dateTime.Hour.Should().Be(hour);
dateTime.Minute.Should().Be(minute);
dateTime.Second.Should().Be(second);
dateTime.Millisecond.Should().Be(millisecond);

var convertedValue = (DateTime) timeOnly;
convertedValue.Should().Be(dateTime);

cmd.CommandText = "DROP TABLE TimeOnlyTestTable;";
cmd.ExecuteNonQuery();
}
}
43 changes: 43 additions & 0 deletions DuckDB.NET.Test/Parameters/TimestampTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,47 @@ public void BindTest(int year, int mon, int day, int hour, int minute, int secon

receivedTime.Should().Be(expectedValue);
}

[Theory]
[InlineData(1992, 09, 20, 12, 15, 17, 350)]
[InlineData(2022, 05, 04, 12, 17, 15, 450)]
[InlineData(2022, 04, 05, 18, 15, 17, 125)]
public void InsertAndQueryTest(int year, int mon, int day, byte hour, byte minute, byte second, int millisecond)
{
using var connection = new DuckDBConnection(DuckDBConnectionStringBuilder.InMemoryConnectionString);
connection.Open();

var expectedValue = new DateTime(year, mon, day, hour, minute, second, millisecond);

using var cmd = connection.CreateCommand();
cmd.CommandText = "CREATE TABLE TimestampTestTable (a INTEGER, b TIMESTAMP);";
cmd.ExecuteNonQuery();

cmd.CommandText = "INSERT INTO TimestampTestTable (a, b) VALUES (42, ?);";
cmd.Parameters.Add(new DuckDBParameter(expectedValue));
cmd.ExecuteNonQuery();

cmd.Parameters.Clear();
cmd.CommandText = "SELECT * FROM TimestampTestTable LIMIT 1;";

var reader = cmd.ExecuteReader();
reader.Read();

reader.GetFieldType(1).Should().Be(typeof(DateTime));

var dateTime = reader.GetDateTime(1);

dateTime.Year.Should().Be(year);
dateTime.Month.Should().Be(mon);
dateTime.Day.Should().Be(day);
dateTime.Hour.Should().Be(hour);
dateTime.Minute.Should().Be(minute);
dateTime.Second.Should().Be(second);
dateTime.Millisecond.Should().Be(millisecond);

expectedValue.Should().Be(dateTime);

cmd.CommandText = "DROP TABLE TimestampTestTable;";
cmd.ExecuteNonQuery();
}
}

0 comments on commit 29959ef

Please sign in to comment.