-
Notifications
You must be signed in to change notification settings - Fork 16
Add support for filtering and proper pagination #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cd1b46f
Add support for filtering and proper pagination.
guzman-raphael eaf9c94
Return total query count to facilate paging.
guzman-raphael 2f3f01e
Bump the complexity.
guzman-raphael a1acfe9
Update docs and fix bug with unrestricted queries.
guzman-raphael b4c141d
Adjust to expect string for all values except NULL and update tests.
guzman-raphael 4cb1120
Optimize by way of reduce.
guzman-raphael 06ac74e
Remove unnecessary import.
guzman-raphael 17ffca3
Modify name to be more generic in context.
guzman-raphael 9c11258
Resolve merge conflicts.
guzman-raphael File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| pytest | ||
| pytest-cov | ||
| flake8 | ||
| Faker |
This file contains hidden or 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,112 @@ | ||
| from os import getenv | ||
| import pytest | ||
| from dj_gui_api_server.DJGUIAPIServer import app | ||
| import datajoint as dj | ||
| from json import dumps | ||
| from base64 import b64encode | ||
| from urllib.parse import urlencode | ||
| from datetime import date, datetime | ||
| from random import randint, choice, seed, getrandbits | ||
| from faker import Faker | ||
| seed('lock') # Pin down randomizer between runs | ||
| faker = Faker() | ||
| Faker.seed(0) # Pin down randomizer between runs | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def client(): | ||
| with app.test_client() as client: | ||
| yield client | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def token(client): | ||
| yield client.post('/api/login', json=dict(databaseAddress=getenv('TEST_DB_SERVER'), | ||
| username=getenv('TEST_DB_USER'), | ||
| password=getenv('TEST_DB_PASS'))).json['jwt'] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def virtual_module(): | ||
| dj.config['safemode'] = False | ||
| connection = dj.conn(host=getenv('TEST_DB_SERVER'), | ||
| user=getenv('TEST_DB_USER'), | ||
| password=getenv('TEST_DB_PASS'), reset=True) | ||
| schema = dj.Schema('filter') | ||
|
|
||
| @schema | ||
| class Student(dj.Lookup): | ||
| definition = """ | ||
| student_id: int | ||
| --- | ||
| student_name: varchar(50) | ||
| student_ssn: varchar(20) | ||
| student_enroll_date: datetime | ||
| student_balance: float | ||
| student_parking_lot=null : varchar(20) | ||
| student_out_of_state: bool | ||
| """ | ||
| contents = [(i, faker.name(), faker.ssn(), faker.date_between_dates( | ||
| date_start=date(2021, 1, 1), date_end=date(2021, 1, 31)), | ||
| round(randint(1000, 3000), 2), | ||
| choice([None, 'LotA', 'LotB', 'LotC']), | ||
| bool(getrandbits(1))) for i in range(100)] | ||
|
|
||
| yield dj.VirtualModule('filter', 'filter') | ||
| schema.drop() | ||
| connection.close() | ||
| dj.config['safemode'] = True | ||
|
|
||
|
|
||
| def test_filters(token, client, virtual_module): | ||
| # 'between' dates | ||
| restriction = [dict(attributeName='student_enroll_date', operation='>', | ||
| value='2021-01-07'), | ||
| dict(attributeName='student_enroll_date', operation='<', | ||
| value='2021-01-17')] | ||
| encoded_restriction = b64encode(dumps(restriction).encode('utf-8')).decode('utf-8') | ||
| q = dict(limit=10, page=1, order='student_enroll_date DESC', | ||
| restriction=encoded_restriction) | ||
| REST_records = client.post(f'/api/fetch_tuples?{urlencode(q)}', | ||
| headers=dict(Authorization=f'Bearer {token}'), | ||
| json=dict(schemaName='filter', | ||
| tableName='Student')).json['tuples'] | ||
| assert len(REST_records) == 10 | ||
| assert REST_records[0][3] == datetime(2021, 1, 16).timestamp() | ||
| # 'equal' null | ||
| restriction = [dict(attributeName='student_parking_lot', operation='=', value=None)] | ||
| encoded_restriction = b64encode(dumps(restriction).encode('utf-8')).decode('utf-8') | ||
| q = dict(limit=10, page=2, order='student_id ASC', | ||
| restriction=encoded_restriction) | ||
| REST_records = client.post(f'/api/fetch_tuples?{urlencode(q)}', | ||
| headers=dict(Authorization=f'Bearer {token}'), | ||
| json=dict(schemaName='filter', | ||
| tableName='Student')).json['tuples'] | ||
| assert len(REST_records) == 10 | ||
| assert all([r[5] is None for r in REST_records]) | ||
| assert REST_records[0][0] == 34 | ||
| # not equal int | ||
| restriction = [dict(attributeName='student_id', operation='!=', value='2')] | ||
| encoded_restriction = b64encode(dumps(restriction).encode('utf-8')).decode('utf-8') | ||
| q = dict(limit=10, page=1, order='student_id ASC', | ||
| restriction=encoded_restriction) | ||
| REST_records = client.post(f'/api/fetch_tuples?{urlencode(q)}', | ||
| headers=dict(Authorization=f'Bearer {token}'), | ||
| json=dict(schemaName='filter', | ||
| tableName='Student')).json['tuples'] | ||
| assert len(REST_records) == 10 | ||
| assert all([r[0] != 2 for r in REST_records]) | ||
| assert REST_records[-1][0] == 10 | ||
| # equal 'Norma Fisher' and in_state student (bool) | ||
| restriction = [dict(attributeName='student_name', operation='=', value='Norma Fisher'), | ||
| dict(attributeName='student_out_of_state', operation='=', value='0')] | ||
| encoded_restriction = b64encode(dumps(restriction).encode('utf-8')).decode('utf-8') | ||
| q = dict(limit=10, page=1, order='student_id ASC', | ||
| restriction=encoded_restriction) | ||
| REST_records = client.post(f'/api/fetch_tuples?{urlencode(q)}', | ||
| headers=dict(Authorization=f'Bearer {token}'), | ||
| json=dict(schemaName='filter', | ||
| tableName='Student')).json['tuples'] | ||
| assert len(REST_records) == 1 | ||
| assert REST_records[0][1] == 'Norma Fisher' | ||
| assert REST_records[0][6] == 0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.