|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | + |
| 20 | +import unittest |
| 21 | + |
| 22 | +from sqlalchemy.pool import NullPool |
| 23 | + |
| 24 | +from airflow import settings |
| 25 | +from tests.compat import patch |
| 26 | +from tests.test_utils.config import conf_vars |
| 27 | + |
| 28 | +SQL_ALCHEMY_CONNECT_ARGS = { |
| 29 | + 'test': 43503, |
| 30 | + 'dict': { |
| 31 | + 'is': 1, |
| 32 | + 'supported': 'too' |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +class TestSqlAlchemySettings(unittest.TestCase): |
| 38 | + def setUp(self): |
| 39 | + self.old_engine = settings.engine |
| 40 | + self.old_session = settings.Session |
| 41 | + self.old_conn = settings.SQL_ALCHEMY_CONN |
| 42 | + settings.SQL_ALCHEMY_CONN = "mysql+foobar://user:pass@host/dbname?inline=param&another=param" |
| 43 | + |
| 44 | + def tearDown(self): |
| 45 | + settings.engine = self.old_engine |
| 46 | + settings.Session = self.old_session |
| 47 | + settings.SQL_ALCHEMY_CONN = self.old_conn |
| 48 | + |
| 49 | + @patch('airflow.settings.setup_event_handlers') |
| 50 | + @patch('airflow.settings.scoped_session') |
| 51 | + @patch('airflow.settings.sessionmaker') |
| 52 | + @patch('airflow.settings.create_engine') |
| 53 | + def test_configure_orm_with_default_values(self, |
| 54 | + mock_create_engine, |
| 55 | + mock_sessionmaker, |
| 56 | + mock_scoped_session, |
| 57 | + mock_setup_event_handlers): |
| 58 | + settings.configure_orm() |
| 59 | + mock_create_engine.assert_called_once_with( |
| 60 | + settings.SQL_ALCHEMY_CONN, |
| 61 | + connect_args={}, |
| 62 | + encoding='utf-8', |
| 63 | + max_overflow=10, |
| 64 | + pool_pre_ping=True, |
| 65 | + pool_recycle=1800, |
| 66 | + pool_size=5 |
| 67 | + ) |
| 68 | + |
| 69 | + @patch('airflow.settings.setup_event_handlers') |
| 70 | + @patch('airflow.settings.scoped_session') |
| 71 | + @patch('airflow.settings.sessionmaker') |
| 72 | + @patch('airflow.settings.create_engine') |
| 73 | + def test_sql_alchemy_connect_args(self, |
| 74 | + mock_create_engine, |
| 75 | + mock_sessionmaker, |
| 76 | + mock_scoped_session, |
| 77 | + mock_setup_event_handlers): |
| 78 | + config = { |
| 79 | + ('core', 'sql_alchemy_connect_args'): 'tests.test_sqlalchemy_config.SQL_ALCHEMY_CONNECT_ARGS', |
| 80 | + ('core', 'sql_alchemy_pool_enabled'): 'False' |
| 81 | + } |
| 82 | + with conf_vars(config): |
| 83 | + settings.configure_orm() |
| 84 | + mock_create_engine.assert_called_once_with( |
| 85 | + settings.SQL_ALCHEMY_CONN, |
| 86 | + connect_args=SQL_ALCHEMY_CONNECT_ARGS, |
| 87 | + poolclass=NullPool, |
| 88 | + encoding='utf-8' |
| 89 | + ) |
| 90 | + |
| 91 | + @patch('airflow.settings.setup_event_handlers') |
| 92 | + @patch('airflow.settings.scoped_session') |
| 93 | + @patch('airflow.settings.sessionmaker') |
| 94 | + @patch('airflow.settings.create_engine') |
| 95 | + def test_sql_alchemy_invalid_connect_args(self, |
| 96 | + mock_create_engine, |
| 97 | + mock_sessionmaker, |
| 98 | + mock_scoped_session, |
| 99 | + mock_setup_event_handlers): |
| 100 | + config = { |
| 101 | + ('core', 'sql_alchemy_connect_args'): 'does.not.exist', |
| 102 | + ('core', 'sql_alchemy_pool_enabled'): 'False' |
| 103 | + } |
| 104 | + with self.assertRaises(ImportError): |
| 105 | + with conf_vars(config): |
| 106 | + settings.configure_orm() |
0 commit comments