Skip to content
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

Unit tests #172

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

CALL sp_mamba_dim_agegroup_create();
CALL sp_mamba_dim_agegroup_insert();
CALL sp_mamba_dim_agegroup_update();
CALL sp_mamba_dim_agegroup_update();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GOmare can we please run through this together


-- $END
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- $BEGIN

CALL sp_mamba_dim_agegroup_test_create();
CALL sp_mamba_dim_agegroup_test_insert();

-- $END
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
-- $BEGIN
-- -------------------------------------------------------------------------------------
-- Unit Test for sp_mamba_dim_agegroup_create procedure
-- -------------------------------------------------------------------------------------

DROP PROCEDURE IF EXISTS test_sp_mamba_dim_agegroup_create;

DELIMITER //

CREATE PROCEDURE test_sp_mamba_dim_agegroup_create()
BEGIN
-- Step 1: Variables for test results
DECLARE failed_message VARCHAR(255) DEFAULT '';
DECLARE test_failed INT DEFAULT 0;

-- Step 2: Clean up any existing table before running the test
DROP TABLE IF EXISTS mamba_dim_agegroup;

-- Step 3: Call the sp_mamba_dim_agegroup_create procedure to create the table
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SET test_failed = 1;
SET failed_message = 'Failed to create mamba_dim_agegroup table via sp_mamba_dim_agegroup_create.';
END;

-- Call the stored procedure to create the table
CALL sp_mamba_dim_agegroup_create();
END;

-- Step 4: Validate the creation of the table
IF NOT EXISTS (SELECT * FROM information_schema.tables WHERE table_name = 'mamba_dim_agegroup') THEN
SET test_failed = 1;
SET failed_message = 'Table mamba_dim_agegroup was not created by sp_mamba_dim_agegroup_create.';
END IF;

-- Step 5: Validate the structure of the created table
DECLARE expected_columns VARCHAR(255) DEFAULT 'id, age, datim_agegroup, datim_age_val, normal_agegroup, normal_age_val';
DECLARE actual_columns VARCHAR(255);

SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY ORDINAL_POSITION) INTO actual_columns
FROM information_schema.columns
WHERE table_name = 'mamba_dim_agegroup';

-- Check if the actual columns match the expected columns
IF actual_columns != expected_columns THEN
SET test_failed = 1;
SET failed_message = CONCAT('Table structure does not match. Expected: ', expected_columns, ' Found: ', actual_columns);
END IF;

-- Step 6: Return result
IF test_failed = 1 THEN
SELECT CONCAT('Unit Test Failed: ', failed_message) AS result;
ELSE
SELECT 'Unit Test Passed: mamba_dim_agegroup table created successfully via sp_mamba_dim_agegroup_create with the correct structure.' AS result;
END IF;
END //

DELIMITER ;

-- Run the unit test
CALL test_sp_mamba_dim_agegroup_create();

-- Clean up after the test
DROP PROCEDURE IF EXISTS test_sp_mamba_dim_agegroup_create;


-- $END
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
-- $BEGIN
-- -------------------------------------------------------------------------------------
-- Unit Test for sp_mamba_dim_agegroup_insert procedure
-- -------------------------------------------------------------------------------------

DROP PROCEDURE IF EXISTS test_sp_mamba_dim_agegroup_insert;

DELIMITER //

CREATE PROCEDURE test_sp_mamba_dim_agegroup_insert()
BEGIN
-- Step 1: Variables for test results
DECLARE test_failed INT DEFAULT 0;
DECLARE failed_message VARCHAR(255) DEFAULT '';

-- Step 2: Clean up existing data in the table before running the test
DELETE FROM mamba_dim_agegroup;

-- Step 3: Insert test data into the source table (if necessary)
-- Assuming the source table has already existing data and no new data is inserted here
-- (If source data needs setup, do it here by inserting test data)

-- Step 4: Call the sp_mamba_dim_agegroup_insert procedure to insert the data
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SET test_failed = 1;
SET failed_message = 'Failed to insert data into mamba_dim_agegroup via sp_mamba_dim_agegroup_insert.';
END;

-- Call the stored procedure to insert data
CALL sp_mamba_dim_agegroup_insert();
END;

-- Step 5: Validate that data has been inserted into the mamba_dim_agegroup table
DECLARE row_count INT;

SELECT COUNT(*) INTO row_count FROM mamba_dim_agegroup;

