From b38a8f4a943306a4a978e9b40fea9f5f2d7193e7 Mon Sep 17 00:00:00 2001 From: ines Date: Sat, 7 Oct 2017 02:06:21 +0200 Subject: [PATCH] Fix and update pipe methods tests --- spacy/tests/pipeline/test_add_pipe.py | 43 ------------ spacy/tests/pipeline/test_pipe_methods.py | 84 +++++++++++++++++++++++ 2 files changed, 84 insertions(+), 43 deletions(-) delete mode 100644 spacy/tests/pipeline/test_add_pipe.py create mode 100644 spacy/tests/pipeline/test_pipe_methods.py diff --git a/spacy/tests/pipeline/test_add_pipe.py b/spacy/tests/pipeline/test_add_pipe.py deleted file mode 100644 index 13fb4acaf..000000000 --- a/spacy/tests/pipeline/test_add_pipe.py +++ /dev/null @@ -1,43 +0,0 @@ -from __future__ import unicode_literals -import pytest - -from ... import language -from ...language import Language - -@pytest.fixture -def nlp(): - return Language() - -@pytest.fixture -def name(): - return 'parser' - -def new_pipe(doc): - return doc - - -def test_add_pipe_no_name(nlp): - nlp.add_pipe(new_pipe) - assert 'new_pipe' in nlp.pipe_names - -def test_add_pipe_duplicate_name(nlp): - nlp.add_pipe(new_pipe, name='duplicate_name') - with pytest.raises(ValueError): - nlp.add_pipe(new_pipe, name='duplicate_name') - - -def test_add_pipe_first(nlp, name): - nlp.add_pipe(new_pipe, name=name, first=True) - assert nlp.pipeline[0][0] == name - - -def test_add_pipe_last(nlp, name): - nlp.add_pipe(lambda doc: doc, name='lambda_pipe') - nlp.add_pipe(new_pipe, name=name, last=True) - assert nlp.pipeline[0][0] != name - assert nlp.pipeline[-1][0] == name - - -def test_cant_add_pipe_first_and_last(nlp): - with pytest.raises(ValueError): - nlp.add_pipe(new_pipe, first=True, last=True) diff --git a/spacy/tests/pipeline/test_pipe_methods.py b/spacy/tests/pipeline/test_pipe_methods.py new file mode 100644 index 000000000..5ec78aefb --- /dev/null +++ b/spacy/tests/pipeline/test_pipe_methods.py @@ -0,0 +1,84 @@ +# coding: utf8 +from __future__ import unicode_literals + +import pytest + +from ...language import Language + + +@pytest.fixture +def nlp(): + return Language() + + +def new_pipe(doc): + return doc + + +def test_add_pipe_no_name(nlp): + nlp.add_pipe(new_pipe) + assert 'new_pipe' in nlp.pipe_names + + +def test_add_pipe_duplicate_name(nlp): + nlp.add_pipe(new_pipe, name='duplicate_name') + with pytest.raises(ValueError): + nlp.add_pipe(new_pipe, name='duplicate_name') + + +@pytest.mark.parametrize('name', ['parser']) +def test_add_pipe_first(nlp, name): + nlp.add_pipe(new_pipe, name=name, first=True) + assert nlp.pipeline[0][0] == name + + +@pytest.mark.parametrize('name1,name2', [('parser', 'lambda_pipe')]) +def test_add_pipe_last(nlp, name1, name2): + nlp.add_pipe(lambda doc: doc, name=name2) + nlp.add_pipe(new_pipe, name=name1, last=True) + assert nlp.pipeline[0][0] != name1 + assert nlp.pipeline[-1][0] == name1 + + +def test_cant_add_pipe_first_and_last(nlp): + with pytest.raises(ValueError): + nlp.add_pipe(new_pipe, first=True, last=True) + + +@pytest.mark.parametrize('name', ['my_component']) +def test_get_pipe(nlp, name): + with pytest.raises(KeyError): + nlp.get_pipe(name) + nlp.add_pipe(new_pipe, name=name) + assert nlp.get_pipe(name) == new_pipe + + +@pytest.mark.parametrize('name,replacement', [('my_component', lambda doc: doc)]) +def test_replace_pipe(nlp, name, replacement): + with pytest.raises(ValueError): + nlp.replace_pipe(name, new_pipe) + nlp.add_pipe(new_pipe, name=name) + nlp.replace_pipe(name, replacement) + assert nlp.get_pipe(name) != new_pipe + assert nlp.get_pipe(name) == replacement + + +@pytest.mark.parametrize('old_name,new_name', [('old_pipe', 'new_pipe')]) +def test_rename_pipe(nlp, old_name, new_name): + with pytest.raises(ValueError): + nlp.rename_pipe(old_name, new_name) + nlp.add_pipe(new_pipe, name=old_name) + nlp.rename_pipe(old_name, new_name) + assert nlp.pipeline[0][0] == new_name + + +@pytest.mark.parametrize('name', ['my_component']) +def test_remove_pipe(nlp, name): + with pytest.raises(ValueError): + nlp.remove_pipe(name) + nlp.add_pipe(new_pipe, name=name) + assert len(nlp.pipeline) == 1 + removed_name, removed_component = nlp.remove_pipe(name) + assert not len(nlp.pipeline) + assert removed_name == name + assert removed_component == new_pipe