diff --git a/db/columns.py b/db/columns.py index 1e44d71aae..cb200309b5 100644 --- a/db/columns.py +++ b/db/columns.py @@ -1,6 +1,6 @@ import json -from db.connection import exec_msar_func +from db import connection as db_conn from db.deprecated.types.base import PostgresType @@ -45,7 +45,7 @@ def get_column_info_for_table(table, conn): Args: table: The table for which we want column info. """ - return exec_msar_func(conn, 'get_column_info', table).fetchone()[0] + return db_conn.exec_msar_func(conn, 'get_column_info', table).fetchone()[0] def alter_columns_in_table(table_oid, column_data_list, conn): @@ -61,7 +61,7 @@ def alter_columns_in_table(table_oid, column_data_list, conn): transformed_column_data = [ _transform_column_alter_dict(column) for column in column_data_list ] - exec_msar_func( + db_conn.exec_msar_func( conn, 'alter_columns', table_oid, json.dumps(transformed_column_data) ) return len(column_data_list) @@ -136,7 +136,7 @@ def add_columns_to_table(table_oid, column_data_list, conn): transformed_column_data = [ _transform_column_create_dict(col) for col in column_data_list ] - result = exec_msar_func( + result = db_conn.exec_msar_func( conn, 'add_columns', table_oid, @@ -194,6 +194,6 @@ def drop_columns_from_table(table_oid, column_attnums, conn): column_attnums: The attnums of the columns to drop. conn: A psycopg connection to the relevant database. """ - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'drop_columns', table_oid, *column_attnums ).fetchone()[0] diff --git a/db/constraints.py b/db/constraints.py index 170a773284..57a8c1ac43 100644 --- a/db/constraints.py +++ b/db/constraints.py @@ -1,11 +1,10 @@ import json -from db.connection import exec_msar_func -from db.connection import select_from_msar_func +from db import connection as db_conn def get_constraints_for_table(table_oid, conn): - return select_from_msar_func(conn, 'get_constraints_for_table', table_oid) + return db_conn.select_from_msar_func(conn, 'get_constraints_for_table', table_oid) def create_constraint(table_oid, constraint_obj_list, conn): @@ -19,7 +18,7 @@ def create_constraint(table_oid, constraint_obj_list, conn): Returns: Returns a list of oid(s) of constraints for a given table. """ - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'add_constraints', table_oid, json.dumps(constraint_obj_list) ).fetchone()[0] @@ -35,6 +34,6 @@ def drop_constraint_via_oid(table_oid, constraint_oid, conn): Returns: The name of the dropped constraint. """ - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'drop_constraint', table_oid, constraint_oid ).fetchone()[0] diff --git a/db/databases.py b/db/databases.py index d80c899f9f..c962a9acd8 100644 --- a/db/databases.py +++ b/db/databases.py @@ -1,15 +1,16 @@ -from db.connection import exec_msar_func from psycopg import sql +from db import connection as db_conn + def get_database(conn): - return exec_msar_func(conn, 'get_current_database_info').fetchone()[0] + return db_conn.exec_msar_func(conn, 'get_current_database_info').fetchone()[0] def drop_database(database_oid, conn): cursor = conn.cursor() conn.autocommit = True - drop_database_query = exec_msar_func( + drop_database_query = db_conn.exec_msar_func( conn, 'drop_database_query', database_oid diff --git a/db/links.py b/db/links.py index 75f116fa88..c3f2f37da9 100644 --- a/db/links.py +++ b/db/links.py @@ -1,6 +1,6 @@ import json -from db.connection import exec_msar_func +from db import connection as db_conn def add_foreign_key_column( @@ -22,7 +22,7 @@ def add_foreign_key_column( unique_link: Whether to make the link one-to-one instead of many-to-one. """ - exec_msar_func( + db_conn.exec_msar_func( conn, 'add_foreign_key_column', column_name, @@ -51,7 +51,7 @@ def add_mapping_table( The elements of the mapping_columns list must have the form {"column_name": , "referent_table_oid": } """ - exec_msar_func( + db_conn.exec_msar_func( conn, 'add_mapping_table', schema_oid, diff --git a/db/roles.py b/db/roles.py index 98f0a87e76..5eb7b82c45 100644 --- a/db/roles.py +++ b/db/roles.py @@ -1,81 +1,81 @@ import json -from db.connection import exec_msar_func +from db import connection as db_conn def list_roles(conn): - return exec_msar_func(conn, 'list_roles').fetchone()[0] + return db_conn.exec_msar_func(conn, 'list_roles').fetchone()[0] def get_current_role_from_db(conn): - return exec_msar_func(conn, 'get_current_role').fetchone()[0] + return db_conn.exec_msar_func(conn, 'get_current_role').fetchone()[0] def list_db_priv(conn): - return exec_msar_func(conn, 'list_db_priv').fetchone()[0] + return db_conn.exec_msar_func(conn, 'list_db_priv').fetchone()[0] def list_schema_privileges(schema_oid, conn): - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'list_schema_privileges', schema_oid ).fetchone()[0] def list_table_privileges(table_oid, conn): - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'list_table_privileges', table_oid ).fetchone()[0] def create_role(rolename, password, login, conn): - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'create_role', rolename, password, login ).fetchone()[0] def drop_role(role_oid, conn): - exec_msar_func(conn, 'drop_role', role_oid) + db_conn.exec_msar_func(conn, 'drop_role', role_oid) def set_members_to_role(parent_role_oid, members, conn): - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'set_members_to_role', parent_role_oid, members ).fetchone()[0] def transfer_database_ownership(new_owner_oid, conn): - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'transfer_database_ownership', new_owner_oid ).fetchone()[0] def transfer_schema_ownership(schema_oid, new_owner_oid, conn): - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'transfer_schema_ownership', schema_oid, new_owner_oid ).fetchone()[0] def transfer_table_ownership(table_oid, new_owner_oid, conn): - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'transfer_table_ownership', table_oid, new_owner_oid ).fetchone()[0] def replace_database_privileges_for_roles(conn, privileges): - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'replace_database_privileges_for_roles', json.dumps(privileges) ).fetchone()[0] def replace_schema_privileges_for_roles(conn, schema_oid, privileges): - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'replace_schema_privileges_for_roles', schema_oid, json.dumps(privileges) ).fetchone()[0] def replace_table_privileges_for_roles(conn, table_oid, privileges): - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'replace_table_privileges_for_roles', table_oid, json.dumps(privileges) ).fetchone()[0] diff --git a/db/schemas.py b/db/schemas.py index 32fc1e7b76..8de0749e6a 100644 --- a/db/schemas.py +++ b/db/schemas.py @@ -1,13 +1,13 @@ import json -from db.connection import exec_msar_func +from db import connection as db_conn def list_schemas(conn): - return exec_msar_func(conn, 'list_schemas').fetchone()[0] + return db_conn.exec_msar_func(conn, 'list_schemas').fetchone()[0] def get_schema(schema_oid, conn): - return exec_msar_func(conn, 'get_schema', schema_oid).fetchone()[0] + return db_conn.exec_msar_func(conn, 'get_schema', schema_oid).fetchone()[0] def patch_schema(schema_oid, conn, patch): @@ -24,7 +24,7 @@ def patch_schema(schema_oid, conn, patch): Returns: The SchemaInfo describing the user-defined schema in the database. """ - return exec_msar_func(conn, "patch_schema", schema_oid, json.dumps(patch)).fetchone()[0] + return db_conn.exec_msar_func(conn, "patch_schema", schema_oid, json.dumps(patch)).fetchone()[0] def create_schema(schema_name, conn, owner_oid, description=None): @@ -42,7 +42,7 @@ def create_schema(schema_name, conn, owner_oid, description=None): Returns: The SchemaInfo describing the user-defined schema in the database. """ - return exec_msar_func(conn, 'create_schema', schema_name, owner_oid, description).fetchone()[0] + return db_conn.exec_msar_func(conn, 'create_schema', schema_name, owner_oid, description).fetchone()[0] def drop_schema_via_oid(conn, id, cascade=False): @@ -56,4 +56,4 @@ def drop_schema_via_oid(conn, id, cascade=False): id: the OID of the schema to drop. cascade: Whether to drop the dependent objects. """ - exec_msar_func(conn, 'drop_schema', id, cascade).fetchone() + db_conn.exec_msar_func(conn, 'drop_schema', id, cascade).fetchone() diff --git a/db/tables.py b/db/tables.py index 5a5f901a96..2ca8fa5765 100644 --- a/db/tables.py +++ b/db/tables.py @@ -1,5 +1,5 @@ import json -from db.connection import exec_msar_func +from db import connection as db_conn from db.columns import _transform_column_alter_dict from db.deprecated.types.base import PostgresType @@ -14,7 +14,7 @@ def get_table(table, conn): Args: table: The table for which we want table info. """ - return exec_msar_func(conn, 'get_table', table).fetchone()[0] + return db_conn.exec_msar_func(conn, 'get_table', table).fetchone()[0] def get_table_info(schema, conn): @@ -27,11 +27,11 @@ def get_table_info(schema, conn): Args: schema: The schema for which we want table info. """ - return exec_msar_func(conn, 'get_table_info', schema).fetchone()[0] + return db_conn.exec_msar_func(conn, 'get_table_info', schema).fetchone()[0] def list_joinable_tables(table_oid, conn, max_depth): - return exec_msar_func(conn, 'get_joinable_tables', max_depth, table_oid).fetchone()[0] + return db_conn.exec_msar_func(conn, 'get_joinable_tables', max_depth, table_oid).fetchone()[0] def get_preview(table_oid, column_list, conn, limit=20): @@ -47,7 +47,7 @@ def get_preview(table_oid, column_list, conn, limit=20): if you wish to alter these settings permanantly for the columns see tables/alter.py. """ transformed_column_data = [_transform_column_alter_dict(col) for col in column_list] - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'get_preview', table_oid, json.dumps(transformed_column_data), limit ).fetchone()[0] @@ -67,7 +67,7 @@ def alter_table_on_database(table_oid, table_data_dict, conn): "columns": of column_data describing columns to alter. } """ - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'alter_table', table_oid, json.dumps(table_data_dict) ).fetchone()[0] @@ -95,7 +95,7 @@ def create_table_on_database( Returns: Returns the OID and name of the created table. """ - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'add_mathesar_table', schema_oid, @@ -131,7 +131,7 @@ def prepare_table_for_import( "type": {"name": PostgresType.TEXT.id} } for column_name in column_names ] - import_info = exec_msar_func( + import_info = db_conn.exec_msar_func( conn, 'prepare_table_for_import', schema_oid, @@ -162,7 +162,7 @@ def drop_table_from_database(table_oid, conn, cascade=False): Returns: Returns the fully qualified name of the dropped table. """ - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'drop_table', table_oid, cascade ).fetchone()[0] @@ -181,7 +181,7 @@ def infer_table_column_data_types(conn, table_oid): result of `format_type` for the inferred type of each column. Restricted to columns to which the user has access. """ - return exec_msar_func( + return db_conn.exec_msar_func( conn, 'infer_table_column_data_types', table_oid ).fetchone()[0] @@ -189,7 +189,7 @@ def infer_table_column_data_types(conn, table_oid): def move_columns_to_referenced_table( conn, source_table_oid, target_table_oid, move_column_attnums ): - exec_msar_func( + db_conn.exec_msar_func( conn, 'move_columns_to_referenced_table', source_table_oid, @@ -205,7 +205,7 @@ def split_table( extracted_table_name, relationship_fk_column_name=None ): - extracted_table_oid, new_fkey_attnum = exec_msar_func( + extracted_table_oid, new_fkey_attnum = db_conn.exec_msar_func( conn, 'extract_columns_from_table', old_table_oid, diff --git a/db/tests/test_columns.py b/db/tests/test_columns.py index 30e3cd2d82..ca9e214884 100644 --- a/db/tests/test_columns.py +++ b/db/tests/test_columns.py @@ -2,11 +2,11 @@ from unittest.mock import patch import pytest -from db import columns +from db import connection, columns def test_get_column_info_for_table(): - with patch.object(columns, 'exec_msar_func') as mock_exec: + with patch.object(connection, 'exec_msar_func') as mock_exec: mock_exec.return_value.fetchone = lambda: ('a', 'b') result = columns.get_column_info_for_table('table', 'conn') mock_exec.assert_called_once_with('conn', 'get_column_info', 'table') @@ -14,7 +14,7 @@ def test_get_column_info_for_table(): def test_alter_columns_in_table_basic(): - with patch.object(columns, 'exec_msar_func') as mock_exec: + with patch.object(connection, 'exec_msar_func') as mock_exec: columns.alter_columns_in_table( 123, [ @@ -59,7 +59,7 @@ def test_add_columns_name(in_name, out_name): Here, we just check that the PostgreSQL function is called properly, when given a (maybe empty) name param """ - with patch.object(columns, "exec_msar_func") as mock_exec: + with patch.object(connection, "exec_msar_func") as mock_exec: columns.add_columns_to_table(123, [{"name": in_name}], "conn") call_args = mock_exec.call_args_list[0][0] assert call_args[0] == "conn" @@ -76,7 +76,7 @@ def test_add_columns_type(in_type, out_type): Here, we just check that the PostgreSQL function is called properly when given a (maybe empty) type """ - with patch.object(columns, "exec_msar_func") as mock_exec: + with patch.object(connection, "exec_msar_func") as mock_exec: columns.add_columns_to_table(123, [{"type": in_type}], "conn") call_args = mock_exec.call_args_list[0][0] assert call_args[0] == "conn" @@ -96,7 +96,7 @@ def test_add_columns_type_options(in_options, out_options): Here, we just check that the PostgreSQL function is called properly when given a (maybe empty) type options dict. """ - with patch.object(columns, "exec_msar_func") as mock_exec: + with patch.object(connection, "exec_msar_func") as mock_exec: columns.add_columns_to_table(123, [{"type_options": in_options}], "conn") call_args = mock_exec.call_args_list[0][0] assert call_args[0] == "conn" @@ -107,7 +107,7 @@ def test_add_columns_type_options(in_options, out_options): def test_drop_columns(): - with patch.object(columns, 'exec_msar_func') as mock_exec: + with patch.object(connection, 'exec_msar_func') as mock_exec: mock_exec.return_value.fetchone = lambda: (3,) result = columns.drop_columns_from_table(123, [1, 3, 5], 'conn') mock_exec.assert_called_once_with('conn', 'drop_columns', 123, 1, 3, 5) @@ -115,7 +115,7 @@ def test_drop_columns(): def test_drop_columns_single(): - with patch.object(columns, 'exec_msar_func') as mock_exec: + with patch.object(connection, 'exec_msar_func') as mock_exec: mock_exec.return_value.fetchone = lambda: (1,) result = columns.drop_columns_from_table(123, [1], 'conn') mock_exec.assert_called_once_with('conn', 'drop_columns', 123, 1) diff --git a/db/tests/test_roles.py b/db/tests/test_roles.py index 6e6c641b23..a732e9bdc9 100644 --- a/db/tests/test_roles.py +++ b/db/tests/test_roles.py @@ -1,10 +1,10 @@ import json from unittest.mock import patch -from db import roles +from db import connection, roles def test_list_roles(): - with patch.object(roles, 'exec_msar_func') as mock_exec: + with patch.object(connection, 'exec_msar_func') as mock_exec: mock_exec.return_value.fetchone = lambda: ('a', 'b') result = roles.list_roles('conn') mock_exec.assert_called_once_with('conn', 'list_roles') @@ -12,7 +12,7 @@ def test_list_roles(): def test_list_schema_privileges(): - with patch.object(roles, 'exec_msar_func') as mock_exec: + with patch.object(connection, 'exec_msar_func') as mock_exec: mock_exec.return_value.fetchone = lambda: ('a', 'b') result = roles.list_schema_privileges(123456, 'conn') mock_exec.assert_called_once_with('conn', 'list_schema_privileges', 123456) @@ -21,7 +21,7 @@ def test_list_schema_privileges(): def test_replace_database_privileges_for_roles(): priv_spec = [{"role_oid": 1234, "privileges": ["CONNECT", "CREATE"]}] - with patch.object(roles, 'exec_msar_func') as mock_exec: + with patch.object(connection, 'exec_msar_func') as mock_exec: mock_exec.return_value.fetchone = lambda: ('a', 'b') result = roles.replace_database_privileges_for_roles('conn', priv_spec) mock_exec.assert_called_once_with( @@ -33,7 +33,7 @@ def test_replace_database_privileges_for_roles(): def test_replace_schema_privileges_for_roles(): schema_oid = 12345 priv_spec = [{"role_oid": 1234, "privileges": ["UPDATE", "CREATE"]}] - with patch.object(roles, 'exec_msar_func') as mock_exec: + with patch.object(connection, 'exec_msar_func') as mock_exec: mock_exec.return_value.fetchone = lambda: ('a', 'b') result = roles.replace_schema_privileges_for_roles( 'conn', schema_oid, priv_spec diff --git a/db/tests/test_tables.py b/db/tests/test_tables.py index c08100603d..ac6bd27eaa 100644 --- a/db/tests/test_tables.py +++ b/db/tests/test_tables.py @@ -1,10 +1,10 @@ import json from unittest.mock import patch -from db import tables +from db import connection, tables def test_get_table_info(): - with patch.object(tables, 'exec_msar_func') as mock_exec: + with patch.object(connection, 'exec_msar_func') as mock_exec: mock_exec.return_value.fetchone = lambda: ('a', 'b') result = tables.get_table_info('schema', 'conn') mock_exec.assert_called_once_with('conn', 'get_table_info', 'schema') @@ -12,7 +12,7 @@ def test_get_table_info(): def test_alter_table(): - with patch.object(tables, 'exec_msar_func') as mock_exec: + with patch.object(connection, 'exec_msar_func') as mock_exec: tables.alter_table_on_database( 12345, {"name": "newname", "description": "this is a comment", "columns": {}}, diff --git a/mathesar/tests/conftest.py b/mathesar/tests/conftest.py index b83e177e2e..1a9ed6923d 100644 --- a/mathesar/tests/conftest.py +++ b/mathesar/tests/conftest.py @@ -4,10 +4,12 @@ import pytest import responses from copy import deepcopy +from unittest.mock import patch from django.conf import settings from rest_framework.test import APIClient +from db import connection from mathesar.models.users import User from fixtures.utils import create_scoped_fixtures @@ -24,6 +26,26 @@ def mocked_responses(): yield rsps +@pytest.fixture +def mocked_exec_msar_func(): + """ + Lets you patch the db.connection.exec_msar_func() for testing. + """ + with patch.object(connection, "exec_msar_func") as mock: + mock.return_value = mock + yield mock + + +@pytest.fixture +def mocked_select_from_msar_func(): + """ + Lets you patch the db.connection.select_from_msar_func() for testing. + """ + with patch.object(connection, "select_from_msar_func") as mock: + mock.return_value = mock + yield mock + + @pytest.fixture(autouse=True) def enable_db_access_for_all_tests(db): pass diff --git a/mathesar/tests/rpc/columns/test_c_base.py b/mathesar/tests/rpc/columns/test_c_base.py index 69d7bc767c..1d89286360 100644 --- a/mathesar/tests/rpc/columns/test_c_base.py +++ b/mathesar/tests/rpc/columns/test_c_base.py @@ -4,14 +4,16 @@ Fixtures: rf(pytest-django): Provides mocked `Request` objects. monkeypatch(pytest): Lets you monkeypatch an object for testing. + mocked_exec_msar_func(mathesar/tests/conftest.py): Lets you patch the exec_msar_func() for testing. """ +import json from contextlib import contextmanager from mathesar.rpc import columns from mathesar.models.users import User -def test_columns_list(rf, monkeypatch): +def test_columns_list(rf, monkeypatch, mocked_exec_msar_func): request = rf.post('/api/rpc/v0/', data={}) request.user = User(username='alice', password='pass1234') table_oid = 23457 @@ -27,57 +29,7 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_column_info(_table_oid, conn): - if _table_oid != table_oid: - raise AssertionError('incorrect parameters passed') - return [ - { - 'id': 1, 'name': 'id', 'type': 'integer', - 'default': {'value': 'identity', 'is_dynamic': True}, - 'nullable': False, 'description': None, 'primary_key': True, - 'type_options': None, - 'has_dependents': True, - 'current_role_priv': ['SELECT', 'INSERT', 'UPDATE'], - 'valid_target_types': ['text'] - }, { - 'id': 2, 'name': 'numcol', 'type': 'numeric', - 'default': {'value': "'8'::numeric", 'is_dynamic': False}, - 'nullable': True, - 'description': 'My super numeric column', - 'primary_key': False, - 'type_options': {'scale': None, 'precision': None}, - 'has_dependents': False, - 'current_role_priv': ['SELECT', 'INSERT', 'UPDATE'], - 'valid_target_types': ['text'] - }, { - 'id': 4, 'name': 'numcolmod', 'type': 'numeric', - 'default': None, - 'nullable': True, 'description': None, 'primary_key': False, - 'type_options': {'scale': 3, 'precision': 5}, - 'has_dependents': False, - 'current_role_priv': ['SELECT', 'INSERT', 'UPDATE'], - 'valid_target_types': ['text'] - }, { - 'id': 8, 'name': 'ivlcolmod', 'type': 'interval', - 'default': None, - 'nullable': True, 'description': None, 'primary_key': False, - 'type_options': {'fields': 'day to second'}, - 'has_dependents': False, - 'current_role_priv': ['SELECT', 'INSERT', 'UPDATE'], - 'valid_target_types': ['text'] - }, { - 'id': 10, 'name': 'arrcol', 'type': '_array', - 'default': None, - 'nullable': True, 'description': None, 'primary_key': False, - 'type_options': {'item_type': 'character varying', 'length': 3}, - 'has_dependents': False, - 'current_role_priv': ['SELECT', 'INSERT', 'UPDATE'], - 'valid_target_types': None - }, - ] - monkeypatch.setattr(columns.base, 'connect', mock_connect) - monkeypatch.setattr(columns.base, 'get_column_info_for_table', mock_column_info) expect_col_list = [ { 'id': 1, 'name': 'id', 'type': 'integer', @@ -123,11 +75,14 @@ def mock_column_info(_table_oid, conn): 'valid_target_types': None } ] + mocked_exec_msar_func.fetchone.return_value = [expect_col_list] actual_col_list = columns.list_(table_oid=23457, database_id=database_id, request=request) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert actual_col_list == expect_col_list + assert call_args[2] == table_oid -def test_columns_patch(rf, monkeypatch): +def test_columns_patch(rf, monkeypatch, mocked_exec_msar_func): request = rf.post('/api/rpc/v0/', data={}) request.user = User(username='alice', password='pass1234') table_oid = 23457 @@ -144,23 +99,22 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_column_alter(_table_oid, _column_data_list, conn): - if _table_oid != table_oid or _column_data_list != column_data_list: - raise AssertionError('incorrect parameters passed') - return 1 - monkeypatch.setattr(columns.base, 'connect', mock_connect) - monkeypatch.setattr(columns.base, 'alter_columns_in_table', mock_column_alter) + mocked_exec_msar_func.fetchone.return_value = [1] actual_result = columns.patch( column_data_list=column_data_list, table_oid=table_oid, database_id=database_id, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] + transformed_column_data = [{'attnum': 3, 'name': 'newname'}] assert actual_result == 1 + assert call_args[2] == table_oid + assert call_args[3] == json.dumps(transformed_column_data) -def test_columns_add(rf, monkeypatch): +def test_columns_add(rf, monkeypatch, mocked_exec_msar_func): request = rf.post('/api/rpc/v0/', data={}) request.user = User(username='alice', password='pass1234') table_oid = 23457 @@ -177,23 +131,28 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_column_create(_table_oid, _column_data_list, conn): - if _table_oid != table_oid or _column_data_list != column_data_list: - raise AssertionError('incorrect parameters passed') - return [3, 4] - monkeypatch.setattr(columns.base, 'connect', mock_connect) - monkeypatch.setattr(columns.base, 'add_columns_to_table', mock_column_create) + mocked_exec_msar_func.fetchone.return_value = [[3, 4]] actual_result = columns.add( column_data_list=column_data_list, table_oid=table_oid, database_id=database_id, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] + transformed_column_data = [ + { + 'name': 'newname', 'type': {'name': 'character varying', 'options': {}}, + 'not_null': False, 'default': None, 'description': None + } + ] assert actual_result == [3, 4] + assert call_args[2] == table_oid + assert call_args[3] == json.dumps(transformed_column_data) + assert call_args[4] is False -def test_columns_delete(rf, monkeypatch): +def test_columns_delete(rf, monkeypatch, mocked_exec_msar_func): request = rf.post('/api/rpc/v0/', data={}) request.user = User(username='alice', password='pass1234') table_oid = 23457 @@ -210,17 +169,15 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_column_drop(_table_oid, _column_attnums, conn): - if _table_oid != table_oid or _column_attnums != column_attnums: - raise AssertionError('incorrect parameters passed') - return 3 - monkeypatch.setattr(columns.base, 'connect', mock_connect) - monkeypatch.setattr(columns.base, 'drop_columns_from_table', mock_column_drop) + mocked_exec_msar_func.fetchone.return_value = [3] actual_result = columns.delete( column_attnums=column_attnums, table_oid=table_oid, database_id=database_id, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert actual_result == 3 + assert call_args[2] == table_oid + assert call_args[3:6] == tuple(column_attnums) diff --git a/mathesar/tests/rpc/tables/test_t_base.py b/mathesar/tests/rpc/tables/test_t_base.py index 9a0f432f90..a5710529b2 100644 --- a/mathesar/tests/rpc/tables/test_t_base.py +++ b/mathesar/tests/rpc/tables/test_t_base.py @@ -4,15 +4,19 @@ Fixtures: rf(pytest-django): Provides mocked `Request` objects. monkeypatch(pytest): Lets you monkeypatch an object for testing. + mocked_exec_msar_func(mathesar/tests/conftest.py): Lets you patch the exec_msar_func() for testing. """ +import json from decimal import Decimal from contextlib import contextmanager +from db.tables import prepare_table_for_import +from db.deprecated.types.base import PostgresType from mathesar.rpc import tables from mathesar.models.users import User -def test_tables_list(rf, monkeypatch): +def test_tables_list(rf, monkeypatch, mocked_exec_msar_func): request = rf.post('/api/rpc/v0', data={}) request.user = User(username='alice', password='pass1234') schema_oid = 2200 @@ -28,25 +32,7 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_table_info(_schema_oid, conn): - if _schema_oid != schema_oid: - raise AssertionError('incorrect parameters passed') - return [ - { - 'oid': 17408, - 'name': 'Authors', - 'schema': schema_oid, - 'description': 'a description on the authors table.' - }, - { - 'oid': 17809, - 'name': 'Books', - 'schema': schema_oid, - 'description': None - } - ] monkeypatch.setattr(tables.base, 'connect', mock_connect) - monkeypatch.setattr(tables.base, 'get_table_info', mock_table_info) expect_table_list = [ { 'oid': 17408, @@ -61,11 +47,14 @@ def mock_table_info(_schema_oid, conn): 'description': None } ] + mocked_exec_msar_func.fetchone.return_value = [expect_table_list] actual_table_list = tables.list_(schema_oid=2200, database_id=11, request=request) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert actual_table_list == expect_table_list + assert call_args[2] == schema_oid -def test_tables_get(rf, monkeypatch): +def test_tables_get(rf, monkeypatch, mocked_exec_msar_func): request = rf.post('/api/rpc/v0', data={}) request.user = User(username='alice', password='pass1234') table_oid = 1964474 @@ -81,28 +70,21 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_table_get(_table_oid, conn): - if _table_oid != table_oid: - raise AssertionError('incorrect parameters passed') - return { - 'oid': table_oid, - 'name': 'Authors', - 'schema': 2200, - 'description': 'a description on the authors table.' - } monkeypatch.setattr(tables.base, 'connect', mock_connect) - monkeypatch.setattr(tables.base, 'get_table', mock_table_get) expect_table_list = { 'oid': table_oid, 'name': 'Authors', 'schema': 2200, 'description': 'a description on the authors table.' } + mocked_exec_msar_func.fetchone.return_value = [expect_table_list] actual_table_list = tables.get(table_oid=1964474, database_id=11, request=request) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert actual_table_list == expect_table_list + assert call_args[2] == table_oid -def test_tables_delete(rf, monkeypatch): +def test_tables_delete(rf, monkeypatch, mocked_exec_msar_func): request = rf.post('/api/rpc/v0', data={}) request.user = User(username='alice', password='pass1234') table_oid = 1964474 @@ -118,18 +100,16 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_drop_table(_table_oid, conn, cascade): - if _table_oid != table_oid: - raise AssertionError('incorrect parameters passed') - return 'public."Table 0"' - monkeypatch.setattr(tables.base, 'connect', mock_connect) - monkeypatch.setattr(tables.base, 'drop_table_from_database', mock_drop_table) + mocked_exec_msar_func.fetchone.return_value = ['public."Table 0"'] deleted_table = tables.delete(table_oid=1964474, database_id=11, request=request) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert deleted_table == 'public."Table 0"' + assert call_args[2] == table_oid + assert call_args[3] is False -def test_tables_add(rf, monkeypatch): +def test_tables_add(rf, monkeypatch, mocked_exec_msar_func): request = rf.post('/api/rpc/v0', data={}) request.user = User(username='alice', password='pass1234') schema_oid = 2200 @@ -145,17 +125,20 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_table_add(table_name, _schema_oid, conn, column_data_list, constraint_data_list, owner_oid, comment): - if _schema_oid != schema_oid: - raise AssertionError('incorrect parameters passed') - return {"oid": 1964474, "name": "newtable"} monkeypatch.setattr(tables.base, 'connect', mock_connect) - monkeypatch.setattr(tables.base, 'create_table_on_database', mock_table_add) + mocked_exec_msar_func.fetchone.return_value = [{"oid": 1964474, "name": "newtable"}] actual_table_info = tables.add(table_name='newtable', schema_oid=2200, database_id=11, request=request) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert actual_table_info == {"oid": 1964474, "name": "newtable"} + assert call_args[2] == schema_oid + assert call_args[3] == 'newtable' + assert call_args[4] == json.dumps([]) + assert call_args[5] == json.dumps([]) + assert call_args[6] is None + assert call_args[7] is None -def test_tables_patch(rf, monkeypatch): +def test_tables_patch(rf, monkeypatch, mocked_exec_msar_func): request = rf.post('/api/rpc/v0', data={}) request.user = User(username='alice', password='pass1234') table_oid = 1964474 @@ -176,12 +159,8 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_table_patch(_table_oid, _table_data_dict, conn): - if _table_oid != table_oid and _table_data_dict != table_data_dict: - raise AssertionError('incorrect parameters passed') - return 'newtabname' monkeypatch.setattr(tables.base, 'connect', mock_connect) - monkeypatch.setattr(tables.base, 'alter_table_on_database', mock_table_patch) + mocked_exec_msar_func.fetchone.return_value = ['newtabname'] altered_table_name = tables.patch( table_oid=1964474, table_data_dict={ @@ -192,7 +171,10 @@ def mock_table_patch(_table_oid, _table_data_dict, conn): database_id=11, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert altered_table_name == 'newtabname' + assert call_args[2] == table_oid + assert call_args[3] == json.dumps(table_data_dict) def test_tables_import(rf, monkeypatch): @@ -228,11 +210,52 @@ def mock_table_import(_data_file_id, table_name, _schema_oid, conn, comment): assert imported_table_info == {"oid": 1964474, "name": "imported_table"} -def test_tables_preview(rf, monkeypatch): +def test_prepare_table_for_import(rf, monkeypatch, mocked_exec_msar_func): + request = rf.post('/api/rpc/v0', data={}) + request.user = User(username='alice', password='pass1234') + schema_oid = 2200 + column_names = ['a', 'b', 'c'] + column_data_list = [ + { + "name": column_name, + "type": {"name": PostgresType.TEXT.id} + } for column_name in column_names + ] + expect_dict = { + 'copy_sql': 'COPY public.imported_table FROM patents.csv', + 'table_oid': 1234, + 'table_name': 'imported_table' + } + mocked_exec_msar_func.fetchone.return_value = [expect_dict] + copy_sql, table_oid, table_name = prepare_table_for_import( + table_name="imported_table", + schema_oid=schema_oid, + column_names=column_names, + header=True, + conn=True + ) + call_args = mocked_exec_msar_func.call_args_list[0][0] + assert copy_sql == expect_dict['copy_sql'] + assert table_oid == expect_dict['table_oid'] + assert table_name == expect_dict['table_name'] + assert call_args[2] == schema_oid + assert call_args[3] == 'imported_table' + assert call_args[4] == json.dumps(column_data_list) + # TODO: Consider parametrizing these params + assert call_args[5] is True # header + assert call_args[6] is None # delimiter + assert call_args[7] is None # escapechar + assert call_args[8] is None # quotechar + assert call_args[9] is None # encoding + assert call_args[10] is None # comment + + +def test_tables_preview(rf, monkeypatch, mocked_exec_msar_func): request = rf.post('/api/rpc/v0', data={}) request.user = User(username='alice', password='pass1234') table_oid = 1964474 database_id = 11 + column_list = [{'id': 2, 'type': 'numeric', 'type_options': {'precision': 3, 'scale': 2}}] @contextmanager def mock_connect(_database_id, user): @@ -244,32 +267,33 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_table_preview(_table_oid, columns, conn, limit): - if _table_oid != table_oid: - raise AssertionError('incorrect parameters passed') - return [ - {'id': 1, 'length': Decimal('2.0')}, - {'id': 2, 'length': Decimal('3.0')}, - {'id': 3, 'length': Decimal('4.0')}, - {'id': 4, 'length': Decimal('5.22')} - ] monkeypatch.setattr(tables.base, 'connect', mock_connect) - monkeypatch.setattr(tables.base, 'get_preview', mock_table_preview) + expected_records_list = [ + {'id': 1, 'length': Decimal('2.0')}, + {'id': 2, 'length': Decimal('3.0')}, + {'id': 3, 'length': Decimal('4.0')}, + {'id': 4, 'length': Decimal('5.22')} + ] + mocked_exec_msar_func.fetchone.return_value = [expected_records_list] records = tables.get_import_preview( table_oid=1964474, - columns=[{'attnum': 2, 'type': {'name': 'numeric', 'options': {'precision': 3, 'scale': 2}}}], + columns=column_list, database_id=11, request=request ) - assert records == [ - {'id': 1, 'length': Decimal('2.0')}, - {'id': 2, 'length': Decimal('3.0')}, - {'id': 3, 'length': Decimal('4.0')}, - {'id': 4, 'length': Decimal('5.22')} + call_args = mocked_exec_msar_func.call_args_list[0][0] + transformed_col_list = [ + { + 'attnum': 2, 'type': {'name': 'numeric', 'options': {'precision': 3, 'scale': 2}} + } ] + assert records == expected_records_list + assert call_args[2] == table_oid + assert call_args[3] == json.dumps(transformed_col_list) + assert call_args[4] == 20 -def test_list_joinable(rf, monkeypatch): +def test_list_joinable(rf, monkeypatch, mocked_exec_msar_func): request = rf.post('/api/rpc/v0', data={}) request.user = User(username='alice', password='pass1234') table_oid = 2254329 @@ -285,47 +309,6 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_list_joinable_tables(_table_oid, conn, max_depth): - if _table_oid != table_oid: - raise AssertionError('incorrect parameters passed') - return { - 'joinable_tables': [ - { - 'base': 2254329, - 'depth': 1, - 'target': 2254334, - 'fkey_path': [[2254406, False]], - 'join_path': [[[2254329, 2], [2254334, 1]]], - 'multiple_results': False - }, - { - 'base': 2254329, - 'depth': 1, - 'target': 2254350, - 'fkey_path': [[2254411, False]], - 'join_path': [[[2254329, 3], [2254350, 1]]], - 'multiple_results': False - }], - 'target_table_info': { - '2254334': { - 'name': 'Items', - 'columns': { - '1': {'name': 'id', 'type': 'integer'}, - '2': {'name': 'Barcode', 'type': 'text'}, - '3': {'name': 'Acquisition Date', 'type': 'date'}, - '5': {'name': 'Book', 'type': 'integer'} - } - }, - '2254350': { - 'name': 'Patrons', - 'columns': { - '1': {'name': 'id', 'type': 'integer'}, - '2': {'name': 'First Name', 'type': 'text'}, - '3': {'name': 'Last Name', 'type': 'text'} - } - } - } - } expected_dict = { 'joinable_tables': [ { @@ -365,6 +348,9 @@ def mock_list_joinable_tables(_table_oid, conn, max_depth): } } monkeypatch.setattr(tables.base, 'connect', mock_connect) - monkeypatch.setattr(tables.base, 'list_joinable_tables', mock_list_joinable_tables) + mocked_exec_msar_func.fetchone.return_value = [expected_dict] actual_dict = tables.list_joinable(table_oid=2254329, database_id=11, max_depth=1, request=request) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert expected_dict == actual_dict + assert call_args[2] == 1 + assert call_args[3] == table_oid diff --git a/mathesar/tests/rpc/test_constraints.py b/mathesar/tests/rpc/test_constraints.py index 6153594dd0..00898acc99 100644 --- a/mathesar/tests/rpc/test_constraints.py +++ b/mathesar/tests/rpc/test_constraints.py @@ -3,14 +3,17 @@ Fixtures: monkeypatch(pytest): Lets you monkeypatch an object for testing. + mocked_select_from_msar_func(mathesar/tests/conftest.py): Lets you patch the select_from_msar_func() for testing. + mocked_exec_msar_func(mathesar/tests/conftest.py): Lets you patch the exec_msar_func() for testing. """ +import json from contextlib import contextmanager from mathesar.rpc import constraints from mathesar.models.users import User -def test_constraints_list(rf, monkeypatch): +def test_constraints_list(rf, monkeypatch, mocked_select_from_msar_func): request = rf.post('/api/rpc/v0', data={}) request.user = User(username='alice', password='pass1234') table_oid = 2254444 @@ -26,37 +29,7 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_constaints_list(_table_oid, conn): - if _table_oid != table_oid: - raise AssertionError('incorrect parameters passed') - return [ - { - 'oid': 2254567, - 'name': 'Movie Cast Map_Cast Member_fkey', - 'type': 'foreignkey', - 'columns': [4], - 'referent_table_oid': 2254492, - 'referent_columns': [1] - }, - { - 'oid': 2254572, - 'name': 'Movie Cast Map_Movie_fkey', - 'type': 'foreignkey', - 'columns': [3], - 'referent_table_oid': 2254483, - 'referent_columns': [1] - }, - { - 'oid': 2254544, - 'name': 'Movie Cast Map_pkey', - 'type': 'primary', - 'columns': [1], - 'referent_table_oid': 0, - 'referent_columns': None - } - ] monkeypatch.setattr(constraints, 'connect', mock_connect) - monkeypatch.setattr(constraints, 'get_constraints_for_table', mock_constaints_list) expect_constraints_list = [ { 'oid': 2254567, @@ -83,11 +56,14 @@ def mock_constaints_list(_table_oid, conn): 'referent_columns': None } ] + mocked_select_from_msar_func.return_value = expect_constraints_list actual_constraint_list = constraints.list_(table_oid=table_oid, database_id=11, request=request) + call_args = mocked_select_from_msar_func.call_args_list[0][0] assert actual_constraint_list == expect_constraints_list + assert call_args[2] == table_oid -def test_constraints_drop(rf, monkeypatch): +def test_constraints_drop(rf, monkeypatch, mocked_exec_msar_func): request = rf.post('/api/rpc/v0', data={}) request.user = User(username='alice', password='pass1234') table_oid = 2254444 @@ -104,19 +80,18 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_constaints_delete(_table_oid, _constraint_oid, conn): - if _table_oid != table_oid and _constraint_oid != constraint_oid: - raise AssertionError('incorrect parameters passed') - return 'Movie Cast Map_Cast Member_fkey' monkeypatch.setattr(constraints, 'connect', mock_connect) - monkeypatch.setattr(constraints, 'drop_constraint_via_oid', mock_constaints_delete) + mocked_exec_msar_func.fetchone.return_value = ['Movie Cast Map_Cast Member_fkey'] constraint_name = constraints.delete( table_oid=table_oid, constraint_oid=constraint_oid, database_id=11, request=request ) - assert constraint_name == 'Movie Cast Map_Cast Member_fkey' + call_args = mocked_exec_msar_func.call_args_list[0][0] + assert constraint_name == mocked_exec_msar_func.fetchone.return_value[0] + assert call_args[2] == table_oid + assert call_args[3] == constraint_oid -def test_constraints_create(rf, monkeypatch): +def test_constraints_create(rf, monkeypatch, mocked_exec_msar_func): request = rf.post('/api/rpc/v0', data={}) request.user = User(username='alice', password='pass1234') table_oid = 2254444 @@ -144,13 +119,12 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_constaints_create(_table_oid, _constraint_def_list, conn): - if _table_oid != table_oid and _constraint_def_list != constraint_def_list: - raise AssertionError('incorrect parameters passed') - return [2254833, 2254567, 2254544] monkeypatch.setattr(constraints, 'connect', mock_connect) - monkeypatch.setattr(constraints, 'create_constraint', mock_constaints_create) + mocked_exec_msar_func.fetchone.return_value = [[2254833, 2254567, 2254544]] constraint_oids = constraints.add( table_oid=table_oid, constraint_def_list=constraint_def_list, database_id=11, request=request ) - assert constraint_oids == [2254833, 2254567, 2254544] + call_args = mocked_exec_msar_func.call_args_list[0][0] + assert constraint_oids == mocked_exec_msar_func.fetchone.return_value[0] + assert call_args[2] == table_oid + assert call_args[3] == json.dumps(constraint_def_list) diff --git a/mathesar/tests/rpc/test_data_modeling.py b/mathesar/tests/rpc/test_data_modeling.py index 4b25e829ce..9f43bee5ae 100644 --- a/mathesar/tests/rpc/test_data_modeling.py +++ b/mathesar/tests/rpc/test_data_modeling.py @@ -4,14 +4,16 @@ Fixtures: rf(pytest-django): Provides mocked `Request` objects. monkeypatch(pytest): Lets you monkeypatch an object for testing. + mocked_exec_msar_func(mathesar/tests/conftest.py): Lets you patch the exec_msar_func() for testing. """ +import json from contextlib import contextmanager from mathesar.rpc import data_modeling from mathesar.models.users import User -def test_add_foreign_key_column(rf, monkeypatch): +def test_add_foreign_key_column(rf, monkeypatch, mocked_exec_msar_func): _username = 'alice' _password = 'pass1234' _column_name = 'new_fkey_col' @@ -31,22 +33,7 @@ def mock_connect(database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_add_fkey_col( - conn, - column_name, - referrer_table_oid, - referent_table_oid, - unique_link=False - ): - if ( - column_name != _column_name - or referent_table_oid != _referent_table_oid - or referrer_table_oid != _referrer_table_oid - ): - raise AssertionError('incorrect parameters passed') - monkeypatch.setattr(data_modeling, 'connect', mock_connect) - monkeypatch.setattr(data_modeling.links, 'add_foreign_key_column', mock_add_fkey_col) data_modeling.add_foreign_key_column( column_name=_column_name, referrer_table_oid=_referrer_table_oid, @@ -54,9 +41,14 @@ def mock_add_fkey_col( database_id=_database_id, request=request, ) + call_args = mocked_exec_msar_func.call_args_list[0][0] + assert call_args[2] == _column_name + assert call_args[3] == _referrer_table_oid + assert call_args[4] == _referent_table_oid + assert call_args[5] is False # Make link one-to-one? -def test_add_mapping_table(rf, monkeypatch): +def test_add_mapping_table(rf, monkeypatch, mocked_exec_msar_func): _username = 'alice' _password = 'pass1234' _schema_oid = 1234 @@ -79,21 +71,7 @@ def mock_connect(database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_add_mapping_table( - conn, - schema_oid, - table_name, - mapping_columns, - ): - if ( - schema_oid != _schema_oid - or table_name != _table_name - or mapping_columns != _mapping_columns - ): - raise AssertionError('incorrect parameters passed') - monkeypatch.setattr(data_modeling, 'connect', mock_connect) - monkeypatch.setattr(data_modeling.links, 'add_mapping_table', mock_add_mapping_table) data_modeling.add_mapping_table( table_name=_table_name, mapping_columns=_mapping_columns, @@ -101,9 +79,13 @@ def mock_add_mapping_table( database_id=_database_id, request=request, ) + call_args = mocked_exec_msar_func.call_args_list[0][0] + assert call_args[2] == _schema_oid + assert call_args[3] == _table_name + assert call_args[4] == json.dumps(_mapping_columns) -def test_suggest_types(rf, monkeypatch): +def test_suggest_types(rf, monkeypatch, mocked_exec_msar_func): _username = 'alice' _password = 'pass1234' _table_oid = 12345 @@ -121,20 +103,17 @@ def mock_connect(database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_suggest_types(conn, table_oid): - if table_oid != _table_oid: - raise AssertionError('incorrect parameters passed') - monkeypatch.setattr(data_modeling, 'connect', mock_connect) - monkeypatch.setattr(data_modeling.tables, 'infer_table_column_data_types', mock_suggest_types) data_modeling.suggest_types( table_oid=_table_oid, database_id=_database_id, request=request, ) + call_args = mocked_exec_msar_func.call_args_list[0][0] + assert call_args[2] == _table_oid -def test_split_table(rf, monkeypatch): +def test_split_table(rf, monkeypatch, mocked_exec_msar_func): _username = 'alice' _password = 'pass1234' _table_oid = 12345 @@ -155,23 +134,8 @@ def mock_connect(database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_split_table( - conn, - table_oid, - column_attnums, - extracted_table_name, - relationship_fk_column_name - ): - if ( - table_oid != _table_oid - and column_attnums != _column_attnums - and extracted_table_name != _extracted_table_name - and relationship_fk_column_name != _relationship_fk_column_name - ): - raise AssertionError('incorrect parameters passed') - monkeypatch.setattr(data_modeling, 'connect', mock_connect) - monkeypatch.setattr(data_modeling.tables, 'split_table', mock_split_table) + mocked_exec_msar_func.fetchone.return_value = [[12345, 6]] data_modeling.split_table( table_oid=_table_oid, column_attnums=_column_attnums, @@ -180,9 +144,14 @@ def mock_split_table( database_id=_database_id, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] + assert call_args[2] == _table_oid + assert call_args[3] == _column_attnums + assert call_args[4] == _extracted_table_name + assert call_args[5] == _relationship_fk_column_name -def test_move_columns(rf, monkeypatch): +def test_move_columns(rf, monkeypatch, mocked_exec_msar_func): _username = 'alice' _password = 'pass1234' _source_table_oid = 12345 @@ -202,21 +171,7 @@ def mock_connect(database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_move_columns( - conn, - source_table_oid, - target_table_oid, - move_column_attnums - ): - if ( - source_table_oid != _source_table_oid - and target_table_oid != _target_table_oid - and move_column_attnums != _move_column_attnums - ): - raise AssertionError('incorrect parameters passed') - monkeypatch.setattr(data_modeling, 'connect', mock_connect) - monkeypatch.setattr(data_modeling.tables, 'move_columns_to_referenced_table', mock_move_columns) data_modeling.move_columns( source_table_oid=_source_table_oid, target_table_oid=_target_table_oid, @@ -224,3 +179,7 @@ def mock_move_columns( database_id=_database_id, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] + assert call_args[2] == _source_table_oid + assert call_args[3] == _target_table_oid + assert call_args[4] == _move_column_attnums diff --git a/mathesar/tests/rpc/test_database_privileges.py b/mathesar/tests/rpc/test_database_privileges.py index f836a54edd..4ce681e644 100644 --- a/mathesar/tests/rpc/test_database_privileges.py +++ b/mathesar/tests/rpc/test_database_privileges.py @@ -4,14 +4,16 @@ Fixtures: rf(pytest-django): Provides mocked `Request` objects. monkeypatch(pytest): Lets you monkeypatch an object for testing. + mocked_exec_msar_func(mathesar/tests/conftest.py): Lets you patch the exec_msar_func() for testing. """ +import json from contextlib import contextmanager from mathesar.rpc.databases import privileges from mathesar.models.users import User -def test_database_privileges_set_for_roles(rf, monkeypatch): +def test_database_privileges_set_for_roles(rf, monkeypatch, mocked_exec_msar_func): _username = 'alice' _password = 'pass1234' _database_id = 2 @@ -29,31 +31,21 @@ def mock_connect(database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_replace_privileges( - conn, - privileges, - ): - if privileges != _privileges: - raise AssertionError('incorrect parameters passed') - return _privileges + [{"role_oid": 67890, "direct": ["CONNECT", "TEMPORARY"]}] - monkeypatch.setattr(privileges, 'connect', mock_connect) - monkeypatch.setattr( - privileges, - 'replace_database_privileges_for_roles', - mock_replace_privileges - ) expect_response = [ {"role_oid": 12345, "direct": ["CONNECT"]}, {"role_oid": 67890, "direct": ["CONNECT", "TEMPORARY"]} ] + mocked_exec_msar_func.fetchone.return_value = [expect_response] actual_response = privileges.replace_for_roles( privileges=_privileges, database_id=_database_id, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert actual_response == expect_response + assert call_args[2] == json.dumps(_privileges) -def test_transfer_db_ownership(rf, monkeypatch): +def test_transfer_db_ownership(rf, monkeypatch, mocked_exec_msar_func): _username = 'alice' _password = 'pass1234' _database_id = 2 @@ -70,27 +62,19 @@ def mock_connect(database_id, user): pass else: raise AssertionError('incorrect parameters passed') - - def mock_tansfer_db_ownership( - new_owner_oid, - conn - ): - if new_owner_oid != _new_owner_oid: - raise AssertionError('incorrect parameters passed') - return { - 'oid': 1988103, - 'name': 'mathesar', - 'owner_oid': new_owner_oid, - 'current_role_priv': ['CONNECT', 'CREATE', 'TEMPORARY'], - 'current_role_owns': True - } + expect_response = { + 'oid': 1988103, + 'name': 'mathesar', + 'owner_oid': _new_owner_oid, + 'current_role_priv': ['CONNECT', 'CREATE', 'TEMPORARY'], + 'current_role_owns': True + } monkeypatch.setattr(privileges, 'connect', mock_connect) - monkeypatch.setattr( - privileges, - 'transfer_database_ownership', - mock_tansfer_db_ownership - ) - privileges.transfer_ownership( + mocked_exec_msar_func.fetchone.return_value = [expect_response] + actual_response = privileges.transfer_ownership( new_owner_oid=_new_owner_oid, database_id=_database_id, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] + assert actual_response == expect_response + assert call_args[2] == _new_owner_oid diff --git a/mathesar/tests/rpc/test_records.py b/mathesar/tests/rpc/test_records.py index 4c0f9bb8cc..7d5ccc99cf 100644 --- a/mathesar/tests/rpc/test_records.py +++ b/mathesar/tests/rpc/test_records.py @@ -4,14 +4,16 @@ Fixtures: rf(pytest-django): Provides mocked `Request` objects. monkeypatch(pytest): Lets you monkeypatch an object for testing. + mocked_exec_msar_func(mathesar/tests/conftest.py): Lets you patch the exec_msar_func() for testing. """ +import json from contextlib import contextmanager from mathesar.rpc import records from mathesar.models.users import User -def test_records_list(rf, monkeypatch): +def test_records_list(rf, monkeypatch, mocked_exec_msar_func): username = 'alice' password = 'pass1234' table_oid = 23457 @@ -29,35 +31,7 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_list_records( - conn, - _table_oid, - limit=None, - offset=None, - order=None, - filter=None, - group=None, - return_record_summaries=False, - table_record_summary_templates=None, - ): - if _table_oid != table_oid or return_record_summaries is False: - raise AssertionError('incorrect parameters passed') - return { - "count": 50123, - "results": [{"1": "abcde", "2": 12345}, {"1": "fghij", "2": 67890}], - "query": 'SELECT mycol AS "1", anothercol AS "2" FROM mytable LIMIT 2', - "grouping": { - "columns": [2], - "groups": [ - {"id": 3, "count": 8, "results_eq": {"1": "lsfj", "2": 3422}} - ] - }, - "linked_record_summaries": {"2": {"12345": "blkjdfslkj"}}, - "record_summaries": {"3": "abcde"} - } - monkeypatch.setattr(records, 'connect', mock_connect) - monkeypatch.setattr(records, 'list_records_from_table', mock_list_records) expect_records_list = { "count": 50123, "results": [{"1": "abcde", "2": 12345}, {"1": "fghij", "2": 67890}], @@ -71,16 +45,26 @@ def mock_list_records( "record_summaries": {"3": "abcde"}, "query": 'SELECT mycol AS "1", anothercol AS "2" FROM mytable LIMIT 2', } + mocked_exec_msar_func.fetchone.return_value = [expect_records_list] actual_records_list = records.list_( table_oid=table_oid, database_id=database_id, return_record_summaries=True, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert actual_records_list == expect_records_list + assert call_args[2] == table_oid + assert call_args[3] is None # limit + assert call_args[4] is None # offset + assert call_args[5] is None # order + assert call_args[6] is None # filter + assert call_args[7] is None # group + assert call_args[8] is True # return_record_summaries + assert call_args[9] == json.dumps({}) # summary template -def test_records_get(rf, monkeypatch): +def test_records_get(rf, monkeypatch, mocked_exec_msar_func): username = 'alice' password = 'pass1234' table_oid = 23457 @@ -99,26 +83,7 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_get_record( - conn, - _record_id, - _table_oid, - return_record_summaries=False, - table_record_summary_templates=None, - ): - if _table_oid != table_oid or _record_id != record_id or return_record_summaries is False: - raise AssertionError('incorrect parameters passed') - return { - "count": 1, - "results": [{"1": "abcde", "2": 12345}, {"1": "fghij", "2": 67890}], - "query": 'SELECT mycol AS "1", anothercol AS "2" FROM mytable LIMIT 2', - "grouping": None, - "linked_record_summaries": {"2": {"12345": "blkjdfslkj"}}, - "record_summaries": {"3": "abcde"}, - } - monkeypatch.setattr(records, 'connect', mock_connect) - monkeypatch.setattr(records, 'get_record_from_table', mock_get_record) expect_record = { "count": 1, "results": [{"1": "abcde", "2": 12345}, {"1": "fghij", "2": 67890}], @@ -127,6 +92,7 @@ def mock_get_record( "record_summaries": {"3": "abcde"}, "query": 'SELECT mycol AS "1", anothercol AS "2" FROM mytable LIMIT 2', } + mocked_exec_msar_func.fetchone.return_value = [expect_record] actual_record = records.get( record_id=record_id, table_oid=table_oid, @@ -134,10 +100,15 @@ def mock_get_record( return_record_summaries=True, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert actual_record == expect_record + assert call_args[2] == table_oid + assert call_args[3] == record_id + assert call_args[4] is True # return_record_summaries + assert call_args[5] == json.dumps({}) # table_record_summary_templates -def test_records_add(rf, monkeypatch): +def test_records_add(rf, monkeypatch, mocked_exec_msar_func): username = 'alice' password = 'pass1234' table_oid = 23457 @@ -156,28 +127,13 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_add_record( - conn, - _record_def, - _table_oid, - return_record_summaries=False, - table_record_summary_templates=None, - ): - if _table_oid != table_oid or _record_def != record_def or return_record_summaries is False: - raise AssertionError('incorrect parameters passed') - return { - "results": [_record_def], - "linked_record_summaries": {"2": {"12345": "blkjdfslkj"}}, - "record_summaries": {"3": "abcde"}, - } - monkeypatch.setattr(records, 'connect', mock_connect) - monkeypatch.setattr(records, 'add_record_to_table', mock_add_record) expect_record = { "results": [record_def], "linked_record_summaries": {"2": {"12345": "blkjdfslkj"}}, "record_summaries": {"3": "abcde"}, } + mocked_exec_msar_func.fetchone.return_value = [expect_record] actual_record = records.add( record_def=record_def, table_oid=table_oid, @@ -185,10 +141,15 @@ def mock_add_record( return_record_summaries=True, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert actual_record == expect_record + assert call_args[2] == table_oid + assert call_args[3] == json.dumps(record_def) + assert call_args[4] is True # return_record_summaries + assert call_args[5] == json.dumps({}) # table_record_summary_templates -def test_records_patch(rf, monkeypatch): +def test_records_patch(rf, monkeypatch, mocked_exec_msar_func): username = 'alice' password = 'pass1234' record_id = 243 @@ -208,34 +169,13 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_patch_record( - conn, - _record_def, - _record_id, - _table_oid, - return_record_summaries=False, - table_record_summary_templates=None, - ): - if ( - _table_oid != table_oid - or _record_def != record_def - or _record_id != record_id - or return_record_summaries is False - ): - raise AssertionError('incorrect parameters passed') - return { - "results": [_record_def | {"3": "another"}], - "linked_record_summaries": {"2": {"12345": "blkjdfslkj"}}, - "record_summaries": {"3": "abcde"}, - } - monkeypatch.setattr(records, 'connect', mock_connect) - monkeypatch.setattr(records, 'patch_record_in_table', mock_patch_record) expect_record = { "results": [record_def | {"3": "another"}], "linked_record_summaries": {"2": {"12345": "blkjdfslkj"}}, "record_summaries": {"3": "abcde"}, } + mocked_exec_msar_func.fetchone.return_value = [expect_record] actual_record = records.patch( record_def=record_def, record_id=record_id, @@ -245,10 +185,16 @@ def mock_patch_record( table_record_summary_templates=None, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert actual_record == expect_record + assert call_args[2] == table_oid + assert call_args[3] == record_id + assert call_args[4] == json.dumps(record_def) + assert call_args[5] is True # return_record_summaries + assert call_args[6] == json.dumps({}) # table_record_summary_templates -def test_records_delete(rf, monkeypatch): +def test_records_delete(rf, monkeypatch, mocked_exec_msar_func): username = 'alice' password = 'pass1234' table_oid = 23457 @@ -267,25 +213,19 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_delete_records( - conn, - _record_ids, - _table_oid, - ): - if _table_oid != table_oid or _record_ids != record_ids: - raise AssertionError('incorrect parameters passed') - return 2 - monkeypatch.setattr(records, 'connect', mock_connect) - monkeypatch.setattr(records, 'delete_records_from_table', mock_delete_records) expect_result = 2 + mocked_exec_msar_func.fetchone.return_value = [expect_result] actual_result = records.delete( record_ids=record_ids, table_oid=table_oid, database_id=database_id, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert actual_result == expect_result + assert call_args[2] == table_oid + assert call_args[3] == json.dumps(record_ids) -def test_records_search(rf, monkeypatch): +def test_records_search(rf, monkeypatch, mocked_exec_msar_func): username = 'alice' password = 'pass1234' table_oid = 23457 @@ -303,26 +243,7 @@ def mock_connect(_database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_search_records( - conn, - _table_oid, - search=[], - limit=10, - return_record_summaries=False, - table_record_summary_templates=None, - ): - if _table_oid != table_oid or return_record_summaries is False: - raise AssertionError('incorrect parameters passed') - return { - "count": 50123, - "results": [{"1": "abcde", "2": 12345}, {"1": "fghij", "2": 67890}], - "linked_record_summaries": {"2": {"12345": "blkjdfslkj"}}, - "record_summaries": {"3": "abcde"}, - "query": 'SELECT mycol AS "1", anothercol AS "2" FROM mytable LIMIT 2', - } - monkeypatch.setattr(records, 'connect', mock_connect) - monkeypatch.setattr(records, 'search_records_from_table', mock_search_records) expect_records_list = { "count": 50123, "results": [{"1": "abcde", "2": 12345}, {"1": "fghij", "2": 67890}], @@ -331,10 +252,17 @@ def mock_search_records( "record_summaries": {"3": "abcde"}, "query": 'SELECT mycol AS "1", anothercol AS "2" FROM mytable LIMIT 2', } + mocked_exec_msar_func.fetchone.return_value = [expect_records_list] actual_records_list = records.search( table_oid=table_oid, database_id=database_id, return_record_summaries=True, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert actual_records_list == expect_records_list + assert call_args[2] == table_oid + assert call_args[3] == json.dumps([]) # search query + assert call_args[4] == 10 # limit + assert call_args[5] is True # return_record_summaries + assert call_args[6] == json.dumps({}) # table_record_summary_templates diff --git a/mathesar/tests/rpc/test_roles.py b/mathesar/tests/rpc/test_roles.py index f6ebb516b1..cf3f693ea8 100644 --- a/mathesar/tests/rpc/test_roles.py +++ b/mathesar/tests/rpc/test_roles.py @@ -4,6 +4,7 @@ Fixtures: rf(pytest-django): Provides mocked `Request` objects. monkeypatch(pytest): Lets you monkeypatch an object for testing. + mocked_exec_msar_func(mathesar/tests/conftest.py): Lets you patch the exec_msar_func() for testing. """ from contextlib import contextmanager @@ -70,7 +71,7 @@ def mock_list_roles(conn): roles.list_(database_id=_database_id, request=request) -def test_roles_add(rf, monkeypatch): +def test_roles_add(rf, monkeypatch, mocked_exec_msar_func): _username = 'alice' _password = 'pass1234' _database_id = 2 @@ -87,30 +88,15 @@ def mock_connect(database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_create_role(rolename, password, login, conn): - if ( - rolename != _username - or password != _password - ): - raise AssertionError('incorrect parameters passed') - return { - 'oid': '2573190', - 'name': 'alice', - 'login': False, - 'super': False, - 'members': None, - 'inherits': True, - 'create_db': False, - 'create_role': False, - 'description': None - } - monkeypatch.setattr(roles.base, 'connect', mock_connect) - monkeypatch.setattr(roles.base, 'create_role', mock_create_role) roles.add(rolename=_username, database_id=_database_id, password=_password, login=True, request=request) + call_args = mocked_exec_msar_func.call_args_list[0][0] + assert call_args[2] == _username + assert call_args[3] == _password + assert call_args[4] is True # login role? -def test_roles_delete(rf, monkeypatch): +def test_roles_delete(rf, monkeypatch, mocked_exec_msar_func): _username = 'alice' _password = 'pass1234' _database_id = 2 @@ -128,16 +114,10 @@ def mock_connect(database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_drop_role(role_oid, conn): - if ( - role_oid != _role_oid - ): - raise AssertionError('incorrect parameters passed') - return None - monkeypatch.setattr(roles.base, 'connect', mock_connect) - monkeypatch.setattr(roles.base, 'drop_role', mock_drop_role) roles.delete(role_oid=_role_oid, database_id=_database_id, request=request) + call_args = mocked_exec_msar_func.call_args_list[0][0] + assert call_args[2] == _role_oid def test_get_current_role(rf, monkeypatch): @@ -189,7 +169,7 @@ def mock_get_current_role(conn): roles.get_current_role(database_id=_database_id, request=request) -def test_roles_set_members(rf, monkeypatch): +def test_roles_set_members(rf, monkeypatch, mocked_exec_msar_func): _username = 'alice' _password = 'pass1234' _database_id = 2 @@ -208,24 +188,8 @@ def mock_connect(database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_set_roles(parent_role_oid, members, conn): - if ( - parent_role_oid != _parent_role_oid - or members != _members - ): - raise AssertionError('incorrect parameters passed') - return { - 'oid': 10, - 'name': 'mathesar', - 'login': True, - 'super': True, - 'members': [{'oid': 2573031, 'admin': False}, {'oid': 2573040, 'admin': False}], - 'inherits': True, - 'create_db': True, - 'create_role': True, - 'description': None - } - monkeypatch.setattr(roles.base, 'connect', mock_connect) - monkeypatch.setattr(roles.base, 'set_members_to_role', mock_set_roles) roles.set_members(parent_role_oid=_parent_role_oid, database_id=_database_id, members=_members, request=request) + call_args = mocked_exec_msar_func.call_args_list[0][0] + assert call_args[2] == _parent_role_oid + assert call_args[3] == _members diff --git a/mathesar/tests/rpc/test_schema_privileges.py b/mathesar/tests/rpc/test_schema_privileges.py index d06ba60376..e0f8c16e2a 100644 --- a/mathesar/tests/rpc/test_schema_privileges.py +++ b/mathesar/tests/rpc/test_schema_privileges.py @@ -4,14 +4,16 @@ Fixtures: rf(pytest-django): Provides mocked `Request` objects. monkeypatch(pytest): Lets you monkeypatch an object for testing. + mocked_exec_msar_func(mathesar/tests/conftest.py): Lets you patch the exec_msar_func() for testing. """ +import json from contextlib import contextmanager from mathesar.rpc.schemas import privileges as schema_privileges from mathesar.models.users import User -def test_schema_privileges_list_direct(rf, monkeypatch): +def test_schema_privileges_list_direct(rf, monkeypatch, mocked_exec_msar_func): _username = 'alice' _password = 'pass1234' _database_id = 2 @@ -30,28 +32,18 @@ def mock_connect(database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_list_privileges( - schema_oid, - conn, - ): - if schema_oid != _schema_oid: - raise AssertionError('incorrect parameters passed') - return _privileges - monkeypatch.setattr(schema_privileges, 'connect', mock_connect) - monkeypatch.setattr( - schema_privileges, - 'list_schema_privileges', - mock_list_privileges - ) expect_response = _privileges + mocked_exec_msar_func.fetchone.return_value = [expect_response] actual_response = schema_privileges.list_direct( schema_oid=_schema_oid, database_id=_database_id, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert actual_response == expect_response + assert call_args[2] == _schema_oid -def test_schema_privileges_replace_for_roles(rf, monkeypatch): +def test_schema_privileges_replace_for_roles(rf, monkeypatch, mocked_exec_msar_func): _username = 'alice' _password = 'pass1234' _schema_oid = 654321 @@ -70,33 +62,23 @@ def mock_connect(database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_replace_privileges( - conn, - schema_oid, - privileges, - ): - if privileges != _privileges or schema_oid != _schema_oid: - raise AssertionError('incorrect parameters passed') - return _privileges + [{"role_oid": 67890, "direct": ["USAGE", "CREATE"]}] - monkeypatch.setattr(schema_privileges, 'connect', mock_connect) - monkeypatch.setattr( - schema_privileges, - 'replace_schema_privileges_for_roles', - mock_replace_privileges - ) expect_response = [ {"role_oid": 12345, "direct": ["USAGE"]}, {"role_oid": 67890, "direct": ["USAGE", "CREATE"]} ] + mocked_exec_msar_func.fetchone.return_value = [expect_response] actual_response = schema_privileges.replace_for_roles( privileges=_privileges, schema_oid=_schema_oid, database_id=_database_id, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert actual_response == expect_response + assert call_args[2] == _schema_oid + assert call_args[3] == json.dumps(_privileges) -def test_transfer_schema_ownership(rf, monkeypatch): +def test_transfer_schema_ownership(rf, monkeypatch, mocked_exec_msar_func): _username = 'alice' _password = 'pass1234' _database_id = 2 @@ -115,29 +97,10 @@ def mock_connect(database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_tansfer_schema_ownership( - schema_oid, - new_owner_oid, - conn - ): - if schema_oid != _schema_oid and new_owner_oid != _new_owner_oid: - raise AssertionError('incorrect parameters passed') - return { - 'oid': schema_oid, - 'name': 'testingpriv', - 'owner_oid': 2573031, - 'description': None, - 'table_count': 2, - 'current_role_owns': True, - 'current_role_priv': ['USAGE', 'CREATE'] - } - monkeypatch.setattr(schema_privileges, 'connect', mock_connect) - monkeypatch.setattr( - schema_privileges, - 'transfer_schema_ownership', - mock_tansfer_schema_ownership - ) schema_privileges.transfer_ownership( schema_oid=_schema_oid, new_owner_oid=_new_owner_oid, database_id=_database_id, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] + assert call_args[2] == _schema_oid + assert call_args[3] == _new_owner_oid diff --git a/mathesar/tests/rpc/test_table_privileges.py b/mathesar/tests/rpc/test_table_privileges.py index cbd8510e4b..1a75eb3013 100644 --- a/mathesar/tests/rpc/test_table_privileges.py +++ b/mathesar/tests/rpc/test_table_privileges.py @@ -4,14 +4,16 @@ Fixtures: rf(pytest-django): Provides mocked `Request` objects. monkeypatch(pytest): Lets you monkeypatch an object for testing. + mocked_exec_msar_func(mathesar/tests/conftest.py): Lets you patch the exec_msar_func() for testing. """ +import json from contextlib import contextmanager from mathesar.rpc.tables import privileges as table_privileges from mathesar.models.users import User -def test_table_privileges_list_direct(rf, monkeypatch): +def test_table_privileges_list_direct(rf, monkeypatch, mocked_exec_msar_func): _username = 'alice' _password = 'pass1234' _database_id = 2 @@ -42,28 +44,18 @@ def mock_connect(database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_list_privileges( - table_oid, - conn, - ): - if table_oid != _table_oid: - raise AssertionError('incorrect parameters passed') - return _privileges - monkeypatch.setattr(table_privileges, 'connect', mock_connect) - monkeypatch.setattr( - table_privileges, - 'list_table_privileges', - mock_list_privileges - ) expect_response = _privileges + mocked_exec_msar_func.fetchone.return_value = [expect_response] actual_response = table_privileges.list_direct( table_oid=_table_oid, database_id=_database_id, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert actual_response == expect_response + assert call_args[2] == _table_oid -def test_table_privileges_replace_for_roles(rf, monkeypatch): +def test_table_privileges_replace_for_roles(rf, monkeypatch, mocked_exec_msar_func): _username = 'alice' _password = 'pass1234' _table_oid = 654321 @@ -82,43 +74,23 @@ def mock_connect(database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_replace_privileges( - conn, - table_oid, - privileges, - ): - if privileges != _privileges or table_oid != _table_oid: - raise AssertionError('incorrect parameters passed') - return _privileges + [{ - "role_oid": 67890, - "direct": [ - "INSERT", - "SELECT", - "UPDATE", - "DELETE", - "TRUNCATE", - "REFERENCES", - "TRIGGER" - ]}] - monkeypatch.setattr(table_privileges, 'connect', mock_connect) - monkeypatch.setattr( - table_privileges, - 'replace_table_privileges_for_roles', - mock_replace_privileges - ) expect_response = [ {"role_oid": 12345, "direct": ["SELECT", "UPDATE", "DELETE"]}, {"role_oid": 67890, "direct": ["INSERT", "SELECT", "UPDATE", "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER"]} ] + mocked_exec_msar_func.fetchone.return_value = [expect_response] actual_response = table_privileges.replace_for_roles( privileges=_privileges, table_oid=_table_oid, database_id=_database_id, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] assert actual_response == expect_response + assert call_args[2] == _table_oid + assert call_args[3] == json.dumps(_privileges) -def test_transfer_table_ownership(rf, monkeypatch): +def test_transfer_table_ownership(rf, monkeypatch, mocked_exec_msar_func): _username = 'alice' _password = 'pass1234' _database_id = 2 @@ -137,37 +109,10 @@ def mock_connect(database_id, user): else: raise AssertionError('incorrect parameters passed') - def mock_tansfer_table_ownership( - table_oid, - new_owner_oid, - conn - ): - if table_oid != _table_oid and new_owner_oid != _new_owner_oid: - raise AssertionError('incorrect parameters passed') - return { - 'oid': table_oid, - 'name': 'x', - 'schema': 2200, - 'owner_oid': 2573031, - 'description': None, - 'current_role_owns': True, - 'current_role_priv': [ - 'SELECT', - 'INSERT', - 'UPDATE', - 'DELETE', - 'TRUNCATE', - 'REFERENCES', - 'TRIGGER' - ] - } - monkeypatch.setattr(table_privileges, 'connect', mock_connect) - monkeypatch.setattr( - table_privileges, - 'transfer_table_ownership', - mock_tansfer_table_ownership - ) table_privileges.transfer_ownership( table_oid=_table_oid, new_owner_oid=_new_owner_oid, database_id=_database_id, request=request ) + call_args = mocked_exec_msar_func.call_args_list[0][0] + assert call_args[2] == _table_oid + assert call_args[3] == _new_owner_oid