-- Check if data has been inserted
IF row_count = 0 THEN
SET test_failed = 1;
SET failed_message = 'No data was inserted into the mamba_dim_agegroup table.';
END IF;

-- Step 6: Optionally, validate some specific values in the inserted rows (e.g., check a specific age group)
DECLARE test_agegroup VARCHAR(50);
SELECT datim_agegroup INTO test_agegroup FROM mamba_dim_agegroup LIMIT 1;

IF test_agegroup IS NULL THEN
SET test_failed = 1;
SET failed_message = 'Data in mamba_dim_agegroup is not as expected.';
END IF;

-- Step 7: Return result
IF test_failed = 1 THEN
SELECT CONCAT('Unit Test Failed: ', failed_message) AS result;
ELSE
SELECT 'Unit Test Passed: Data successfully inserted into mamba_dim_agegroup via sp_mamba_dim_agegroup_insert.' AS result;
END IF;
END //

DELIMITER ;

-- Run the unit test
CALL test_sp_mamba_dim_agegroup_insert();

-- Clean up after the test
DROP PROCEDURE IF EXISTS test_sp_mamba_dim_agegroup_insert;


-- $END
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- $BEGIN
CALL sp_mamba_dim_concept_test_create;
CALL sp_mamba_dim_concept_test_insert();

-- $END
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
-- -------------------------------------------------------------------------------------
-- Unit Test for sp_mamba_dim_concept_create
-- -------------------------------------------------------------------------------------

DROP PROCEDURE IF EXISTS test_sp_mamba_dim_concept_create;

DELIMITER //

CREATE PROCEDURE test_sp_mamba_dim_concept_create()
BEGIN
-- Step 1: Variables for test results
DECLARE failed_message VARCHAR(255) DEFAULT '';
DECLARE test_failed INT DEFAULT 0;

-- Step 2: Clean up any existing table before running the test
DROP TABLE IF EXISTS mamba_dim_concept;

-- Step 3: Execute the procedure to be tested
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SET test_failed = 1;
SET failed_message = 'Procedure sp_mamba_dim_concept_create failed to execute.';
END;

-- Call the procedure to create the table
CALL sp_mamba_dim_concept_create();
END;

-- Step 4: Validate the creation of the table
IF NOT EXISTS (SELECT * FROM information_schema.tables WHERE table_name = 'mamba_dim_concept') THEN
SET test_failed = 1;
SET failed_message = 'Table mamba_dim_concept was not created.';
END IF;

-- Step 5: Validate the structure of the created table
DECLARE expected_columns VARCHAR(255) DEFAULT 'concept_id, name, description, date_created, date_retired, retired, retire_reason, retired_by, incremental_record';
DECLARE actual_columns VARCHAR(255);

SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY ORDINAL_POSITION) INTO actual_columns
FROM information_schema.columns
WHERE table_name = 'mamba_dim_concept';

-- Check if the actual columns match expected columns
IF actual_columns != expected_columns THEN
SET test_failed = 1;
SET failed_message = CONCAT('Table structure does not match. Expected: ', expected_columns, ' Found: ', actual_columns);
END IF;

-- Step 6: Return result
IF test_failed = 1 THEN
SELECT CONCAT('Unit Test Failed: ', failed_message) AS result;
ELSE
SELECT 'Unit Test Passed: sp_mamba_dim_concept_create executed successfully and table created.' AS result;
END IF;
END //

DELIMITER ;

-- Run the unit test
CALL test_sp_mamba_dim_concept_create();

-- Clean up after the test
DROP PROCEDURE IF EXISTS test_sp_mamba_dim_concept_create;
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
-- -------------------------------------------------------------------------------------
-- Unit Test for sp_mamba_dim_concept_insert
-- -------------------------------------------------------------------------------------

DROP PROCEDURE IF EXISTS test_sp_mamba_dim_concept_insert;

DELIMITER //

CREATE PROCEDURE test_sp_mamba_dim_concept_insert()
BEGIN
-- Step 1: Variables for test results
DECLARE failed_message VARCHAR(255) DEFAULT '';
DECLARE test_failed INT DEFAULT 0;

-- Step 2: Clear relevant test data in the target table for isolation
TRUNCATE TABLE mamba_dim_concept;

-- Step 3: Ensure the ETL settings table (_mamba_etl_user_settings) has relevant locales
DELETE FROM _mamba_etl_user_settings WHERE concepts_locale IN ('en', 'fr');
INSERT INTO _mamba_etl_user_settings (concepts_locale) VALUES ('en'), ('fr');

