Skip to content

Commit

Permalink
[#161] Fix: Client.ask zero result case
Browse files Browse the repository at this point in the history
When there's zero results, the method should not yield an empty
dictionary. Otherwise counting number of results could be wrong (think
`len(list(site.ask('...')))`)
  • Loading branch information
danmichaelo committed May 17, 2017
1 parent d4ba7c1 commit dc1dcf7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
30 changes: 12 additions & 18 deletions mwclient/client.py
Expand Up @@ -976,19 +976,17 @@ def ask(self, query, title=None):
API doc: https://semantic-mediawiki.org/wiki/Ask_API
Returns:
Generator for retrieving all search results.
Iterating over the generator gives a dictionary containing the answers
to the query.
The dictionary is empty if the query is valid but there are no answers.
Generator for retrieving all search results, with each answer as a dictionary.
If the query is invalid, an APIError is raised. A valid query with zero
results will not raise any error.
Examples:
>>> query = "[[Category:my cat]]|[[Has name::a name]]|?Has property"
>>> answer = site.ask(query)
>>> for a in answer:
>>> for key, value in a.items()
>>> print(key)
>>> print(value)
>>> for answer in site.ask(query):
>>> for title, data in answer.items()
>>> print(title)
>>> print(data)
"""
kwargs = {}
if title is None:
Expand All @@ -998,12 +996,8 @@ def ask(self, query, title=None):
while offset is not None:
results = self.raw_api('ask', query=u'{query}|offset={offset}'.format(
query=query, offset=offset), http_method='GET', **kwargs)
if self.handle_api_result(results):
answer = results['query'].get('results')
if answer:
offset = results.get('query-continue-offset')
for key, value in answer.items():
yield {key: value}
else: # query returns no results
offset = None
yield {}
self.handle_api_result(results) # raises APIError on error
offset = results.get('query-continue-offset')
answers = results['query'].get('results') or {}
for key, value in answers.items():
yield {key: value}
4 changes: 2 additions & 2 deletions tests/test_client.py
Expand Up @@ -299,14 +299,14 @@ def test_smw_error_response(self):
site = self.stdSetup()
self.httpShouldReturn(json.dumps({
'error': {
'query': 'Certains « <nowiki>[[</nowiki> » dans votre requête n’ont pas été clos par des « ]] » correspondants.'
'query': u'Certains « <nowiki>[[</nowiki> » dans votre requête n’ont pas été clos par des « ]] » correspondants.'
}
}), method='GET')
with pytest.raises(mwclient.errors.APIError) as excinfo:
list(site.ask('test'))

assert excinfo.value.code is None
assert excinfo.value.info == 'Certains « <nowiki>[[</nowiki> » dans votre requête n’ont pas été clos par des « ]] » correspondants.'
assert excinfo.value.info == u'Certains « <nowiki>[[</nowiki> » dans votre requête n’ont pas été clos par des « ]] » correspondants.'
assert len(responses.calls) == 1

@responses.activate
Expand Down

0 comments on commit dc1dcf7

Please sign in to comment.