Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions src/apify_haystack/_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ def __init__(
`default_dataset_mapping_function`.
run_input (dict, optional): The input parameters for the Actor you want to run. This can be provided
either in the `run` method or during the class instantiation. If specified in both places,
the inputs are merged and the values from `run` take precedence.
the inputs are merged and the values from `run` take precedence. Without any input, the
Actor runs with its default input.
build (str, optional): Optionally specifies the Actor build to run.
It can be either a build tag or build number.
memory_mbytes (int, optional): Optional memory limit for the run, in megabytes.
Expand Down Expand Up @@ -160,7 +161,8 @@ def run(self, run_input: dict | None = None) -> dict[str, list[Document]]:
Args:
run_input (dict, optional): The input parameters for the Actor you want to run. This can be provided
either in the `run` method or during the class instantiation. If specified in both places,
the inputs are merged and the values from `run` take precedence.
the inputs are merged and the values from `run` take precedence. Without any input, the
Actor runs with its default input.

Example:
.. code-block:: python
Expand All @@ -172,8 +174,7 @@ def run(self, run_input: dict | None = None) -> dict[str, list[Document]]:
)
dataset = actor.run(run_input={"startUrls": [{"url": "https://haystack.deepset.ai/"}]})
"""
if not (run_input := _merge_inputs(self.run_input, run_input)):
raise ValueError('Please provide the run_input either in the constructor or in the run method.')
run_input = _merge_inputs(self.run_input, run_input)

client = self._get_client()
run = client.actor(self.actor_id).call(
Expand All @@ -193,10 +194,10 @@ async def run_async(self, run_input: dict | None = None) -> dict[str, list[Docum
Args:
run_input (dict, optional): The input parameters for the Actor you want to run. This can be provided
either in the `run_async` method or during the class instantiation. If specified in both places,
the inputs are merged and the values from `run_async` take precedence.
the inputs are merged and the values from `run_async` take precedence. Without any input, the
Actor runs with its default input.
"""
if not (run_input := _merge_inputs(self.run_input, run_input)):
raise ValueError('Please provide the run_input either in the constructor or in the run method.')
run_input = _merge_inputs(self.run_input, run_input)

