-
Notifications
You must be signed in to change notification settings - Fork 357
Adding test cases for primary key evaluation for complex composite views #552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Ayush Agarwal (ayush3797)
merged 37 commits into
main
from
dev/agarwalayush/BootStrapFailureTestCase
Jul 28, 2022
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
68539fd
Negative test case for undeterminitstic primary key
952a7ec
Formatting fix
61caf2a
Trying to fix build error on remote
2151026
Ättempt 2
3f36b29
Attempting to fix test
5c66b49
Merge branch 'main' into dev/agarwalayush/BootStrapFailureTestCase
ayush3797 69b179f
Using Assert exception
33c84ea
Merge branch 'dev/agarwalayush/BootStrapFailureTestCase' of https://g…
3a1a9dd
Merge with main
45087ab
Fixing build failures
2794ce3
Using more appropriate statuscode
7c2bd09
Using test cleanup
fb44efd
Passing entity key as an argument rather than having special case
f83a948
Merge branch 'main' into dev/agarwalayush/BootStrapFailureTestCase
ayush3797 6cabf86
Sync with main
402951e
Assert on substatuscode
71dbdff
Merge branch 'main' into dev/agarwalayush/BootStrapFailureTestCase
ayush3797 a19c59e
Update DataGateway.Service.Tests/Unittests/BootStrapFailureTest.cs
ayush3797 58b67d9
Adding test cases for MySql/PostgreSql
0882d6c
pull with remote
51ad300
Removing previous unittest
1be3292
Attempting to fix build failures
a657cee
Merge branch 'main' into dev/agarwalayush/BootStrapFailureTestCase
ayush3797 3734aa3
Test restructuring
6b786b9
Merge branch 'dev/agarwalayush/BootStrapFailureTestCase' of https://g…
f329ae4
Formatting fix
e7dedc3
Merge remote-tracking branch 'origin/main' into dev/agarwalayush/Boot…
1b12e29
Moving test file to appropriate directory
18d23ac
Adding comments
44fa2b7
Adding find test for mysql view
790f551
Merge with main
2fe2038
Null check on custom entities
c4992a4
Merge branch 'main' into dev/agarwalayush/BootStrapFailureTestCase
ayush3797 704b075
Seperating db specific logic from addentity function
d574c2d
Removing schema parameter
5113972
Fixed method add entity
141456e
Merge branch 'main' into dev/agarwalayush/BootStrapFailureTestCase
ayush3797 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
DataGateway.Service.Tests/SqlTests/RestBootstrapTests/PrimaryKeyTestsForCompositeViews.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| using System.Collections.Generic; | ||
| using System.Net; | ||
| using System.Threading.Tasks; | ||
| 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 | ||
| { | ||
| /// <summary> | ||
| /// 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. | ||
| /// </summary> | ||
| [TestClass] | ||
| 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'"; | ||
|
|
||
| /// <summary> | ||
| /// 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. | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| [TestMethod, TestCategory(TestCategory.MSSQL)] | ||
| public async Task MsSqlPrimaryKeyOnComplexCompositeView() | ||
| { | ||
| // Create query to be executed on the database to add the view. | ||
| string compositeViewDbQuery = $"EXEC(" + | ||
| _compositeViewQuery + | ||
| ")"; | ||
|
|
||
| await SetupDatabaseAsync(compositeViewDbQuery, TestCategory.MSSQL, true); | ||
|
ayush3797 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// 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. | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| [TestMethod, TestCategory(TestCategory.POSTGRESQL)] | ||
| public async Task PostgreSqlPrimaryKeyOnComplexCompositeView() | ||
| { | ||
| // Create query to be executed on the database to add the view. | ||
| string compositeViewDbQuery = $"DO $do$ " + | ||
| $"BEGIN " + | ||
| $"EXECUTE(" + | ||
| _compositeViewQuery + | ||
| "); " + | ||
| "END " + | ||
| "$do$"; | ||
|
|
||
| await SetupDatabaseAsync(compositeViewDbQuery, TestCategory.POSTGRESQL, true); | ||
|
ayush3797 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Test to validate that the runtime boots up successfully when the primary | ||
| /// key can be determined for a complex composite view for MySql. | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| [TestMethod, TestCategory(TestCategory.MYSQL)] | ||
| public async Task MySqlPrimaryKeyOnComplexCompositeView() | ||
| { | ||
| // Create query to be executed on the database to add the view. | ||
| string compositeViewDbQuery = $"prepare stmt4 from " + | ||
| _compositeViewQuery + | ||
| ";" + | ||
| "execute stmt4"; | ||
| await SetupDatabaseAsync(compositeViewDbQuery, TestCategory.MYSQL, false); | ||
|
ayush3797 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// 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. | ||
| /// </summary> | ||
| /// <param name="compositeDbViewquery">Query to add composite view to database.</param> | ||
| /// <param name="dbEngine">The database engine. For eg. MsSql.</param> | ||
| /// <param name="isExceptionExpected">Boolean value indicating whether boot up is expected to fail.</param> | ||
| /// <returns></returns> | ||
| private static async Task SetupDatabaseAsync(string compositeDbViewquery, string dbEngine, bool isExceptionExpected) | ||
| { | ||
| // Setup dependencies | ||
| DatabaseEngine = dbEngine; | ||
| string[] customEntity = { _compositeViewName, _compositeViewName, "" }; | ||
| if (isExceptionExpected) | ||
| { | ||
| DataGatewayException ex = await Assert.ThrowsExceptionAsync<DataGatewayException>(() => | ||
| InitializeTestFixture(null, new List<string> { compositeDbViewquery }, new List<string[]> { customEntity })); | ||
|
ayush3797 marked this conversation as resolved.
|
||
| 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 InitializeTestFixture(null, new List<string> { compositeDbViewquery }, | ||
|
ayush3797 marked this conversation as resolved.
|
||
| new List<string[]> { 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"; | ||
|
|
||
| // Perform GET operation on the view. | ||
| await SetupAndRunRestApiTest( | ||
| primaryKeyRoute: "book_id/1/author_id/123", | ||
| queryString: string.Empty, | ||
| entity: _compositeViewName, | ||
| sqlQuery: query, | ||
| controller: _restController | ||
| ); | ||
|
ayush3797 marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Runs after every test to reset the database state | ||
| /// </summary> | ||
| [TestCleanup] | ||
| public async Task TestCleanup() | ||
| { | ||
| string dropViewQuery = $"DROP VIEW IF EXISTS {_compositeViewName}"; | ||
| await _queryExecutor.ExecuteQueryAsync(dropViewQuery, parameters: null); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.