from unittest import mock from vector_utils.connections import transporter def test_transporter_class(ftp_server): """Test connection base class.""" class ConnObj: connection_type = 'ftp' domain_name = 'localhost' user_name = ftp_server.username password = ftp_server.password port = ftp_server.server_port c = transporter.Transporter(ConnObj) check_connection_fails = False try: c.check_connection() except TypeError: # we expect this to fail because ftp connection class requires an arg check_connection_fails = True assert check_connection_fails is True assert c.conn_obj.connection_type == 'ftp' def test_transporter_class_bad_type(): """Test connection base class.""" class ConnObj: connection_type = 'bad_connection_type' bad_connection = False try: transporter.Transporter(ConnObj) except: bad_connection = True assert bad_connection is True def test_transporter_class_logger( sftp_server_and_conn_obj, tmpdir): """Test sftp connection class downloads files and logger.""" sftpserver, conn_obj = sftp_server_and_conn_obj with sftpserver.serve_content({'a_dir': {'somefile.txt': 'File content'}}): logger = mock.MagicMock() c = transporter.Transporter(conn_obj, logger) local_file_location = '{}/{}'.format(tmpdir, 'somefile.txt') transfer_files_list = [{ 'local': local_file_location, 'remote': '/a_dir/somefile.txt' }] c.transfer_files(transfer_files_list, transfer_mode='download') assert tmpdir.listdir() == [local_file_location] c.close_connection() assert logger.info.call_count == 4 def test_transporter_class_plusmusic( sftp_server_and_conn_obj, tmpdir): """Test plusmusic connection class downloads files and logger.""" sftpserver, conn_obj = sftp_server_and_conn_obj with sftpserver.serve_content({'a_dir': {'somefile.txt': 'File content'}}): logger = mock.MagicMock() conn_obj.connection_type = 'plusmusic' c = transporter.Transporter(conn_obj, logger) local_file_location = '{}/{}'.format(tmpdir, 'somefile.txt') transfer_files_list = [{ 'local': local_file_location, 'remote': '/a_dir/somefile.txt' }] c.transfer_files(transfer_files_list, transfer_mode='download') assert tmpdir.listdir() == [local_file_location] c.close_connection() assert logger.info.call_count == 4