diff options
-rw-r--r-- | migrations/auth/20221110_05_BaNtL-create-roles-table.py | 26 | ||||
-rw-r--r-- | tests/unit/auth/test_create_table_migrations.py | 1 | ||||
-rw-r--r-- | tests/unit/auth/test_migrations_indexes.py | 88 |
3 files changed, 115 insertions, 0 deletions
diff --git a/migrations/auth/20221110_05_BaNtL-create-roles-table.py b/migrations/auth/20221110_05_BaNtL-create-roles-table.py new file mode 100644 index 0000000..bf238f6 --- /dev/null +++ b/migrations/auth/20221110_05_BaNtL-create-roles-table.py @@ -0,0 +1,26 @@ +""" +Create 'roles' table +""" + +from yoyo import step + +__depends__ = {'20221110_04_6PRFQ-create-genotype-resources-table'} + +steps = [ + step( + """ + CREATE TABLE IF NOT EXISTS roles( + group_id TEXT NOT NULL, + role_id TEXT NOT NULL PRIMARY KEY, + role_name TEXT NOT NULL, + FOREIGN KEY(group_id) REFERENCES groups(group_id) + ) WITHOUT ROWID + """, + "DROP TABLE IF EXISTS roles"), + step( + """ + CREATE INDEX IF NOT EXISTS idx_tbl_roles_cols_group_id + ON roles(group_id) + """, + "DROP INDEX IF EXISTS idx_tbl_roles_cols_group_id") +] diff --git a/tests/unit/auth/test_create_table_migrations.py b/tests/unit/auth/test_create_table_migrations.py index 6898d9d..99d045e 100644 --- a/tests/unit/auth/test_create_table_migrations.py +++ b/tests/unit/auth/test_create_table_migrations.py @@ -19,6 +19,7 @@ migrations_and_tables = ( ("20221110_02_z1dWf-create-mrna-resources-table.py", "mrna_resources"), ("20221110_03_ka3W0-create-phenotype-resources-table.py", "phenotype_resources"), ("20221110_04_6PRFQ-create-genotype-resources-table.py", "genotype_resources"), + ("20221110_05_BaNtL-create-roles-table.py", "roles")) @pytest.mark.unit_test @pytest.mark.parametrize("migration_file,the_table", migrations_and_tables) diff --git a/tests/unit/auth/test_migrations_indexes.py b/tests/unit/auth/test_migrations_indexes.py new file mode 100644 index 0000000..e18a452 --- /dev/null +++ b/tests/unit/auth/test_migrations_indexes.py @@ -0,0 +1,88 @@ +"""Test that indexes are created and removed.""" + +from contextlib import closing + +import pytest +import sqlite3 + +from gn3.migrations import get_migration, apply_migrations, rollback_migrations +from tests.unit.auth.conftest import ( + apply_single_migration, rollback_single_migration, migrations_up_to) + +query = """ +SELECT name FROM sqlite_master WHERE type='index' AND tbl_name = ? +AND name= ? +""" + +migrations_tables_and_indexes = ( + ("20221110_05_BaNtL-create-roles-table.py", "roles", + "idx_tbl_roles_cols_group_id"),) + +@pytest.mark.unit_test +@pytest.mark.parametrize( + "migration_file,the_table,the_index", migrations_tables_and_indexes) +def test_index_created( + auth_testdb_path, auth_migrations_dir, backend, migration_file, + the_table, the_index): + """ + GIVEN: A database migration + WHEN: The migration is applied + THEN: Ensure the given index is created for the provided table + """ + migration_path=f"{auth_migrations_dir}/{migration_file}" + older_migrations = migrations_up_to(migration_path, auth_migrations_dir) + the_migration = get_migration(migration_path) + query_params = (the_table, the_index) + apply_migrations(backend, older_migrations) + with closing(sqlite3.connect(auth_testdb_path)) as conn, closing(conn.cursor()) as cursor: + cursor.execute(query, query_params) + result_before_migration = cursor.fetchall() + apply_single_migration(backend, the_migration) + cursor.execute(query, query_params) + result_after_migration = cursor.fetchall() + + rollback_migrations(backend, older_migrations + [the_migration]) + assert the_index not in [row[0] for row in result_before_migration], ( + f"Index '{the_index}' was not found for table '{the_table}'.") + assert ( + len(result_after_migration) == 1 + and result_after_migration[0][0] == the_index), ( + f"Index '{the_index}' was not found for table '{the_table}'.") + +@pytest.mark.unit_test +@pytest.mark.parametrize( + "migration_file,the_table,the_index", migrations_tables_and_indexes) +def test_index_dropped( + auth_testdb_path, auth_migrations_dir, backend, migration_file, + the_table, the_index): + """ + GIVEN: A database migration + WHEN: The migration is rolled-back + THEN: Ensure the given index no longer exists for the given table + """ + migration_path=f"{auth_migrations_dir}/{migration_file}" + older_migrations = migrations_up_to(migration_path, auth_migrations_dir) + the_migration = get_migration(migration_path) + query_params = (the_table, the_index) + apply_migrations(backend, older_migrations) + with closing(sqlite3.connect(auth_testdb_path)) as conn, closing(conn.cursor()) as cursor: + cursor.execute(query, query_params) + result_before_migration = cursor.fetchall() + apply_single_migration(backend, the_migration) + cursor.execute(query, query_params) + result_after_migration = cursor.fetchall() + rollback_single_migration(backend, the_migration) + cursor.execute(query, query_params) + result_after_rollback = cursor.fetchall() + + rollback_migrations(backend, older_migrations) + assert the_index not in [row[0] for row in result_before_migration], ( + f"Index '{the_index}' was found for table '{the_table}' before " + "migration") + assert ( + len(result_after_migration) == 1 + and result_after_migration[0][0] == the_index), ( + f"Index '{the_index}' was not found for table '{the_table}'.") + assert the_index not in [row[0] for row in result_after_rollback], ( + f"Index '{the_index}' was found for table '{the_table}' after " + "rollback") |