Skip to content

Commit

Permalink
Moved to nose2 for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aolieman committed Jul 18, 2016
1 parent e3b690c commit 6f8b5f3
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 95 deletions.
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-r requirements.txt
nose2>=0.6.5
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# Testing also requires nose>=1.2.1
requests>=2.10.0
180 changes: 86 additions & 94 deletions spotlight/tests.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
SKIP_ORDERED_DICT_TESTS = False
try:
from collections import OrderedDict
except ImportError:
SKIP_ORDERED_DICT_TESTS = True
import sys
sys.stderr.write('Skipping _dict_cleanup due to OrderedDict not being '
'available.\n')

from collections import namedtuple
from nose.tools import eq_, nottest, raises

from collections import namedtuple, OrderedDict
from nose2.tools.such import helper
import spotlight


@nottest
# Expose unittest assertions to function-style tests
assert_equals = helper.assertEquals
assert_raises = helper.assertRaises


def fake_request_post(self, *args, **kwargs):
RawResponse = namedtuple('RawResponse', ['reason', ])
hear_me_RawR = RawResponse(reason='Just a fake reason.')
Expand All @@ -32,62 +26,62 @@ def raise_for_status(self):


def test_number_convert():
eq_(spotlight._convert_number('0'), 0)
eq_(spotlight._convert_number('0.2'), 0.2)
eq_(spotlight._convert_number(True), True)
eq_(spotlight._convert_number('evi'), 'evi')
assert_equals(spotlight._convert_number('0'), 0)
assert_equals(spotlight._convert_number('0.2'), 0.2)
assert_equals(spotlight._convert_number(True), True)
assert_equals(spotlight._convert_number('evi'), 'evi')
# Testing the footnote workaround.
eq_(spotlight._convert_number([1]), '[1]')
assert_equals(spotlight._convert_number([1]), '[1]')


@raises(spotlight.SpotlightException)
def test_protocol_missing():
spotlight.annotate('localhost', 'asdasdasd',
headers={'fake_response': 'invalid json',
'fake_status': 502})
with assert_raises(spotlight.SpotlightException):
spotlight.annotate('localhost', 'asdasdasd',
headers={'fake_response': b'invalid json',
'fake_status': 502})


@raises(spotlight.requests.exceptions.HTTPError)
def test_http_fail():
spotlight.annotate('http://localhost', 'asdasdasd',
headers={'fake_response': 'invalid json',
'fake_status': 502})
with assert_raises(spotlight.requests.exceptions.HTTPError):
spotlight.annotate('http://localhost', 'asdasdasd',
headers={'fake_response': b'invalid json',
'fake_status': 502})


@raises(ValueError)
def test_annotation_invalid_json():
spotlight.annotate('http://localhost', 'asdasdasd',
headers={'fake_response': 'invalid json'})
with assert_raises(ValueError):
spotlight.annotate('http://localhost', 'asdasdasd',
headers={'fake_response': b'invalid json'})


@raises(spotlight.SpotlightException)
def test_missing_resources():
spotlight.annotate('http://localhost', 'asdasdasd',
headers={'fake_response': '{"Test": "Win"}'})
with assert_raises(spotlight.SpotlightException):
spotlight.annotate('http://localhost', 'asdasdasd',
headers={'fake_response': b'{"Test": "Win"}'})


@raises(ValueError)
def test_candidates_invalid_json():
spotlight.annotate('http://localhost', 'asdasdasd',
headers={'fake_response': 'invalid json'})
with assert_raises(ValueError):
spotlight.annotate('http://localhost', 'asdasdasd',
headers={'fake_response': b'invalid json'})


@raises(spotlight.SpotlightException)
def test_missing_annotation():
spotlight.candidates('http://localhost', 'asdasdasd',
headers={'fake_response': '{"Test": "Win"}'})
with assert_raises(spotlight.SpotlightException):
spotlight.candidates('http://localhost', 'asdasdasd',
headers={'fake_response': b'{"Test": "Win"}'})


@raises(spotlight.SpotlightException)
def test_missing_surfaceForms():
spotlight.candidates('http://localhost', 'asdasdasd',
headers={'fake_response': '{"annotation": {"Test": "Win"}}'})
with assert_raises(spotlight.SpotlightException):
spotlight.candidates('http://localhost', 'asdasdasd',
headers={'fake_response': b'{"annotation": {"Test": "Win"}}'})


