Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
Remove unneeded checks
Better registration of languages
Use coverage.py directly
  • Loading branch information
Yeray Diaz Diaz committed May 19, 2018
1 parent 1b01925 commit b6f360a
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 29 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

tests:
pytest -m "not acceptance"
coverage run -m pytest -m "not acceptance"
coverage report

tests-acceptance:
pytest -m "acceptance"
Expand Down
3 changes: 2 additions & 1 deletion lunr/match_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def __init__(self, term, field, metadata):
}

def __repr__(self):
return '<MatchData "{}">'.format(','.join(self.metadata.keys()))
return '<MatchData "{}">'.format(
','.join(sorted(self.metadata.keys())))

def combine(self, other):
"""An instance of lunr.MatchData will be created for every term that
Expand Down
8 changes: 0 additions & 8 deletions lunr/query_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ def parse_field_or_term(cls, parser):
@classmethod
def parse_field(cls, parser):
lexeme = parser.consume_lexeme()
if lexeme is None:
return

if lexeme['string'] not in parser.query.all_fields:
raise QueryParseError(
Expand All @@ -87,8 +85,6 @@ def parse_field(cls, parser):
@classmethod
def parse_term(cls, parser):
lexeme = parser.consume_lexeme()
if lexeme is None:
return

parser.current_clause.term = lexeme['string'].lower()
if '*' in lexeme['string']:
Expand All @@ -99,8 +95,6 @@ def parse_term(cls, parser):
@classmethod
def parse_edit_distance(cls, parser):
lexeme = parser.consume_lexeme()
if lexeme is None:
return

try:
edit_distance = int(lexeme['string'])
Expand All @@ -115,8 +109,6 @@ def parse_edit_distance(cls, parser):
@classmethod
def parse_boost(cls, parser):
lexeme = parser.consume_lexeme()
if lexeme is None:
return

try:
boost = int(lexeme['string'])
Expand Down
19 changes: 12 additions & 7 deletions lunr/stemmer_languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
'sv': 'swedish'
}

try:
try: # pragma: no cover
from nltk.stem.snowball import SnowballStemmer
LANGUAGE_SUPPORT = True
except ImportError:
except ImportError: # pragma: no cover
LANGUAGE_SUPPORT = False


Expand Down Expand Up @@ -55,13 +55,18 @@ def wrapped_stem(token, metadata=None):
return token.update(wrapped_stem)


if LANGUAGE_SUPPORT:
# TODO: registering all possible stemmers feels unnecessary but it solves
# deserializing with arbitrary language functions. Ideally the schema would
# provide the language(s) for the index and we could register the stemmers
# as needed
def register_languages():
"""Register all supported languages to ensure compatibility."""
for language in SUPPORTED_LANGUAGES:
language_stemmer = partial(
nltk_stemmer, get_language_stemmer(language))
Pipeline.register_function(
language_stemmer, 'stemmer-{}'.format(language))


if LANGUAGE_SUPPORT: # pragma: no cover
# TODO: registering all possible stemmers feels unnecessary but it solves
# deserializing with arbitrary language functions. Ideally the schema would
# provide the language(s) for the index and we could register the stemmers
# as needed
register_languages()
4 changes: 2 additions & 2 deletions requirements/test.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-e .[languages]
pytest==3.5.1
pytest-cov==2.5.1
mock==2.0.0
tox==3.0.0
flake8==3.5.0
flake8==3.5.0
coverage==4.5.1
19 changes: 15 additions & 4 deletions tests/test_language_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

from lunr import lunr
from lunr.stemmer_languages import LANGUAGE_SUPPORT
from lunr.stemmer_languages import LANGUAGE_SUPPORT, SUPPORTED_LANGUAGES
from lunr.pipeline import Pipeline

documents = [
Expand All @@ -26,19 +26,30 @@


def test_lunr_function_raises_if_unsupported_language():
assert LANGUAGE_SUPPORT is True
assert LANGUAGE_SUPPORT is True, (
'NLTK not found, please run `pip install -e .[languages]`')
with pytest.raises(RuntimeError):
lunr('id', ['title', 'text'], documents, 'foo')


def test_lunr_function_registers_nltk_stemmer():
assert LANGUAGE_SUPPORT is True
assert LANGUAGE_SUPPORT is True, (
'NLTK not found, please run `pip install -e .[languages]`')
lunr('id', ['title', 'text'], documents, 'en')
assert 'stemmer-en' in Pipeline.registered_functions


def test_search_stems_search_terms():
assert LANGUAGE_SUPPORT is True
assert LANGUAGE_SUPPORT is True, (
'NLTK not found, please run `pip install -e .[languages]`')
idx = lunr('id', ['title', 'text'], documents, 'es')
results = idx.search('inventando') # stemmed to "invent"
assert len(results) == 2


def test_register_languages():
assert LANGUAGE_SUPPORT is True, (
'NLTK not found, please run `pip install -e .[languages]`')

for lang in SUPPORTED_LANGUAGES:
assert 'stemmer-{}'.format(lang) in Pipeline.registered_functions
2 changes: 1 addition & 1 deletion tests/test_match_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def setup_method(self, method):
MatchData('baz', 'body', {'position': [4]}))

def test_repr(self):
assert repr(self.match) == '<MatchData "foo,bar,baz">'
assert repr(self.match) == '<MatchData "bar,baz,foo">'

def test_combine_terms(self):
assert sorted(
Expand Down
14 changes: 13 additions & 1 deletion tests/test_query_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def parse(q):

class TestQueryParser:

def test_parse_empty_string(self):
clauses = parse('')
assert len(clauses) == 0

def test_parse_single_term(self):
clauses = parse('foo')
assert len(clauses) == 1
Expand Down Expand Up @@ -44,10 +48,18 @@ def test_multiple_terms(self):
assert clauses[0].term == 'foo'
assert clauses[1].term == 'bar'

def test_field_without_a_term(self):
def test_unknown_field(self):
with pytest.raises(QueryParseError):
parse('unknown:foo')

def test_field_without_a_term(self):
with pytest.raises(QueryParseError):
parse('title:')

def test_field_twice(self):
with pytest.raises(QueryParseError):
parse('title:title:')

def test_term_with_field(self):
clauses = parse('title:foo')
assert len(clauses) == 1
Expand Down
10 changes: 6 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ envlist = py27,py34,py35,py36,pypy,pypy3,flake8

[testenv]
deps = -rrequirements/test.txt
commands = pytest
commands =
coverage run -m pytest -m "not acceptance"
coverage report
coverage xml
pytest -m "acceptance"

[testenv:flake8]
basepython = python3.6
Expand All @@ -23,10 +27,8 @@ exclude_lines =
ignore_errors = True
omit =
tests/*
lunr/stemmer.py
show_missing = True

[pytest]
addopts = --cov=lunr --cov-report=xml --cov-report=term

[flake8]
exclude = lunr/stemmer.py

0 comments on commit b6f360a

Please sign in to comment.