client = self._get_async_client()
run = await client.actor(self.actor_id).call(
Expand Down Expand Up @@ -260,7 +261,8 @@ def __init__(
`default_dataset_mapping_function`.
task_input (dict, optional): The input parameters for the task you want to run. This can be provided
either in the `run` method or during the class instantiation. If specified in both places,
the inputs are merged and the values from `run` take precedence.
the inputs are merged and the values from `run` take precedence. Without any input, the
task runs with its saved input.
build (str, optional): Optionally specifies the Actor build to run.
It can be either a build tag or build number.
memory_mbytes (int, optional): Optional memory limit for the run, in megabytes.
Expand Down Expand Up @@ -288,10 +290,10 @@ def run(self, task_input: dict | None = None) -> dict[str, list[Document]]:
Args:
task_input (dict, optional): The input parameters for the task you want to run. This can be provided
either in the `run` method or during the class instantiation. If specified in both places,
the inputs are merged and the values from `run` take precedence.
the inputs are merged and the values from `run` take precedence. Without any input, the
task runs with its saved input.
"""
if not (task_input := _merge_inputs(self.task_input, task_input)):
raise ValueError('Please provide the task_input either in the constructor or in the run method.')
task_input = _merge_inputs(self.task_input, task_input)

client = self._get_client()
run = client.task(self.task_id).call(
Expand All @@ -311,10 +313,10 @@ async def run_async(self, task_input: dict | None = None) -> dict[str, list[Docu
Args:
task_input (dict, optional): The input parameters for the task you want to run. This can be provided
either in the `run_async` method or during the class instantiation. If specified in both places,
the inputs are merged and the values from `run_async` take precedence.
the inputs are merged and the values from `run_async` take precedence. Without any input, the
task runs with its saved input.
"""
if not (task_input := _merge_inputs(self.task_input, task_input)):
raise ValueError('Please provide the task_input either in the constructor or in the run method.')
task_input = _merge_inputs(self.task_input, task_input)

client = self._get_async_client()
run = await client.task(self.task_id).call(
Expand Down Expand Up @@ -375,9 +377,8 @@ def _serialize_mapping_function(dataset_mapping_function: Callable[[dict], Docum


def _merge_inputs(input1: dict | None, input2: dict | None) -> dict | None:
if input1 and input2:
return input1 | input2
return input1 or input2
merged = (input1 or {}) | (input2 or {})
return merged or None


def _load_documents(
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ def test_task_call_loads_run_dataset(task_id: str) -> None:


async def test_task_call_run_async_loads_run_dataset(task_id: str) -> None:
"""`run_async` runs the task through the async client and loads the same documents."""
task = ApifyDatasetFromTaskCall(task_id, task_input={'maxPagesPerCrawl': 1})
"""`run_async` without input runs the task with its saved input and loads the same documents."""
task = ApifyDatasetFromTaskCall(task_id)

result = await task.run_async()
await task.close_async()
Expand Down
69 changes: 40 additions & 29 deletions tests/unit/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,33 @@ def test_actor_call_run_input_overrides_constructor_input(client: Mock) -> None:
assert client.actor.return_value.call.call_args.kwargs['run_input'] == {'a': 1, 'b': 2}


def test_actor_call_without_input_raises(client: Mock) -> None:
"""Running an Actor without input in either the constructor or `run` raises a `ValueError`."""
actor = ApifyDatasetFromActorCall('actor-id', to_document, apify_token=TOKEN)
EMPTY_INPUTS = [
pytest.param(None, id='no input'),
pytest.param({}, id='empty input'),
]

with pytest.raises(ValueError, match='Please provide the run_input'):
actor.run()
INPUT_COMPONENTS = [
pytest.param(ApifyDatasetFromActorCall, 'actor', 'run_input', id='actor call'),
pytest.param(ApifyDatasetFromTaskCall, 'task', 'task_input', id='task call'),
]


@pytest.mark.parametrize('empty_input', EMPTY_INPUTS)
@pytest.mark.parametrize(('component_class', 'resource', 'input_name'), INPUT_COMPONENTS)
def test_run_without_input_sends_no_input(
client: Mock,
component_class: type,
resource: str,
input_name: str,
empty_input: dict | None,
) -> None:
"""Without input, the run starts with no input, so the Actor uses its defaults and the task its saved input."""
component = component_class('id', to_document, empty_input, apify_token=TOKEN)

result = component.run(empty_input)

client.actor.assert_not_called()
assert getattr(client, resource).return_value.call.call_args.kwargs[input_name] is None
assert result == {'documents': [to_document(item) for item in ITEMS]}


def test_actor_call_without_run_raises(client: Mock) -> None:
Expand Down Expand Up @@ -289,16 +308,6 @@ def test_task_call_without_run_raises(client: Mock) -> None:
task.run()


def test_task_call_without_input_raises(client: Mock) -> None:
"""Running a task without input in either the constructor or `run` raises a `ValueError`."""
task = ApifyDatasetFromTaskCall('task-id', to_document, apify_token=TOKEN)

with pytest.raises(ValueError, match='Please provide the task_input'):
task.run()

client.task.assert_not_called()


@pytest.mark.parametrize(
'component',
[
Expand Down Expand Up @@ -481,20 +490,22 @@ async def test_task_call_run_async_runs_task_and_loads_its_dataset(async_client:
assert result == {'documents': [to_document(item) for item in ITEMS]}


@pytest.mark.parametrize(
('component', 'match'),
[
pytest.param(ApifyDatasetFromActorCall('id', to_document, apify_token=TOKEN), 'run_input', id='actor call'),
pytest.param(ApifyDatasetFromTaskCall('id', to_document, apify_token=TOKEN), 'task_input', id='task call'),
],
)
async def test_run_async_without_input_raises(async_client: Mock, component: Any, match: str) -> None:
"""`run_async` without input in either the constructor or the call raises a `ValueError`."""
with pytest.raises(ValueError, match=match):
await component.run_async()
@pytest.mark.parametrize('empty_input', EMPTY_INPUTS)
@pytest.mark.parametrize(('component_class', 'resource', 'input_name'), INPUT_COMPONENTS)
async def test_run_async_without_input_sends_no_input(
async_client: Mock,
component_class: type,
resource: str,
input_name: str,
empty_input: dict | None,
) -> None:
"""Without input, `run_async` starts the run with no input."""
component = component_class('id', to_document, empty_input, apify_token=TOKEN)

async_client.actor.assert_not_called()
async_client.task.assert_not_called()
result = await component.run_async(empty_input)

assert getattr(async_client, resource).return_value.call.call_args.kwargs[input_name] is None
assert result == {'documents': [to_document(item) for item in ITEMS]}


async def test_run_async_without_run_raises(async_client: Mock) -> None:
Expand Down
Loading