def test_open_maybe_zipped_archive(self, mocked_ZipFile, mocked_is_zipfile): mocked_is_zipfile.return_value = True instance = mocked_ZipFile.return_value instance.open.return_value = mock.mock_open(read_data="data") utils.open_maybe_zipped('/path/to/archive.zip/deep/path/to/file.txt') mocked_is_zipfile.assert_called_once() (args, kwargs) = mocked_is_zipfile.call_args_list[0] self.assertEqual('/path/to/archive.zip', args[0]) mocked_ZipFile.assert_called_once() (args, kwargs) = mocked_ZipFile.call_args_list[0] self.assertEqual('/path/to/archive.zip', args[0]) instance.open.assert_called_once() (args, kwargs) = instance.open.call_args_list[0] self.assertEqual('deep/path/to/file.txt', args[0])
def _load(self, dag): try: code = None # Read code with open_maybe_zipped(dag.fileloc, 'r') as f: code = f.read() except Exception as ex: logging.error(ex) flash('Error loading DAG [{dag.dag_id}]'.format(dag=dag), 'error') finally: return code
def _save(self, dag): try: code = None code = request.form['code'] with open_maybe_zipped(dag.fileloc, 'w') as f: f.write(code) flash('DAG [{dag.dag_id}] saved successfully'.format(dag=dag), 'error') except Exception as ex: logging.error(ex) flash('Error saving DAG [{dag.dag_id}]'.format(dag=dag), 'error') finally: return code
def test_open_maybe_zipped_normal_file_with_zip_in_name(self): path = '/path/to/fakearchive.zip.other/file.txt' with mock.patch('io.open', mock.mock_open(read_data="data")) as mock_file: utils.open_maybe_zipped(path) mock_file.assert_called_with(path, mode='r')
def test_open_maybe_zipped_normal_file(self): with mock.patch('io.open', mock.mock_open(read_data="data")) as mock_file: utils.open_maybe_zipped('/path/to/some/file.txt') mock_file.assert_called_with('/path/to/some/file.txt', mode='r')
def test_open_maybe_zipped_normal_file_with_zip_in_name(self): path = '/path/to/fakearchive.zip.other/file.txt' with mock.patch( 'io.open', mock.mock_open(read_data="data")) as mock_file: utils.open_maybe_zipped(path) mock_file.assert_called_with(path, mode='r')
def test_open_maybe_zipped_normal_file(self): with mock.patch( 'io.open', mock.mock_open(read_data="data")) as mock_file: utils.open_maybe_zipped('/path/to/some/file.txt') mock_file.assert_called_with('/path/to/some/file.txt', mode='r')