Convert on-object lists into properties. Add tests for these properties.

This commit is contained in:
Jason R. Coombs 2016-09-03 12:49:33 -04:00
parent 9115d23d83
commit d4e09bb905
3 changed files with 53 additions and 25 deletions

View File

@ -7,6 +7,8 @@
* ``Client.query`` now accepts keyword arguments * ``Client.query`` now accepts keyword arguments
and parameters passed directly to Wolfram|Alpha and parameters passed directly to Wolfram|Alpha
as URL parameters. as URL parameters.
* ``Result.pods`` now returns an iterator and not
a list.
2.4 2.4
=== ===

View File

@ -74,28 +74,31 @@ class Result(ErrorHandler, object):
def __init__(self, stream): def __init__(self, stream):
self.tree = xmltodict.parse(stream, dict_constructor=dict)['queryresult'] self.tree = xmltodict.parse(stream, dict_constructor=dict)['queryresult']
self._handle_error(self.tree) self._handle_error(self.tree)
self.info = []
try: @property
self.pods = list(map(Pod, self.tree['pod'])) def info(self):
self.info.append(self.pods) """
except KeyError: The pods, assumptions, and warnings of this result.
self.pods = None """
try: return itertools.chain(self.pods, self.assumptions, self.warnings)
self.assumptions = list(map(Assumption, self.tree['assumptions']))
self.info.append(self.assumptions) @property
except KeyError: def pods(self):
self.assumptions = None return map(Pod, self.tree.get('pod', []))
try:
self.warnings = list(map(Warning, self.tree['warnings'])) @property
self.info.append(self.warnings) def assumptions(self):
except KeyError: return map(Assumption, self.tree.get('assumptions', []))
self.warnings = None
@property
def warnings(self):
return map(Warning, self.tree.get('warnings', []))
def __iter__(self): def __iter__(self):
return iter(self.info) return self.info
def __len__(self): def __len__(self):
return len(self.info) return sum(1 for _ in self.info)
@property @property
def results(self): def results(self):

View File

@ -9,7 +9,7 @@ import pytest
import wolframalpha import wolframalpha
@pytest.fixture @pytest.fixture(scope='session')
def API_key(): def API_key():
""" """
To run the tests fully, the environment must be configured To run the tests fully, the environment must be configured
@ -22,26 +22,49 @@ def API_key():
pytest.skip("Need WOLFRAMALPHA_API_KEY in environment") pytest.skip("Need WOLFRAMALPHA_API_KEY in environment")
@pytest.fixture @pytest.fixture(scope='session')
def client(API_key): def client(API_key):
return wolframalpha.Client(API_key) return wolframalpha.Client(API_key)
def test_basic(client):
res = client.query('30 deg C in deg F') @pytest.fixture(scope='module')
assert len(res.pods) > 0 def temp_result(client):
return client.query('30 deg C in deg F')
def test_basic(temp_result):
res = temp_result
assert len(list(res.pods)) > 0
result, = res.results result, = res.results
assert result.text == '86 °F (degrees Fahrenheit)' assert result.text == '86 °F (degrees Fahrenheit)'
assert result.texts == ['86 °F (degrees Fahrenheit)'] assert result.texts == ['86 °F (degrees Fahrenheit)']
def test_results_iterator(client): def test_results_iterator(temp_result):
""" """
A Result.results should be an iterator. A Result.results should be an iterator.
""" """
res = client.query('30 deg C in deg F') res = temp_result
next(res.results) next(res.results)
def test_properties(temp_result):
"""
A result should have a number of properties.
"""
res = temp_result
info = list(res.info)
warnings = list(res.warnings)
assert all(isinstance(item, wolframalpha.Warning) for item in warnings)
assumptions = list(res.assumptions)
assert all(
isinstance(item, wolframalpha.Assumption)
for item in assumptions
)
pods = list(res.pods)
assert all(isinstance(item, wolframalpha.Pod) for item in pods)
assert len(info) == len(pods) + len(warnings) + len(assumptions)
def test_invalid_app_id(): def test_invalid_app_id():
client = wolframalpha.Client('abcdefg') client = wolframalpha.Client('abcdefg')
with pytest.raises(Exception): with pytest.raises(Exception):