200 lines
8.2 KiB
C#
200 lines
8.2 KiB
C#
using Microsoft.Data.SqlClient;
|
|
using NUnit.Framework;
|
|
using Sarand.Net.Standard.Base.Dbas;
|
|
using Sarand.Net.Standard.Dbas.Base;
|
|
using Sarand.Net.Standard.Dbas.Data;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.Common;
|
|
using System.Data.Odbc;
|
|
using System.Linq;
|
|
|
|
namespace Sacs.UnitTests.Net.Standard.Base.Dbas
|
|
{
|
|
[TestFixture]
|
|
public class DatabaseProvider_Tester
|
|
{
|
|
// --- Definitions ---
|
|
|
|
private class ParameterTestItem : DbDataItem
|
|
{
|
|
public ParameterTestItem() : base() { }
|
|
public override string SchemaName => "dbo";
|
|
public override string TableName => "TestItem";
|
|
public override void PopulateFrom(DbCommand dbCommand, string tableAlias, string filter) { }
|
|
public override void PopulateFrom(DbDataReader dbDataReader) { }
|
|
public override bool ExecuteInsertCommand(DbCommand dbCommand) => false;
|
|
public void AddParameter<T>(DbCommand command, string name, T value) => AddCommandParameter(command, name, value);
|
|
}
|
|
|
|
// --- Provider Selection ---
|
|
|
|
[Test]
|
|
public void OpenOdbcDatabase_CreatesSqlClientAndForcedOdbcConnections()
|
|
{
|
|
SacsDatabaseConsole.OdbcConfig config = new SacsDatabaseConsole.OdbcConfig
|
|
{
|
|
Dbms = DbmsType.MsSql,
|
|
DatabaseName = "TestDb",
|
|
Location = "MsSql:localhost\\TestDb",
|
|
ConnectionString = "Driver={SQL Server};Server=localhost;Database=TestDb;Trusted_Connection=Yes;"
|
|
};
|
|
|
|
SacsDatabase database = SacsDatabaseConsole.OpenOdbcDatabase(config);
|
|
using DbConnection sqlConnection = database.CreateConnection();
|
|
using DbConnection odbcConnection = database.CreateConnection(true);
|
|
|
|
Assert.That(sqlConnection, Is.TypeOf<SqlConnection>());
|
|
Assert.That(sqlConnection.ConnectionString, Does.Contain("Server=localhost"));
|
|
Assert.That(sqlConnection.ConnectionString, Does.Contain("Database=TestDb"));
|
|
Assert.That(odbcConnection, Is.TypeOf<OdbcConnection>());
|
|
Assert.That(odbcConnection.ConnectionString, Is.EqualTo(config.ConnectionString));
|
|
}
|
|
|
|
[Test]
|
|
public void OpenOdbcDatabase_CreatesOdbcConnectionForNonSqlDatabase()
|
|
{
|
|
SacsDatabaseConsole.OdbcConfig config = new SacsDatabaseConsole.OdbcConfig
|
|
{
|
|
Dbms = DbmsType.Postgres,
|
|
DatabaseName = "TestDb",
|
|
ConnectionString = "Driver={PostgreSQL Unicode};Server=localhost;Database=TestDb;"
|
|
};
|
|
|
|
SacsDatabase database = SacsDatabaseConsole.OpenOdbcDatabase(config);
|
|
using DbConnection connection = database.CreateConnection();
|
|
|
|
Assert.That(connection, Is.TypeOf<OdbcConnection>());
|
|
Assert.That(connection.ConnectionString, Is.EqualTo(config.ConnectionString));
|
|
}
|
|
|
|
[Test]
|
|
public void CreateDataAdapter_ReturnsOdbcAdapter()
|
|
{
|
|
SacsDatabaseConsole.OdbcConfig config = new SacsDatabaseConsole.OdbcConfig
|
|
{
|
|
Dbms = DbmsType.General,
|
|
DatabaseName = "TestDb",
|
|
ConnectionString = "Driver={Test Driver};Database=TestDb;"
|
|
};
|
|
|
|
SacsDatabase database = SacsDatabaseConsole.OpenOdbcDatabase(config);
|
|
using DbDataAdapter adapter = database.CreateDataAdapter();
|
|
|
|
Assert.That(adapter, Is.TypeOf<OdbcDataAdapter>());
|
|
}
|
|
|
|
// --- Command Parameters ---
|
|
|
|
[Test]
|
|
public void AddCommandParameter_AssignsValuesNullsAndExplicitTypes()
|
|
{
|
|
ParameterTestItem item = new ParameterTestItem();
|
|
using SqlCommand command = new SqlCommand();
|
|
|
|
item.AddParameter(command, "@Count", 12);
|
|
item.AddParameter<int?>(command, "@MissingCount", null);
|
|
item.AddParameter<byte[]>(command, "@Data", null);
|
|
|
|
Assert.That(command.Parameters[0].ParameterName, Is.EqualTo("@Count"));
|
|
Assert.That(command.Parameters[0].Value, Is.EqualTo(12));
|
|
Assert.That(command.Parameters[0].DbType, Is.EqualTo(DbType.Int32));
|
|
Assert.That(command.Parameters[1].Value, Is.EqualTo(DBNull.Value));
|
|
Assert.That(command.Parameters[1].DbType, Is.EqualTo(DbType.Int32));
|
|
Assert.That(command.Parameters[2].Value, Is.EqualTo(DBNull.Value));
|
|
Assert.That(command.Parameters[2].DbType, Is.EqualTo(DbType.Binary));
|
|
}
|
|
|
|
[Test]
|
|
public void AddCommandParameter_NormalisesSqlAndOdbcDates()
|
|
{
|
|
ParameterTestItem item = new ParameterTestItem();
|
|
DateTime value = new DateTime(2026, 9, 14, 10, 20, 30, 456, DateTimeKind.Utc);
|
|
using SqlCommand sqlCommand = new SqlCommand();
|
|
using OdbcCommand odbcCommand = new OdbcCommand();
|
|
|
|
item.AddParameter(sqlCommand, "@When", value);
|
|
item.AddParameter(odbcCommand, "When", value);
|
|
|
|
Assert.That(sqlCommand.Parameters[0].DbType, Is.EqualTo(DbType.DateTime2));
|
|
Assert.That(odbcCommand.Parameters[0].DbType, Is.EqualTo(DbType.DateTime));
|
|
Assert.That(sqlCommand.Parameters[0].Value, Is.EqualTo(value.AddMilliseconds(-456)));
|
|
Assert.That(odbcCommand.Parameters[0].Value, Is.EqualTo(value.AddMilliseconds(-456)));
|
|
}
|
|
|
|
[Test]
|
|
public void AddCommandParameter_MapsSentinelDatesToDatabaseNull()
|
|
{
|
|
ParameterTestItem item = new ParameterTestItem();
|
|
using SqlCommand command = new SqlCommand();
|
|
|
|
item.AddParameter(command, "@Minimum", DateTime.MinValue);
|
|
item.AddParameter<DateTime?>(command, "@Maximum", DateTime.MaxValue);
|
|
item.AddParameter<DateTime?>(command, "@Missing", null);
|
|
|
|
Assert.That(command.Parameters[0].Value, Is.EqualTo(DBNull.Value));
|
|
Assert.That(command.Parameters[1].Value, Is.EqualTo(DBNull.Value));
|
|
Assert.That(command.Parameters[2].Value, Is.EqualTo(DBNull.Value));
|
|
Assert.That(command.Parameters[0].DbType, Is.EqualTo(DbType.DateTime2));
|
|
Assert.That(command.Parameters[1].DbType, Is.EqualTo(DbType.DateTime2));
|
|
Assert.That(command.Parameters[2].DbType, Is.EqualTo(DbType.DateTime2));
|
|
}
|
|
|
|
[Test]
|
|
public void AddCommandParameter_CreatesOdbcValuesWithExpectedTypesAndOrder()
|
|
{
|
|
ParameterTestItem item = new ParameterTestItem();
|
|
Guid identifier = Guid.Parse("024f2943-12a6-46be-88cf-49980f81e603");
|
|
byte[] data = new byte[] { 1, 2, 3 };
|
|
using OdbcCommand command = new OdbcCommand();
|
|
|
|
item.AddParameter(command, "@Text", "value");
|
|
item.AddParameter(command, "@Short", (short)12);
|
|
item.AddParameter(command, "@Integer", 34);
|
|
item.AddParameter(command, "@Long", 56L);
|
|
item.AddParameter(command, "@Byte", (byte)78);
|
|
item.AddParameter(command, "@Single", 90.1f);
|
|
item.AddParameter(command, "@Double", 23.4d);
|
|
item.AddParameter(command, "@Decimal", 78.9m);
|
|
item.AddParameter(command, "@Boolean", true);
|
|
item.AddParameter(command, "@Identifier", identifier);
|
|
item.AddParameter(command, "@Data", data);
|
|
|
|
Assert.That(command.Parameters.Cast<OdbcParameter>().Select(parameter => parameter.ParameterName),
|
|
Is.EqualTo(new[] { "@Text", "@Short", "@Integer", "@Long", "@Byte", "@Single", "@Double", "@Decimal", "@Boolean", "@Identifier", "@Data" }));
|
|
Assert.That(command.Parameters.Cast<OdbcParameter>().Select(parameter => parameter.DbType),
|
|
Is.EqualTo(new[] { DbType.String, DbType.Int16, DbType.Int32, DbType.Int64, DbType.Byte, DbType.Single, DbType.Double, DbType.Decimal, DbType.Boolean, DbType.Guid, DbType.Binary }));
|
|
Assert.That(command.Parameters[0].Value, Is.EqualTo("value"));
|
|
Assert.That(command.Parameters[9].Value, Is.EqualTo(identifier));
|
|
Assert.That(command.Parameters[10].Value, Is.SameAs(data));
|
|
}
|
|
|
|
[Test]
|
|
public void AddCommandParameter_CreatesTypedOdbcNulls()
|
|
{
|
|
ParameterTestItem item = new ParameterTestItem();
|
|
using OdbcCommand command = new OdbcCommand();
|
|
|
|
item.AddParameter<short?>(command, "@Short", null);
|
|
item.AddParameter<int?>(command, "@Integer", null);
|
|
item.AddParameter<long?>(command, "@Long", null);
|
|
item.AddParameter<byte?>(command, "@Byte", null);
|
|
item.AddParameter<float?>(command, "@Single", null);
|
|
item.AddParameter<double?>(command, "@Double", null);
|
|
item.AddParameter<decimal?>(command, "@Decimal", null);
|
|
item.AddParameter<bool?>(command, "@Boolean", null);
|
|
item.AddParameter<Guid?>(command, "@Identifier", null);
|
|
item.AddParameter<DateTime?>(command, "@Date", null);
|
|
item.AddParameter<byte[]>(command, "@Data", null);
|
|
|
|
Assert.That(command.Parameters.Cast<OdbcParameter>().Select(parameter => parameter.Value),
|
|
Is.All.EqualTo(DBNull.Value));
|
|
Assert.That(command.Parameters.Cast<OdbcParameter>().Select(parameter => parameter.DbType),
|
|
Is.EqualTo(new[] { DbType.Int16, DbType.Int32, DbType.Int64, DbType.Byte, DbType.Single, DbType.Double, DbType.Decimal, DbType.Boolean, DbType.Guid, DbType.DateTime, DbType.Binary }));
|
|
}
|
|
|
|
// --- The End ---
|
|
}
|
|
}
|