2024-02-05 17:46:45 +01:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from haystack.core.component.sockets import InputSocket, Sockets
|
|
|
|
from haystack.core.pipeline import Pipeline
|
|
|
|
from haystack.testing.factory import component_class
|
|
|
|
|
|
|
|
|
|
|
|
class TestSockets:
|
|
|
|
def test_init(self):
|
|
|
|
comp = component_class("SomeComponent", input_types={"input_1": int, "input_2": int})()
|
|
|
|
sockets = {"input_1": InputSocket("input_1", int), "input_2": InputSocket("input_2", int)}
|
|
|
|
io = Sockets(component=comp, sockets_dict=sockets, sockets_io_type=InputSocket)
|
|
|
|
assert io._component == comp
|
|
|
|
assert "input_1" in io.__dict__
|
|
|
|
assert io.__dict__["input_1"] == comp.__haystack_input__._sockets_dict["input_1"]
|
|
|
|
assert "input_2" in io.__dict__
|
|
|
|
assert io.__dict__["input_2"] == comp.__haystack_input__._sockets_dict["input_2"]
|
|
|
|
|
|
|
|
def test_init_with_empty_sockets(self):
|
|
|
|
comp = component_class("SomeComponent")()
|
|
|
|
io = Sockets(component=comp, sockets_dict={}, sockets_io_type=InputSocket)
|
|
|
|
|
|
|
|
assert io._component == comp
|
|
|
|
assert io._sockets_dict == {}
|
|
|
|
|
|
|
|
def test_getattribute(self):
|
|
|
|
comp = component_class("SomeComponent", input_types={"input_1": int, "input_2": int})()
|
|
|
|
io = Sockets(component=comp, sockets_dict=comp.__haystack_input__._sockets_dict, sockets_io_type=InputSocket)
|
|
|
|
|
|
|
|
assert io.input_1 == comp.__haystack_input__._sockets_dict["input_1"]
|
|
|
|
assert io.input_2 == comp.__haystack_input__._sockets_dict["input_2"]
|
|
|
|
|
|
|
|
def test_getattribute_non_existing_socket(self):
|
|
|
|
comp = component_class("SomeComponent", input_types={"input_1": int, "input_2": int})()
|
|
|
|
io = Sockets(component=comp, sockets_dict=comp.__haystack_input__._sockets_dict, sockets_io_type=InputSocket)
|
|
|
|
|
|
|
|
with pytest.raises(AttributeError):
|
|
|
|
io.input_3
|
|
|
|
|
|
|
|
def test_repr(self):
|
|
|
|
comp = component_class("SomeComponent", input_types={"input_1": int, "input_2": int})()
|
|
|
|
io = Sockets(component=comp, sockets_dict=comp.__haystack_input__._sockets_dict, sockets_io_type=InputSocket)
|
|
|
|
res = repr(io)
|
2024-02-08 11:46:10 +01:00
|
|
|
assert res == "Inputs:\n - input_1: int\n - input_2: int"
|