def test_single_candidate():
# Test with a single returned candidate, as was reported by issue #3.
# Thanks to aolieman for the awesome test data!
data = """
data = b"""
{
"annotation":{
"@text":"Industrial Design at the Technische Universiteit Delft",
Expand Down Expand Up @@ -123,61 +117,59 @@ def test_single_candidate():
candidates = spotlight.candidates('http://localhost', 'asdasdasd',
headers={'fake_response': data})
expected_out = [
{u'resource':
[
{
'resource': [
{
u'finalScore': 0.8754365122251001,
u'support': 3,
u'uri': u'Technische_Universiteit_Delft',
u'label': u'Technische Universiteit Delft',
u'types': u'',
u'percentageOfSecondRank': 0.1422872887244497,
u'priorScore': 2.8799662606192636e-08,
u'contextualScore': 0.9991813164782087
'finalScore': 0.8754365122251001,
'support': 3,
'uri': 'Technische_Universiteit_Delft',
'label': 'Technische Universiteit Delft',
'types': '',
'percentageOfSecondRank': 0.1422872887244497,
'priorScore': 2.8799662606192636e-08,
'contextualScore': 0.9991813164782087
},
{
u'finalScore': 0.12456348777489806,
u'support': 521,
u'uri': u'Delft_University_of_Technology',
u'label': u'Delft University of Technology',
u'types': u'DBpedia:Agent, Schema:Organization, DBpedia:Organisation, Schema:EducationalOrganization, DBpedia:EducationalInstitution, Schema:CollegeOrUniversity, DBpedia:University',
u'percentageOfSecondRank': 0.0,
u'priorScore': 5.001541405942121e-06,
u'contextualScore': 0.0008186418452925803
'finalScore': 0.12456348777489806,
'support': 521,
'uri': 'Delft_University_of_Technology',
'label': 'Delft University of Technology',
'types': 'DBpedia:Agent, Schema:Organization, DBpedia:Organisation, Schema:EducationalOrganization, DBpedia:EducationalInstitution, Schema:CollegeOrUniversity, DBpedia:University',
'percentageOfSecondRank': 0.0,
'priorScore': 5.001541405942121e-06,
'contextualScore': 0.0008186418452925803
},
],
u'name': u'Technische Universiteit Delft',
u'offset': 25
],
'name': 'Technische Universiteit Delft',
'offset': 25
}
]
eq_(candidates, expected_out)


if not SKIP_ORDERED_DICT_TESTS:
def test_dict_key_cleanup():
dirty_dict = OrderedDict()
dirty_dict['@dirty'] = 'value'
dirty_dict['@empty'] = None # None values should be removed.
dirty_dict['@recursive'] = OrderedDict()
dirty_dict['@recursive']['tests'] = '1'
dirty_dict['@recursive']['stuff'] = OrderedDict()
more = OrderedDict()
more['something'] = 'isgoingon'
moremore = OrderedDict()
moremore['@moar'] = True
moar_iterable = [more, moremore]
dirty_dict['@recursive']['stuff'] = moar_iterable

clean_dict = OrderedDict()
clean_dict['dirty'] = 'value'
clean_dict['recursive'] = OrderedDict()
clean_dict['recursive']['tests'] = 1
clean_dict['recursive']['stuff'] = OrderedDict()
more = OrderedDict()
more['something'] = 'isgoingon'
moremore = OrderedDict()
moremore['moar'] = True
moar_iterable = [more, moremore]
clean_dict['recursive']['stuff'] = moar_iterable
eq_(spotlight._dict_cleanup(dirty_dict, dict_type=OrderedDict),
clean_dict)
assert_equals(candidates, expected_out)


def test_dict_key_cleanup():
dirty_dict = OrderedDict()
dirty_dict['@dirty'] = 'value'
dirty_dict['@empty'] = None # None values should be removed.
dirty_dict['@recursive'] = OrderedDict()
dirty_dict['@recursive']['tests'] = '1'
dirty_dict['@recursive']['stuff'] = OrderedDict()
more = OrderedDict()
more['something'] = 'isgoingon'
moremore = OrderedDict()
moremore['@moar'] = True
moar_iterable = [more, moremore]
dirty_dict['@recursive']['stuff'] = moar_iterable

clean_dict = OrderedDict()
clean_dict['dirty'] = 'value'
clean_dict['recursive'] = OrderedDict()
clean_dict['recursive']['tests'] = 1
clean_dict['recursive']['stuff'] = OrderedDict()
more = OrderedDict()
more['something'] = 'isgoingon'
moremore = OrderedDict()
moremore['moar'] = True
moar_iterable = [more, moremore]
clean_dict['recursive']['stuff'] = moar_iterable
assert_equals(spotlight._dict_cleanup(dirty_dict, dict_type=OrderedDict), clean_dict)

0 comments on commit 6f8b5f3

Please sign in to comment.