File: test_language_support.py

package info (click to toggle)
python-lunr 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,644 kB
  • sloc: python: 3,811; javascript: 114; makefile: 60
file content (77 lines) | stat: -rw-r--r-- 2,503 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import json
import tempfile

import pytest

from lunr import lunr
from lunr.index import Index
from tests.utils import read_json_fixture, run_node_script, assert_results_match


@pytest.mark.acceptance
def test_languages_query_results_match_javascript_results():
    query_string = "resistencia"
    js_results = run_node_script("language_query.js", query_string).split("\n")
    data = read_json_fixture("lang_es.json")
    index = lunr(
        ref="id", fields=("title", "text"), documents=data["docs"], languages="es"
    )
    results = index.search(query_string)
    assert_results_match(results, js_results, tol=0.1)


@pytest.mark.acceptance
def test_js_serialized_lang_index_can_be_loaded_and_produces_same_results():
    json_path = run_node_script("language_serialize_index.js")
    with open(json_path) as fd:
        js_serialized_index = fd.read()

    index = Index.load(js_serialized_index)
    query_string = "imperio"
    results = index.search(query_string)
    js_results = run_node_script("language_query.js", query_string).split("\n")
    assert_results_match(results, js_results)


@pytest.mark.acceptance
def test_serialized_lang_index_can_be_loaded_in_js_and_produces_same_results():
    data = read_json_fixture("lang_es.json")
    index = lunr(
        ref="id", fields=("title", "text"), documents=data["docs"], languages="es"
    )
    query_string = "imperio"
    results = index.search(query_string)
    serialized_index = index.serialize()

    with tempfile.NamedTemporaryFile(delete=False) as fp:
        fp.write(json.dumps(serialized_index).encode())

    js_results = run_node_script(
        "language_load_serialized_index_and_search.js", fp.name, query_string
    ).split("\n")
    assert_results_match(results, js_results)


@pytest.mark.acceptance
def test_serialized_multilang_index_can_be_loaded_in_js_and_results_equal():
    data = read_json_fixture("lang_es_en.json")
    index = lunr(
        ref="id",
        fields=("title", "text"),
        documents=data["docs"],
        languages=["es", "en"],
    )
    query_string = "taxation"
    results = index.search(query_string)
    serialized_index = index.serialize()

    with tempfile.NamedTemporaryFile(delete=False) as fp:
        fp.write(json.dumps(serialized_index).encode())

    js_results = run_node_script(
        "language_load_serialized_index_and_search.js",
        fp.name,
        query_string,
        "lang_es_en.json",
    ).split("\n")
    assert_results_match(results, js_results)