-
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3599 from mathesar-foundation/tables_list
Implement `tables.list` rpc endpoint
- Loading branch information
Showing
9 changed files
with
252 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Optional, TypedDict | ||
|
||
from modernrpc.core import rpc_method, REQUEST_KEY | ||
from modernrpc.auth.basic import http_basic_auth_login_required | ||
|
||
from db.tables.operations.select import get_table_info | ||
from mathesar.rpc.exceptions.handlers import handle_rpc_exceptions | ||
from mathesar.rpc.utils import connect | ||
|
||
|
||
class TableInfo(TypedDict): | ||
""" | ||
Information about a table. | ||
Attributes: | ||
oid: The `oid` of the table in the schema. | ||
name: The name of the table. | ||
schema: The `oid` of the schema where the table lives. | ||
description: The description of the table. | ||
""" | ||
oid: int | ||
name: str | ||
schema: int | ||
description: Optional[str] | ||
|
||
|
||
@rpc_method(name="tables.list") | ||
@http_basic_auth_login_required | ||
@handle_rpc_exceptions | ||
def list_(*, schema_oid: int, database_id: int, **kwargs) -> list[TableInfo]: | ||
""" | ||
List information about tables for a schema. Exposed as `list`. | ||
Args: | ||
schema_oid: Identity of the schema in the user's database. | ||
database_id: The Django id of the database containing the table. | ||
Returns: | ||
A list of table details. | ||
""" | ||
user = kwargs.get(REQUEST_KEY).user | ||
with connect(database_id, user) as conn: | ||
raw_table_info = get_table_info(schema_oid, conn) | ||
return [ | ||
TableInfo(tab) for tab in raw_table_info | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
This file tests the table RPC functions. | ||
Fixtures: | ||
rf(pytest-django): Provides mocked `Request` objects. | ||
monkeypatch(pytest): Lets you monkeypatch an object for testing. | ||
""" | ||
from contextlib import contextmanager | ||
|
||
from mathesar.rpc import tables | ||
from mathesar.models.users import User | ||
|
||
|
||
def test_tables_list(rf, monkeypatch): | ||
request = rf.post('/api/rpc/v0', data={}) | ||
request.user = User(username='alice', password='pass1234') | ||
schema_oid = 2200 | ||
database_id = 11 | ||
|
||
@contextmanager | ||
def mock_connect(_database_id, user): | ||
if _database_id == database_id and user.username == 'alice': | ||
try: | ||
yield True | ||
finally: | ||
pass | ||
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, 'connect', mock_connect) | ||
monkeypatch.setattr(tables, 'get_table_info', mock_table_info) | ||
expect_table_list = [ | ||
{ | ||
'oid': 17408, | ||
'name': 'Authors', | ||
'schema': schema_oid, | ||
'description': 'a description on the authors table.' | ||
}, | ||
{ | ||
'oid': 17809, | ||
'name': 'Books', | ||
'schema': schema_oid, | ||
'description': None | ||
} | ||
] | ||
actual_table_list = tables.list_(schema_oid=2200, database_id=11, request=request) | ||
assert actual_table_list == expect_table_list |