2023-03-14 17:01:19 +01:00
|
|
|
from typing import Dict, Any
|
2023-05-05 19:47:32 +02:00
|
|
|
from dataclasses import dataclass
|
2023-03-14 17:01:19 +01:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2023-05-22 16:02:58 +02:00
|
|
|
from haystack.preview import Pipeline, component, NoSuchStoreError, ComponentInput, ComponentOutput
|
2023-03-14 17:01:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MockStore:
|
2023-05-22 16:02:58 +02:00
|
|
|
...
|
2023-03-14 17:01:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.unit
|
|
|
|
def test_pipeline_store_api():
|
|
|
|
store_1 = MockStore()
|
|
|
|
store_2 = MockStore()
|
|
|
|
pipe = Pipeline()
|
|
|
|
|
|
|
|
pipe.add_store(name="first_store", store=store_1)
|
|
|
|
pipe.add_store(name="second_store", store=store_2)
|
|
|
|
|
|
|
|
assert pipe.list_stores() == ["first_store", "second_store"]
|
|
|
|
|
|
|
|
assert pipe.get_store("first_store") == store_1
|
|
|
|
assert pipe.get_store("second_store") == store_2
|
|
|
|
with pytest.raises(NoSuchStoreError):
|
|
|
|
pipe.get_store("third_store")
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.unit
|
|
|
|
def test_pipeline_stores_in_params():
|
|
|
|
store_1 = MockStore()
|
|
|
|
store_2 = MockStore()
|
|
|
|
|
2023-04-17 12:20:42 +02:00
|
|
|
@component
|
|
|
|
class MockComponent:
|
2023-05-05 19:47:32 +02:00
|
|
|
@dataclass
|
2023-05-22 16:02:58 +02:00
|
|
|
class Input(ComponentInput):
|
2023-05-05 19:47:32 +02:00
|
|
|
value: int
|
2023-05-22 16:02:58 +02:00
|
|
|
stores: Dict[str, Any]
|
2023-03-14 17:01:19 +01:00
|
|
|
|
2023-05-22 16:02:58 +02:00
|
|
|
@dataclass
|
|
|
|
class Output(ComponentOutput):
|
|
|
|
value: int
|
|
|
|
|
|
|
|
def run(self, data: Input) -> Output:
|
|
|
|
assert data.stores == {"first_store": store_1, "second_store": store_2}
|
|
|
|
return MockComponent.Output(value=data.value)
|
2023-03-14 17:01:19 +01:00
|
|
|
|
|
|
|
pipe = Pipeline()
|
2023-04-17 12:20:42 +02:00
|
|
|
pipe.add_component("component", MockComponent())
|
2023-03-14 17:01:19 +01:00
|
|
|
|
|
|
|
pipe.add_store(name="first_store", store=store_1)
|
|
|
|
pipe.add_store(name="second_store", store=store_2)
|
|
|
|
|
2023-06-16 12:07:00 +02:00
|
|
|
assert pipe.run(data={"component": MockComponent.Input(value=1)}) == {"component": MockComponent.Output(value=1)}
|