Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions Source/tests/Csla.test/Csla.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<PackageReference Include="MSTest.TestAdapter" Version="3.9.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.9.1" />
<PackageReference Include="System.Data.Common" Version="4.3.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.2" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
<PackageReference Include="System.Resources.Extensions" Version="10.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.0" />
Expand Down Expand Up @@ -81,12 +81,6 @@
</ItemGroup>

<ItemGroup>
<None Update="DataPortalTestDatabase.mdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="DataPortalTestDatabase_log.ldf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
23 changes: 6 additions & 17 deletions Source/tests/Csla.test/DPException/DataPortalExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public void Initialize()
TestResults.Reinitialise();
}

#if DEBUG
[TestMethod]
[TestCategory("SkipOnCIServer")]
public void CheckInnerExceptionsOnSave()
{
IDataPortal<DataPortal.TransactionalRoot> dataPortal = _testDIContext.CreateDataPortal<DataPortal.TransactionalRoot>();
Expand All @@ -43,7 +41,6 @@ public void CheckInnerExceptionsOnSave()
string baseException = string.Empty;
string baseInnerException = string.Empty;
string baseInnerInnerException = string.Empty;
string exceptionSource = string.Empty;

try
{
Expand All @@ -54,31 +51,23 @@ public void CheckInnerExceptionsOnSave()
baseException = ex.Message;
baseInnerException = ex.InnerException.Message;
baseInnerInnerException = ex.InnerException.InnerException?.Message;
exceptionSource = ex.InnerException.InnerException?.Source;
Assert.IsNull(ex.BusinessObject, "Business object shouldn't be returned");
}

//check base exception
Assert.IsTrue(baseException.StartsWith("DataPortal.Update failed"), "Exception should start with 'DataPortal.Update failed'");
Assert.IsTrue(baseException.Contains("String or binary data would be truncated."),
"Exception should contain 'String or binary data would be truncated.'");
Assert.IsTrue(baseException.Contains("CHECK constraint failed"),
"Exception should contain 'CHECK constraint failed'");
//check inner exception
Assert.AreEqual("TransactionalRoot.DataPortal_Insert method call failed", baseInnerException);
//check inner exception of inner exception
Assert.AreEqual("String or binary data would be truncated.\r\nThe statement has been terminated.", baseInnerInnerException);
//check inner exception of inner exception (SQLite CHECK constraint violation)
Assert.IsTrue(baseInnerInnerException.Contains("CHECK constraint failed"),
"Inner inner exception should contain 'CHECK constraint failed'");

//check what caused inner exception's inner exception (i.e. the root exception)
#if (NETFRAMEWORK)
Assert.AreEqual(".Net SqlClient Data Provider", exceptionSource);
#else
Assert.AreEqual("Core Microsoft SqlClient Data Provider", exceptionSource);
#endif

//verify that the implemented method, DataPortal_OnDataPortal
//verify that the implemented method, DataPortal_OnDataPortal
//was called for the business object that threw the exception
Assert.AreEqual("Called", TestResults.GetResult("OnDataPortalException"));
}
#endif

[TestMethod]
public void CheckInnerExceptionsOnDelete()
Expand Down
95 changes: 20 additions & 75 deletions Source/tests/Csla.test/DataPortal/DataPortalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//-----------------------------------------------------------------------

using Csla.Test.DataBinding;
using Microsoft.Data.SqlClient;
using System.Data.SQLite;
using Csla.TestHelpers;
using Microsoft.Extensions.DependencyInjection;
using Csla.Configuration;
Expand All @@ -19,7 +19,6 @@
using Csla.Server;
using System.Security.Principal;
using FluentAssertions.Execution;
using System.ComponentModel; // added

namespace Csla.Test.DataPortal
{
Expand All @@ -40,30 +39,17 @@ public void Initialize()
TestResults.Reinitialise();
}

private static string CONNECTION_STRING = WellKnownValues.DataPortalTestDatabase;
private static string CONNECTION_STRING => WellKnownValues.DataPortalTestDatabase;
public void ClearDataBase()
{
SqlConnection cn = new SqlConnection(CONNECTION_STRING);
SqlCommand cm = new SqlCommand("DELETE FROM Table2", cn);

try
{
cn.Open();
cm.ExecuteNonQuery();
}
catch (Exception)
{
//do nothing
}
finally
{
cn.Close();
}
using var cn = new SQLiteConnection(CONNECTION_STRING);
cn.Open();
using var cm = cn.CreateCommand();
cm.CommandText = "DELETE FROM Table2";
cm.ExecuteNonQuery();
}

