Search code examples
pythonairflowpython-unittest

Mocking NamedTemporaryFile in Python Unittest


I have a function like below:

def my_funct(configuration, **context):
    my_file_path = os.path.join(FILE_PATH, configuration["id"])
    f = NamedTemporaryFile("w", delete=False, dir=my_file_path)
    formatted_data_output_file = f.name
    f.close()
    task_instance.xcom_push(key="formatted_data_output_file",value=formatted_data_output_file)

I am trying to test the xcom_push key/value but I can't figure out how to mock the NamedTemporaryFile

I have the following test:

@patch("my_script.NamedTemporaryFile")
def test_xcom_push_vlaue(self, mock_tmp_file):
    dag = DAG(dag_id="mock_dag", start_date=now())

    task = DummyOperator(
        dag=dag,
        task_id="test_task",
    )

    mock_task_instance = TaskInstance(
        task,
        execution_date=now(),
    )

    context = {
        "dag": dag,
        "task_instance": mock_task_instance,
    }

    mock_tmp_file.return_value.__enter__.return_value.name = "name"

    with patch.object(
        mock_task_instance, "xcom_pull", side_effect=[self.configuration]
    ):

        with patch.object(mock_task_instance, "xcom_push") as mock_xcom_push:
            my_funct(configuration = {}, **context)

    mock_xcom_push.assert_called_once_with(
        key="formatted_data_output_file",
        value="/my/file/path/%s" % self.configuration["id"] + "/name"
    )

I get the following error when running the test: TypeError: expected str, bytes or os.PathLike object, not MagicMock I am new to mocking an having trouble figuring it out, any help would be appriciated.

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

p = <MagicMock name='NamedTemporaryFile().name'id='139929782915024'>

def dirname(p):"""Returns the directory component of a pathname"""
>       p = os.fspath(p)
E       TypeError: expected str, bytes or os.PathLike object, not MagicMock

/usr/local/lib/python3.7/posixpath.py:156: TypeError
-------------- generated xml file: /opt/airflow/test_results.xml ---------------

Solution

  • Why are you patching the __enter__? You're not using a with statement.

    Try replacing this line:

    mock_tmp_file.return_value.__enter__.return_value.name = "name"
    

    With this:

    mock_tmp_file.return_value.name = "name"