Skip to content

Commit 875010b

Browse files
committed
restriction query parameter handling and test
1 parent 2f65ada commit 875010b

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

pharus/component_interface.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""This module is a GUI component library of various common interfaces."""
2+
from base64 import b64decode
23
import json
34
import datajoint as dj
45
import re
@@ -126,9 +127,8 @@ def dj_query_route(self):
126127
record_header, table_records, total_count = _DJConnector._fetch_records(
127128
query=fetch_metadata["query"] & self.restriction,
128129
fetch_args=fetch_metadata["fetch_args"],
129-
limit=int(request.args["limit"]) if "limit" in request.args else 1000,
130-
page=int(request.args["page"]) if "page" in request.args else 1,
131130
)
131+
132132
return (
133133
NumpyEncoder.dumps(
134134
dict(
@@ -301,6 +301,9 @@ def dj_query_route(self):
301301
limit=int(request.args["limit"]) if "limit" in request.args else 1000,
302302
page=int(request.args["page"]) if "page" in request.args else 1,
303303
order=request.args["order"].split(",") if "order" in request.args else None,
304+
restriction=json.loads(b64decode(request.args["restriction"]))
305+
if "restriction" in request.args
306+
else [],
304307
)
305308

306309
return (

tests/test_api_gen.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from . import SCHEMA_PREFIX, token, client, connection, schemas_simple
2+
from base64 import b64encode
23
import json
34

45

@@ -130,3 +131,23 @@ def test_dynamic_restriction(token, client, schemas_simple):
130131
}
131132
)
132133
assert expected_json == json.dumps(REST_response.get_json(), sort_keys=True)
134+
135+
136+
def test_fetch_restriction(token, client, schemas_simple):
137+
restriction = [{"attributeName": "a_id", "operation": "=", "value": 1}]
138+
encoded = b64encode(json.dumps(restriction).encode("utf-8"))
139+
REST_response = client.get(
140+
f"/query1?restriction={encoded.decode()}",
141+
headers=dict(Authorization=f"Bearer {token}"),
142+
)
143+
# should restrict in the query parameter by a_id=1
144+
expected_json = json.dumps(
145+
{
146+
"recordHeader": ["a_id", "b_id", "a_name", "b_number"],
147+
"records": [
148+
[1, 21, "Bernie", 7.77],
149+
],
150+
"totalCount": 1,
151+
}
152+
)
153+
assert expected_json == json.dumps(REST_response.get_json(), sort_keys=True)

0 commit comments

Comments
 (0)