From 2e790ce4caf8f40ec54bff06ec857c2bb777e7be Mon Sep 17 00:00:00 2001 From: Meysam Date: Sat, 12 Sep 2020 01:43:43 +0430 Subject: [PATCH] file-transfer: writing tests and ensuring that all is going well (#2413) * file-transfer: writing tests and ensuring that all is going well * def send_file(filename: str = "mytext.txt", testing: bool = False) -> None: * send_file(filename="mytext.txt", testing=True) * Update send_file.py * requirements.txt: lxml Co-authored-by: Christian Clauss --- file_transfer/send_file.py | 17 ++++++-------- file_transfer/tests/test_send_file.py | 32 +++++++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 file_transfer/tests/test_send_file.py diff --git a/file_transfer/send_file.py b/file_transfer/send_file.py index 6494114a9..5b53471df 100644 --- a/file_transfer/send_file.py +++ b/file_transfer/send_file.py @@ -1,11 +1,6 @@ -if __name__ == "__main__": - import socket # Import socket module +def send_file(filename: str = "mytext.txt", testing: bool = False) -> None: + import socket - ONE_CONNECTION_ONLY = ( - True # Set this to False if you wish to continuously accept connections - ) - - filename = "mytext.txt" port = 12312 # Reserve a port for your service. sock = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name @@ -29,10 +24,12 @@ if __name__ == "__main__": print("Done sending") conn.close() - if ( - ONE_CONNECTION_ONLY - ): # This is to make sure that the program doesn't hang while testing + if testing: # Allow the test to complete break sock.shutdown(1) sock.close() + + +if __name__ == "__main__": + send_file() diff --git a/file_transfer/tests/test_send_file.py b/file_transfer/tests/test_send_file.py new file mode 100644 index 000000000..170c2c0ae --- /dev/null +++ b/file_transfer/tests/test_send_file.py @@ -0,0 +1,32 @@ +from unittest.mock import patch, Mock + + +from file_transfer.send_file import send_file + + +@patch("socket.socket") +@patch("builtins.open") +def test_send_file_running_as_expected(file, sock): + # ===== initialization ===== + conn = Mock() + sock.return_value.accept.return_value = conn, Mock() + f = iter([1, None]) + file.return_value.__enter__.return_value.read.side_effect = lambda _: next(f) + + # ===== invoke ===== + send_file(filename="mytext.txt", testing=True) + + # ===== ensurance ===== + sock.assert_called_once() + sock.return_value.bind.assert_called_once() + sock.return_value.listen.assert_called_once() + sock.return_value.accept.assert_called_once() + conn.recv.assert_called_once() + + file.return_value.__enter__.assert_called_once() + file.return_value.__enter__.return_value.read.assert_called() + + conn.send.assert_called_once() + conn.close.assert_called_once() + sock.return_value.shutdown.assert_called_once() + sock.return_value.close.assert_called_once() diff --git a/requirements.txt b/requirements.txt index 8362afc62..b070ffdf6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,7 @@ black fake_useragent flake8 keras +lxml matplotlib mypy numpy