From 53cd22f142e36db90f0dfc92ed17976a8b5d03fc Mon Sep 17 00:00:00 2001 From: Kim Santiago <31145923+kisantia@users.noreply.github.com> Date: Fri, 28 Jun 2019 10:01:19 -0700 Subject: [PATCH] Add more validation for DacFx tests (#6120) * add checking for tables and data * addressing comments --- .../integration-tests/src/dacpac.test.ts | 4 ++ extensions/integration-tests/src/utils.ts | 59 ++++++++++++++++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/extensions/integration-tests/src/dacpac.test.ts b/extensions/integration-tests/src/dacpac.test.ts index d0d0e9d48a..95393bbcc2 100644 --- a/extensions/integration-tests/src/dacpac.test.ts +++ b/extensions/integration-tests/src/dacpac.test.ts @@ -40,6 +40,8 @@ if (context.RunTest) { // Deploy dacpac const deployResult = await dacfxService.deployDacpac(path.join(__dirname, 'testData/Database1.dacpac'), databaseName, false, ownerUri, azdata.TaskExecutionMode.execute); await utils.assertDatabaseCreationResult(databaseName, ownerUri, retryCount); + await utils.assertTableCreationResult(databaseName, 'dbo', 'Table1', ownerUri, retryCount); + await utils.assertTableCreationResult(databaseName, 'dbo', 'Table2', ownerUri, retryCount); assert(deployResult.success === true && deployResult.errorMessage === '', `Deploy dacpac should succeed Expected: there should be no error. Actual Error message: "${deployResult.errorMessage}"`); // Extract dacpac @@ -74,6 +76,8 @@ if (context.RunTest) { // Import bacpac const importResult = await dacfxService.importBacpac(path.join(__dirname, 'testData/Database1.bacpac'), databaseName, ownerUri, azdata.TaskExecutionMode.execute); await utils.assertDatabaseCreationResult(databaseName, ownerUri, retryCount); + await utils.assertTableCreationResult(databaseName, 'dbo', 'Table1', ownerUri, retryCount, true); + await utils.assertTableCreationResult(databaseName, 'dbo', 'Table2', ownerUri, retryCount, true); assert(importResult.success === true && importResult.errorMessage === '', `Expected: Import bacpac should succeed and there should be no error. Actual Error message: "${importResult.errorMessage}"`); // Export bacpac diff --git a/extensions/integration-tests/src/utils.ts b/extensions/integration-tests/src/utils.ts index d33b443a94..050dbc211b 100644 --- a/extensions/integration-tests/src/utils.ts +++ b/extensions/integration-tests/src/utils.ts @@ -108,7 +108,6 @@ export async function assertDatabaseCreationResult(databaseName: string, ownerUr let result: azdata.SimpleExecuteResult; while (retryCount > 0) { --retryCount; - await sleep(5000); let query = `BEGIN TRY SELECT name FROM master.dbo.sysdatabases WHERE name='${databaseName}' @@ -120,6 +119,8 @@ export async function assertDatabaseCreationResult(databaseName: string, ownerUr if (result.rowCount > 0) { break; } + + await sleep(5000); } assert(result.rowCount === 1, `Database ${databaseName} should be created`); @@ -141,6 +142,58 @@ export async function assertFileGenerationResult(filepath: string, retryCount: n } assert(exists, `File ${filepath} is expected to be present`); - assert(fs.readFileSync(filepath).byteLength > 0, 'File should not be empty'); + assert(fs.readFileSync(filepath).byteLength > 0, 'File ${filepath} should not be empty'); fs.unlinkSync(filepath); -} \ No newline at end of file +} + +/** + * + * @param databaseName name of database where to look for table + * @param tableName table to look for + * @param schema schema to look for + * @param ownerUri owner uri + * @param retryCount number of times to retry with a 5 second wait between each try + * @param checkForData whether or not to check if the table has data + * Checks for table existing + */ +export async function assertTableCreationResult(databaseName: string, schema: string, tableName: string, ownerUri: string, retryCount: number, checkForData?: boolean): Promise { + let result: azdata.SimpleExecuteResult; + while (retryCount > 0) { + --retryCount; + let query = `BEGIN TRY + USE ${databaseName} + SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '${schema}' AND TABLE_NAME = '${tableName}' + END TRY + BEGIN CATCH + SELECT ERROR_MESSAGE() AS ErrorMessage; + END CATCH`; + result = await runQuery(query, ownerUri); + if (result.rowCount > 0) { + break; + } + await sleep(5000); + } + + assert(result.rowCount === 1, `Table ${tableName} should be created. ${result.rowCount} rows were found`); + assert(result.columnInfo[0].columnName !== 'ErrorMessage', `Checking for table creation threw error ${result.rows[0][0].displayValue}`); + + if (checkForData) { + while (retryCount > 0) { + let query = `BEGIN TRY + USE ${databaseName} + SELECT * FROM ${tableName} + END TRY + BEGIN CATCH + SELECT ERROR_MESSAGE() AS ErrorMessage; + END CATCH`; + result = await runQuery(query, ownerUri); + if (result.rowCount > 0) { + break; + } + await sleep(5000); + } + + assert(result.rowCount > 0, `Table ${tableName} should have at least one row of data. ${result.rowCount} rows were found`); + assert(result.columnInfo[0].columnName !== 'ErrorMessage', `Checking for table creation threw error ${result.rows[0][0].displayValue}`); + } +}