From f0f5e1620e6a3293e96dfafcc4c23ffdc5e33783 Mon Sep 17 00:00:00 2001 From: Mahesh Kale Date: Tue, 23 Jan 2024 14:19:00 +0530 Subject: [PATCH 1/2] Working Postgre testContainers --- Ginger/GingerCoreNET/GingerCoreNET.csproj | 2 +- .../DatabaseTest/CouchbaseIntegrationTests.cs | 126 ++++++++++++++++++ .../DatabaseTest/OracleIntegrationTests.cs | 90 +++++++++++++ .../PostgreSqlIntegrationTests.cs | 80 +++++++++++ .../GingerCoreNETUnitTest.csproj | 6 +- .../UnitTestRepositoryItemFactory.cs | 3 +- 6 files changed, 304 insertions(+), 3 deletions(-) create mode 100644 Ginger/GingerCoreNETUnitTest/DatabaseTest/CouchbaseIntegrationTests.cs create mode 100644 Ginger/GingerCoreNETUnitTest/DatabaseTest/OracleIntegrationTests.cs create mode 100644 Ginger/GingerCoreNETUnitTest/DatabaseTest/PostgreSqlIntegrationTests.cs diff --git a/Ginger/GingerCoreNET/GingerCoreNET.csproj b/Ginger/GingerCoreNET/GingerCoreNET.csproj index 64beba455c..1710f68703 100644 --- a/Ginger/GingerCoreNET/GingerCoreNET.csproj +++ b/Ginger/GingerCoreNET/GingerCoreNET.csproj @@ -286,7 +286,7 @@ - + diff --git a/Ginger/GingerCoreNETUnitTest/DatabaseTest/CouchbaseIntegrationTests.cs b/Ginger/GingerCoreNETUnitTest/DatabaseTest/CouchbaseIntegrationTests.cs new file mode 100644 index 0000000000..7c0306d29f --- /dev/null +++ b/Ginger/GingerCoreNETUnitTest/DatabaseTest/CouchbaseIntegrationTests.cs @@ -0,0 +1,126 @@ +using Couchbase; +using Couchbase.Configuration.Client; +using GingerCore.Actions; +using GingerCore.Environments; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Testcontainers.Couchbase; + +namespace GingerCoreNETUnitTest.DatabaseTest +{ + [TestClass] + public sealed class CouchbaseIntegrationTests + { + + private static readonly CouchbaseContainer _couchbaseContainer = new CouchbaseBuilder().Build(); + + [ClassInitialize] + public static async Task ClassInitialize(TestContext context) + { + await _couchbaseContainer.StartAsync(); + await CreateTestData(); + + } + + [TestCleanup] + public Task DisposeAsync() + { + return _couchbaseContainer.DisposeAsync().AsTask(); + } + public static async Task CreateTestData() + { + + try + { + // var clusterOptions = new ClusterOptions(); + // clusterOptions.ConnectionString = _couchbaseContainer.GetConnectionString(); + // clusterOptions.UserName = CouchbaseBuilder.DefaultUsername; + // clusterOptions.Password = CouchbaseBuilder.DefaultPassword; + + // var cluster = await Cluster.ConnectAsync(clusterOptions) + //.ConfigureAwait(true); + + // // When + // var ping = await cluster.PingAsync() + // .ConfigureAwait(true); + + // var bucket = await cluster.BucketAsync(_couchbaseContainer.Buckets.Single().Name) + // .ConfigureAwait(true); + + // When + //var database = await client.Database.PutAsync() + // .ConfigureAwait(true); + var cluster = new Cluster(new ClientConfiguration + { + Servers = new List { new Uri("http://" + _couchbaseContainer.GetConnectionString()) }, + UseSsl = false + }); + + cluster.Authenticate(CouchbaseBuilder.DefaultUsername, CouchbaseBuilder.DefaultPassword); + + var clusterManager = cluster.CreateManager(CouchbaseBuilder.DefaultUsername, CouchbaseBuilder.DefaultPassword); + var buckets = clusterManager.ListBuckets().Value; + //var result = clusterManager.CreateBucket(new BucketSettings() { Name = "GingerBucket", BucketType = Couchbase.Core.Buckets.BucketTypeEnum.Couchbase, AuthType = Couchbase.Authentication.AuthType.None }); + //var bucket = cluster.OpenBucket("GingerBucket"); + + //// Insert sample data + //var document = new Document + //{ + // Id = "Customer", + // Content = new[]{ + // new { Name = "Mahesh", Value = 1 , Dept = "AQE I&T"}, + // new { Name = "Jinendra", Value = 2, Dept = "AQE I&T"} + //}, + //}; + //var upsertResult = await bucket.UpsertAsync(document); + } + catch (Exception ex) + { + + throw; + } + } + + + [TestMethod] + public void ShouldReturnTwoCustomers() + { + // Given + Database db = new Database(); + ActDBValidation mact = new ActDBValidation(); + //actDBValidation + + db.DBType = Database.eDBTypes.Couchbase; + DatabaseOperations databaseOperations = new DatabaseOperations(db); + db.DatabaseOperations = databaseOperations; + //ATT LS DB + //Db.DatabaseOperations.TNSCalculated + db.TNS = "http://" + _couchbaseContainer.GetConnectionString(); + db.User = CouchbaseBuilder.DefaultUsername; + db.Pass = CouchbaseBuilder.DefaultPassword; + // NoSqlBase noSqlBase = new GingerCouchbase(ActDBValidation.eDBValidationType.FreeSQL, db, mact); + mact.DB = db; + + Boolean connectionSuccessful = mact.DB.DatabaseOperations.Connect(); + + // When + List recs = new List(); + if (connectionSuccessful) + { + mact.SQL = "SELECT * FROM `Ginger-bucket` WHERE META().id = 1"; + mact.Execute(); + + //db.DatabaseOperations.CloseConnection(); + } + + //then + Assert.AreEqual(connectionSuccessful, true, "Connected Successfully to CouchbaseDB"); + //Assert.IsNotNull(recs.Count, 0); + Assert.AreEqual(recs.Count, 2, "Fetched 2 records from Postgre Table"); + } + + } +} diff --git a/Ginger/GingerCoreNETUnitTest/DatabaseTest/OracleIntegrationTests.cs b/Ginger/GingerCoreNETUnitTest/DatabaseTest/OracleIntegrationTests.cs new file mode 100644 index 0000000000..61f7be6463 --- /dev/null +++ b/Ginger/GingerCoreNETUnitTest/DatabaseTest/OracleIntegrationTests.cs @@ -0,0 +1,90 @@ +using GingerCore.Environments; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Testcontainers.Oracle; +using Oracle; +using Oracle.ManagedDataAccess.Client; + +namespace GingerCoreNETUnitTest.DatabaseTest +{ + [TestClass] + public class OracleIntegrationTests + { + private static readonly OracleContainer _oracleContainer = new OracleBuilder().Build(); + + [ClassInitialize] + public static async Task ClassInitialize(TestContext context) + { + await _oracleContainer.StartAsync(); + await CreateTestData(); + + } + + [TestCleanup] + public Task DisposeAsync() + { + return _oracleContainer.DisposeAsync().AsTask(); + } + public static async Task CreateTestData() + { + try + { + var scriptContent = "CREATE TABLE Customer (Id NUMBER PRIMARY KEY, Name varchar2(50));" + + "INSERT INTO Customer (Id, Name) VALUES (1, 'Mahesh'); " + + "INSERT INTO Customer (Id, Name) VALUES (2, 'kale');"; + //var execResult = await _oracleContainer.ExecScriptAsync(scriptContent) + // .ConfigureAwait(false); + + using (var connection = new OracleConnection(_oracleContainer.GetConnectionString())) + { + await connection.OpenAsync(); + + // Example: Create a table and insert test data + using (var command = connection.CreateCommand()) + { + command.CommandText = scriptContent; + await command.ExecuteNonQueryAsync(); + } + } + + } + catch (Exception ex) + { + + } + } + + [TestMethod] + public void ShouldReturnTwoCustomers() + { + // Given + Database db = new Database(); + DatabaseOperations databaseOperations = new DatabaseOperations(db); + db.DatabaseOperations = databaseOperations; + db.DBType = Database.eDBTypes.Oracle; + //ATT LS DB + db.ConnectionString = _oracleContainer.GetConnectionString(); + Boolean connectionSuccessful = db.DatabaseOperations.Connect(); + + + // When + + List recs = new List(); + if (connectionSuccessful) + { + + string sqlQuery = "SELECT Id, Name FROM Customer"; + recs = db.DatabaseOperations.FreeSQL(sqlQuery); + + db.DatabaseOperations.CloseConnection(); + } + + //then + Assert.AreEqual(connectionSuccessful, true, "Connected Successfully to Oracle"); + //Assert.IsNotNull(recs.Count, 0); + Assert.AreEqual(recs.Count, 2, "Fetched 2 records from Oracle Table"); + } + } +} diff --git a/Ginger/GingerCoreNETUnitTest/DatabaseTest/PostgreSqlIntegrationTests.cs b/Ginger/GingerCoreNETUnitTest/DatabaseTest/PostgreSqlIntegrationTests.cs new file mode 100644 index 0000000000..d574686c5b --- /dev/null +++ b/Ginger/GingerCoreNETUnitTest/DatabaseTest/PostgreSqlIntegrationTests.cs @@ -0,0 +1,80 @@ +using GingerCore.Environments; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Npgsql; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Testcontainers.PostgreSql; + +namespace GingerCoreNETUnitTest.DatabaseTest +{ + [TestClass] + public sealed class PostgreSqlIntegrationTests + { + private static readonly PostgreSqlContainer _postgres = new PostgreSqlBuilder() + .WithImage("postgres:15-alpine") + .Build(); + + [ClassInitialize] + public static async Task ClassInitialize(TestContext context) + { + await _postgres.StartAsync(); + await CreateTestData(); + + } + + [TestCleanup] + public Task DisposeAsync() + { + return _postgres.DisposeAsync().AsTask(); + } + public static async Task CreateTestData() + { + using (var connection = new NpgsqlConnection(_postgres.GetConnectionString())) + { + await connection.OpenAsync(); + + // Create test data + using (var command = connection.CreateCommand()) + { + command.CommandText = "CREATE TABLE Customer (Id serial PRIMARY KEY, Name varchar(255));" + + "INSERT INTO Customer (Name) VALUES ('1'), ('Mahesh');" + + "INSERT INTO Customer (Name) VALUES ('1'), ('Kale');"; + await command.ExecuteNonQueryAsync(); + } + } + + } + + [TestMethod] + public void ShouldReturnTwoCustomers() + { + // Given + Database db = new Database(); + DatabaseOperations databaseOperations = new DatabaseOperations(db); + db.DatabaseOperations = databaseOperations; + db.DBType = Database.eDBTypes.PostgreSQL; + //ATT LS DB + db.ConnectionString = _postgres.GetConnectionString(); + Boolean connectionSuccessful = db.DatabaseOperations.Connect(); + + + // When + + List recs = new List(); + if (connectionSuccessful) + { + + string sqlQuery = "SELECT Id, Name FROM Customer"; + recs = db.DatabaseOperations.FreeSQL(sqlQuery); + + db.DatabaseOperations.CloseConnection(); + } + + //then + Assert.AreEqual(connectionSuccessful, true, "Connected Successfully to PostgreSQL"); + //Assert.IsNotNull(recs.Count, 0); + Assert.AreEqual(recs.Count,2, "Fetched 2 records from Postgre Table"); + } + } +} \ No newline at end of file diff --git a/Ginger/GingerCoreNETUnitTest/GingerCoreNETUnitTest.csproj b/Ginger/GingerCoreNETUnitTest/GingerCoreNETUnitTest.csproj index 46bba969dc..b100ffa811 100644 --- a/Ginger/GingerCoreNETUnitTest/GingerCoreNETUnitTest.csproj +++ b/Ginger/GingerCoreNETUnitTest/GingerCoreNETUnitTest.csproj @@ -85,7 +85,7 @@ - + @@ -94,6 +94,10 @@ + + + + diff --git a/Ginger/GingerCoreNETUnitTest/RunTestslib/UnitTestRepositoryItemFactory.cs b/Ginger/GingerCoreNETUnitTest/RunTestslib/UnitTestRepositoryItemFactory.cs index d9abeae890..d4be0482f1 100644 --- a/Ginger/GingerCoreNETUnitTest/RunTestslib/UnitTestRepositoryItemFactory.cs +++ b/Ginger/GingerCoreNETUnitTest/RunTestslib/UnitTestRepositoryItemFactory.cs @@ -9,6 +9,7 @@ using GingerCore.Environments; using GingerCoreNET.ALMLib; using GingerCoreNET.SourceControl; +using Oracle.ManagedDataAccess.Client; using System; using System.Collections.Generic; using System.Data.Common; @@ -160,7 +161,7 @@ public IWebserviceDriverWindow GetWebserviceDriverWindow(BusinessFlow businessFl public DbConnection GetOracleConnection(string ConnectionString) { - throw new NotImplementedException(); + return new OracleConnection(ConnectionString); } public bool IsSharedRepositoryItem(RepositoryItemBase item) From 13b0eed7eb27a52d6be073aa210b1c9c13d66996 Mon Sep 17 00:00:00 2001 From: Mahesh Kale Date: Mon, 29 Jan 2024 16:05:11 +0530 Subject: [PATCH 2/2] TestContainters --- .../DatabaseTest/CouchbaseIntegrationTests.cs | 20 +++++++++---------- .../PostgreSqlIntegrationTests.cs | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Ginger/GingerCoreNETUnitTest/DatabaseTest/CouchbaseIntegrationTests.cs b/Ginger/GingerCoreNETUnitTest/DatabaseTest/CouchbaseIntegrationTests.cs index 7c0306d29f..d66a9e2568 100644 --- a/Ginger/GingerCoreNETUnitTest/DatabaseTest/CouchbaseIntegrationTests.cs +++ b/Ginger/GingerCoreNETUnitTest/DatabaseTest/CouchbaseIntegrationTests.cs @@ -65,17 +65,17 @@ public static async Task CreateTestData() var buckets = clusterManager.ListBuckets().Value; //var result = clusterManager.CreateBucket(new BucketSettings() { Name = "GingerBucket", BucketType = Couchbase.Core.Buckets.BucketTypeEnum.Couchbase, AuthType = Couchbase.Authentication.AuthType.None }); //var bucket = cluster.OpenBucket("GingerBucket"); - + var bucket = cluster.OpenBucket(_couchbaseContainer.Buckets.FirstOrDefault().Name); //// Insert sample data - //var document = new Document - //{ - // Id = "Customer", - // Content = new[]{ - // new { Name = "Mahesh", Value = 1 , Dept = "AQE I&T"}, - // new { Name = "Jinendra", Value = 2, Dept = "AQE I&T"} - //}, - //}; - //var upsertResult = await bucket.UpsertAsync(document); + var document = new Document + { + Id = "Customer", + Content = new[]{ + new { Name = "Mahesh", Value = 1 , Dept = "AQE I&T"}, + new { Name = "Jinendra", Value = 2, Dept = "AQE I&T"} + }, + }; + var upsertResult = await bucket.UpsertAsync(document); } catch (Exception ex) { diff --git a/Ginger/GingerCoreNETUnitTest/DatabaseTest/PostgreSqlIntegrationTests.cs b/Ginger/GingerCoreNETUnitTest/DatabaseTest/PostgreSqlIntegrationTests.cs index d574686c5b..e1dcb3e989 100644 --- a/Ginger/GingerCoreNETUnitTest/DatabaseTest/PostgreSqlIntegrationTests.cs +++ b/Ginger/GingerCoreNETUnitTest/DatabaseTest/PostgreSqlIntegrationTests.cs @@ -38,8 +38,8 @@ public static async Task CreateTestData() using (var command = connection.CreateCommand()) { command.CommandText = "CREATE TABLE Customer (Id serial PRIMARY KEY, Name varchar(255));" + - "INSERT INTO Customer (Name) VALUES ('1'), ('Mahesh');" + - "INSERT INTO Customer (Name) VALUES ('1'), ('Kale');"; + "INSERT INTO Customer (Name) VALUES ('1'), ('Mahesh kale');" + + "INSERT INTO Customer (Name) VALUES ('1'), ('Jinendra Ghodke');"; await command.ExecuteNonQueryAsync(); } }