Fixing bug in archives search where offset could not be greater than 100. (#117)

This commit is contained in:
lrnselfreliance 2022-04-14 09:21:15 -06:00 • committed by Roland
parent 372056eefd
commit fda8636c92
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
3 changed files with 40 additions and 22 deletions

View file

@ -41,7 +41,6 @@ async def fetch_domains(_: Request):
archive_limit_limiter = api_param_limiter(100)
archive_offset_limiter = api_param_limiter(100, 0)
@bp.post('/search')
@ -56,7 +55,7 @@ async def search_archives(_: Request, body: schema.ArchiveSearchRequest):
search_str = body.search_str
domain = body.domain
limit = archive_limit_limiter(body.limit)
offset = archive_offset_limiter(body.offset)
offset = body.offset or 0
archives, total = lib.search(search_str, domain, limit, offset)
ret = dict(archives=archives, totals=dict(archives=total))

View file

@ -579,11 +579,14 @@ def search(search_str: str, domain: str, limit: int, offset: int) -> Tuple[List[
columns = 'id, COUNT(*) OVER() AS total'
params = dict(offset=offset, limit=limit)
wheres = ''
order_by = '1 DESC'
if search_str:
columns = 'id, ts_rank_cd(textsearch, websearch_to_tsquery(%(search_str)s)), COUNT(*) OVER() AS total'
params['search_str'] = search_str
wheres += '\nAND textsearch @@ websearch_to_tsquery(%(search_str)s)'
# highest rank, then most recent
order_by = '2 DESC, archive_datetime DESC'
if domain:
curs.execute('SELECT id FROM domains WHERE domain=%s', (domain,))
@ -602,10 +605,11 @@ def search(search_str: str, domain: str, limit: int, offset: int) -> Tuple[List[
WHERE
singlefile_path IS NOT NULL AND singlefile_path != ''
{wheres}
ORDER BY 2 DESC, archive_datetime DESC -- highest rank, then most recent
ORDER BY {order_by}
OFFSET %(offset)s
LIMIT %(limit)s
'''
print(stmt, offset, limit)
curs.execute(stmt, params)
results = [dict(i) for i in curs.fetchall()]
total = results[0]['total'] if results else 0

View file

@ -2,57 +2,72 @@ import json
from http import HTTPStatus
def check_results(test_client, data, ids):
request, response = test_client.post('/api/archive/search', content=json.dumps(data))
assert response.status_code == HTTPStatus.OK, response.json
assert [i['id'] for i in response.json['archives']] == ids, \
f'{response.json["archives"][0]["id"]}..{response.json["archives"][19]["id"]}'
def test_archives_search(test_session, archive_directory, archive_factory, test_client):
"""
Archives can be searched by their title and their contents.
"""
"""Archives can be searched by their title and their contents."""
archive_factory('example.com', 'https://example.com/one', 'my archive', 'foo bar qux')
archive_factory('example.com', 'https://example.com/one', 'other archive', 'foo baz qux qux')
archive_factory('example.org', title='archive third', contents='baz qux qux qux')
archive_factory('example.org') # has no contents
def check_results(data, ids):
request, response = test_client.post('/api/archive/search', content=json.dumps(data))
assert response.status_code == HTTPStatus.OK, response.json
assert [i['id'] for i in response.json['archives']] == ids
# 1 and 2 contain "foo".
data = {'search_str': 'foo'}
check_results(data, [2, 1])
check_results(test_client, data, [2, 1])
# 2 and 3 contain "baz".
data = {'search_str': 'baz'}
check_results(data, [3, 2])
check_results(test_client, data, [3, 2])
# 1 contains "bar".
data = {'search_str': 'bar'}
check_results(data, [1, ])
check_results(test_client, data, [1, ])
# No archives contain "huzzah"
data = {'search_str': 'huzzah'}
check_results(data, [])
check_results(test_client, data, [])
# Only 3 contains "baz" and is in domain "example.org"
data = {'search_str': 'baz', 'domain': 'example.org'}
check_results(data, [3, ])
check_results(test_client, data, [3, ])
# 1's title contains "my", this is ignored by Postgres.
data = {'search_str': 'my'}
check_results(data, [])
check_results(test_client, data, [])
# 3's title contains "third".
data = {'search_str': 'third'}
check_results(data, [3, ])
check_results(test_client, data, [3, ])
# All titles contain "qux", but they contain different amounts. They are ordered by the amount.
data = {'search_str': 'qux'}
check_results(data, [3, 2, 1])
check_results(test_client, data, [3, 2, 1])
def test_search_offset(archive_factory, test_client):
"""Archive search can be offset."""
for i in range(500):
archive_factory('example.com', f'https://example.com/{i}', contents='foo bar')
data = {'search_str': None, 'offset': 0}
check_results(test_client, data, list(range(500, 480, -1)))
data = {'search_str': None, 'offset': 20}
check_results(test_client, data, list(range(480, 460, -1)))
data = {'search_str': None, 'offset': 100}
check_results(test_client, data, list(range(400, 380, -1)))
data = {'search_str': None, 'offset': 200}
check_results(test_client, data, list(range(300, 280, -1)))
data = {'search_str': None, 'offset': 500}
check_results(test_client, data, [])
def test_archives_search_no_query(archive_factory, test_client):
"""
Archive Search API endpoint does not require data in the body.
"""
"""Archive Search API endpoint does not require data in the body."""
# Add 100 random archives.
for _ in range(100):
archive_factory('example.com')