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: open_maybe_zipped(path) mock_file.assert_called_once_with(path, mode='r')
def test_open_maybe_zipped_archive(self, mocked_zip_file, mocked_is_zipfile): mocked_is_zipfile.return_value = True instance = mocked_zip_file.return_value instance.open.return_value = mock.mock_open(read_data="data") open_maybe_zipped('/path/to/archive.zip/deep/path/to/file.txt') mocked_is_zipfile.assert_called_once_with('/path/to/archive.zip') mocked_zip_file.assert_called_once_with('/path/to/archive.zip', mode='r') instance.open.assert_called_once_with('deep/path/to/file.txt')
def read_packaged_file(fileloc): """ Opens a plain file from a packages dag input as the relative path of the file to dag :return: str content of file """ with open_maybe_zipped(fileloc, "r") as f: content = f.read() return content.decode("utf-8")
def _compare_example_dags(self, example_dags): with create_session() as session: for dag in example_dags.values(): self.assertTrue(DagCode.has_dag(dag.fileloc)) dag_fileloc_hash = DagCode.dag_fileloc_hash(dag.fileloc) result = session.query( DagCode.fileloc, DagCode.fileloc_hash, DagCode.source_code) \ .filter(DagCode.fileloc == dag.fileloc) \ .filter(DagCode.fileloc_hash == dag_fileloc_hash) \ .one() self.assertEqual(result.fileloc, dag.fileloc) with open_maybe_zipped(dag.fileloc, 'r') as source: source_code = source.read() self.assertEqual(result.source_code, source_code)
def _compare_example_dags(self, example_dags): with create_session() as session: for dag in example_dags.values(): if dag.is_subdag: dag.fileloc = dag.parent_dag.fileloc assert DagCode.has_dag(dag.fileloc) dag_fileloc_hash = DagCode.dag_fileloc_hash(dag.fileloc) result = ( session.query(DagCode.fileloc, DagCode.fileloc_hash, DagCode.source_code) .filter(DagCode.fileloc == dag.fileloc) .filter(DagCode.fileloc_hash == dag_fileloc_hash) .one() ) assert result.fileloc == dag.fileloc with open_maybe_zipped(dag.fileloc, 'r') as source: source_code = source.read() assert result.source_code == source_code
def test_open_maybe_zipped_normal_file(self): with mock.patch('builtins.open', mock.mock_open(read_data="data")) as mock_file: open_maybe_zipped('/path/to/some/file.txt') mock_file.assert_called_once_with('/path/to/some/file.txt', mode='r')
def _get_code_from_file(fileloc): with open_maybe_zipped(fileloc, 'r') as f: code = f.read() return code
def test_open_maybe_zipped_archive(self): test_file_path = os.path.join(TEST_DAGS_FOLDER, "test_zip.zip", "test_zip.py") with open_maybe_zipped(test_file_path, 'r') as test_file: content = test_file.read() assert isinstance(content, str)
def test_open_maybe_zipped_normal_file(self): test_file_path = os.path.join(TEST_DAGS_FOLDER, "no_dags.py") with open_maybe_zipped(test_file_path, 'r') as test_file: content = test_file.read() assert isinstance(content, str)
def _read_code(cls, fileloc: str): with open_maybe_zipped(fileloc, 'r') as source: source_code = source.read() return source_code