diff --git a/src/apify_haystack/_dataset.py b/src/apify_haystack/_dataset.py index 438dd76..c38ba78 100644 --- a/src/apify_haystack/_dataset.py +++ b/src/apify_haystack/_dataset.py @@ -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. @@ -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 @@ -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( @@ -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( @@ -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. @@ -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( @@ -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( @@ -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( diff --git a/tests/integration/test_dataset.py b/tests/integration/test_dataset.py index ba301c0..045e2b1 100644 --- a/tests/integration/test_dataset.py +++ b/tests/integration/test_dataset.py @@ -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() diff --git a/tests/unit/test_dataset.py b/tests/unit/test_dataset.py index b69f016..90c3bdf 100644 --- a/tests/unit/test_dataset.py +++ b/tests/unit/test_dataset.py @@ -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: @@ -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', [ @@ -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: