aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/auth
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2022-11-10 12:20:43 +0300
committerFrederick Muriuki Muriithi2022-11-10 12:20:43 +0300
commit4e9af79511eebfa7039d180c30d115a8ad6d2cd1 (patch)
treedfd6dddc92d54c1ab73971c3a64b0faaf2f36ca4 /tests/unit/auth
parent25a92b6ebcdbc1453a9f8ea4a46dea96c906af71 (diff)
downloadgenenetwork3-4e9af79511eebfa7039d180c30d115a8ad6d2cd1.tar.gz
Migrations: migration for 'roles' table
* migrations/auth/20221110_05_BaNtL-create-roles-table.py: new migration * tests/unit/auth/test_create_table_migrations.py: test new migration * tests/unit/auth/test_migrations_indexes.py: test new migration
Diffstat (limited to 'tests/unit/auth')
-rw-r--r--tests/unit/auth/test_create_table_migrations.py1
-rw-r--r--tests/unit/auth/test_migrations_indexes.py88
2 files changed, 89 insertions, 0 deletions
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")