|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +from unittest import mock |
| 19 | + |
| 20 | +import pytest |
| 21 | +from mock import patch |
| 22 | +from requests import HTTPError |
| 23 | +from tenacity import RetryError |
| 24 | + |
| 25 | +from airflow.providers.google.cloud.hooks import dataprep |
| 26 | + |
| 27 | +JOB_ID = 1234567 |
| 28 | +URL = "https://api.clouddataprep.com/v4/jobGroups" |
| 29 | +TOKEN = "1111" |
| 30 | +EXTRA = {"token": TOKEN} |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(scope="class") |
| 34 | +def mock_hook(): |
| 35 | + with mock.patch("airflow.hooks.base_hook.BaseHook.get_connection") as conn: |
| 36 | + hook = dataprep.GoogleDataprepHook(dataprep_conn_id="dataprep_conn_id") |
| 37 | + conn.return_value.extra_dejson = EXTRA |
| 38 | + yield hook |
| 39 | + |
| 40 | + |
| 41 | +class TestGoogleDataprepHook: |
| 42 | + def test_get_token(self, mock_hook): |
| 43 | + assert mock_hook._token == TOKEN |
| 44 | + |
| 45 | + @patch("airflow.providers.google.cloud.hooks.dataprep.requests.get") |
| 46 | + def test_mock_should_be_called_once_with_params(self, mock_get_request, mock_hook): |
| 47 | + mock_hook.get_jobs_for_job_group(job_id=JOB_ID) |
| 48 | + mock_get_request.assert_called_once_with( |
| 49 | + f"{URL}/{JOB_ID}/jobs", |
| 50 | + headers={ |
| 51 | + "Content-Type": "application/json", |
| 52 | + "Authorization": f"Bearer {TOKEN}", |
| 53 | + }, |
| 54 | + ) |
| 55 | + |
| 56 | + @patch( |
| 57 | + "airflow.providers.google.cloud.hooks.dataprep.requests.get", |
| 58 | + side_effect=[HTTPError(), mock.MagicMock()], |
| 59 | + ) |
| 60 | + def test_should_pass_after_retry(self, mock_get_request, mock_hook): |
| 61 | + mock_hook.get_jobs_for_job_group(JOB_ID) |
| 62 | + assert mock_get_request.call_count == 2 |
| 63 | + |
| 64 | + @patch( |
| 65 | + "airflow.providers.google.cloud.hooks.dataprep.requests.get", |
| 66 | + side_effect=[mock.MagicMock(), HTTPError()], |
| 67 | + ) |
| 68 | + def test_should_not_retry_after_success(self, mock_get_request, mock_hook): |
| 69 | + mock_hook.get_jobs_for_job_group.retry.sleep = mock.Mock() # pylint: disable=no-member |
| 70 | + mock_hook.get_jobs_for_job_group(JOB_ID) |
| 71 | + assert mock_get_request.call_count == 1 |
| 72 | + |
| 73 | + @patch( |
| 74 | + "airflow.providers.google.cloud.hooks.dataprep.requests.get", |
| 75 | + side_effect=[ |
| 76 | + HTTPError(), |
| 77 | + HTTPError(), |
| 78 | + HTTPError(), |
| 79 | + HTTPError(), |
| 80 | + mock.MagicMock(), |
| 81 | + ], |
| 82 | + ) |
| 83 | + def test_should_retry_after_four_errors(self, mock_get_request, mock_hook): |
| 84 | + mock_hook.get_jobs_for_job_group.retry.sleep = mock.Mock() # pylint: disable=no-member |
| 85 | + mock_hook.get_jobs_for_job_group(JOB_ID) |
| 86 | + assert mock_get_request.call_count == 5 |
| 87 | + |
| 88 | + @patch( |
| 89 | + "airflow.providers.google.cloud.hooks.dataprep.requests.get", |
| 90 | + side_effect=[HTTPError(), HTTPError(), HTTPError(), HTTPError(), HTTPError()], |
| 91 | + ) |
| 92 | + def test_raise_error_after_five_calls(self, mock_get_request, mock_hook): |
| 93 | + with pytest.raises(RetryError) as err: |
| 94 | + mock_hook.get_jobs_for_job_group.retry.sleep = mock.Mock() # pylint: disable=no-member |
| 95 | + mock_hook.get_jobs_for_job_group(JOB_ID) |
| 96 | + assert "HTTPError" in str(err) |
| 97 | + assert mock_get_request.call_count == 5 |
0 commit comments