Skip to content

Commit

Permalink
Merge pull request #4015 from mathesar-foundation/mock_exec_msar
Browse files Browse the repository at this point in the history
Use mocked `exec_msar_func` calls for RPC tests
  • Loading branch information
mathemancer authored Nov 8, 2024
2 parents e5e0025 + d056112 commit e83db0c
Show file tree
Hide file tree
Showing 20 changed files with 368 additions and 686 deletions.
10 changes: 5 additions & 5 deletions db/columns.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]
9 changes: 4 additions & 5 deletions db/constraints.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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]

Expand All @@ -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]
7 changes: 4 additions & 3 deletions db/databases.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions db/links.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -51,7 +51,7 @@ def add_mapping_table(
The elements of the mapping_columns list must have the form
{"column_name": <str>, "referent_table_oid": <int>}
"""
exec_msar_func(
db_conn.exec_msar_func(
conn,
'add_mapping_table',
schema_oid,
Expand Down
30 changes: 15 additions & 15 deletions db/roles.py
Original file line number Diff line number Diff line change
@@ -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]
12 changes: 6 additions & 6 deletions db/schemas.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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()
24 changes: 12 additions & 12 deletions db/tables.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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]

Expand All @@ -67,7 +67,7 @@ def alter_table_on_database(table_oid, table_data_dict, conn):
"columns": <list> 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]

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]

Expand All @@ -181,15 +181,15 @@ 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]


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,
Expand All @@ -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,
Expand Down
Loading

0 comments on commit e83db0c

Please sign in to comment.