2023-11-27 15:16:35 +01:00
|
|
|
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2024-02-09 16:10:48 +01:00
|
|
|
import time
|
2024-02-09 14:44:24 +01:00
|
|
|
from unittest.mock import MagicMock, patch
|
2023-11-27 15:16:35 +01:00
|
|
|
|
2024-02-09 16:10:48 +01:00
|
|
|
import flaky
|
2023-11-27 15:16:35 +01:00
|
|
|
import pytest
|
|
|
|
import requests
|
|
|
|
|
2023-11-28 09:58:56 +01:00
|
|
|
from haystack.core.errors import PipelineDrawingError
|
2024-02-09 14:44:24 +01:00
|
|
|
from haystack.core.pipeline import Pipeline
|
|
|
|
from haystack.core.pipeline.draw import _to_mermaid_image, _to_mermaid_text
|
|
|
|
from haystack.testing.sample_components import AddFixedValue, Double
|
2023-11-27 15:16:35 +01:00
|
|
|
|
|
|
|
|
2024-02-09 16:10:48 +01:00
|
|
|
@flaky.flaky(max_runs=5, rerun_filter=lambda *_: time.sleep(5))
|
2023-11-28 09:58:56 +01:00
|
|
|
@pytest.mark.integration
|
2024-02-09 14:44:24 +01:00
|
|
|
def test_to_mermaid_image(test_files):
|
2023-11-27 15:16:35 +01:00
|
|
|
pipe = Pipeline()
|
|
|
|
pipe.add_component("comp1", Double())
|
|
|
|
pipe.add_component("comp2", Double())
|
|
|
|
pipe.connect("comp1", "comp2")
|
|
|
|
|
2024-02-09 14:44:24 +01:00
|
|
|
image_data = _to_mermaid_image(pipe.graph)
|
|
|
|
test_image = test_files / "test_mermaid_graph.png"
|
|
|
|
assert test_image.read_bytes() == image_data
|
2023-11-27 15:16:35 +01:00
|
|
|
|
|
|
|
|
2024-02-09 14:44:24 +01:00
|
|
|
@patch("haystack.core.pipeline.draw.requests")
|
|
|
|
def test_to_mermaid_image_does_not_edit_graph(mock_requests):
|
|
|
|
pipe = Pipeline()
|
|
|
|
pipe.add_component("comp1", AddFixedValue(add=3))
|
|
|
|
pipe.add_component("comp2", Double())
|
|
|
|
pipe.connect("comp1.result", "comp2.value")
|
|
|
|
pipe.connect("comp2.value", "comp1.value")
|
|
|
|
|
|
|
|
mock_requests.get.return_value = MagicMock(status_code=200)
|
|
|
|
expected_pipe = pipe.to_dict()
|
|
|
|
_to_mermaid_image(pipe.graph)
|
|
|
|
assert expected_pipe == pipe.to_dict()
|
|
|
|
|
|
|
|
|
|
|
|
def test_to_mermaid_image_failing_request(tmp_path):
|
2023-11-27 15:16:35 +01:00
|
|
|
pipe = Pipeline()
|
|
|
|
pipe.add_component("comp1", Double())
|
|
|
|
pipe.add_component("comp2", Double())
|
|
|
|
pipe.connect("comp1", "comp2")
|
|
|
|
pipe.connect("comp2", "comp1")
|
|
|
|
|
2024-02-09 14:44:24 +01:00
|
|
|
with patch("haystack.core.pipeline.draw.requests.get") as mock_get:
|
2023-11-27 15:16:35 +01:00
|
|
|
|
|
|
|
def raise_for_status(self):
|
|
|
|
raise requests.HTTPError()
|
|
|
|
|
|
|
|
mock_response = MagicMock()
|
|
|
|
mock_response.status_code = 429
|
|
|
|
mock_response.content = '{"error": "too many requests"}'
|
|
|
|
mock_response.raise_for_status = raise_for_status
|
|
|
|
mock_get.return_value = mock_response
|
|
|
|
|
|
|
|
with pytest.raises(PipelineDrawingError, match="There was an issue with https://mermaid.ink/"):
|
2024-02-09 14:44:24 +01:00
|
|
|
_to_mermaid_image(pipe.graph)
|
2023-11-27 15:16:35 +01:00
|
|
|
|
|
|
|
|
2024-02-09 14:44:24 +01:00
|
|
|
def test_to_mermaid_text():
|
2023-11-27 15:16:35 +01:00
|
|
|
pipe = Pipeline()
|
|
|
|
pipe.add_component("comp1", AddFixedValue(add=3))
|
|
|
|
pipe.add_component("comp2", Double())
|
|
|
|
pipe.connect("comp1.result", "comp2.value")
|
|
|
|
pipe.connect("comp2.value", "comp1.value")
|
|
|
|
|
2024-02-09 14:44:24 +01:00
|
|
|
text = _to_mermaid_text(pipe.graph)
|
2023-11-27 15:16:35 +01:00
|
|
|
assert (
|
2024-02-09 14:44:24 +01:00
|
|
|
text
|
2023-11-27 15:16:35 +01:00
|
|
|
== """
|
|
|
|
%%{ init: {'theme': 'neutral' } }%%
|
|
|
|
|
|
|
|
graph TD;
|
|
|
|
|
|
|
|
comp1["<b>comp1</b><br><small><i>AddFixedValue<br><br>Optional inputs:<ul style='text-align:left;'><li>add (Optional[int])</li></ul></i></small>"]:::component -- "result -> value<br><small><i>int</i></small>" --> comp2["<b>comp2</b><br><small><i>Double</i></small>"]:::component
|
|
|
|
comp2["<b>comp2</b><br><small><i>Double</i></small>"]:::component -- "value -> value<br><small><i>int</i></small>" --> comp1["<b>comp1</b><br><small><i>AddFixedValue<br><br>Optional inputs:<ul style='text-align:left;'><li>add (Optional[int])</li></ul></i></small>"]:::component
|
|
|
|
|
|
|
|
classDef component text-align:center;
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-02-09 14:44:24 +01:00
|
|
|
def test_to_mermaid_text_does_not_edit_graph():
|
2023-11-27 15:16:35 +01:00
|
|
|
pipe = Pipeline()
|
2024-02-09 14:44:24 +01:00
|
|
|
pipe.add_component("comp1", AddFixedValue(add=3))
|
2023-11-27 15:16:35 +01:00
|
|
|
pipe.add_component("comp2", Double())
|
2024-02-09 14:44:24 +01:00
|
|
|
pipe.connect("comp1.result", "comp2.value")
|
|
|
|
pipe.connect("comp2.value", "comp1.value")
|
2023-11-27 15:16:35 +01:00
|
|
|
|
2024-02-09 14:44:24 +01:00
|
|
|
expected_pipe = pipe.to_dict()
|
|
|
|
_to_mermaid_text(pipe.graph)
|
|
|
|
assert expected_pipe == pipe.to_dict()
|