-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix parameter binding in statements with cast. Fixes 245
- Loading branch information
Showing
3 changed files
with
94 additions
and
15 deletions.
There are no files selected for viewing
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
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,66 @@ | ||
using System; | ||
using DuckDB.NET.Data; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace DuckDB.NET.Test.Parameters; | ||
|
||
public class ExplicitCastTests(DuckDBDatabaseFixture db) : DuckDBTestBase(db) | ||
{ | ||
[Fact] | ||
public void CastWithFunction() | ||
{ | ||
using var command = Connection.CreateCommand(); | ||
command.CommandText = "SELECT strptime($date_as_text, '%Y-%m-%d %H:%M:%S') AS example;"; | ||
command.Parameters.Add(new DuckDBParameter("date_as_text", "2023-04-02 01:01:00")); | ||
var scalar = command.ExecuteScalar(); | ||
|
||
scalar.Should().BeOfType<DateTime>(); | ||
} | ||
|
||
[Fact] | ||
public void CastWithExplicitCastToTimestamp() | ||
{ | ||
using var command = Connection.CreateCommand(); | ||
command.CommandText = "SELECT $date_as_text::timestamp AS example;"; | ||
command.Parameters.Add(new DuckDBParameter("date_as_text", "2023-04-02 01:01:00")); | ||
var scalar = command.ExecuteScalar(); | ||
|
||
scalar.Should().BeOfType<DateTime>(); | ||
} | ||
|
||
[Fact] | ||
public void CastWithExplicitCastFromInt() | ||
{ | ||
using var command = Connection.CreateCommand(); | ||
command.CommandText = "SELECT $my_num::varchar AS example;"; | ||
command.Parameters.Add(new DuckDBParameter("my_num", 42)); | ||
var scalar = command.ExecuteScalar(); | ||
|
||
scalar.Should().BeOfType<string>(); | ||
scalar.Should().Be("42"); | ||
} | ||
|
||
[Fact] | ||
public void CastWithExplicitCastToInt() | ||
{ | ||
using var command = Connection.CreateCommand(); | ||
command.CommandText = "SELECT $my_num::int AS example;"; | ||
command.Parameters.Add(new DuckDBParameter("my_num", "42")); | ||
var scalar = command.ExecuteScalar(); | ||
|
||
scalar.Should().BeOfType<int>(); | ||
scalar.Should().Be(42); | ||
} | ||
|
||
[Fact] | ||
public void CastWithExplicitCastToIntWrong() | ||
{ | ||
using var command = Connection.CreateCommand(); | ||
command.CommandText = "SELECT $my_num::int AS example;"; | ||
command.Parameters.Add(new DuckDBParameter("my_num", "Giorgi")); | ||
|
||
command.Invoking(c => c.ExecuteScalar()) | ||
.Should().Throw<DuckDBException>().WithMessage("*Conversion Error*"); | ||
} | ||
} |
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