Skip to content

Commit

Permalink
move threading tests to test_threading
Browse files Browse the repository at this point in the history
  • Loading branch information
kmike committed Apr 21, 2014
1 parent 8ece979 commit f539f92
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 55 deletions.
2 changes: 1 addition & 1 deletion tests/test_analyzer.py
Expand Up @@ -85,7 +85,6 @@
('сапают', ['сапать']), # и никаких местоимений!

('кюди', ['кюдить', 'кюдь', 'кюди']), # и никаких "человек"

]

NON_PRODUCTIVE_BUGS_DATA = [
Expand Down Expand Up @@ -272,3 +271,4 @@ def test_normal_forms(self):
class TetsPunctuationPredictor:
def test_tag(self):
assert morph.tag('…') == [morph.TagClass('PNCT')]

58 changes: 5 additions & 53 deletions tests/test_parsing.py
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import concurrent.futures
import random
import concurrent.futures
import pytest
import pymorphy2
from .utils import morph
from .utils import morph, assert_parse_is_correct


def _to_test_data(text):
Expand Down Expand Up @@ -91,7 +91,7 @@ def _to_test_data(text):
Foo foo LATN
I i LATN
# ============== abbreviations 1
# ============== common lowercased abbreviations
# should normal forms be expanded?
руб рубль NOUN,inan,masc,Fixd,Abbr plur,gent
Expand All @@ -112,13 +112,13 @@ def _to_test_data(text):
Сердюков сердюков NOUN,anim,masc,Surn sing,nomn
Третьяк третьяк NOUN,anim,masc,Surn sing,nomn
# ============== abbreviations 1
# ============== common lowercased abbreviations
# should normal forms be expanded?
г г NOUN,inan,masc,Fixd,Abbr sing,loc2
п п NOUN,inan,masc,Fixd,Abbr sing,accs
# ============== abbreviations 2
# ============== uppercased abbreviations
# it seems is not possible to properly guess gender and number
ГКРФ гкрф NOUN,inan,masc,Sgtm,Fixd,Abbr sing,nomn
Expand Down Expand Up @@ -147,17 +147,6 @@ def run_for_all(parses):
return pytest.mark.parametrize(("word", "normal_form", "tag"), parses)


def assert_parse_is_correct(parse, word, normal_form, tag):
"""
Check if one of the word parses has normal form ``normal_form``
and tag ``tag``.
"""
for p in parse:
if p.normal_form == normal_form and str(p.tag) == tag:
return
assert False, parse


# ====== Tests:
def _test_has_parse(parses):
@run_for_all(parses)
Expand Down Expand Up @@ -187,40 +176,3 @@ def test_tag_produces_the_same_as_parse(word, normal_form, tag):
test_tag = _test_tag(PARSES)
test_tag_title = _test_tag(PARSES_TITLE)
test_tag_upper = _test_tag(PARSES_UPPER)


def _check_analyzer(morph, parses):
for word, normal_form, tag in parses:
parse = morph.parse(word)
assert_parse_is_correct(parse, word, normal_form, tag)


def _check_new_analyzer(parses):
morph = pymorphy2.MorphAnalyzer()
for word, normal_form, tag in parses:
parse = morph.parse(word)
assert_parse_is_correct(parse, word, normal_form, tag)


def _create_morph_analyzer(i):
morph = pymorphy2.MorphAnalyzer()
word, normal_form, tag = random.choice(PARSES)
parse = morph.parse(word)
assert_parse_is_correct(parse, word, normal_form, tag)


def test_threading_single_morph_analyzer():
with concurrent.futures.ThreadPoolExecutor(3) as executor:
res = list(executor.map(_check_analyzer, [morph]*10, [PARSES]*10))


@pytest.mark.xfail # See https://github.com/kmike/pymorphy2/issues/37
def test_threading_multiple_morph_analyzers():
with concurrent.futures.ThreadPoolExecutor(3) as executor:
res = list(executor.map(_check_new_analyzer, [PARSES]*10))


@pytest.mark.xfail # See https://github.com/kmike/pymorphy2/issues/37
def test_threading_create_analyzer():
with concurrent.futures.ThreadPoolExecutor(3) as executor:
res = list(executor.map(_create_morph_analyzer, range(10)))
45 changes: 45 additions & 0 deletions tests/test_threading.py
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import concurrent.futures
import random
import pytest
import pymorphy2
from .utils import morph, assert_parse_is_correct
from .test_parsing import PARSES


def _check_analyzer(morph, parses):
for word, normal_form, tag in parses:
parse = morph.parse(word)
assert_parse_is_correct(parse, word, normal_form, tag)


def _check_new_analyzer(parses):
morph = pymorphy2.MorphAnalyzer()
for word, normal_form, tag in parses:
parse = morph.parse(word)
assert_parse_is_correct(parse, word, normal_form, tag)


def _create_morph_analyzer(i):
morph = pymorphy2.MorphAnalyzer()
word, normal_form, tag = random.choice(PARSES)
parse = morph.parse(word)
assert_parse_is_correct(parse, word, normal_form, tag)


def test_threading_single_morph_analyzer():
with concurrent.futures.ThreadPoolExecutor(3) as executor:
res = list(executor.map(_check_analyzer, [morph]*10, [PARSES]*10))


@pytest.mark.xfail # See https://github.com/kmike/pymorphy2/issues/37
def test_threading_multiple_morph_analyzers():
with concurrent.futures.ThreadPoolExecutor(3) as executor:
res = list(executor.map(_check_new_analyzer, [PARSES]*10))


@pytest.mark.xfail # See https://github.com/kmike/pymorphy2/issues/37
def test_threading_create_analyzer():
with concurrent.futures.ThreadPoolExecutor(3) as executor:
res = list(executor.map(_create_morph_analyzer, range(10)))
13 changes: 12 additions & 1 deletion tests/utils.py
Expand Up @@ -2,4 +2,15 @@
from __future__ import absolute_import
import pymorphy2

morph = pymorphy2.MorphAnalyzer()
morph = pymorphy2.MorphAnalyzer()

def assert_parse_is_correct(parses, word, normal_form, tag):
"""
Check if one of the word parses has normal form ``normal_form``
and tag ``tag``.
"""
for p in parses:
if p.normal_form == normal_form and str(p.tag) == tag:
return
assert False, parses

0 comments on commit f539f92

Please sign in to comment.