|
7 | 7 | # to you under the Apache License, Version 2.0 (the
|
8 | 8 | # "License"); you may not use this file except in compliance
|
9 | 9 | # with the License. You may obtain a copy of the License at
|
10 |
| -# |
| 10 | +# |
11 | 11 | # http://www.apache.org/licenses/LICENSE-2.0
|
12 |
| -# |
| 12 | +# |
13 | 13 | # Unless required by applicable law or agreed to in writing,
|
14 | 14 | # software distributed under the License is distributed on an
|
15 | 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
@@ -207,5 +207,53 @@ def test_invalid_object_path(self, mock_parent_init):
|
207 | 207 | gcs_bucket_helper.google_cloud_to_local(file_name)
|
208 | 208 |
|
209 | 209 | self.assertEquals(
|
210 |
| - 'Invalid Google Cloud Storage (GCS) object path: {}.'.format(file_name), |
| 210 | + 'Invalid Google Cloud Storage (GCS) object path: {}'.format(file_name), |
| 211 | + str(context.exception)) |
| 212 | + |
| 213 | + @mock.patch( |
| 214 | + 'airflow.contrib.operators.dataflow_operator.GoogleCloudBucketHelper.__init__' |
| 215 | + ) |
| 216 | + def test_valid_object(self, mock_parent_init): |
| 217 | + |
| 218 | + file_name = 'gs://test-bucket/path/to/obj.jar' |
| 219 | + mock_parent_init.return_value = None |
| 220 | + |
| 221 | + gcs_bucket_helper = GoogleCloudBucketHelper() |
| 222 | + gcs_bucket_helper._gcs_hook = mock.Mock() |
| 223 | + |
| 224 | + def _mock_download(bucket, object, filename=None): |
| 225 | + text_file_contents = 'text file contents' |
| 226 | + with open(filename, 'w') as text_file: |
| 227 | + text_file.write(text_file_contents) |
| 228 | + return text_file_contents |
| 229 | + |
| 230 | + gcs_bucket_helper._gcs_hook.download.side_effect = _mock_download |
| 231 | + |
| 232 | + local_file = gcs_bucket_helper.google_cloud_to_local(file_name) |
| 233 | + self.assertIn('obj.jar', local_file) |
| 234 | + |
| 235 | + @mock.patch( |
| 236 | + 'airflow.contrib.operators.dataflow_operator.GoogleCloudBucketHelper.__init__' |
| 237 | + ) |
| 238 | + def test_empty_object(self, mock_parent_init): |
| 239 | + |
| 240 | + file_name = 'gs://test-bucket/path/to/obj.jar' |
| 241 | + mock_parent_init.return_value = None |
| 242 | + |
| 243 | + gcs_bucket_helper = GoogleCloudBucketHelper() |
| 244 | + gcs_bucket_helper._gcs_hook = mock.Mock() |
| 245 | + |
| 246 | + def _mock_download(bucket, object, filename=None): |
| 247 | + text_file_contents = '' |
| 248 | + with open(filename, 'w') as text_file: |
| 249 | + text_file.write(text_file_contents) |
| 250 | + return text_file_contents |
| 251 | + |
| 252 | + gcs_bucket_helper._gcs_hook.download.side_effect = _mock_download |
| 253 | + |
| 254 | + with self.assertRaises(Exception) as context: |
| 255 | + gcs_bucket_helper.google_cloud_to_local(file_name) |
| 256 | + |
| 257 | + self.assertEquals( |
| 258 | + 'Failed to download Google Cloud Storage (GCS) object: {}'.format(file_name), |
211 | 259 | str(context.exception))
|
0 commit comments