Skip to content

Commit

Permalink
Add tests for yandex hook (#9665)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: a6b04d7b9a6117c66e8b586fe9fd2a955827a0e7
  • Loading branch information
subkanthi authored and Cloud Composer Team committed Sep 12, 2024
1 parent 27420c4 commit e04ffd2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
84 changes: 84 additions & 0 deletions tests/providers/yandex/hooks/test_yandex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import unittest
from unittest import mock

from airflow.exceptions import AirflowException
from airflow.providers.yandex.hooks.yandex import YandexCloudBaseHook


class TestYandexHook(unittest.TestCase):

@mock.patch('airflow.hooks.base_hook.BaseHook.get_connection')
@mock.patch('airflow.providers.yandex.hooks.yandex.YandexCloudBaseHook._get_credentials')
def test_client_created_without_exceptions(self, get_credentials_mock,
get_connection_mock):
""" tests `init` method to validate client creation when all parameters are passed """

# Inputs to constructor
default_folder_id = 'test_id'
default_public_ssh_key = 'test_key'

extra_dejson = '{"extras": "extra"}'
get_connection_mock['extra_dejson'] = "sdsd"
get_connection_mock.extra_dejson = '{"extras": "extra"}'
get_connection_mock.return_value = mock.\
Mock(connection_id='yandexcloud_default', extra_dejson=extra_dejson)
get_credentials_mock.return_value = {"token": 122323}

hook = YandexCloudBaseHook(None,
default_folder_id, default_public_ssh_key)
self.assertIsNotNone(hook.client)

@mock.patch('airflow.hooks.base_hook.BaseHook.get_connection')
def test_get_credentials_raise_exception(self, get_connection_mock):

""" tests 'get_credentials' method raising exception if none of the required fields are passed."""

# Inputs to constructor
default_folder_id = 'test_id'
default_public_ssh_key = 'test_key'

extra_dejson = '{"extras": "extra"}'
get_connection_mock['extra_dejson'] = "sdsd"
get_connection_mock.extra_dejson = '{"extras": "extra"}'
get_connection_mock.return_value = mock.Mock(connection_id='yandexcloud_default',
extra_dejson=extra_dejson)

self.assertRaises(AirflowException, YandexCloudBaseHook, None,
default_folder_id, default_public_ssh_key)

@mock.patch('airflow.hooks.base_hook.BaseHook.get_connection')
@mock.patch('airflow.providers.yandex.hooks.yandex.YandexCloudBaseHook._get_credentials')
def test_get_field(self, get_credentials_mock, get_connection_mock):
# Inputs to constructor
default_folder_id = 'test_id'
default_public_ssh_key = 'test_key'

extra_dejson = {"extra__yandexcloud__one": "value_one"}
get_connection_mock['extra_dejson'] = "sdsd"
get_connection_mock.extra_dejson = '{"extras": "extra"}'
get_connection_mock.return_value = mock.Mock(connection_id='yandexcloud_default',
extra_dejson=extra_dejson)
get_credentials_mock.return_value = {"token": 122323}

hook = YandexCloudBaseHook(None,
default_folder_id, default_public_ssh_key)

self.assertEqual(hook._get_field('one'), 'value_one')
3 changes: 1 addition & 2 deletions tests/test_project_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
'tests/providers/microsoft/azure/sensors/test_azure_cosmos.py',
'tests/providers/microsoft/azure/log/test_wasb_task_handler.py',
'tests/providers/microsoft/mssql/hooks/test_mssql.py',
'tests/providers/samba/hooks/test_samba.py',
'tests/providers/yandex/hooks/test_yandex.py'
'tests/providers/samba/hooks/test_samba.py'
}


Expand Down

0 comments on commit e04ffd2

Please sign in to comment.