def test_analyse_texts_with_files(client, pipeline_analyse_text_mock):
    pipeline = Pipeline(Project(client, "LoadTesting"), "discharge")

    with open("tests/resources/texts/text1.txt", "rb") as file1, open(
        "tests/resources/texts/text2.txt", "rb"
    ) as file2:
        results = pipeline.analyse_texts([file1, file2])
        sources = [result.source.replace(os.sep, "/") for result in results]

    assert sources == ["tests/resources/texts/text1.txt", "tests/resources/texts/text2.txt"]
Example #2
0
def test_analyse_texts(client, pipeline_analyse_text_mock):
    pipeline = Pipeline(Project(client, PROJECT_NAME), "discharge")
    file1_path = os.path.join(TEST_DIRECTORY, "resources/texts/text1.txt")
    file2_path = os.path.join(TEST_DIRECTORY, "resources/texts/text2.txt")

    with open(file1_path, "rb") as file1, open(file2_path, "rb") as file2:
        results = list(pipeline.analyse_texts([file1, file2]))
        sources = [result.source.replace(os.sep, "/") for result in results]
        cases = [result.data for result in results]
        exceptions = [result.exception for result in results]

    assert not exceptions[0]
    assert not exceptions[1]
    assert sources[0].endswith("tests/resources/texts/text1.txt")
    assert sources[1].endswith("tests/resources/texts/text2.txt")
def test_analyse_texts_with_paths(client, pipeline_analyse_text_mock):
    pipeline = Pipeline(Project(client, "LoadTesting"), "discharge")

    results = pipeline.analyse_texts(Path("tests/resources/texts").glob("*.txt"))

    expected_results = []
    for input_file in Path("tests/resources/texts").glob("*.txt"):
        with open(input_file, "r", encoding="UTF-8") as input_io:
            expected_results.append(
                {"source": str(input_file).replace(os.sep, "/"), "text": input_io.read()}
            )

    assert [
        {"source": result.source.replace(os.sep, "/"), "text": result.data[0]["coveredText"]}
        for result in sorted(results, key=lambda x: x.source)
    ] == sorted(expected_results, key=lambda x: x["source"])
def test_analyse_texts_with_some_working_and_some_failing(
        client_version_5, requests_mock):
    requests_mock.get(
        f"{API_BASE}/textanalysis/projects/{PROJECT_NAME}/pipelines/discharge/configuration",
        headers={"Content-Type": "application/json"},
        json={
            "payload": {
                "analysisEnginePoolSize": 4
            },
            "errorMessages": [],
        },
    )

    def callback(request, context):
        doc_text = request.text.read().decode("utf-8")
        if doc_text == "works":
            context.status_code = 200
            return {
                "payload": [
                    {
                        "begin": 0,
                        "end": len(doc_text),
                        "type": "uima.tcas.DocumentAnnotation",
                        "coveredText": doc_text
                        # ... truncated ...
                    },
                ],
                "errorMessages": [],
            }
        else:
            context.status_code = 500
            return {
                "payload": [],
                "errorMessages": ["Kaputt!"],
            }

    requests_mock.post(
        f"{API_BASE}/textanalysis/projects/{PROJECT_NAME}/pipelines/discharge/analyseText",
        headers={"Content-Type": "application/json"},
        json=callback,
    )

    pipeline = Pipeline(Project(client_version_5, PROJECT_NAME), "discharge")
    results = list(pipeline.analyse_texts(["works", "fails"]))

    assert results[0].successful() is True
    assert results[1].successful() is False