Skip to content

Commit

Permalink
Fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vanatteveldt committed Nov 16, 2023
1 parent 79d9e02 commit 96e0034
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 82 deletions.
5 changes: 4 additions & 1 deletion amcat4/api/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def _check_query_role(
role = Role.READER
else:
role = Role.METAREADER
print("!!!", highlight, role)
for ix in indices:
check_role(user, role, ix)

Expand Down Expand Up @@ -93,6 +92,8 @@ def get_documents(
"""
indices = index.split(",")
fields = fields and fields.split(",")
if not fields:
fields = ["date", "title", "url"]
_check_query_role(indices, user, fields, highlight)
args = {}
sort = sort and [
Expand Down Expand Up @@ -242,6 +243,8 @@ def query_documents_post(
# to array format: fields: [field1, field2]
if isinstance(fields, str):
fields = [fields]
else:
fields = ["date", "title", "url"]
_check_query_role(indices, user, fields, highlight is not None)

queries = _process_queries(queries)
Expand Down
25 changes: 12 additions & 13 deletions tests/test_api_documents.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from turtle import pos
from amcat4.index import set_role, Role
from tests.conftest import index_docs
from tests.conftest import index_docs, populate_index
from tests.tools import post_json, build_headers, get_json, check


Expand Down Expand Up @@ -52,18 +52,17 @@ def test_documents(client, index, user):


def test_metareader(client, index, index_docs, user, reader):
set_role(index_docs, user, Role.METAREADER)
set_role(index_docs, reader, Role.READER)
set_role(index, user, Role.READER)
set_role(index, user, Role.METAREADER)
set_role(index, reader, Role.READER)
populate_index(index)

r = get_json(
client,
f"/index/{index_docs}/documents?fields=title",
f"/index/{index}/documents?fields=title",
headers=build_headers(user),
)
_id = r["results"][0]["_id"]
url = f"index/{index_docs}/documents/{_id}"
url = f"index/{index}/documents/{_id}"
# Metareader should not be able to retrieve document source
check(client.get(url, headers=build_headers(user)), 401)
check(client.get(url, headers=build_headers(reader)), 200)
Expand All @@ -73,15 +72,15 @@ def get_join(x):

# Metareader should not be able to query text (including highlight)
for ix, u, fields, highlight, outcome in [
(index_docs, user, ["text"], False, 401),
(index, user, ["text"], False, 200),
(index, user, ["text"], False, 401),
(index_docs, user, ["text"], False, 200),
([index_docs, index], user, ["text"], False, 401),
(index_docs, user, ["text", "title"], False, 401),
(index_docs, user, ["title"], False, 200),
(index_docs, reader, ["text"], False, 200),
(index, user, ["text", "title"], False, 401),
(index, user, ["title"], False, 200),
(index, reader, ["text"], False, 200),
([index_docs, index], reader, ["text"], False, 200),
(index_docs, user, ["title"], True, 401),
(index_docs, reader, ["title"], True, 200),
(index, user, ["title"], True, 401),
(index, reader, ["title"], True, 200),
]:
check(
client.get(
Expand Down
48 changes: 41 additions & 7 deletions tests/test_api_pagination.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,56 @@
from amcat4.index import Role, set_role
from tests.conftest import upload
from tests.tools import get_json, post_json


def test_pagination(client, index, user):
"""Does basic pagination work?"""
set_role(index, user, Role.METAREADER)

upload(index, docs=[{"i": i} for i in range(66)])
url = f"/index/{index}/documents"
r = get_json(client, url, user=user, params={"sort": "i", "per_page": 20})
r = get_json(
client, url, user=user, params={"sort": "i", "per_page": 20, "fields": ["i"]}
)
assert r["meta"]["per_page"] == 20
assert r["meta"]["page"] == 0
assert r["meta"]["page_count"] == 4
assert {h["i"] for h in r["results"]} == set(range(20))
r = get_json(client, url, user=user, params={"sort": "i", "per_page": 20, "page": 3})
r = get_json(
client,
url,
user=user,
params={"sort": "i", "per_page": 20, "page": 3, "fields": ["i"]},
)
assert r["meta"]["page"] == 3
assert {h["i"] for h in r["results"]} == {60, 61, 62, 63, 64, 65}
r = get_json(client, url, user=user, params={"sort": "i", "per_page": 20, "page": 4})
r = get_json(
client, url, user=user, params={"sort": "i", "per_page": 20, "page": 4}
)
assert len(r["results"]) == 0
# Test POST query

r = post_json(client, f"/index/{index}/query", expected=200, user=user, json={"sort": "i", "per_page": 20, "page": 3})
r = post_json(
client,
f"/index/{index}/query",
expected=200,
user=user,
json={"sort": "i", "per_page": 20, "page": 3, "fields": ["i"]},
)
assert r["meta"]["page"] == 3
assert {h["i"] for h in r["results"]} == {60, 61, 62, 63, 64, 65}


def test_scroll(client, index, user):
set_role(index, user, Role.METAREADER)
upload(index, docs=[{"i": i} for i in range(66)])
url = f"/index/{index}/documents"
r = get_json(client, url, user=user, params={"sort": "i:desc", "per_page": 30, "scroll": "5m"})
r = get_json(
client,
url,
user=user,
params={"sort": "i:desc", "per_page": 30, "scroll": "5m", "fields": ["i"]},
)
scroll_id = r["meta"]["scroll_id"]
assert scroll_id is not None
assert {h["i"] for h in r["results"]} == set(range(36, 66))
Expand All @@ -38,8 +62,18 @@ def test_scroll(client, index, user):
# Scrolling past the edge should return 404
get_json(client, url, user=user, params={"scroll_id": scroll_id}, expected=404)
# Test POST to query endpoint
r = post_json(client, f"/index/{index}/query", user=user, expected=200,
json={"sort": [{"i": {"order": "desc"}}], "per_page": 30, "scroll": "5m"})
r = post_json(
client,
f"/index/{index}/query",
user=user,
expected=200,
json={
"sort": [{"i": {"order": "desc"}}],
"per_page": 30,
"scroll": "5m",
"fields": ["i"],
},
)
scroll_id = r["meta"]["scroll_id"]
assert scroll_id is not None
assert {h["i"] for h in r["results"]} == set(range(36, 66))
Loading

0 comments on commit 96e0034

Please sign in to comment.