File: utils.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 (44 lines) | stat: -rw-r--r-- 1,271 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
import json
import os
import re
import subprocess

import pytest

PATTERN = r'([^\ ]+) "([^\"]+)" \[([\d\.]*)\]'
DEFAULT_TOLERANCE = 1e-2


def assert_field_vectors_equal(a, b, tol=DEFAULT_TOLERANCE):
    assert a[0] == b[0]
    for x, y in zip(a[1], b[1]):
        assert x == pytest.approx(y, rel=tol)


def assert_vectors_equal(a, b, tol=DEFAULT_TOLERANCE):
    for x, y in zip(a, b):
        assert x == pytest.approx(y, rel=tol)


def assert_results_match(results, js_results, tol=DEFAULT_TOLERANCE):
    assert len(results) == len(js_results) != 0
    for js_result, result in zip(js_results, results):
        id_, title, score = re.match(PATTERN, js_result).groups()
        assert result["ref"] == id_
        assert result["score"] == pytest.approx(float(score), rel=tol)


def read_json_fixture(filename):
    fixture_path = os.path.join(
        os.path.dirname(__file__), "acceptance_tests", "fixtures", filename
    )
    with open(fixture_path) as f:
        return json.loads(f.read())


def run_node_script(filename, *args):
    js_path = os.path.join(
        os.path.dirname(__file__), "acceptance_tests", "javascript", filename
    )
    js_output = subprocess.check_output(["node", js_path] + list(args))
    return js_output.decode("utf-8").strip()