-- Step 4: Execute the procedure to be tested
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SET test_failed = 1;
SET failed_message = 'Procedure sp_mamba_dim_concept_insert failed.';
END;

-- Call the procedure to insert data
CALL sp_mamba_dim_concept_insert();
END;

-- Step 5: Validate the data inserted into `mamba_dim_concept`
-- Assuming existing source data matches the criteria for insert
IF NOT EXISTS (SELECT * FROM mamba_dim_concept WHERE concept_id IN (SELECT concept_id FROM openmrs.concept WHERE voided = 0) LIMIT 1) THEN
SET test_failed = 1;
SET failed_message = 'No expected rows found in mamba_dim_concept after insert.';
END IF;

-- Check if there are duplicate entries for existing concepts
IF (SELECT COUNT(*) FROM mamba_dim_concept WHERE concept_id IN (SELECT concept_id FROM openmrs.concept WHERE voided = 0)) >
(SELECT COUNT(*) FROM openmrs.concept WHERE voided = 0) THEN
SET test_failed = 1;
SET failed_message = 'Unexpected duplicate entries found in mamba_dim_concept.';
END IF;

-- Step 6: Return result
IF test_failed = 1 THEN
SELECT CONCAT('Unit Test Failed: ', failed_message) AS result;
ELSE
SELECT 'Unit Test Passed: sp_mamba_dim_concept_insert executed successfully.' AS result;
END IF;
END //

DELIMITER ;

-- Run the unit test
CALL test_sp_mamba_dim_concept_insert();

-- Clean up after the test
DROP PROCEDURE IF EXISTS test_sp_mamba_dim_concept_insert;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- $BEGIN
CALL sp_mamba_dim_concept_answer_test_create();
CALL sp_mamba_dim_concept_answer_test_insert();

-- $END
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
-- $BEGIN
-- -------------------------------------------------------------------------------------
-- Unit Test for sp_mamba_dim_concept_answer_create procedure
-- -------------------------------------------------------------------------------------

DROP PROCEDURE IF EXISTS test_sp_mamba_dim_concept_answer_create;

DELIMITER //

CREATE PROCEDURE test_sp_mamba_dim_concept_answer_create()
BEGIN
-- Step 1: Variables for test results
DECLARE failed_message VARCHAR(255) DEFAULT '';
DECLARE test_failed INT DEFAULT 0;

-- Step 2: Clean up any existing table before running the test
DROP TABLE IF EXISTS mamba_dim_concept_answer;

-- Step 3: Call the sp_mamba_dim_concept_answer_create procedure to create the table
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SET test_failed = 1;
SET failed_message = 'Failed to create mamba_dim_concept_answer table via sp_mamba_dim_concept_answer_create.';
END;

-- Call the stored procedure to create the table
CALL sp_mamba_dim_concept_answer_create();
END;

-- Step 4: Validate the creation of the table
IF NOT EXISTS (SELECT * FROM information_schema.tables WHERE table_name = 'mamba_dim_concept_answer') THEN
SET test_failed = 1;
SET failed_message = 'Table mamba_dim_concept_answer was not created by sp_mamba_dim_concept_answer_create.';
END IF;

-- Step 5: Validate the structure of the created table
DECLARE expected_columns VARCHAR(255) DEFAULT 'concept_answer_id, concept_id, answer_concept, answer_drug, incremental_record';
DECLARE actual_columns VARCHAR(255);

SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY ORDINAL_POSITION) INTO actual_columns
FROM information_schema.columns
WHERE table_name = 'mamba_dim_concept_answer';

-- Check if the actual columns match the expected columns
IF actual_columns != expected_columns THEN
SET test_failed = 1;
SET failed_message = CONCAT('Table structure does not match. Expected: ', expected_columns, ' Found: ', actual_columns);
END IF;

-- Step 6: Return result
IF test_failed = 1 THEN
SELECT CONCAT('Unit Test Failed: ', failed_message) AS result;
ELSE
SELECT 'Unit Test Passed: mamba_dim_concept_answer table created successfully via sp_mamba_dim_concept_answer_create with the correct structure.' AS result;
END IF;
END //

DELIMITER ;

-- Run the unit test
CALL test_sp_mamba_dim_concept_answer_create();

-- Clean up after the test
DROP PROCEDURE IF EXISTS test_sp_mamba_dim_concept_answer_create;


-- $END
Loading