|
| 1 | +using System; |
| 2 | +using System.Data.Common; |
| 3 | +using DuckDB.NET.Data; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace Dapper.Tests |
| 7 | +{ |
| 8 | + public class DuckDBProvider : DatabaseProvider |
| 9 | + { |
| 10 | + public override DbProviderFactory Factory => DuckDBClientFactory.Instance; |
| 11 | + public override string GetConnectionString() => "Data Source=:memory:"; |
| 12 | + } |
| 13 | + |
| 14 | + public abstract class DuckDBTypeTestBase : TestBase<DuckDBProvider> |
| 15 | + { |
| 16 | + protected DuckDBConnection GetDuckDBConnection(bool open = true) |
| 17 | + => (DuckDBConnection)(open ? Provider.GetOpenConnection() : Provider.GetClosedConnection()); |
| 18 | + |
| 19 | + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] |
| 20 | + public class FactDuckDBAttribute : FactAttribute |
| 21 | + { |
| 22 | + public override string? Skip |
| 23 | + { |
| 24 | + get { return unavailable ?? base.Skip; } |
| 25 | + set { base.Skip = value; } |
| 26 | + } |
| 27 | + |
| 28 | + private static readonly string? unavailable; |
| 29 | + |
| 30 | + static FactDuckDBAttribute() |
| 31 | + { |
| 32 | + try |
| 33 | + { |
| 34 | + using var _ = DatabaseProvider<DuckDBProvider>.Instance.GetOpenConnection(); |
| 35 | + } |
| 36 | + catch (Exception ex) |
| 37 | + { |
| 38 | + unavailable = $"DuckDB is unavailable: {ex.Message}"; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public class DuckDBTests : DuckDBTypeTestBase |
| 45 | + { |
| 46 | + [FactDuckDB] |
| 47 | + public void DuckDBNamedParameter() |
| 48 | + { |
| 49 | + using var connection = GetDuckDBConnection(); |
| 50 | + |
| 51 | + var result = connection.QueryFirst<int>("Select $foo", new {foo = 42}); |
| 52 | + Assert.Equal(42, result); |
| 53 | + } |
| 54 | + |
| 55 | + [FactDuckDB] |
| 56 | + public void DuckDBPositionalParameter() |
| 57 | + { |
| 58 | + using var connection = GetDuckDBConnection(); |
| 59 | + |
| 60 | + var dp = new DynamicParameters(); |
| 61 | + dp.Add("?", 42); |
| 62 | + |
| 63 | + var result = connection.QueryFirst<int>("Select ?", dp); |
| 64 | + Assert.Equal(42, result); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments