Skip to content

Commit 0e68cce

Browse files
committed
Search enhancements
1 parent 1f6a056 commit 0e68cce

4 files changed

Lines changed: 176 additions & 13 deletions

File tree

README.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ Note: This is currently a WIP so will be subject to breaking changes.
1010

1111
Change Log
1212
==========
13-
13+
14+
0.0.9
15+
-----
16+
17+
- Search query_by_kind function
18+
- Search query_by_id supports limit
19+
- Search query supports a specific query
20+
1421
0.0.8
1522
-----
1623

src/osdu/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
# license information.
55
# -----------------------------------------------------------------------------
66

7-
__VERSION__ = "0.0.8"
7+
__VERSION__ = "0.0.9"

src/osdu/search/_client.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,24 @@ def query_all_aggregated(self) -> dict:
6161
response_json = self._client.post_returning_json(self.api_url("query"), request_data)
6262
return response_json
6363

64-
def query(self, kind: str = None, identifier: str = None, limit: int = None) -> dict:
64+
def query(
65+
self, kind: str = None, identifier: str = None, query: str = None, limit: int = None
66+
) -> dict:
6567
"""Query records
6668
6769
Args:
6870
kind (str): kind to query for
6971
identifier (str): id to query for
72+
query (str): a specific query
73+
limit (str): limit on number of records to return
7074
7175
Returns:
7276
dict: containing the result
7377
"""
78+
79+
if identifier is not None and query is not None:
80+
raise ValueError("You can't specify both identifier and query")
81+
7482
request_data = {}
7583
if kind is None:
7684
request_data["kind"] = "*:*:*:*"
@@ -80,13 +88,16 @@ def query(self, kind: str = None, identifier: str = None, limit: int = None) ->
8088
if identifier is not None:
8189
request_data["query"] = f'id:("{identifier}")'
8290

91+
if query is not None:
92+
request_data["query"] = query
93+
8394
if limit is not None:
8495
request_data["limit"] = limit
8596

8697
response_json = self._client.post_returning_json(self.api_url("query"), request_data)
8798
return response_json
8899

89-
def query_by_id(self, identifier: str) -> dict:
100+
def query_by_id(self, identifier: str, limit: int = None) -> dict:
90101
"""Returns a list of all kinds including number of records
91102
92103
Args:
@@ -100,5 +111,25 @@ def query_by_id(self, identifier: str) -> dict:
100111
"query": f'id:("{identifier}")',
101112
}
102113

114+
if limit is not None:
115+
request_data["limit"] = limit
116+
117+
response_json = self._client.post_returning_json(self.api_url("query"), request_data)
118+
return response_json
119+
120+
def query_by_kind(self, kind: str, limit: int = None) -> dict:
121+
"""Returns a list of all records for the given kind
122+
123+
Args:
124+
kind (str): kind to query for
125+
126+
Returns:
127+
dict: containing the result
128+
"""
129+
request_data = {"kind": kind}
130+
131+
if limit is not None:
132+
request_data["limit"] = limit
133+
103134
response_json = self._client.post_returning_json(self.api_url("query"), request_data)
104135
return response_json

tests/search/test_search.py

Lines changed: 134 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,25 @@ def test_query_all_aggregated_http_error(self, _):
114114

115115
# region test query
116116
@params(
117-
("osdu:wks:dataset--File.Generic:1.0.0", None, None),
118-
("*:*:*:*", None, None),
119-
(None, None, None),
120-
(None, "opendes:reference-data--ResourceSecurityClassification:RESTRICTED", None),
121-
(None, "opendes:reference-data--ResourceSecurityClassification:RESTRICTED", 20),
122-
("*:*:*:*", "opendes:reference-data--ResourceSecurityClassification:RESTRICTED", None),
117+
("osdu:wks:dataset--File.Generic:1.0.0", None, None, None),
118+
("*:*:*:*", None, None, None),
119+
(None, None, None, None),
120+
(None, "opendes:reference-data--ResourceSecurityClassification:RESTRICTED", None, None),
121+
(None, "opendes:reference-data--ResourceSecurityClassification:RESTRICTED", None, 20),
122+
(
123+
None,
124+
None,
125+
'data.WellboreID:("opendes:master-data--Wellbore:ad215042-05db-2b7e-e053-c818a488c79a")',
126+
None,
127+
),
128+
(
129+
"*:*:*:*",
130+
"opendes:reference-data--ResourceSecurityClassification:RESTRICTED",
131+
None,
132+
None,
133+
),
123134
)
124-
def test_query(self, kind, identifier, limit):
135+
def test_query(self, kind, identifier, query, limit):
125136
"""Test the query function"""
126137
request_data = {}
127138

@@ -133,6 +144,9 @@ def test_query(self, kind, identifier, limit):
133144
if identifier is not None:
134145
request_data["query"] = f'id:("{identifier}")'
135146

147+
if query is not None:
148+
request_data["query"] = query
149+
136150
if limit is not None:
137151
request_data["limit"] = limit
138152

@@ -150,14 +164,22 @@ def test_query(self, kind, identifier, limit):
150164
client = create_dummy_client()
151165
search_client = SearchClient(client)
152166

153-
response_data = search_client.query(kind, identifier, limit)
167+
response_data = search_client.query(kind, identifier, query, limit)
154168

155169
mock_post_returning_json.assert_called_once()
156170
mock_post_returning_json.assert_called_with(
157171
"http://www.test.com/api/search/v2/query", request_data
158172
)
159173
self.assertEqual(expected_response_data, response_data)
160174

175+
def test_query_cant_pass_id_and_query(self):
176+
"""Test query with id and query faile"""
177+
with self.assertRaises(ValueError):
178+
client = create_dummy_client()
179+
search_client = SearchClient(client)
180+
181+
_ = search_client.query(identifier="id", query="query")
182+
161183
@mock.patch.object(OsduClient, "post_returning_json", side_effect=HTTPError(1))
162184
def test_query_http_error(self, _):
163185
"""Test query http errors are propogated"""
@@ -170,13 +192,47 @@ def test_query_http_error(self, _):
170192
# endregion test query
171193

172194
# region test query_by_id
195+
@params(
196+
("id1", None),
197+
("id1", 2),
198+
("id", None),
199+
("id", 2),
200+
)
201+
def test_query_by_id(self, identifier, limit):
202+
"""Test the query_by_id function"""
203+
request_data = {
204+
"kind": "*:*:*:*",
205+
"query": f'id:("{identifier}")',
206+
}
207+
if limit is not None:
208+
request_data["limit"] = limit
209+
210+
expected_response_data = {
211+
"results": [],
212+
"totalCount": 1,
213+
}
214+
with mock.patch(
215+
"osdu.client.OsduClient.post_returning_json", return_value=expected_response_data
216+
) as mock_post_returning_json:
217+
client = create_dummy_client()
218+
search_client = SearchClient(client)
219+
220+
response_data = search_client.query_by_id(identifier, limit)
221+
222+
mock_post_returning_json.assert_called_once()
223+
mock_post_returning_json.assert_called_with(
224+
"http://www.test.com/api/search/v2/query", request_data
225+
)
226+
self.assertEqual(expected_response_data, response_data)
227+
173228
@params("id1", "id")
174-
def test_query_by_id(self, identifier):
229+
def test_query_by_id_defaults(self, identifier):
175230
"""Test the query_by_id function"""
176231
request_data = {
177232
"kind": "*:*:*:*",
178233
"query": f'id:("{identifier}")',
179234
}
235+
180236
expected_response_data = {
181237
"results": [],
182238
"totalCount": 1,
@@ -206,6 +262,75 @@ def test_query_by_id_http_error(self, _):
206262

207263
# endregion test query_by_id
208264

265+
# region test query_by_kind
266+
@params(
267+
("kind1", None),
268+
("kind1", 2),
269+
("kind", None),
270+
("kind", 2),
271+
)
272+
def test_query_by_kind(self, kind, limit):
273+
"""Test the query_by_kind function"""
274+
request_data = {
275+
"kind": kind,
276+
}
277+
if limit is not None:
278+
request_data["limit"] = limit
279+
280+
expected_response_data = {
281+
"results": [],
282+
"totalCount": 1,
283+
}
284+
with mock.patch(
285+
"osdu.client.OsduClient.post_returning_json", return_value=expected_response_data
286+
) as mock_post_returning_json:
287+
client = create_dummy_client()
288+
search_client = SearchClient(client)
289+
290+
response_data = search_client.query_by_kind(kind, limit)
291+
292+
mock_post_returning_json.assert_called_once()
293+
mock_post_returning_json.assert_called_with(
294+
"http://www.test.com/api/search/v2/query", request_data
295+
)
296+
self.assertEqual(expected_response_data, response_data)
297+
298+
@params("kind1", "kind")
299+
def test_query_by_kind_defaults(self, kind):
300+
"""Test the query_by_kind function"""
301+
request_data = {
302+
"kind": kind,
303+
}
304+
305+
expected_response_data = {
306+
"results": [],
307+
"totalCount": 1,
308+
}
309+
with mock.patch(
310+
"osdu.client.OsduClient.post_returning_json", return_value=expected_response_data
311+
) as mock_post_returning_json:
312+
client = create_dummy_client()
313+
search_client = SearchClient(client)
314+
315+
response_data = search_client.query_by_kind(kind)
316+
317+
mock_post_returning_json.assert_called_once()
318+
mock_post_returning_json.assert_called_with(
319+
"http://www.test.com/api/search/v2/query", request_data
320+
)
321+
self.assertEqual(expected_response_data, response_data)
322+
323+
@mock.patch.object(OsduClient, "post_returning_json", side_effect=HTTPError(1))
324+
def test_query_by_kind_http_error(self, _):
325+
"""Test query_by_kind http errors are propogated"""
326+
with self.assertRaises(HTTPError):
327+
client = create_dummy_client()
328+
search_client = SearchClient(client)
329+
330+
_ = search_client.query_by_kind("kind")
331+
332+
# endregion test query_by_kind
333+
209334

210335
if __name__ == "__main__":
211336
import nose2

0 commit comments

Comments
 (0)