From 44cf1ee60caf831e6d0ec193899d23cd9eed9f84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Ordo=C3=B1ez?= Date: Thu, 25 Feb 2021 11:25:27 -0300 Subject: [PATCH] Added tests and upgraded to version 2.6.1. --- .../CqlDatabaseConnector.cs | 3 + .../Paradigm.ORM.Data.Cassandra.csproj | 2 +- .../MySqlDatabaseConnector.cs | 3 + .../Paradigm.ORM.Data.MySql.csproj | 2 +- .../Paradigm.ORM.Data.PostgreSql.csproj | 2 +- .../PostgreSqlDatabaseConnector.cs | 3 + .../Paradigm.ORM.Data.SqlServer.csproj | 2 +- .../SqlDatabaseConnector.cs | 3 + .../Paradigm.ORM.Data.csproj | 2 +- .../Fixtures/ConnectionStrings.cs | 8 +- .../Tests/TransactionTest.cs | 132 ++++++++++++++++++ .../Tests/MySql/DatabaseAccessTest.vb | 2 +- .../Tests/MySql/StoredProcedureTest.vb | 2 +- 13 files changed, 155 insertions(+), 11 deletions(-) create mode 100644 src/Paradigm.ORM.Tests/Tests/TransactionTest.cs diff --git a/src/Paradigm.ORM.Data.Cassandra/CqlDatabaseConnector.cs b/src/Paradigm.ORM.Data.Cassandra/CqlDatabaseConnector.cs index 4af1ebf..e4d121b 100644 --- a/src/Paradigm.ORM.Data.Cassandra/CqlDatabaseConnector.cs +++ b/src/Paradigm.ORM.Data.Cassandra/CqlDatabaseConnector.cs @@ -239,6 +239,9 @@ public bool IsOpen() /// public IDatabaseTransaction CreateTransaction() { + if (!this.IsOpen()) + this.Open(); + return this.CreateTransaction(IsolationLevel.Serializable); } diff --git a/src/Paradigm.ORM.Data.Cassandra/Paradigm.ORM.Data.Cassandra.csproj b/src/Paradigm.ORM.Data.Cassandra/Paradigm.ORM.Data.Cassandra.csproj index 42b9b37..5ebe205 100644 --- a/src/Paradigm.ORM.Data.Cassandra/Paradigm.ORM.Data.Cassandra.csproj +++ b/src/Paradigm.ORM.Data.Cassandra/Paradigm.ORM.Data.Cassandra.csproj @@ -2,7 +2,7 @@ netstandard2.1 - 2.6.0 + 2.6.1 Miracle Devs Miracle Devs Paradigm.ORM diff --git a/src/Paradigm.ORM.Data.MySql/MySqlDatabaseConnector.cs b/src/Paradigm.ORM.Data.MySql/MySqlDatabaseConnector.cs index bc58b01..8d92c27 100644 --- a/src/Paradigm.ORM.Data.MySql/MySqlDatabaseConnector.cs +++ b/src/Paradigm.ORM.Data.MySql/MySqlDatabaseConnector.cs @@ -239,6 +239,9 @@ public bool IsOpen() /// public IDatabaseTransaction CreateTransaction() { + if (!this.IsOpen()) + this.Open(); + return this.CreateTransaction(IsolationLevel.Serializable); } diff --git a/src/Paradigm.ORM.Data.MySql/Paradigm.ORM.Data.MySql.csproj b/src/Paradigm.ORM.Data.MySql/Paradigm.ORM.Data.MySql.csproj index 8ef4d39..65554ab 100644 --- a/src/Paradigm.ORM.Data.MySql/Paradigm.ORM.Data.MySql.csproj +++ b/src/Paradigm.ORM.Data.MySql/Paradigm.ORM.Data.MySql.csproj @@ -12,7 +12,7 @@ git Miracle Devs Miracle Devs - 2.6.0 + 2.6.1 Paradigm.ORM diff --git a/src/Paradigm.ORM.Data.PostgreSql/Paradigm.ORM.Data.PostgreSql.csproj b/src/Paradigm.ORM.Data.PostgreSql/Paradigm.ORM.Data.PostgreSql.csproj index 9b1381e..b9896b3 100644 --- a/src/Paradigm.ORM.Data.PostgreSql/Paradigm.ORM.Data.PostgreSql.csproj +++ b/src/Paradigm.ORM.Data.PostgreSql/Paradigm.ORM.Data.PostgreSql.csproj @@ -11,7 +11,7 @@ Miracle Devs Miracle Devs true - 2.6.0 + 2.6.1 Paradigm.ORM diff --git a/src/Paradigm.ORM.Data.PostgreSql/PostgreSqlDatabaseConnector.cs b/src/Paradigm.ORM.Data.PostgreSql/PostgreSqlDatabaseConnector.cs index 895dbcc..8971a6d 100644 --- a/src/Paradigm.ORM.Data.PostgreSql/PostgreSqlDatabaseConnector.cs +++ b/src/Paradigm.ORM.Data.PostgreSql/PostgreSqlDatabaseConnector.cs @@ -239,6 +239,9 @@ public bool IsOpen() /// public IDatabaseTransaction CreateTransaction() { + if (!this.IsOpen()) + this.Open(); + return this.CreateTransaction(IsolationLevel.Serializable); } diff --git a/src/Paradigm.ORM.Data.SqlServer/Paradigm.ORM.Data.SqlServer.csproj b/src/Paradigm.ORM.Data.SqlServer/Paradigm.ORM.Data.SqlServer.csproj index 0cabc53..0f2d2fb 100644 --- a/src/Paradigm.ORM.Data.SqlServer/Paradigm.ORM.Data.SqlServer.csproj +++ b/src/Paradigm.ORM.Data.SqlServer/Paradigm.ORM.Data.SqlServer.csproj @@ -12,7 +12,7 @@ icon.png https://github.com/MiracleDevs/Paradigm.ORM.git git - 2.6.0 + 2.6.1 Paradigm.ORM diff --git a/src/Paradigm.ORM.Data.SqlServer/SqlDatabaseConnector.cs b/src/Paradigm.ORM.Data.SqlServer/SqlDatabaseConnector.cs index 97a83e4..076e201 100644 --- a/src/Paradigm.ORM.Data.SqlServer/SqlDatabaseConnector.cs +++ b/src/Paradigm.ORM.Data.SqlServer/SqlDatabaseConnector.cs @@ -239,6 +239,9 @@ public bool IsOpen() /// public IDatabaseTransaction CreateTransaction() { + if (!this.IsOpen()) + this.Open(); + return this.CreateTransaction(IsolationLevel.Serializable); } diff --git a/src/Paradigm.ORM.Data/Paradigm.ORM.Data.csproj b/src/Paradigm.ORM.Data/Paradigm.ORM.Data.csproj index 600e659..445851d 100644 --- a/src/Paradigm.ORM.Data/Paradigm.ORM.Data.csproj +++ b/src/Paradigm.ORM.Data/Paradigm.ORM.Data.csproj @@ -14,7 +14,7 @@ git Miracle Devs Miracle Devs - 2.6.0 + 2.6.1 Paradigm.ORM diff --git a/src/Paradigm.ORM.Tests/Fixtures/ConnectionStrings.cs b/src/Paradigm.ORM.Tests/Fixtures/ConnectionStrings.cs index 1eb62db..4ba56a4 100644 --- a/src/Paradigm.ORM.Tests/Fixtures/ConnectionStrings.cs +++ b/src/Paradigm.ORM.Tests/Fixtures/ConnectionStrings.cs @@ -2,12 +2,12 @@ { public class ConnectionStrings { - public const string Cql = "Contact Points=localhost;Port=9042;Default Keyspace=test;Username=root"; + public const string Cql = "Contact Points=127.0.0.1;Port=9042;Default Keyspace=test;Username=root"; - public const string MySql = "Server=localhost;Database=test;User=root;Password=Paradigm_Test_1234;Connection Timeout=3600;Allow User Variables=True;POOLING=true"; + public const string MySql = "Server=127.0.0.1;Database=test;User=root;Password=Paradigm_Test_1234;Connection Timeout=3600;Allow User Variables=True;POOLING=true"; - public const string PSql = "Server=localhost;User Id=postgres;Password=Paradigm_Test_1234;Timeout=3;Database=test"; + public const string PSql = "Server=127.0.0.1;User Id=postgres;Password=Paradigm_Test_1234;Timeout=3;Database=test"; - public const string MsSql = "Server=localhost;User=sa;Password=Paradigm_Test_1234;Connection Timeout=3600"; + public const string MsSql = "Server=127.0.0.1;User=sa;Password=Paradigm_Test_1234;Connection Timeout=3600"; } } \ No newline at end of file diff --git a/src/Paradigm.ORM.Tests/Tests/TransactionTest.cs b/src/Paradigm.ORM.Tests/Tests/TransactionTest.cs new file mode 100644 index 0000000..54c847b --- /dev/null +++ b/src/Paradigm.ORM.Tests/Tests/TransactionTest.cs @@ -0,0 +1,132 @@ +using System; +using System.Collections.Generic; +using FluentAssertions; +using Paradigm.ORM.Data.ValueProviders; +using Paradigm.ORM.Tests.Fixtures; +using Paradigm.ORM.Tests.Fixtures.MySql; +using Paradigm.ORM.Tests.Fixtures.PostgreSql; +using Paradigm.ORM.Tests.Fixtures.Sql; +using Paradigm.ORM.Tests.Fixtures.Cql; +using NUnit.Framework; +using Paradigm.ORM.Data.Mappers; + + +namespace Paradigm.ORM.Tests.Tests +{ + [TestFixture] + public class TransactionTest + { + [Order(1)] + [TestCase(typeof(MySqlCrudCommandFixture))] + [TestCase(typeof(SqlCrudCommandFixture))] + [TestCase(typeof(PostgreSqlCrudCommandFixture))] + public void ShouldCreateATransaction(Type fixtureType) + { + var fixture = Activator.CreateInstance(fixtureType) as CrudCommandFixtureBase; + + fixture.CreateDatabase(); + fixture.CreateTables(); + + var insertCommandBuilder = fixture.Connector.GetCommandBuilderFactory().CreateInsertCommandBuilder(fixture.GetParentDescriptor()); + var entityToInsert = fixture.CreateNewEntity(); + var valueProvider = new ClassValueProvider(fixture.Connector, new List { entityToInsert }); + valueProvider.MoveNext(); + var insertCommand = insertCommandBuilder.GetCommand(valueProvider); + + using (var transaction = fixture.Connector.CreateTransaction()) + { + if (fixtureType == typeof(CqlCrudCommandFixture)) + insertCommand.ExecuteNonQuery().Should().Be(-1); + else + insertCommand.ExecuteNonQuery().Should().Be(1); + } + + fixture.DropDatabase(); + } + + [Order(2)] + [TestCase(typeof(MySqlCrudCommandFixture))] + [TestCase(typeof(SqlCrudCommandFixture))] + [TestCase(typeof(PostgreSqlCrudCommandFixture))] + public void ShouldCommitATransaction(Type fixtureType) + { + var fixture = Activator.CreateInstance(fixtureType) as CrudCommandFixtureBase; + + fixture.CreateDatabase(); + fixture.CreateTables(); + + var insertCommandBuilder = fixture.Connector.GetCommandBuilderFactory().CreateInsertCommandBuilder(fixture.GetParentDescriptor()); + var selectCommandBuilder = fixture.Connector.GetCommandBuilderFactory().CreateSelectCommandBuilder(fixture.GetParentDescriptor()); + var entityToInsert = fixture.CreateNewEntity(); + var valueProvider = new ClassValueProvider(fixture.Connector, new List { entityToInsert }); + valueProvider.MoveNext(); + var insertCommand = insertCommandBuilder.GetCommand(valueProvider); + var selectCommand = selectCommandBuilder.GetCommand(); + + using (var transaction = fixture.Connector.CreateTransaction()) + { + if (fixtureType == typeof(CqlCrudCommandFixture)) + insertCommand.ExecuteNonQuery().Should().Be(-1); + else + insertCommand.ExecuteNonQuery().Should().Be(1); + + transaction.Commit(); + } + + using (var reader = selectCommand.ExecuteReader()) + { + reader.Should().NotBeNull(); + + var mapper = new DatabaseReaderMapper(fixture.Connector, fixture.GetParentDescriptor()); + var results = mapper.Map(reader); + + results.Should().NotBeNull().And.HaveCount(1); + } + + fixture.DropDatabase(); + } + + [Order(3)] + [TestCase(typeof(MySqlCrudCommandFixture))] + [TestCase(typeof(SqlCrudCommandFixture))] + [TestCase(typeof(PostgreSqlCrudCommandFixture))] + public void ShouldRollbackATransaction(Type fixtureType) + { + var fixture = Activator.CreateInstance(fixtureType) as CrudCommandFixtureBase; + + fixture.CreateDatabase(); + fixture.CreateTables(); + + var insertCommandBuilder = fixture.Connector.GetCommandBuilderFactory().CreateInsertCommandBuilder(fixture.GetParentDescriptor()); + var selectCommandBuilder = fixture.Connector.GetCommandBuilderFactory().CreateSelectCommandBuilder(fixture.GetParentDescriptor()); + var entityToInsert = fixture.CreateNewEntity(); + var valueProvider = new ClassValueProvider(fixture.Connector, new List { entityToInsert }); + valueProvider.MoveNext(); + var insertCommand = insertCommandBuilder.GetCommand(valueProvider); + var selectCommand = selectCommandBuilder.GetCommand(); + + using (var transaction = fixture.Connector.CreateTransaction()) + { + if (fixtureType == typeof(CqlCrudCommandFixture)) + insertCommand.ExecuteNonQuery().Should().Be(-1); + else + insertCommand.ExecuteNonQuery().Should().Be(1); + + transaction.Rollback(); + } + + using (var reader = selectCommand.ExecuteReader()) + { + reader.Should().NotBeNull(); + + var mapper = new DatabaseReaderMapper(fixture.Connector, fixture.GetParentDescriptor()); + var results = mapper.Map(reader); + + results.Should().NotBeNull().And.HaveCount(0); + } + + fixture.DropDatabase(); + } + + } +} diff --git a/src/Paradigm.ORM.Vb.Tests/Tests/MySql/DatabaseAccessTest.vb b/src/Paradigm.ORM.Vb.Tests/Tests/MySql/DatabaseAccessTest.vb index cae5085..7fa68b4 100644 --- a/src/Paradigm.ORM.Vb.Tests/Tests/MySql/DatabaseAccessTest.vb +++ b/src/Paradigm.ORM.Vb.Tests/Tests/MySql/DatabaseAccessTest.vb @@ -15,7 +15,7 @@ Namespace Tests.MySql Public Sub Initialize() - Connector = New MySqlDatabaseConnector("Server=localhost;Database=test;User=root;Password=Paradigm_Test_1234;Connection Timeout=3600") + Connector = New MySqlDatabaseConnector("Server=127.0.0.1;Database=test;User=root;Password=Paradigm_Test_1234;Connection Timeout=3600") Connector.Open() Connector.ExecuteNonQuery(" diff --git a/src/Paradigm.ORM.Vb.Tests/Tests/MySql/StoredProcedureTest.vb b/src/Paradigm.ORM.Vb.Tests/Tests/MySql/StoredProcedureTest.vb index 3bf5c2e..096675a 100644 --- a/src/Paradigm.ORM.Vb.Tests/Tests/MySql/StoredProcedureTest.vb +++ b/src/Paradigm.ORM.Vb.Tests/Tests/MySql/StoredProcedureTest.vb @@ -14,7 +14,7 @@ Namespace Tests.MySql Public Sub Initialize() - Connector = New MySqlDatabaseConnector("Server=localhost;Database=test;User=root;Password=Paradigm_Test_1234;Connection Timeout=3600") + Connector = New MySqlDatabaseConnector("Server=127.0.0.1;Database=test;User=root;Password=Paradigm_Test_1234;Connection Timeout=3600") Connector.Open() Connector.ExecuteNonQuery("