| 1 |
import json |
| 2 |
import urllib.parse |
| 3 |
import uuid |
| 4 |
|
| 5 |
import pytest |
| 6 |
import responses |
| 7 |
|
| 8 |
import globus_sdk |
| 9 |
from globus_sdk._missing import filter_missing |
| 10 |
from globus_sdk.testing import get_last_request, load_response |
| 11 |
from tests.common import register_api_route_fixture_file |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture |
| 15 |
def search_client(): |
| 16 |
client = globus_sdk.SearchClient() |
| 17 |
with client.retry_config.tune(max_retries=0): |
| 18 |
yield client |
| 19 |
|
| 20 |
|
| 21 |
def test_search_query_simple(search_client): |
| 22 |
meta = load_response(search_client.search).metadata |
| 23 |
|
| 24 |
res = search_client.search(meta["index_id"], q="foo") |
| 25 |
assert res.http_status == 200 |
| 26 |
|
| 27 |
data = res.data |
| 28 |
assert isinstance(data, dict) |
| 29 |
assert data["gmeta"][0]["entries"][0]["content"]["foo"] == "bar" |
| 30 |
|
| 31 |
req = get_last_request() |
| 32 |
assert req.body is None |
| 33 |
parsed_qs = urllib.parse.parse_qs(urllib.parse.urlparse(req.url).query) |
| 34 |
assert parsed_qs == {"q": ["foo"]} |
| 35 |
|
| 36 |
|
| 37 |
@pytest.mark.parametrize("query_doc", [{"q": "foo"}, {"q": "foo", "limit": 10}]) |
| 38 |
def test_search_post_query_simple(search_client, query_doc): |
| 39 |
meta = load_response(search_client.post_search).metadata |
| 40 |
|
| 41 |
res = search_client.post_search(meta["index_id"], query_doc) |
| 42 |
assert res.http_status == 200 |
| 43 |
|
| 44 |
data = res.data |
| 45 |
assert isinstance(data, dict) |
| 46 |
assert data["gmeta"][0]["entries"][0]["content"]["foo"] == "bar" |
| 47 |
|
| 48 |
req = get_last_request() |
| 49 |
assert req.body is not None |
| 50 |
req_body = json.loads(req.body) |
| 51 |
assert req_body == dict(query_doc) |
| 52 |
|
| 53 |
|
| 54 |
def test_search_post_query_simple_with_v1_helper(search_client): |
| 55 |
query_doc = globus_sdk.SearchQueryV1(q="foo") |
| 56 |
meta = load_response(search_client.post_search).metadata |
| 57 |
|
| 58 |
res = search_client.post_search(meta["index_id"], query_doc) |
| 59 |
assert res.http_status == 200 |
| 60 |
|
| 61 |
data = res.data |
| 62 |
assert isinstance(data, dict) |
| 63 |
assert data["gmeta"][0]["entries"][0]["content"]["foo"] == "bar" |
| 64 |
|
| 65 |
req = get_last_request() |
| 66 |
assert req.body is not None |
| 67 |
req_body = json.loads(req.body) |
| 68 |
assert req_body == {"@version": "query#1.0.0", "q": "foo"} |
| 69 |
|
| 70 |
|
| 71 |
@pytest.mark.parametrize("doc_type", ("dict", "helper")) |
| 72 |
def test_search_post_query_arg_overrides(search_client, doc_type): |
| 73 |
meta = load_response(search_client.post_search).metadata |
| 74 |
|
| 75 |
if doc_type == "dict": |
| 76 |
query_doc = {"q": "foo", "limit": 10, "offset": 0} |
| 77 |
elif doc_type == "helper": |
| 78 |
query_doc = globus_sdk.SearchQueryV1(q="foo", limit=10, offset=0) |
| 79 |
else: |
| 80 |
raise NotImplementedError(doc_type) |
| 81 |
res = search_client.post_search(meta["index_id"], query_doc, limit=100, offset=150) |
| 82 |
assert res.http_status == 200 |
| 83 |
|
| 84 |
data = res.data |
| 85 |
assert isinstance(data, dict) |
| 86 |
assert data["gmeta"][0]["entries"][0]["content"]["foo"] == "bar" |
| 87 |
|
| 88 |
req = get_last_request() |
| 89 |
assert req.body is not None |
| 90 |
req_body = json.loads(req.body) |
| 91 |
assert req_body != dict(query_doc) |
| 92 |
assert req_body["q"] == query_doc["q"] |
| 93 |
assert req_body["limit"] == 100 |
| 94 |
assert req_body["offset"] == 150 |
| 95 |
# important! these should be unchanged (no side-effects) |
| 96 |
assert query_doc["limit"] == 10 |
| 97 |
assert query_doc["offset"] == 0 |
| 98 |
|
| 99 |
|
| 100 |
@pytest.mark.parametrize( |
| 101 |
"query_doc", |
| 102 |
[ |
| 103 |
{"q": "foo"}, |
| 104 |
globus_sdk.SearchScrollQuery("foo"), |
| 105 |
], |
| 106 |
) |
| 107 |
def test_search_paginated_scroll_query(search_client, query_doc): |
| 108 |
index_id = str(uuid.uuid1()) |
| 109 |
register_api_route_fixture_file( |
| 110 |
"search", |
| 111 |
f"/v1/index/{index_id}/scroll", |
| 112 |
"scroll_result_1.json", |
| 113 |
method="POST", |
| 114 |
match=[responses.matchers.json_params_matcher({"q": "foo"})], |
| 115 |
) |
| 116 |
register_api_route_fixture_file( |
| 117 |
"search", |
| 118 |
f"/v1/index/{index_id}/scroll", |
| 119 |
"scroll_result_2.json", |
| 120 |
method="POST", |
| 121 |
match=[ |
| 122 |
responses.matchers.json_params_matcher( |
| 123 |
{"q": "foo", "marker": "3d34900e3e4211ebb0a806b2af333354"} |
| 124 |
) |
| 125 |
], |
| 126 |
) |
| 127 |
|
| 128 |
data = list(search_client.paginated.scroll(index_id, query_doc).items()) |
| 129 |
assert len(responses.calls) == 2 |
| 130 |
assert len(data) == 2 |
| 131 |
|
| 132 |
assert isinstance(data[0], dict) |
| 133 |
assert data[0]["entries"][0]["content"]["foo"] == "bar" |
| 134 |
|
| 135 |
assert isinstance(data[1], dict) |
| 136 |
assert data[1]["entries"][0]["content"]["foo"] == "baz" |
| 137 |
|
| 138 |
# confirm that pagination was not side-effecting |
| 139 |
assert "marker" not in filter_missing(query_doc) |