Files
DP44/DataPRO/DASFactoryDb.Tests/DbWrapperShould.cs
2026-04-17 14:55:32 -04:00

205 lines
6.2 KiB
C#

using NSubstitute;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DASFactoryDb.Tests
{
[TestFixture]
class DbWrapperShould
{
DbWrapper sut;
[SetUp]
public void SetUp()
{
//Arrange
sut = DbWrapper.Connection;
}
[TearDown]
public void TearDown()
{
sut.ResetLocalDASFactoryConnection();
}
[Test]
public void UserName_ShouldSetAndGet()
{
//Act
sut.Username = "Name";
var result = sut.Username;
//Assert
Assert.That(result, Is.EqualTo("Name"));
}
[Test]
public void Password_ShouldSetAndGet()
{
//Act
sut.Password = "Pass";
var result = sut.Password;
//Assert
Assert.That(result, Is.EqualTo("Pass"));
}
[Test]
public void DBName_ShouldSetAndGet()
{
//Act
sut.DBName = "DataPro";
var result = sut.DBName;
//Assert
Assert.That(result, Is.EqualTo("DataPro"));
}
[Test]
public void Server_ShouldSetAndGet()
{
//Act
sut.Server = "localdb\\DataProInstance";
var result = sut.Server;
//Assert
Assert.That(result, Is.EqualTo("localdb\\DataProInstance"));
}
[Test]
public void GetLocalDASFactoryConnectionString_NullServer_ShouldThrowException()
{
//Arrange
sut.Server = null;
//Act & Assert
Assert.That(DbWrapper._usingNTLMAuthentication, Is.EqualTo(true));
Assert.That(
() => sut.GetLocalDASFactoryConnectionString()
, Throws.TypeOf<Exception>()
.With
.Property("Message")
.EqualTo("Empty Server"));
}
[Test]
public void GetLocalDASFactoryConnectionString_DefaultState()
{
//Arrange
sut.Server = "MyServer";
//Act
var result = sut.GetLocalDASFactoryConnectionString();
//Assert
Assert.That(DbWrapper._usingNTLMAuthentication, Is.EqualTo(true));
Assert.That(result, Is.EqualTo("Server=MyServer;Database=DASFactory;Trusted_Connection=TRUE;"));
}
[Test]
public void GetLocalDASFactoryConnectionString_ServerHasValue()
{
//Arrange
sut.Server = "TestServer";
//Act
var result = sut.GetLocalDASFactoryConnectionString();
//Assert
// Assert.That(sut.UsingNTLMAuthentication , Is.EqualTo(true));
Assert.That(DbWrapper._usingNTLMAuthentication, Is.EqualTo(true));
Assert.That(result, Is.EqualTo("Server=TestServer;Database=DASFactory;Trusted_Connection=TRUE;"));
}
[Test]
public void GetLocalDASFactoryConnectionString_ServerHasValueAndFalseUsingNTLMAuthentication()
{
//Arrange
sut.Server = "TestServer";
DbWrapper._usingNTLMAuthentication = false;
//Act
var result = sut.GetLocalDASFactoryConnectionString();
//Assert
Assert.That(result, Is.EqualTo("Server=TestServer;Database=DASFactory;User Id=;Password=;"));
}
[Test]
public void GetLocalDASFactoryConnectionString_ServerHasValueAndFalseUsingNTLMAuthentication_SwitchToTrueUsingNTLMAuthentication()
{
//Arrange
sut.Server = "TestServer";
DbWrapper._usingNTLMAuthentication = false;
//Act
var result = sut.GetLocalDASFactoryConnectionString();
//Assert
Assert.That(result, Is.EqualTo("Server=TestServer;Database=DASFactory;User Id=;Password=;"));
//Arrange
//Important to reset the connection otherwise the class needs to be redesgined to not be singleton
sut.ResetLocalDASFactoryConnection();
//Act
result = sut.GetLocalDASFactoryConnectionString();
//Assert
Assert.That(result, Is.EqualTo("Server=TestServer;Database=DASFactory;Trusted_Connection=TRUE;"));
}
[Ignore("Not testing DAL")]
[Test]
public void GetDASFactoryCommand_ShouldBeOpened()
{
//Arrange
string connectionString = "Server=TestServer;Database=DASFactory;Trusted_Connection=TRUE;";
System.Data.IDbCommand dbCommand = Substitute.For<System.Data.IDbCommand>();
System.Data.IDbConnection dbConnection = Substitute.For<System.Data.IDbConnection>();
dbConnection.ConnectionString = connectionString;
dbConnection.State.Returns(System.Data.ConnectionState.Open);
//Act
System.Data.IDbCommand result = null;
//Uncomment if decided to unit test the Data Access Layer
//result = DbWrapper.GetDASFactoryCommand(dbCommand, dbConnection);
Assert.That(result, Is.Not.Null);
var state = result.Connection.State;
//Assert
Assert.That(result.Connection.State, Is.EqualTo(System.Data.ConnectionState.Open));
Assert.That(result.Connection.ConnectionString, Is.Not.Empty.And.EqualTo(connectionString));
}
[Test]
public void GetDeviceId_ShouldThrowException_WhenNotConnected()
{
//Arrange
DbWrapper.Connected = false;
//Act & Assert
Assert.That(
() => DbWrapper.GetDeviceId("SOMESERIAL")
, Throws.TypeOf<Exception>()
.With
.Property("Message")
.EqualTo("Not connected"));
}
[Test]
public void Connection_ShouldBeSameReference()
{
Assert.That(DbWrapper.Connection, Is.SameAs(DbWrapper.Connection));
}
}
}