Exemplo n.º 1
0
def test_index_hits():
    src = """
Europe
    Russia [url=www.russia.ru]
        Moscow
            Europe [info="a place in Moscow"]
    England
        London
"""
    text = 'Europe: London, EUROPE CENTER in Moscow'
    root = loads(src)
    normalizer = lambda txt: ' '.join(re.compile('\w+').findall(txt.lower()))
    terms_idx = Index(root, normalizer=normalizer)

    hits = terms_idx.hits(text, indirect_hits=False)
    assert 4 == len(hits)
    assert [
        Hit(root[0], dhits=2, ihits=None),
        Hit(root[0, 0, 0, 0], dhits=2, ihits=None),
        Hit(root[0, 1, 0], dhits=1, ihits=None),
        Hit(root[0, 0, 0], dhits=1, ihits=None)
    ] == hits

    hits = terms_idx.hits(text)
    assert 6 == len(hits)
    assert [
        Hit(root[0], dhits=2, ihits=4),
        Hit(root[0, 0, 0, 0], dhits=2, ihits=0),
        Hit(root[0, 1, 0], dhits=1, ihits=0),
        Hit(root[0, 0, 0], dhits=1, ihits=2),
        Hit(root[0, 0], dhits=0, ihits=3),
        Hit(root[0, 1], dhits=0, ihits=1),
    ] == hits
Exemplo n.º 2
0
def test_index_normalization():
    src = """Europe\n\tGermany\n\tMunich\n\tBerlin"""
    text = 'munich BERLIN'
    root = loads(src)
    normalizer = lambda txt: txt.lower()
    terms_idx = Index(root, normalizer=normalizer)
    hits = terms_idx.hits(text, indirect_hits=False)
    assert 2 == len(hits)
    assert 'Munich' == hits[0].node.term
    assert 'Berlin' == hits[1].node.term
Exemplo n.º 3
0
def test_index_build():
    root = loads(TEST_SRC)
    terms_idx = Index(root)
    assert 'A' in terms_idx
    assert 'B' in terms_idx
    assert 'C' in terms_idx
    assert 'D' in terms_idx
    assert 'E' in terms_idx
    assert 'X' not in terms_idx

    nodes = terms_idx.get('D')
    assert 1 == len(nodes)
    assert 'D' == nodes[0].term

    assert None == terms_idx.get('X')
Exemplo n.º 4
0
def test_base_case():
    src = """
A
    B
        # comment 1 for C

        C # comment 2 for C
        D
# comment for E
E [key1=value1 "key 2"="value 2" ]
    """
    root = loads(src)
    assert 'A' == root[0].term

    assert 'B' == root[0][0].term
    assert 'B' == root[0, 0].term

    assert 'C' == root[0, 0, 0].term

    assert 'D' == root[0, 0, 1].term
    assert 'D' == root[0, 0, 1].term

    assert 'E' == root[1].term
    assert {'key1': 'value1', 'key 2': 'value 2'} == root[1].data
Exemplo n.º 5
0
def test_empty_content_parsing():
    tree = loads('')
    assert [] == tree.children
    assert None == tree.term
Exemplo n.º 6
0
def test_parse_src_with_invalid_data():
    s = 'A [key1 = value1]'  # space before or after = is not allowed
    with pytest.raises(SyntaxError) as ex:
        loads(s)
    assert 'invalid data string format' in str(ex)