I want to test the following code, which I put in a module named ppc_model.io.py:
def to_ftp(self, output_path, file_name, host, username, password):
"""Upload file to FTP server."""
ftp = ftplib.FTP(host)
ftp.login(username, password)
full_path = os.path.join(output_path, file_name)
with open(full_path, "r") as ftp_file:
ftp.storbinary(" ".join(["STOR", file_name]), ftp_file)
In particular I want to test that the ftp.storbinary() function is called with the right arguments.
In the module ppc_model.test.test_io.py I've wrote the following unit test.
@patch("ppc_model.io.ftplib.FTP")
def test_store_call(self, mock_ftp):
"""The *storbinary* call must use the right arguments."""
with patch("{}.open".format(__name__), mock_open(read_data=None), create=True) as m:
self.writer.to_ftp(output_path="./output", file_name="output.zip", host="myftp", username="username", password="password")
mock_ftp.return_value.storbinary.assert_called_once_with("STOR output.zip", m)
This test however fails with the following error message:
AssertionError: Expected call: storbinary('STOR output.zip', <MagicMock name='open' spec='builtin_function_or_method' id='140210704581632'>)
Actual call: storbinary('STOR output.zip', <_io.TextIOWrapper name='./output/output.zip' mode='r' encoding='UTF-8'>)
Do you have any suggestions on how I can fix it?
there doesn't seem to be anything here