Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/tests/providers/redis/hooks/test_redis.py: 0%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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.
18from __future__ import annotations
20from unittest import mock
22import pytest
24from airflow.exceptions import AirflowProviderDeprecationWarning
25from airflow.models import Connection
26from airflow.providers.redis.hooks.redis import RedisHook
28pytestmark = pytest.mark.db_test
31class TestRedisHook:
32 deprecation_message = (
33 "Extra parameter `ssl_cert_file` deprecated and will be removed "
34 "in a future release. Please use `ssl_certfile` instead."
35 )
37 def test_get_conn(self):
38 hook = RedisHook(redis_conn_id="redis_default")
39 assert hook.redis is None
41 assert hook.host is None, "host initialised as None."
42 assert hook.port is None, "port initialised as None."
43 assert hook.password is None, "password initialised as None."
44 assert hook.db is None, "db initialised as None."
45 assert hook.get_conn() is hook.get_conn(), "Connection initialized only if None."
47 @mock.patch("airflow.providers.redis.hooks.redis.Redis")
48 @mock.patch(
49 "airflow.providers.redis.hooks.redis.RedisHook.get_connection",
50 return_value=Connection(
51 login="user",
52 password="password",
53 host="remote_host",
54 port=1234,
55 extra="""{
56 "db": 2,
57 "ssl": true,
58 "ssl_cert_reqs": "required",
59 "ssl_ca_certs": "/path/to/custom/ca-cert",
60 "ssl_keyfile": "/path/to/key-file",
61 "ssl_certfile": "/path/to/cert-file",
62 "ssl_check_hostname": true
63 }""",
64 ),
65 )
66 def test_get_conn_with_extra_config(self, mock_get_connection, mock_redis):
67 connection = mock_get_connection.return_value
68 hook = RedisHook()
70 hook.get_conn()
71 mock_redis.assert_called_once_with(
72 host=connection.host,
73 username=connection.login,
74 password=connection.password,
75 port=connection.port,
76 db=connection.extra_dejson["db"],
77 ssl=connection.extra_dejson["ssl"],
78 ssl_cert_reqs=connection.extra_dejson["ssl_cert_reqs"],
79 ssl_ca_certs=connection.extra_dejson["ssl_ca_certs"],
80 ssl_keyfile=connection.extra_dejson["ssl_keyfile"],
81 ssl_certfile=connection.extra_dejson["ssl_certfile"],
82 ssl_check_hostname=connection.extra_dejson["ssl_check_hostname"],
83 )
85 @mock.patch("airflow.providers.redis.hooks.redis.Redis")
86 @mock.patch(
87 "airflow.providers.redis.hooks.redis.RedisHook.get_connection",
88 return_value=Connection(
89 password="password",
90 host="remote_host",
91 port=1234,
92 extra="""{
93 "db": 2,
94 "ssl": true,
95 "ssl_cert_reqs": "required",
96 "ssl_ca_certs": "/path/to/custom/ca-cert",
97 "ssl_keyfile": "/path/to/key-file",
98 "ssl_cert_file": "/path/to/cert-file",
99 "ssl_check_hostname": true
100 }""",
101 ),
102 )
103 def test_get_conn_with_deprecated_extra_config(self, mock_get_connection, mock_redis):
104 connection = mock_get_connection.return_value
105 hook = RedisHook()
107 with pytest.warns(AirflowProviderDeprecationWarning, match=self.deprecation_message):
108 hook.get_conn()
109 mock_redis.assert_called_once_with(
110 host=connection.host,
111 password=connection.password,
112 username=None,
113 port=connection.port,
114 db=connection.extra_dejson["db"],
115 ssl=connection.extra_dejson["ssl"],
116 ssl_cert_reqs=connection.extra_dejson["ssl_cert_reqs"],
117 ssl_ca_certs=connection.extra_dejson["ssl_ca_certs"],
118 ssl_keyfile=connection.extra_dejson["ssl_keyfile"],
119 ssl_certfile=connection.extra_dejson["ssl_cert_file"],
120 ssl_check_hostname=connection.extra_dejson["ssl_check_hostname"],
121 )
123 def test_get_conn_password_stays_none(self):
124 hook = RedisHook(redis_conn_id="redis_default")
125 hook.get_conn()
126 assert hook.password is None