#if DEBUG
[TestMethod]
[TestCategory("SkipOnCIServer")]
public void TestTransactionScopeUpdate()
{
IDataPortal<TransactionalRoot> dataPortal = _testDIContext.CreateDataPortal<TransactionalRoot>();
Expand All @@ -73,35 +59,17 @@ public void TestTransactionScopeUpdate()
tr.LastName = "Johnson";
tr.SmallColumn = "abc";

try
{
tr = tr.Save();
}
catch (Exception ex)
{
if (IsTransactionScopeEnvironmentUnavailable(ex))
Assert.Inconclusive("Skipping TransactionScope test: transactional infrastructure (MSDTC / distributed transactions) not available (0x89c5010a).");
throw;
}

SqlConnection cn = new SqlConnection(CONNECTION_STRING);
SqlCommand cm = new SqlCommand("SELECT * FROM Table2", cn);
tr = tr.Save();

try
using (var cn = new SQLiteConnection(CONNECTION_STRING))
{
cn.Open();
SqlDataReader dr = cm.ExecuteReader();
using var cm = cn.CreateCommand();
cm.CommandText = "SELECT * FROM Table2";
using var dr = cm.ExecuteReader();

//will have rows since no exception was thrown on the insert
Assert.AreEqual(true, dr.HasRows);
dr.Close();
}
catch (Exception)
{
//do nothing
}
finally
{
cn.Close();
}

ClearDataBase();
Expand All @@ -117,47 +85,24 @@ public void TestTransactionScopeUpdate()
}
catch (Exception ex)
{
if (IsTransactionScopeEnvironmentUnavailable(ex))
Assert.Inconclusive("Skipping TransactionScope test: transactional infrastructure (MSDTC / distributed transactions) not available (0x89c5010a).");
Assert.IsTrue(ex.Message.StartsWith("DataPortal.Update failed"), "Invalid exception message");
}

try
//within the DataPortal_Insert method, two commands are run to insert data into
//the database. Here we verify that both commands have been rolled back
using (var cn = new SQLiteConnection(CONNECTION_STRING))
{
cn.Open();
SqlDataReader dr = cm.ExecuteReader();
using var cm = cn.CreateCommand();
cm.CommandText = "SELECT * FROM Table2";
using var dr = cm.ExecuteReader();

//should not have rows since both commands were rolled back
Assert.AreEqual(false, dr.HasRows);
dr.Close();
}
catch (Exception)
{
//do nothing
}
finally
{
cn.Close();
}

ClearDataBase();
}
#endif

private static bool IsTransactionScopeEnvironmentUnavailable(Exception ex)
{
// Walk inner exceptions to find Win32Exception 0x89c5010a
while (ex != null)
{
if (ex is Win32Exception w32)
{
if (w32.NativeErrorCode == unchecked((int)0x89c5010a) ||
w32.Message.Contains("0x89c5010a", StringComparison.OrdinalIgnoreCase))
return true;
}
ex = ex.InnerException;
}
return false;
}

[TestMethod]
public void StronglyTypedDataPortalMethods()
Expand Down
24 changes: 10 additions & 14 deletions Source/tests/Csla.test/DataPortal/TransactionalRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// <summary>no summary</summary>
//-----------------------------------------------------------------------

using Microsoft.Data.SqlClient;
using System.Data.SQLite;

namespace Csla.Test.DataPortal
{
Expand Down Expand Up @@ -119,33 +119,29 @@ protected void DataPortal_Fetch(object criteria)
[Transactional(TransactionalTypes.TransactionScope)]
[Insert]
protected void DataPortal_Insert()
{
SqlConnection cn = new SqlConnection(WellKnownValues.DataPortalTestDatabase);
{
using var cn = new SQLiteConnection(WellKnownValues.DataPortalTestDatabase);
string firstName = FirstName;
string lastName = LastName;
string smallColumn = SmallColumn;

cn.Open();

//this command will always execute successfully
//since it inserts a string less than 5 characters
//into SmallColumn
SqlCommand cm1 = new SqlCommand();
cm1.Connection = cn;
using var cm1 = cn.CreateCommand();
cm1.CommandText = "INSERT INTO Table2(FirstName, LastName, SmallColumn) VALUES('Bill', 'Thompson', 'abc')";

//this command will throw an exception
//if SmallColumn is set to a string longer than
//if SmallColumn is set to a string longer than
//5 characters
SqlCommand cm2 = new SqlCommand();
cm2.Connection = cn;
//use stringbuilder
cm2.CommandText = "INSERT INTO Table2(FirstName, LastName, SmallColumn) VALUES('";
cm2.CommandText += firstName;
cm2.CommandText += "', '" + lastName + "', '" + smallColumn + "')";
using var cm2 = cn.CreateCommand();
cm2.CommandText = "INSERT INTO Table2(FirstName, LastName, SmallColumn) VALUES('" +
firstName + "', '" + lastName + "', '" + smallColumn + "')";

cn.Open();
cm1.ExecuteNonQuery();
cm2.ExecuteNonQuery();
cn.Close();

TestResults.Reinitialise();
TestResults.Add("TransactionalRoot", "Inserted");
Expand Down
Loading
Loading