From 68539fdd0e652070f97071443783f345db038cca Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Wed, 13 Jul 2022 21:23:03 +0530 Subject: [PATCH 01/25] Negative test case for undeterminitstic primary key --- .../SqlTests/SqlTestHelper.cs | 46 ++++++++++++--- .../Unittests/BootStrapFailureTest.cs | 58 +++++++++++++++++++ 2 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs diff --git a/DataGateway.Service.Tests/SqlTests/SqlTestHelper.cs b/DataGateway.Service.Tests/SqlTests/SqlTestHelper.cs index 6dcb0182ba..8a30da5c5d 100644 --- a/DataGateway.Service.Tests/SqlTests/SqlTestHelper.cs +++ b/DataGateway.Service.Tests/SqlTests/SqlTestHelper.cs @@ -40,7 +40,7 @@ public static IOptionsMonitor LoadConfig(string environment) RuntimeConfigPath configPath = config.Get(); RuntimeConfig runtimeConfig = configPath.LoadRuntimeConfigValue(); - AddMissingEntitiesToConfig(runtimeConfig); + AddMissingEntitiesToConfig(runtimeConfig, "magazines", "foo"); return Mock.Of>(_ => _.CurrentValue == runtimeConfig); } @@ -66,12 +66,12 @@ public static void RemoveAllRelationshipBetweenEntities(RuntimeConfig runtimeCon /// customized for testing purposes. /// /// - private static void AddMissingEntitiesToConfig(RuntimeConfig config) + public static void AddMissingEntitiesToConfig(RuntimeConfig config, string dbObjectName, string nameSpace="") { - string magazineSource = config.DatabaseType is DatabaseType.mysql ? "\"magazines\"" : "\"foo.magazines\""; - string magazineEntityJsonString = + string source = config.DatabaseType is DatabaseType.mysql || string.IsNullOrEmpty(nameSpace)? $"\"{dbObjectName}\"" : $"\"{nameSpace}.{dbObjectName}\""; + string entityJsonString = @"{ - ""source"": " + magazineSource + @", + ""source"": " + source + @", ""graphql"": true, ""permissions"": [ { @@ -94,10 +94,42 @@ private static void AddMissingEntitiesToConfig(RuntimeConfig config) } }; - Entity magazineEntity = JsonSerializer.Deserialize(magazineEntityJsonString, options); - config.Entities.Add("Magazine", magazineEntity); + Entity entity = JsonSerializer.Deserialize(entityJsonString, options); + string entityKey = dbObjectName.Equals("magazines") ? "Magazine" : dbObjectName; + config.Entities.Add(entityKey, entity); } + public static void AddCompositeViewToConfig(RuntimeConfig runtimeConfig) + { + string compositeViewSource = "\"books_authors\""; + string entityJsonString = + @"{ + ""source"": " + compositeViewSource + @", + ""graphql"": true, + ""permissions"": [ + { + ""role"": ""anonymous"", + ""actions"": [ ""read"" ] + }, + { + ""role"": ""authenticated"", + ""actions"": [" + $" \"{ActionType.CREATE}\", \"{ActionType.READ}\", \"{ActionType.DELETE}\", \"{ActionType.UPDATE}\" ]" + + @"} + ] + }"; + + JsonSerializerOptions options = new() + { + PropertyNameCaseInsensitive = true, + Converters = + { + new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) + } + }; + + Entity entity = JsonSerializer.Deserialize(entityJsonString, options); + runtimeConfig.Entities.Add("books_authors", entity); + } /// /// Converts strings to JSON objects and does a deep compare /// diff --git a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs new file mode 100644 index 0000000000..06505410fa --- /dev/null +++ b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs @@ -0,0 +1,58 @@ +using System.IO; +using System.Net; +using System.Threading.Tasks; +using Azure.DataGateway.Service.Exceptions; +using Azure.DataGateway.Service.Tests.SqlTests; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Azure.DataGateway.Service.Tests.Unittests +{ + [TestClass] + public class BootStrapFailureTest : SqlTestBase + { + /// + /// Test to validate that the runtime fails during bootstrap if the primary + /// key cannot be determined for a database object. + /// + /// + [TestMethod] + public async Task IndeterministicPrimaryKeyOnDatabaseObject() + { + _testCategory = TestCategory.MSSQL; + _runtimeConfig = SqlTestHelper.LoadConfig(_testCategory).CurrentValue; + SqlTestHelper.RemoveAllRelationshipBetweenEntities(_runtimeConfig); + SqlTestHelper.AddMissingEntitiesToConfig(_runtimeConfig, "books_authors"); + SqlTestBase.SetUpSQLMetadataProvider(); + + // Add composite view whose primary key cannot be determined. + string dbQuery = File.ReadAllText($"{_testCategory}Books.sql"); + string compositeViewQuery = "EXEC('CREATE VIEW books_authors as SELECT books.title, authors.[name], " + + "authors.[birthdate], books.id as book_id, authors.id as author_id " + + "FROM dbo.books INNER JOIN dbo.book_author_link ON books.[id] = book_author_link.book_id " + + "INNER JOIN authors ON authors.[id] = book_author_link.author_id')"; + + // Execute the query to add it to the database. + await _queryExecutor.ExecuteQueryAsync(dbQuery + compositeViewQuery, parameters: null); + try + { + await _sqlMetadataProvider.InitializeAsync(); + } + catch(DataGatewayException ex) + { + Assert.AreEqual(HttpStatusCode.NotImplemented, ex.StatusCode); + Assert.AreEqual("Primary key not configured on the given database object books_authors", ex.Message); + } + finally + { + string dropViewQuery = "DROP VIEW IF EXISTS books_authors"; + await _queryExecutor.ExecuteQueryAsync(dropViewQuery, parameters: null); + } + } + + [TestCleanup] + public async Task TestCleanup() + { + await ResetDbStateAsync(); + } + } +} From 952a7ec1ba25f0278a408c6e7683facb6901e7e2 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Wed, 13 Jul 2022 21:36:20 +0530 Subject: [PATCH 02/25] Formatting fix --- DataGateway.Service.Tests/SqlTests/SqlTestHelper.cs | 4 ++-- DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/DataGateway.Service.Tests/SqlTests/SqlTestHelper.cs b/DataGateway.Service.Tests/SqlTests/SqlTestHelper.cs index 8a30da5c5d..460f888d7b 100644 --- a/DataGateway.Service.Tests/SqlTests/SqlTestHelper.cs +++ b/DataGateway.Service.Tests/SqlTests/SqlTestHelper.cs @@ -66,9 +66,9 @@ public static void RemoveAllRelationshipBetweenEntities(RuntimeConfig runtimeCon /// customized for testing purposes. /// /// - public static void AddMissingEntitiesToConfig(RuntimeConfig config, string dbObjectName, string nameSpace="") + public static void AddMissingEntitiesToConfig(RuntimeConfig config, string dbObjectName, string nameSpace = "") { - string source = config.DatabaseType is DatabaseType.mysql || string.IsNullOrEmpty(nameSpace)? $"\"{dbObjectName}\"" : $"\"{nameSpace}.{dbObjectName}\""; + string source = config.DatabaseType is DatabaseType.mysql || string.IsNullOrEmpty(nameSpace) ? $"\"{dbObjectName}\"" : $"\"{nameSpace}.{dbObjectName}\""; string entityJsonString = @"{ ""source"": " + source + @", diff --git a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs index 06505410fa..3caae4938d 100644 --- a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs +++ b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs @@ -37,7 +37,7 @@ public async Task IndeterministicPrimaryKeyOnDatabaseObject() { await _sqlMetadataProvider.InitializeAsync(); } - catch(DataGatewayException ex) + catch (DataGatewayException ex) { Assert.AreEqual(HttpStatusCode.NotImplemented, ex.StatusCode); Assert.AreEqual("Primary key not configured on the given database object books_authors", ex.Message); From 61caf2aaedbbb2defc7e63c1c986914a2b495a77 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Thu, 14 Jul 2022 11:03:15 +0530 Subject: [PATCH 03/25] Trying to fix build error on remote --- .../SqlTests/SqlTestHelper.cs | 31 ------------------- .../Unittests/BootStrapFailureTest.cs | 9 ++---- 2 files changed, 2 insertions(+), 38 deletions(-) diff --git a/DataGateway.Service.Tests/SqlTests/SqlTestHelper.cs b/DataGateway.Service.Tests/SqlTests/SqlTestHelper.cs index 460f888d7b..683f4101b2 100644 --- a/DataGateway.Service.Tests/SqlTests/SqlTestHelper.cs +++ b/DataGateway.Service.Tests/SqlTests/SqlTestHelper.cs @@ -99,37 +99,6 @@ public static void AddMissingEntitiesToConfig(RuntimeConfig config, string dbObj config.Entities.Add(entityKey, entity); } - public static void AddCompositeViewToConfig(RuntimeConfig runtimeConfig) - { - string compositeViewSource = "\"books_authors\""; - string entityJsonString = - @"{ - ""source"": " + compositeViewSource + @", - ""graphql"": true, - ""permissions"": [ - { - ""role"": ""anonymous"", - ""actions"": [ ""read"" ] - }, - { - ""role"": ""authenticated"", - ""actions"": [" + $" \"{ActionType.CREATE}\", \"{ActionType.READ}\", \"{ActionType.DELETE}\", \"{ActionType.UPDATE}\" ]" + - @"} - ] - }"; - - JsonSerializerOptions options = new() - { - PropertyNameCaseInsensitive = true, - Converters = - { - new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) - } - }; - - Entity entity = JsonSerializer.Deserialize(entityJsonString, options); - runtimeConfig.Entities.Add("books_authors", entity); - } /// /// Converts strings to JSON objects and does a deep compare /// diff --git a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs index 3caae4938d..ed9cb1b61f 100644 --- a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs +++ b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs @@ -11,7 +11,7 @@ namespace Azure.DataGateway.Service.Tests.Unittests public class BootStrapFailureTest : SqlTestBase { /// - /// Test to validate that the runtime fails during bootstrap if the primary + /// Test to validate that the runtime fails and throws an exception during bootstrap when the primary /// key cannot be determined for a database object. /// /// @@ -46,13 +46,8 @@ public async Task IndeterministicPrimaryKeyOnDatabaseObject() { string dropViewQuery = "DROP VIEW IF EXISTS books_authors"; await _queryExecutor.ExecuteQueryAsync(dropViewQuery, parameters: null); + await _queryExecutor.ExecuteQueryAsync(dbQuery, parameters: null); } } - - [TestCleanup] - public async Task TestCleanup() - { - await ResetDbStateAsync(); - } } } From 215102648a29de1632e9033aab03f8fb4429e7a3 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Thu, 14 Jul 2022 11:08:28 +0530 Subject: [PATCH 04/25] =?UTF-8?q?=C3=84ttempt=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs index ed9cb1b61f..ae0c94e857 100644 --- a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs +++ b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs @@ -46,7 +46,6 @@ public async Task IndeterministicPrimaryKeyOnDatabaseObject() { string dropViewQuery = "DROP VIEW IF EXISTS books_authors"; await _queryExecutor.ExecuteQueryAsync(dropViewQuery, parameters: null); - await _queryExecutor.ExecuteQueryAsync(dbQuery, parameters: null); } } } From 3f36b295a798d7b0337d565e185b01437c64c83d Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Thu, 14 Jul 2022 20:51:09 +0530 Subject: [PATCH 05/25] Attempting to fix test --- DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs index ae0c94e857..fb7060e3ae 100644 --- a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs +++ b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs @@ -7,7 +7,7 @@ namespace Azure.DataGateway.Service.Tests.Unittests { - [TestClass] + [TestClass, TestCategory(TestCategory.MSSQL)] public class BootStrapFailureTest : SqlTestBase { /// From 69b179fdfeabe3fe23dc2b30038921d5d2b50bf6 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Tue, 19 Jul 2022 11:17:37 +0530 Subject: [PATCH 06/25] Using Assert exception --- .../Unittests/BootStrapFailureTest.cs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs index fb7060e3ae..8ea4672662 100644 --- a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs +++ b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs @@ -1,3 +1,4 @@ +using System; using System.IO; using System.Net; using System.Threading.Tasks; @@ -10,6 +11,7 @@ namespace Azure.DataGateway.Service.Tests.Unittests [TestClass, TestCategory(TestCategory.MSSQL)] public class BootStrapFailureTest : SqlTestBase { + private static readonly string _compositeViewName = "books_authors"; /// /// Test to validate that the runtime fails and throws an exception during bootstrap when the primary /// key cannot be determined for a database object. @@ -26,25 +28,22 @@ public async Task IndeterministicPrimaryKeyOnDatabaseObject() // Add composite view whose primary key cannot be determined. string dbQuery = File.ReadAllText($"{_testCategory}Books.sql"); - string compositeViewQuery = "EXEC('CREATE VIEW books_authors as SELECT books.title, authors.[name], " + + string compositeViewQuery = $"EXEC('CREATE VIEW {_compositeViewName} as SELECT books.title, authors.[name], " + "authors.[birthdate], books.id as book_id, authors.id as author_id " + "FROM dbo.books INNER JOIN dbo.book_author_link ON books.[id] = book_author_link.book_id " + "INNER JOIN authors ON authors.[id] = book_author_link.author_id')"; - // Execute the query to add it to the database. + // Execute the query to add view to the database. await _queryExecutor.ExecuteQueryAsync(dbQuery + compositeViewQuery, parameters: null); try { - await _sqlMetadataProvider.InitializeAsync(); - } - catch (DataGatewayException ex) - { + DataGatewayException ex = await Assert.ThrowsExceptionAsync(() => _sqlMetadataProvider.InitializeAsync()); Assert.AreEqual(HttpStatusCode.NotImplemented, ex.StatusCode); - Assert.AreEqual("Primary key not configured on the given database object books_authors", ex.Message); + Assert.AreEqual($"Primary key not configured on the given database object {_compositeViewName}", ex.Message); } finally { - string dropViewQuery = "DROP VIEW IF EXISTS books_authors"; + string dropViewQuery = $"DROP VIEW IF EXISTS {_compositeViewName}"; await _queryExecutor.ExecuteQueryAsync(dropViewQuery, parameters: null); } } From 45087ab379c7f06caf0475c6ebddeacfbdf5ab36 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Tue, 19 Jul 2022 12:14:41 +0530 Subject: [PATCH 07/25] Fixing build failures --- DataGateway.Service.Tests/SqlTests/SqlTestBase.cs | 2 +- DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs b/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs index a9bd9e290f..0b0e9217eb 100644 --- a/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs +++ b/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs @@ -66,7 +66,7 @@ protected static async Task InitializeTestFixture(TestContext context, string te Mock> configProviderLogger = new(); RuntimeConfigProvider.ConfigProviderLogger = configProviderLogger.Object; RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); - TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, "magazines"); + TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, "magazines", "foo"); _runtimeConfigProvider = TestHelper.GetRuntimeConfigProvider(_runtimeConfig); SetUpSQLMetadataProvider(); diff --git a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs index 1963b240c2..05ab796d92 100644 --- a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs +++ b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs @@ -1,4 +1,3 @@ -using System; using System.IO; using System.Net; using System.Threading.Tasks; From 2794ce3306bba6e3a822b2cf285cd83bf35f0367 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Tue, 19 Jul 2022 12:35:24 +0530 Subject: [PATCH 08/25] Using more appropriate statuscode --- .../Unittests/BootStrapFailureTest.cs | 17 +++-------------- .../MetadataProviders/SqlMetadataProvider.cs | 2 +- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs index 05ab796d92..b27b3148fa 100644 --- a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs +++ b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs @@ -21,23 +21,12 @@ public class BootStrapFailureTest : SqlTestBase [TestMethod] public async Task IndeterministicPrimaryKeyOnDatabaseObject() { - /* - * _testCategory = TestCategory.POSTGRESQL; - RuntimeConfigPath configPath = TestHelper.GetRuntimeConfigPath(_testCategory); - Mock> configProviderLogger = new(); - RuntimeConfigProvider.ConfigProviderLogger = configProviderLogger.Object; - RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); - SqlTestHelper.RemoveAllRelationshipBetweenEntities(_runtimeConfig); - _runtimeConfigProvider = TestHelper.GetRuntimeConfigProvider(_runtimeConfig); - SetUpSQLMetadataProvider(); - await ResetDbStateAsync(); - await _sqlMetadataProvider.InitializeAsync(); - */ _testCategory = TestCategory.MSSQL; RuntimeConfigPath configPath = TestHelper.GetRuntimeConfigPath(_testCategory); RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); SqlTestHelper.RemoveAllRelationshipBetweenEntities(_runtimeConfig); - TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, "books_authors"); + TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, _compositeViewName); + _runtimeConfigProvider = TestHelper.GetRuntimeConfigProvider(_runtimeConfig); SetUpSQLMetadataProvider(); // Add composite view whose primary key cannot be determined. @@ -52,7 +41,7 @@ public async Task IndeterministicPrimaryKeyOnDatabaseObject() try { DataGatewayException ex = await Assert.ThrowsExceptionAsync(() => _sqlMetadataProvider.InitializeAsync()); - Assert.AreEqual(HttpStatusCode.NotImplemented, ex.StatusCode); + Assert.AreEqual(HttpStatusCode.ServiceUnavailable, ex.StatusCode); Assert.AreEqual($"Primary key not configured on the given database object {_compositeViewName}", ex.Message); } finally diff --git a/DataGateway.Service/Services/MetadataProviders/SqlMetadataProvider.cs b/DataGateway.Service/Services/MetadataProviders/SqlMetadataProvider.cs index b3cda665c2..2d0b8c6166 100644 --- a/DataGateway.Service/Services/MetadataProviders/SqlMetadataProvider.cs +++ b/DataGateway.Service/Services/MetadataProviders/SqlMetadataProvider.cs @@ -553,7 +553,7 @@ private async Task PopulateTableDefinitionAsync( { throw new DataGatewayException( message: $"Primary key not configured on the given database object {tableName}", - statusCode: System.Net.HttpStatusCode.NotImplemented, + statusCode: System.Net.HttpStatusCode.ServiceUnavailable, subStatusCode: DataGatewayException.SubStatusCodes.ErrorInInitialization); } From 7c2bd098983d1bdb3d4de75d7b2c3795f9055982 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Tue, 19 Jul 2022 13:49:28 +0530 Subject: [PATCH 09/25] Using test cleanup --- .../Unittests/BootStrapFailureTest.cs | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs index b27b3148fa..f5b9f1d868 100644 --- a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs +++ b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs @@ -38,17 +38,19 @@ public async Task IndeterministicPrimaryKeyOnDatabaseObject() // Execute the query to add view to the database. await _queryExecutor.ExecuteQueryAsync(dbQuery + compositeViewQuery, parameters: null); - try - { - DataGatewayException ex = await Assert.ThrowsExceptionAsync(() => _sqlMetadataProvider.InitializeAsync()); - Assert.AreEqual(HttpStatusCode.ServiceUnavailable, ex.StatusCode); - Assert.AreEqual($"Primary key not configured on the given database object {_compositeViewName}", ex.Message); - } - finally - { - string dropViewQuery = $"DROP VIEW IF EXISTS {_compositeViewName}"; - await _queryExecutor.ExecuteQueryAsync(dropViewQuery, parameters: null); - } + DataGatewayException ex = await Assert.ThrowsExceptionAsync(() => _sqlMetadataProvider.InitializeAsync()); + Assert.AreEqual(HttpStatusCode.ServiceUnavailable, ex.StatusCode); + Assert.AreEqual($"Primary key not configured on the given database object {_compositeViewName}", ex.Message); + } + + /// + /// Runs after every test to reset the database state + /// + [TestCleanup] + public async Task TestCleanup() + { + string dropViewQuery = $"DROP VIEW IF EXISTS {_compositeViewName}"; + await _queryExecutor.ExecuteQueryAsync(dropViewQuery, parameters: null); } } } From fb44efda4b218c49e0abcee0353a7fb4446f0abb Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Wed, 20 Jul 2022 11:45:41 +0530 Subject: [PATCH 10/25] Passing entity key as an argument rather than having special case --- .../SqlTests/SqlTestBase.cs | 2 +- DataGateway.Service.Tests/TestHelper.cs | 18 ++++++++---------- .../Unittests/BootStrapFailureTest.cs | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs b/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs index 0b0e9217eb..560173a598 100644 --- a/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs +++ b/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs @@ -66,7 +66,7 @@ protected static async Task InitializeTestFixture(TestContext context, string te Mock> configProviderLogger = new(); RuntimeConfigProvider.ConfigProviderLogger = configProviderLogger.Object; RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); - TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, "magazines", "foo"); + TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, "Magazine", "magazines", "foo"); _runtimeConfigProvider = TestHelper.GetRuntimeConfigProvider(_runtimeConfig); SetUpSQLMetadataProvider(); diff --git a/DataGateway.Service.Tests/TestHelper.cs b/DataGateway.Service.Tests/TestHelper.cs index 73183de357..0cf22c7c99 100644 --- a/DataGateway.Service.Tests/TestHelper.cs +++ b/DataGateway.Service.Tests/TestHelper.cs @@ -112,15 +112,14 @@ public static RuntimeConfig GetRuntimeConfig(RuntimeConfigProvider configProvide /// /// Temporary Helper function to ensure that in testing we have an entity - /// that can have a custom schema. We create a new entity of 'Magazine' with - /// a schema of 'foo' for table 'magazines', and then add this entity to our - /// runtime configuration. Because MySql will not have a schema we need a way - /// to customize this entity, which this helper function provides. Ultimately - /// this will be replaced with a JSON string in the tests that can be fully - /// customized for testing purposes. + /// that can have a custom schema. Ultimately this will be replaced with a JSON string + /// in the tests that can be fully customized for testing purposes. /// - /// - public static void AddMissingEntitiesToConfig(RuntimeConfig config, string dbObjectName, string nameSpace = "") + /// Runtimeconfig object + /// The key with which the entity is to be added. + /// The source name of the entity. + /// The namespace in which the entity is present. + public static void AddMissingEntitiesToConfig(RuntimeConfig config, string dbObjectKey, string dbObjectName, string nameSpace = "") { string source = config.DatabaseType is DatabaseType.mysql || string.IsNullOrEmpty(nameSpace) ? $"\"{dbObjectName}\"" : $"\"{nameSpace}.{dbObjectName}\""; string entityJsonString = @@ -149,8 +148,7 @@ public static void AddMissingEntitiesToConfig(RuntimeConfig config, string dbObj }; Entity entity = JsonSerializer.Deserialize(entityJsonString, options); - string entityKey = dbObjectName.Equals("magazines") ? "Magazine" : dbObjectName; - config.Entities.Add(entityKey, entity); + config.Entities.Add(dbObjectKey, entity); } } } diff --git a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs index f5b9f1d868..b476c4c64e 100644 --- a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs +++ b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs @@ -25,7 +25,7 @@ public async Task IndeterministicPrimaryKeyOnDatabaseObject() RuntimeConfigPath configPath = TestHelper.GetRuntimeConfigPath(_testCategory); RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); SqlTestHelper.RemoveAllRelationshipBetweenEntities(_runtimeConfig); - TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, _compositeViewName); + TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, _compositeViewName, _compositeViewName); _runtimeConfigProvider = TestHelper.GetRuntimeConfigProvider(_runtimeConfig); SetUpSQLMetadataProvider(); From 6cabf864484ef15df26e8b1fa3a82b5bb30af72d Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Wed, 20 Jul 2022 14:02:55 +0530 Subject: [PATCH 11/25] Sync with main --- .../Unittests/BootStrapFailureTest.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs index b476c4c64e..755b7cc82a 100644 --- a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs +++ b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs @@ -13,6 +13,12 @@ namespace Azure.DataGateway.Service.Tests.Unittests public class BootStrapFailureTest : SqlTestBase { private static readonly string _compositeViewName = "books_authors"; + + [ClassInitialize] + public static void Setup(TestContext context) + { + DatabaseEngine = TestCategory.MSSQL; + } /// /// Test to validate that the runtime fails and throws an exception during bootstrap when the primary /// key cannot be determined for a database object. @@ -21,8 +27,7 @@ public class BootStrapFailureTest : SqlTestBase [TestMethod] public async Task IndeterministicPrimaryKeyOnDatabaseObject() { - _testCategory = TestCategory.MSSQL; - RuntimeConfigPath configPath = TestHelper.GetRuntimeConfigPath(_testCategory); + RuntimeConfigPath configPath = TestHelper.GetRuntimeConfigPath(DatabaseEngine); RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); SqlTestHelper.RemoveAllRelationshipBetweenEntities(_runtimeConfig); TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, _compositeViewName, _compositeViewName); @@ -30,7 +35,7 @@ public async Task IndeterministicPrimaryKeyOnDatabaseObject() SetUpSQLMetadataProvider(); // Add composite view whose primary key cannot be determined. - string dbQuery = File.ReadAllText($"{_testCategory}Books.sql"); + string dbQuery = File.ReadAllText($"{DatabaseEngine}Books.sql"); string compositeViewQuery = $"EXEC('CREATE VIEW {_compositeViewName} as SELECT books.title, authors.[name], " + "authors.[birthdate], books.id as book_id, authors.id as author_id " + "FROM dbo.books INNER JOIN dbo.book_author_link ON books.[id] = book_author_link.book_id " + From 402951e43dd9fc07798ef3f63f9dfeed54e81e96 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Thu, 21 Jul 2022 09:42:57 +0530 Subject: [PATCH 12/25] Assert on substatuscode --- DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs index 755b7cc82a..453c672ba9 100644 --- a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs +++ b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs @@ -46,6 +46,7 @@ public async Task IndeterministicPrimaryKeyOnDatabaseObject() DataGatewayException ex = await Assert.ThrowsExceptionAsync(() => _sqlMetadataProvider.InitializeAsync()); Assert.AreEqual(HttpStatusCode.ServiceUnavailable, ex.StatusCode); Assert.AreEqual($"Primary key not configured on the given database object {_compositeViewName}", ex.Message); + Assert.AreEqual(DataGatewayException.SubStatusCodes.ErrorInInitialization, ex.SubStatusCode); } /// From a19c59e504228af3770dfda17e15b635b83c7382 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal <34566234+ayush3797@users.noreply.github.com> Date: Thu, 21 Jul 2022 10:42:38 +0530 Subject: [PATCH 13/25] Update DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs Co-authored-by: Aniruddh Munde --- DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs index 453c672ba9..134f1177b5 100644 --- a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs +++ b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs @@ -19,6 +19,7 @@ public static void Setup(TestContext context) { DatabaseEngine = TestCategory.MSSQL; } + /// /// Test to validate that the runtime fails and throws an exception during bootstrap when the primary /// key cannot be determined for a database object. From 58b67d9cd72ba4ca22a9410b6bed8070302e73d9 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Fri, 22 Jul 2022 14:57:44 +0530 Subject: [PATCH 14/25] Adding test cases for MySql/PostgreSql --- .../Unittests/BootStrapFailureTest.cs | 62 --------- .../PrimaryKeyTestsForCompositeViews.cs | 123 ++++++++++++++++++ 2 files changed, 123 insertions(+), 62 deletions(-) delete mode 100644 DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs create mode 100644 DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs diff --git a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs deleted file mode 100644 index 453c672ba9..0000000000 --- a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System.IO; -using System.Net; -using System.Threading.Tasks; -using Azure.DataGateway.Config; -using Azure.DataGateway.Service.Configurations; -using Azure.DataGateway.Service.Exceptions; -using Azure.DataGateway.Service.Tests.SqlTests; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace Azure.DataGateway.Service.Tests.Unittests -{ - [TestClass, TestCategory(TestCategory.MSSQL)] - public class BootStrapFailureTest : SqlTestBase - { - private static readonly string _compositeViewName = "books_authors"; - - [ClassInitialize] - public static void Setup(TestContext context) - { - DatabaseEngine = TestCategory.MSSQL; - } - /// - /// Test to validate that the runtime fails and throws an exception during bootstrap when the primary - /// key cannot be determined for a database object. - /// - /// - [TestMethod] - public async Task IndeterministicPrimaryKeyOnDatabaseObject() - { - RuntimeConfigPath configPath = TestHelper.GetRuntimeConfigPath(DatabaseEngine); - RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); - SqlTestHelper.RemoveAllRelationshipBetweenEntities(_runtimeConfig); - TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, _compositeViewName, _compositeViewName); - _runtimeConfigProvider = TestHelper.GetRuntimeConfigProvider(_runtimeConfig); - SetUpSQLMetadataProvider(); - - // Add composite view whose primary key cannot be determined. - string dbQuery = File.ReadAllText($"{DatabaseEngine}Books.sql"); - string compositeViewQuery = $"EXEC('CREATE VIEW {_compositeViewName} as SELECT books.title, authors.[name], " + - "authors.[birthdate], books.id as book_id, authors.id as author_id " + - "FROM dbo.books INNER JOIN dbo.book_author_link ON books.[id] = book_author_link.book_id " + - "INNER JOIN authors ON authors.[id] = book_author_link.author_id')"; - - // Execute the query to add view to the database. - await _queryExecutor.ExecuteQueryAsync(dbQuery + compositeViewQuery, parameters: null); - DataGatewayException ex = await Assert.ThrowsExceptionAsync(() => _sqlMetadataProvider.InitializeAsync()); - Assert.AreEqual(HttpStatusCode.ServiceUnavailable, ex.StatusCode); - Assert.AreEqual($"Primary key not configured on the given database object {_compositeViewName}", ex.Message); - Assert.AreEqual(DataGatewayException.SubStatusCodes.ErrorInInitialization, ex.SubStatusCode); - } - - /// - /// Runs after every test to reset the database state - /// - [TestCleanup] - public async Task TestCleanup() - { - string dropViewQuery = $"DROP VIEW IF EXISTS {_compositeViewName}"; - await _queryExecutor.ExecuteQueryAsync(dropViewQuery, parameters: null); - } - } -} diff --git a/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs b/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs new file mode 100644 index 0000000000..d7e717d963 --- /dev/null +++ b/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs @@ -0,0 +1,123 @@ +using System.IO; +using System.Net; +using System.Threading.Tasks; +using Azure.DataGateway.Config; +using Azure.DataGateway.Service.Configurations; +using Azure.DataGateway.Service.Exceptions; +using Azure.DataGateway.Service.Tests.SqlTests; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Azure.DataGateway.Service.Tests.Unittests +{ + [TestClass, TestCategory(TestCategory.MSSQL)] + public class PrimaryKeyTestsForCompositeViews : SqlTestBase + { + private static readonly string _compositeViewName = "books_authors"; + + /// + /// Test to validate that the runtime fails and throws an exception during bootstrap when the primary + /// key cannot be determined for a complex composite view for MsSql. + /// + /// + [TestMethod] + public async Task MsSqlPrimaryKeyOnComplexCompositeView() + { + DatabaseEngine = TestCategory.MSSQL; + RuntimeConfigPath configPath = TestHelper.GetRuntimeConfigPath(DatabaseEngine); + RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); + SqlTestHelper.RemoveAllRelationshipBetweenEntities(_runtimeConfig); + TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, _compositeViewName, _compositeViewName); + _runtimeConfigProvider = TestHelper.GetRuntimeConfigProvider(_runtimeConfig); + SetUpSQLMetadataProvider(); + + // Add composite view whose primary key cannot be determined. + string dbQuery = File.ReadAllText($"{DatabaseEngine}Books.sql"); + string compositeViewQuery = $"EXEC('CREATE VIEW {_compositeViewName} as SELECT books.title, authors.[name], " + + "authors.[birthdate], books.id as book_id, authors.id as author_id " + + "FROM dbo.books INNER JOIN dbo.book_author_link ON books.[id] = book_author_link.book_id " + + "INNER JOIN authors ON authors.[id] = book_author_link.author_id')"; + + // Execute the query to add view to the database. + await _queryExecutor.ExecuteQueryAsync(dbQuery + compositeViewQuery, parameters: null); + DataGatewayException ex = await Assert.ThrowsExceptionAsync(() => _sqlMetadataProvider.InitializeAsync()); + Assert.AreEqual(HttpStatusCode.ServiceUnavailable, ex.StatusCode); + Assert.AreEqual($"Primary key not configured on the given database object {_compositeViewName}", ex.Message); + Assert.AreEqual(DataGatewayException.SubStatusCodes.ErrorInInitialization, ex.SubStatusCode); + } + + /// + /// Test to validate that the runtime fails and throws an exception during bootstrap when the primary + /// key cannot be determined for a complex composite view for PostgreSql. + /// + /// + [TestMethod] + public async Task PostgreSqlPrimaryKeyOnComplexCompositeView() + { + DatabaseEngine = TestCategory.POSTGRESQL; + RuntimeConfigPath configPath = TestHelper.GetRuntimeConfigPath(DatabaseEngine); + RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); + SqlTestHelper.RemoveAllRelationshipBetweenEntities(_runtimeConfig); + TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, _compositeViewName, _compositeViewName); + _runtimeConfigProvider = TestHelper.GetRuntimeConfigProvider(_runtimeConfig); + SetUpSQLMetadataProvider(); + + // Add composite view whose primary key cannot be determined. + string dbQuery = File.ReadAllText($"{DatabaseEngine}Books.sql"); + string compositeViewQuery = $"DO $do$ " + + $"BEGIN " + + $"EXECUTE('CREATE VIEW {_compositeViewName} as " + + "SELECT books.title, authors.name, " + + "authors.birthdate, books.id as book_id, authors.id as author_id " + + "FROM books INNER JOIN book_author_link ON books.id = book_author_link.book_id " + + "INNER JOIN authors ON authors.id = book_author_link.author_id'); " + + "END " + + "$do$"; + + // Execute the query to add view to the database. + await _queryExecutor.ExecuteQueryAsync(dbQuery + compositeViewQuery, parameters: null); + DataGatewayException ex = await Assert.ThrowsExceptionAsync(() => _sqlMetadataProvider.InitializeAsync()); + Assert.AreEqual(HttpStatusCode.ServiceUnavailable, ex.StatusCode); + Assert.AreEqual($"Primary key not configured on the given database object {_compositeViewName}", ex.Message); + Assert.AreEqual(DataGatewayException.SubStatusCodes.ErrorInInitialization, ex.SubStatusCode); + } + + /// + /// Test to validate that the runtime boots up successfully when the primary + /// key can be determined for a complex composite view for MySql. + /// + /// + [TestMethod] + public async Task MySqlPrimaryKeyOnComplexCompositeView() + { + DatabaseEngine = TestCategory.MYSQL; + RuntimeConfigPath configPath = TestHelper.GetRuntimeConfigPath(DatabaseEngine); + RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); + SqlTestHelper.RemoveAllRelationshipBetweenEntities(_runtimeConfig); + TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, _compositeViewName, _compositeViewName); + _runtimeConfigProvider = TestHelper.GetRuntimeConfigProvider(_runtimeConfig); + SetUpSQLMetadataProvider(); + + // Add composite view whose primary key cannot be determined. + string dbQuery = File.ReadAllText($"{DatabaseEngine}Books.sql"); + string compositeViewQuery = $"prepare stmt4 from 'CREATE VIEW {_compositeViewName} as " + + "SELECT books.title, authors.name, " + + "authors.birthdate, books.id as book_id, authors.id as author_id " + + "FROM books INNER JOIN book_author_link ON books.id = book_author_link.book_id " + + "INNER JOIN authors ON authors.id = book_author_link.author_id';" + + "execute stmt4"; + + // Execute the query to add view to the database. + await _queryExecutor.ExecuteQueryAsync(dbQuery + compositeViewQuery, parameters: null); + } + + /// + /// Runs after every test to reset the database state + /// + [TestCleanup] + public async Task TestCleanup() + { + string dropViewQuery = $"DROP VIEW IF EXISTS {_compositeViewName}"; + await _queryExecutor.ExecuteQueryAsync(dropViewQuery, parameters: null); + } + } +} From 51ad300d2d83574851722a2342f216049fa031bf Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Fri, 22 Jul 2022 15:03:37 +0530 Subject: [PATCH 15/25] Removing previous unittest --- .../Unittests/BootStrapFailureTest.cs | 63 ------------------- 1 file changed, 63 deletions(-) delete mode 100644 DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs diff --git a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs b/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs deleted file mode 100644 index 134f1177b5..0000000000 --- a/DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System.IO; -using System.Net; -using System.Threading.Tasks; -using Azure.DataGateway.Config; -using Azure.DataGateway.Service.Configurations; -using Azure.DataGateway.Service.Exceptions; -using Azure.DataGateway.Service.Tests.SqlTests; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace Azure.DataGateway.Service.Tests.Unittests -{ - [TestClass, TestCategory(TestCategory.MSSQL)] - public class BootStrapFailureTest : SqlTestBase - { - private static readonly string _compositeViewName = "books_authors"; - - [ClassInitialize] - public static void Setup(TestContext context) - { - DatabaseEngine = TestCategory.MSSQL; - } - - /// - /// Test to validate that the runtime fails and throws an exception during bootstrap when the primary - /// key cannot be determined for a database object. - /// - /// - [TestMethod] - public async Task IndeterministicPrimaryKeyOnDatabaseObject() - { - RuntimeConfigPath configPath = TestHelper.GetRuntimeConfigPath(DatabaseEngine); - RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); - SqlTestHelper.RemoveAllRelationshipBetweenEntities(_runtimeConfig); - TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, _compositeViewName, _compositeViewName); - _runtimeConfigProvider = TestHelper.GetRuntimeConfigProvider(_runtimeConfig); - SetUpSQLMetadataProvider(); - - // Add composite view whose primary key cannot be determined. - string dbQuery = File.ReadAllText($"{DatabaseEngine}Books.sql"); - string compositeViewQuery = $"EXEC('CREATE VIEW {_compositeViewName} as SELECT books.title, authors.[name], " + - "authors.[birthdate], books.id as book_id, authors.id as author_id " + - "FROM dbo.books INNER JOIN dbo.book_author_link ON books.[id] = book_author_link.book_id " + - "INNER JOIN authors ON authors.[id] = book_author_link.author_id')"; - - // Execute the query to add view to the database. - await _queryExecutor.ExecuteQueryAsync(dbQuery + compositeViewQuery, parameters: null); - DataGatewayException ex = await Assert.ThrowsExceptionAsync(() => _sqlMetadataProvider.InitializeAsync()); - Assert.AreEqual(HttpStatusCode.ServiceUnavailable, ex.StatusCode); - Assert.AreEqual($"Primary key not configured on the given database object {_compositeViewName}", ex.Message); - Assert.AreEqual(DataGatewayException.SubStatusCodes.ErrorInInitialization, ex.SubStatusCode); - } - - /// - /// Runs after every test to reset the database state - /// - [TestCleanup] - public async Task TestCleanup() - { - string dropViewQuery = $"DROP VIEW IF EXISTS {_compositeViewName}"; - await _queryExecutor.ExecuteQueryAsync(dropViewQuery, parameters: null); - } - } -} From 1be32926323bcf0c02704cb0a6af1acdad1622e9 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Fri, 22 Jul 2022 15:09:16 +0530 Subject: [PATCH 16/25] Attempting to fix build failures --- .../Unittests/PrimaryKeyTestsForCompositeViews.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs b/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs index d7e717d963..e0d0b28726 100644 --- a/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs +++ b/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs @@ -9,7 +9,7 @@ namespace Azure.DataGateway.Service.Tests.Unittests { - [TestClass, TestCategory(TestCategory.MSSQL)] + [TestClass] public class PrimaryKeyTestsForCompositeViews : SqlTestBase { private static readonly string _compositeViewName = "books_authors"; @@ -19,7 +19,7 @@ public class PrimaryKeyTestsForCompositeViews : SqlTestBase /// key cannot be determined for a complex composite view for MsSql. /// /// - [TestMethod] + [TestMethod, TestCategory(TestCategory.MSSQL)] public async Task MsSqlPrimaryKeyOnComplexCompositeView() { DatabaseEngine = TestCategory.MSSQL; @@ -50,7 +50,7 @@ public async Task MsSqlPrimaryKeyOnComplexCompositeView() /// key cannot be determined for a complex composite view for PostgreSql. /// /// - [TestMethod] + [TestMethod, TestCategory(TestCategory.POSTGRESQL)] public async Task PostgreSqlPrimaryKeyOnComplexCompositeView() { DatabaseEngine = TestCategory.POSTGRESQL; @@ -86,7 +86,7 @@ public async Task PostgreSqlPrimaryKeyOnComplexCompositeView() /// key can be determined for a complex composite view for MySql. /// /// - [TestMethod] + [TestMethod, TestCategory(TestCategory.MYSQL)] public async Task MySqlPrimaryKeyOnComplexCompositeView() { DatabaseEngine = TestCategory.MYSQL; From 3734aa3ce11a968745b9b1405da4794d418028b3 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Mon, 25 Jul 2022 12:06:04 +0530 Subject: [PATCH 17/25] Test restructuring --- .../PrimaryKeyTestsForCompositeViews.cs | 98 +++++++++---------- 1 file changed, 45 insertions(+), 53 deletions(-) diff --git a/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs b/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs index e0d0b28726..af6d05b545 100644 --- a/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs +++ b/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs @@ -1,3 +1,4 @@ +using System; using System.IO; using System.Net; using System.Threading.Tasks; @@ -13,6 +14,10 @@ namespace Azure.DataGateway.Service.Tests.Unittests public class PrimaryKeyTestsForCompositeViews : SqlTestBase { private static readonly string _compositeViewName = "books_authors"; + private static readonly string _compositeViewQuery = $"'CREATE VIEW {_compositeViewName} as SELECT books.title, authors.name, " + + $"authors.birthdate, books.id as book_id, authors.id as author_id " + + $"FROM books INNER JOIN book_author_link ON books.id = book_author_link.book_id " + + $"INNER JOIN authors ON authors.id = book_author_link.author_id'"; /// /// Test to validate that the runtime fails and throws an exception during bootstrap when the primary @@ -22,27 +27,12 @@ public class PrimaryKeyTestsForCompositeViews : SqlTestBase [TestMethod, TestCategory(TestCategory.MSSQL)] public async Task MsSqlPrimaryKeyOnComplexCompositeView() { - DatabaseEngine = TestCategory.MSSQL; - RuntimeConfigPath configPath = TestHelper.GetRuntimeConfigPath(DatabaseEngine); - RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); - SqlTestHelper.RemoveAllRelationshipBetweenEntities(_runtimeConfig); - TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, _compositeViewName, _compositeViewName); - _runtimeConfigProvider = TestHelper.GetRuntimeConfigProvider(_runtimeConfig); - SetUpSQLMetadataProvider(); + // Create query to be executed on the database to add the view. + string compositeViewDbQuery = $"EXEC(" + + _compositeViewQuery + + ")"; - // Add composite view whose primary key cannot be determined. - string dbQuery = File.ReadAllText($"{DatabaseEngine}Books.sql"); - string compositeViewQuery = $"EXEC('CREATE VIEW {_compositeViewName} as SELECT books.title, authors.[name], " + - "authors.[birthdate], books.id as book_id, authors.id as author_id " + - "FROM dbo.books INNER JOIN dbo.book_author_link ON books.[id] = book_author_link.book_id " + - "INNER JOIN authors ON authors.[id] = book_author_link.author_id')"; - - // Execute the query to add view to the database. - await _queryExecutor.ExecuteQueryAsync(dbQuery + compositeViewQuery, parameters: null); - DataGatewayException ex = await Assert.ThrowsExceptionAsync(() => _sqlMetadataProvider.InitializeAsync()); - Assert.AreEqual(HttpStatusCode.ServiceUnavailable, ex.StatusCode); - Assert.AreEqual($"Primary key not configured on the given database object {_compositeViewName}", ex.Message); - Assert.AreEqual(DataGatewayException.SubStatusCodes.ErrorInInitialization, ex.SubStatusCode); + await AddViewToDatabaseTestAsync(compositeViewDbQuery, TestCategory.MSSQL, true); } /// @@ -53,32 +43,16 @@ public async Task MsSqlPrimaryKeyOnComplexCompositeView() [TestMethod, TestCategory(TestCategory.POSTGRESQL)] public async Task PostgreSqlPrimaryKeyOnComplexCompositeView() { - DatabaseEngine = TestCategory.POSTGRESQL; - RuntimeConfigPath configPath = TestHelper.GetRuntimeConfigPath(DatabaseEngine); - RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); - SqlTestHelper.RemoveAllRelationshipBetweenEntities(_runtimeConfig); - TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, _compositeViewName, _compositeViewName); - _runtimeConfigProvider = TestHelper.GetRuntimeConfigProvider(_runtimeConfig); - SetUpSQLMetadataProvider(); - - // Add composite view whose primary key cannot be determined. - string dbQuery = File.ReadAllText($"{DatabaseEngine}Books.sql"); - string compositeViewQuery = $"DO $do$ " + + // Create query to be executed on the database to add the view. + string compositeViewDbQuery = $"DO $do$ " + $"BEGIN " + - $"EXECUTE('CREATE VIEW {_compositeViewName} as " + - "SELECT books.title, authors.name, " + - "authors.birthdate, books.id as book_id, authors.id as author_id " + - "FROM books INNER JOIN book_author_link ON books.id = book_author_link.book_id " + - "INNER JOIN authors ON authors.id = book_author_link.author_id'); " + + $"EXECUTE(" + + _compositeViewQuery + + "); " + "END " + "$do$"; - // Execute the query to add view to the database. - await _queryExecutor.ExecuteQueryAsync(dbQuery + compositeViewQuery, parameters: null); - DataGatewayException ex = await Assert.ThrowsExceptionAsync(() => _sqlMetadataProvider.InitializeAsync()); - Assert.AreEqual(HttpStatusCode.ServiceUnavailable, ex.StatusCode); - Assert.AreEqual($"Primary key not configured on the given database object {_compositeViewName}", ex.Message); - Assert.AreEqual(DataGatewayException.SubStatusCodes.ErrorInInitialization, ex.SubStatusCode); + await AddViewToDatabaseTestAsync(compositeViewDbQuery, TestCategory.POSTGRESQL, true); } /// @@ -89,7 +63,19 @@ public async Task PostgreSqlPrimaryKeyOnComplexCompositeView() [TestMethod, TestCategory(TestCategory.MYSQL)] public async Task MySqlPrimaryKeyOnComplexCompositeView() { - DatabaseEngine = TestCategory.MYSQL; + // Create query to be executed on the database to add the view. + string compositeViewDbQuery = $"prepare stmt4 from " + + _compositeViewQuery + + ";" + + "execute stmt4"; + await AddViewToDatabaseTestAsync(compositeViewDbQuery, TestCategory.MYSQL, false); + } + + private static async Task AddViewToDatabaseTestAsync(string compositeDbViewquery, string dbEngine, bool isExceptionExpected) + { + // Setup dependencies + DatabaseEngine = dbEngine; + string dbQuery = File.ReadAllText($"{DatabaseEngine}Books.sql"); RuntimeConfigPath configPath = TestHelper.GetRuntimeConfigPath(DatabaseEngine); RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); SqlTestHelper.RemoveAllRelationshipBetweenEntities(_runtimeConfig); @@ -97,17 +83,23 @@ public async Task MySqlPrimaryKeyOnComplexCompositeView() _runtimeConfigProvider = TestHelper.GetRuntimeConfigProvider(_runtimeConfig); SetUpSQLMetadataProvider(); - // Add composite view whose primary key cannot be determined. - string dbQuery = File.ReadAllText($"{DatabaseEngine}Books.sql"); - string compositeViewQuery = $"prepare stmt4 from 'CREATE VIEW {_compositeViewName} as " + - "SELECT books.title, authors.name, " + - "authors.birthdate, books.id as book_id, authors.id as author_id " + - "FROM books INNER JOIN book_author_link ON books.id = book_author_link.book_id " + - "INNER JOIN authors ON authors.id = book_author_link.author_id';" + - "execute stmt4"; + await _queryExecutor.ExecuteQueryAsync(dbQuery + compositeDbViewquery, parameters: null); + + if (isExceptionExpected) + { + DataGatewayException ex = await Assert.ThrowsExceptionAsync(() => _sqlMetadataProvider.InitializeAsync()); + Assert.AreEqual(HttpStatusCode.ServiceUnavailable, ex.StatusCode); + Assert.AreEqual($"Primary key not configured on the given database object {_compositeViewName}", ex.Message); + Assert.AreEqual(DataGatewayException.SubStatusCodes.ErrorInInitialization, ex.SubStatusCode); + } + else + { + await _sqlMetadataProvider.InitializeAsync(); - // Execute the query to add view to the database. - await _queryExecutor.ExecuteQueryAsync(dbQuery + compositeViewQuery, parameters: null); + // Validate that when exception is not thrown, the view's definition has been + // successfully added to the Entity map. + Assert.IsTrue(_sqlMetadataProvider.EntityToDatabaseObject.ContainsKey(_compositeViewName)); + } } /// From f329ae468d47bedcc506c7f00279d9d941f51126 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Mon, 25 Jul 2022 12:11:03 +0530 Subject: [PATCH 18/25] Formatting fix --- .../Unittests/PrimaryKeyTestsForCompositeViews.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs b/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs index af6d05b545..0b00d67d92 100644 --- a/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs +++ b/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs @@ -1,4 +1,3 @@ -using System; using System.IO; using System.Net; using System.Threading.Tasks; From 1b12e294ab268da9bf4edb92691fd7da5b6f0418 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Tue, 26 Jul 2022 12:48:52 +0530 Subject: [PATCH 19/25] Moving test file to appropriate directory --- .../RestBootstrapTests}/PrimaryKeyTestsForCompositeViews.cs | 3 +-- DataGateway.Service.Tests/TestHelper.cs | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) rename DataGateway.Service.Tests/{Unittests => SqlTests/RestBootstrapTests}/PrimaryKeyTestsForCompositeViews.cs (98%) diff --git a/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs b/DataGateway.Service.Tests/SqlTests/RestBootstrapTests/PrimaryKeyTestsForCompositeViews.cs similarity index 98% rename from DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs rename to DataGateway.Service.Tests/SqlTests/RestBootstrapTests/PrimaryKeyTestsForCompositeViews.cs index 0b00d67d92..da605451b5 100644 --- a/DataGateway.Service.Tests/Unittests/PrimaryKeyTestsForCompositeViews.cs +++ b/DataGateway.Service.Tests/SqlTests/RestBootstrapTests/PrimaryKeyTestsForCompositeViews.cs @@ -4,10 +4,9 @@ using Azure.DataGateway.Config; using Azure.DataGateway.Service.Configurations; using Azure.DataGateway.Service.Exceptions; -using Azure.DataGateway.Service.Tests.SqlTests; using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace Azure.DataGateway.Service.Tests.Unittests +namespace Azure.DataGateway.Service.Tests.SqlTests.RestBootstrapTests { [TestClass] public class PrimaryKeyTestsForCompositeViews : SqlTestBase diff --git a/DataGateway.Service.Tests/TestHelper.cs b/DataGateway.Service.Tests/TestHelper.cs index 0cf22c7c99..cc0779b46d 100644 --- a/DataGateway.Service.Tests/TestHelper.cs +++ b/DataGateway.Service.Tests/TestHelper.cs @@ -118,10 +118,10 @@ public static RuntimeConfig GetRuntimeConfig(RuntimeConfigProvider configProvide /// Runtimeconfig object /// The key with which the entity is to be added. /// The source name of the entity. - /// The namespace in which the entity is present. - public static void AddMissingEntitiesToConfig(RuntimeConfig config, string dbObjectKey, string dbObjectName, string nameSpace = "") + /// The schema in which the entity is present. + public static void AddMissingEntitiesToConfig(RuntimeConfig config, string dbObjectKey, string dbObjectName, string schema = "") { - string source = config.DatabaseType is DatabaseType.mysql || string.IsNullOrEmpty(nameSpace) ? $"\"{dbObjectName}\"" : $"\"{nameSpace}.{dbObjectName}\""; + string source = config.DatabaseType is DatabaseType.mysql || string.IsNullOrEmpty(schema) ? $"\"{dbObjectName}\"" : $"\"{schema}.{dbObjectName}\""; string entityJsonString = @"{ ""source"": " + source + @", From 18d23ac15a72bcb6f4f1baa0b5fe8a1c08b9d74c Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Wed, 27 Jul 2022 10:57:55 +0530 Subject: [PATCH 20/25] Adding comments --- .../PrimaryKeyTestsForCompositeViews.cs | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/DataGateway.Service.Tests/SqlTests/RestBootstrapTests/PrimaryKeyTestsForCompositeViews.cs b/DataGateway.Service.Tests/SqlTests/RestBootstrapTests/PrimaryKeyTestsForCompositeViews.cs index da605451b5..7b9d387bc2 100644 --- a/DataGateway.Service.Tests/SqlTests/RestBootstrapTests/PrimaryKeyTestsForCompositeViews.cs +++ b/DataGateway.Service.Tests/SqlTests/RestBootstrapTests/PrimaryKeyTestsForCompositeViews.cs @@ -8,6 +8,11 @@ namespace Azure.DataGateway.Service.Tests.SqlTests.RestBootstrapTests { + /// + /// Test class to perform tests on MsSql, MySql, PostgreSql for REST to check if the primary key + /// can be determined for a complex composite view. In case it cannot be determined, the runtime + /// would fail during boot up. + /// [TestClass] public class PrimaryKeyTestsForCompositeViews : SqlTestBase { @@ -30,7 +35,7 @@ public async Task MsSqlPrimaryKeyOnComplexCompositeView() _compositeViewQuery + ")"; - await AddViewToDatabaseTestAsync(compositeViewDbQuery, TestCategory.MSSQL, true); + await SetupDatabaseAsync(compositeViewDbQuery, TestCategory.MSSQL, true); } /// @@ -50,7 +55,7 @@ public async Task PostgreSqlPrimaryKeyOnComplexCompositeView() "END " + "$do$"; - await AddViewToDatabaseTestAsync(compositeViewDbQuery, TestCategory.POSTGRESQL, true); + await SetupDatabaseAsync(compositeViewDbQuery, TestCategory.POSTGRESQL, true); } /// @@ -66,10 +71,18 @@ public async Task MySqlPrimaryKeyOnComplexCompositeView() _compositeViewQuery + ";" + "execute stmt4"; - await AddViewToDatabaseTestAsync(compositeViewDbQuery, TestCategory.MYSQL, false); + await SetupDatabaseAsync(compositeViewDbQuery, TestCategory.MYSQL, false); } - private static async Task AddViewToDatabaseTestAsync(string compositeDbViewquery, string dbEngine, bool isExceptionExpected) + /// + /// Helper method to setup dependencies to perform tests and setup the database, + /// i.e. add all the tables and the complex view to the database. + /// + /// Query to add composite view to database. + /// The database engine. For eg. MsSql. + /// Boolean value indicating whether boot up is expected to fail. + /// + private static async Task SetupDatabaseAsync(string compositeDbViewquery, string dbEngine, bool isExceptionExpected) { // Setup dependencies DatabaseEngine = dbEngine; From 44fa2b71ba4d1e8ac85f9369e1ecc5edeb69f03c Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Wed, 27 Jul 2022 12:00:02 +0530 Subject: [PATCH 21/25] Adding find test for mysql view --- .../PrimaryKeyTestsForCompositeViews.cs | 55 +++++++++++++------ .../SqlTests/SqlTestBase.cs | 45 ++++++++++++++- 2 files changed, 81 insertions(+), 19 deletions(-) diff --git a/DataGateway.Service.Tests/SqlTests/RestBootstrapTests/PrimaryKeyTestsForCompositeViews.cs b/DataGateway.Service.Tests/SqlTests/RestBootstrapTests/PrimaryKeyTestsForCompositeViews.cs index 7b9d387bc2..3404c4733a 100644 --- a/DataGateway.Service.Tests/SqlTests/RestBootstrapTests/PrimaryKeyTestsForCompositeViews.cs +++ b/DataGateway.Service.Tests/SqlTests/RestBootstrapTests/PrimaryKeyTestsForCompositeViews.cs @@ -1,9 +1,9 @@ -using System.IO; +using System.Collections.Generic; using System.Net; using System.Threading.Tasks; -using Azure.DataGateway.Config; -using Azure.DataGateway.Service.Configurations; +using Azure.DataGateway.Service.Controllers; using Azure.DataGateway.Service.Exceptions; +using Azure.DataGateway.Service.Services; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Azure.DataGateway.Service.Tests.SqlTests.RestBootstrapTests @@ -86,30 +86,49 @@ private static async Task SetupDatabaseAsync(string compositeDbViewquery, string { // Setup dependencies DatabaseEngine = dbEngine; - string dbQuery = File.ReadAllText($"{DatabaseEngine}Books.sql"); - RuntimeConfigPath configPath = TestHelper.GetRuntimeConfigPath(DatabaseEngine); - RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); - SqlTestHelper.RemoveAllRelationshipBetweenEntities(_runtimeConfig); - TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, _compositeViewName, _compositeViewName); - _runtimeConfigProvider = TestHelper.GetRuntimeConfigProvider(_runtimeConfig); - SetUpSQLMetadataProvider(); - - await _queryExecutor.ExecuteQueryAsync(dbQuery + compositeDbViewquery, parameters: null); - + string[] customEntity = { _compositeViewName, _compositeViewName, "" }; if (isExceptionExpected) { - DataGatewayException ex = await Assert.ThrowsExceptionAsync(() => _sqlMetadataProvider.InitializeAsync()); + DataGatewayException ex = await Assert.ThrowsExceptionAsync(() => + InitializeTestFixture(null, new List { compositeDbViewquery }, new List { customEntity })); Assert.AreEqual(HttpStatusCode.ServiceUnavailable, ex.StatusCode); Assert.AreEqual($"Primary key not configured on the given database object {_compositeViewName}", ex.Message); Assert.AreEqual(DataGatewayException.SubStatusCodes.ErrorInInitialization, ex.SubStatusCode); } else { - await _sqlMetadataProvider.InitializeAsync(); + await InitializeTestFixture(null, new List { compositeDbViewquery }, + new List { customEntity }); + + // Perform a GET operation on the view to confirm that it is functional. + // Set up rest controller. + RestService _restService = new(_queryEngine, + _mutationEngine, + _sqlMetadataProvider, + _httpContextAccessor.Object, + _authorizationService.Object, + _authorizationResolver, + _runtimeConfigProvider); + RestController _restController = new(_restService); + + // Query to validate the GET operation result. + string query = @" + SELECT JSON_OBJECT( 'title', title, 'name', name, 'birthdate', + birthdate, 'book_id', book_id, 'author_id', author_id) AS data + FROM ( + SELECT * + FROM " + _compositeViewName + @" + WHERE book_id = 1 AND author_id = 123 + ) AS subq"; - // Validate that when exception is not thrown, the view's definition has been - // successfully added to the Entity map. - Assert.IsTrue(_sqlMetadataProvider.EntityToDatabaseObject.ContainsKey(_compositeViewName)); + // Perform GET operation on the view. + await SetupAndRunRestApiTest( + primaryKeyRoute: "book_id/1/author_id/123", + queryString: string.Empty, + entity: _compositeViewName, + sqlQuery: query, + controller: _restController + ); } } diff --git a/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs b/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs index 5413e26006..21bac45ae9 100644 --- a/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs +++ b/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs @@ -64,13 +64,21 @@ public abstract class SqlTestBase /// this class. /// /// - protected static async Task InitializeTestFixture(TestContext context) + /// Test specific queries to be executed on database. + /// Test specific entities to be added to database. + /// + protected static async Task InitializeTestFixture(TestContext context, List customQueries = null, + List customEntities = null) { RuntimeConfigPath configPath = TestHelper.GetRuntimeConfigPath($"{DatabaseEngine}"); Mock> configProviderLogger = new(); RuntimeConfigProvider.ConfigProviderLogger = configProviderLogger.Object; RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, "Magazine", "magazines", "foo"); + + // Add custom entities for the test, if any. + AddCustomEntities(customEntities); + _runtimeConfigProvider = TestHelper.GetRuntimeConfigProvider(_runtimeConfig); SetUpSQLMetadataProvider(); @@ -87,6 +95,10 @@ protected static async Task InitializeTestFixture(TestContext context) _httpContextAccessor.Setup(x => x.HttpContext.User).Returns(new ClaimsPrincipal()); await ResetDbStateAsync(); + + //Execute additional queries, if any. + await ExecuteQueriesOnDbAsync(customQueries); + await _sqlMetadataProvider.InitializeAsync(); //Initialize the authorization resolver object @@ -123,6 +135,37 @@ protected static async Task InitializeTestFixture(TestContext context) HttpClient = _application.CreateClient(); } + /// + /// Helper method to add test specific entities to the entity mapping. + /// + /// List of test specific entities. + private static void AddCustomEntities(List customEntities) + { + foreach (string[] customEntity in customEntities) + { + string objectKey = customEntity[0]; + string objectName = customEntity[1]; + string schemaName = customEntity[2]; + TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, objectKey, objectName, schemaName); + } + } + + /// + /// Helper method to execute all the additional queries for a test on the database. + /// + /// + /// + private static async Task ExecuteQueriesOnDbAsync(List customQueries) + { + if (customQueries is not null) + { + foreach (string query in customQueries) + { + await _queryExecutor.ExecuteQueryAsync(query, parameters: null); + } + } + } + protected static void SetUpSQLMetadataProvider() { _sqlMetadataLogger = new Mock>().Object; From 2fe20381472c138e2141b95cc5986ec7bfda4978 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Wed, 27 Jul 2022 12:20:24 +0530 Subject: [PATCH 22/25] Null check on custom entities --- DataGateway.Service.Tests/SqlTests/SqlTestBase.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs b/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs index 6f6b4502d6..fcd235585b 100644 --- a/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs +++ b/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs @@ -146,12 +146,15 @@ protected static async Task InitializeTestFixture(TestContext context, ListList of test specific entities. private static void AddCustomEntities(List customEntities) { - foreach (string[] customEntity in customEntities) + if (customEntities is not null) { - string objectKey = customEntity[0]; - string objectName = customEntity[1]; - string schemaName = customEntity[2]; - TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, objectKey, objectName, schemaName); + foreach (string[] customEntity in customEntities) + { + string objectKey = customEntity[0]; + string objectName = customEntity[1]; + string schemaName = customEntity[2]; + TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, objectKey, objectName, schemaName); + } } } From 704b0758549d884e640bb29e7c3ea4de672ce2df Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Thu, 28 Jul 2022 11:23:01 +0530 Subject: [PATCH 23/25] Seperating db specific logic from addentity function --- DataGateway.Service.Tests/SqlTests/SqlTestBase.cs | 11 ++++++++++- DataGateway.Service.Tests/TestHelper.cs | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs b/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs index fcd235585b..ebaf0f94fe 100644 --- a/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs +++ b/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs @@ -76,7 +76,16 @@ protected static async Task InitializeTestFixture(TestContext context, List> configProviderLogger = new(); RuntimeConfigProvider.ConfigProviderLogger = configProviderLogger.Object; RuntimeConfigProvider.LoadRuntimeConfigValue(configPath, out _runtimeConfig); - TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, "Magazine", "magazines", "foo"); + + // Add magazines entity to the + if (TestCategory.MYSQL.Equals(DatabaseEngine)) + { + TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, "Magazine", "magazines", ""); + } + else + { + TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, "Magazine", "magazines", "foo"); + } // Add custom entities for the test, if any. AddCustomEntities(customEntities); diff --git a/DataGateway.Service.Tests/TestHelper.cs b/DataGateway.Service.Tests/TestHelper.cs index cc0779b46d..2558a94a9b 100644 --- a/DataGateway.Service.Tests/TestHelper.cs +++ b/DataGateway.Service.Tests/TestHelper.cs @@ -121,7 +121,7 @@ public static RuntimeConfig GetRuntimeConfig(RuntimeConfigProvider configProvide /// The schema in which the entity is present. public static void AddMissingEntitiesToConfig(RuntimeConfig config, string dbObjectKey, string dbObjectName, string schema = "") { - string source = config.DatabaseType is DatabaseType.mysql || string.IsNullOrEmpty(schema) ? $"\"{dbObjectName}\"" : $"\"{schema}.{dbObjectName}\""; + string source = string.IsNullOrEmpty(schema) ? $"\"{dbObjectName}\"" : $"\"{schema}.{dbObjectName}\""; string entityJsonString = @"{ ""source"": " + source + @", From d574c2d772fc7fe5ae612103d0a2840e0aa9d79b Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Thu, 28 Jul 2022 21:47:51 +0530 Subject: [PATCH 24/25] Removing schema parameter --- .../PrimaryKeyTestsForCompositeViews.cs | 12 ++++++------ DataGateway.Service.Tests/SqlTests/SqlTestBase.cs | 7 +++---- DataGateway.Service.Tests/TestHelper.cs | 6 ++---- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/DataGateway.Service.Tests/SqlTests/RestBootstrapTests/PrimaryKeyTestsForCompositeViews.cs b/DataGateway.Service.Tests/SqlTests/RestBootstrapTests/PrimaryKeyTestsForCompositeViews.cs index 3404c4733a..ca9419c51d 100644 --- a/DataGateway.Service.Tests/SqlTests/RestBootstrapTests/PrimaryKeyTestsForCompositeViews.cs +++ b/DataGateway.Service.Tests/SqlTests/RestBootstrapTests/PrimaryKeyTestsForCompositeViews.cs @@ -103,12 +103,12 @@ private static async Task SetupDatabaseAsync(string compositeDbViewquery, string // Perform a GET operation on the view to confirm that it is functional. // Set up rest controller. RestService _restService = new(_queryEngine, - _mutationEngine, - _sqlMetadataProvider, - _httpContextAccessor.Object, - _authorizationService.Object, - _authorizationResolver, - _runtimeConfigProvider); + _mutationEngine, + _sqlMetadataProvider, + _httpContextAccessor.Object, + _authorizationService.Object, + _authorizationResolver, + _runtimeConfigProvider); RestController _restController = new(_restService); // Query to validate the GET operation result. diff --git a/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs b/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs index ebaf0f94fe..47adeb1102 100644 --- a/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs +++ b/DataGateway.Service.Tests/SqlTests/SqlTestBase.cs @@ -80,11 +80,11 @@ protected static async Task InitializeTestFixture(TestContext context, List customEntities) { string objectKey = customEntity[0]; string objectName = customEntity[1]; - string schemaName = customEntity[2]; - TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, objectKey, objectName, schemaName); + TestHelper.AddMissingEntitiesToConfig(_runtimeConfig, objectKey, objectName); } } } diff --git a/DataGateway.Service.Tests/TestHelper.cs b/DataGateway.Service.Tests/TestHelper.cs index 2558a94a9b..ed4c0409ef 100644 --- a/DataGateway.Service.Tests/TestHelper.cs +++ b/DataGateway.Service.Tests/TestHelper.cs @@ -118,13 +118,11 @@ public static RuntimeConfig GetRuntimeConfig(RuntimeConfigProvider configProvide /// Runtimeconfig object /// The key with which the entity is to be added. /// The source name of the entity. - /// The schema in which the entity is present. - public static void AddMissingEntitiesToConfig(RuntimeConfig config, string dbObjectKey, string dbObjectName, string schema = "") + public static void AddMissingEntitiesToConfig(RuntimeConfig config, string dbObjectKey, string dbObjectName) { - string source = string.IsNullOrEmpty(schema) ? $"\"{dbObjectName}\"" : $"\"{schema}.{dbObjectName}\""; string entityJsonString = @"{ - ""source"": " + source + @", + ""source"": " + dbObjectName + @", ""graphql"": true, ""permissions"": [ { From 5113972b5a290be3d59fd7d476e6f797f8c041c1 Mon Sep 17 00:00:00 2001 From: Ayush Agarwal Date: Thu, 28 Jul 2022 21:57:04 +0530 Subject: [PATCH 25/25] Fixed method add entity --- DataGateway.Service.Tests/TestHelper.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DataGateway.Service.Tests/TestHelper.cs b/DataGateway.Service.Tests/TestHelper.cs index ed4c0409ef..78f95b052c 100644 --- a/DataGateway.Service.Tests/TestHelper.cs +++ b/DataGateway.Service.Tests/TestHelper.cs @@ -120,9 +120,10 @@ public static RuntimeConfig GetRuntimeConfig(RuntimeConfigProvider configProvide /// The source name of the entity. public static void AddMissingEntitiesToConfig(RuntimeConfig config, string dbObjectKey, string dbObjectName) { + string source = "\"" + dbObjectName + "\""; string entityJsonString = @"{ - ""source"": " + dbObjectName + @", + ""source"": " + source + @", ""graphql"": true, ""permissions"": [ {