Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/tests/providers/redis/hooks/test_redis.py: 0%

24 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:35 +0000

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 

19 

20from unittest import mock 

21 

22from airflow.models import Connection 

23from airflow.providers.redis.hooks.redis import RedisHook 

24 

25 

26class TestRedisHook: 

27 def test_get_conn(self): 

28 hook = RedisHook(redis_conn_id="redis_default") 

29 assert hook.redis is None 

30 

31 assert hook.host is None, "host initialised as None." 

32 assert hook.port is None, "port initialised as None." 

33 assert hook.password is None, "password initialised as None." 

34 assert hook.db is None, "db initialised as None." 

35 assert hook.get_conn() is hook.get_conn(), "Connection initialized only if None." 

36 

37 @mock.patch("airflow.providers.redis.hooks.redis.Redis") 

38 @mock.patch( 

39 "airflow.providers.redis.hooks.redis.RedisHook.get_connection", 

40 return_value=Connection( 

41 password="password", 

42 host="remote_host", 

43 port=1234, 

44 extra="""{ 

45 "db": 2, 

46 "ssl": true, 

47 "ssl_cert_reqs": "required", 

48 "ssl_ca_certs": "/path/to/custom/ca-cert", 

49 "ssl_keyfile": "/path/to/key-file", 

50 "ssl_cert_file": "/path/to/cert-file", 

51 "ssl_check_hostname": true 

52 }""", 

53 ), 

54 ) 

55 def test_get_conn_with_extra_config(self, mock_get_connection, mock_redis): 

56 connection = mock_get_connection.return_value 

57 hook = RedisHook() 

58 

59 hook.get_conn() 

60 mock_redis.assert_called_once_with( 

61 host=connection.host, 

62 password=connection.password, 

63 port=connection.port, 

64 db=connection.extra_dejson["db"], 

65 ssl=connection.extra_dejson["ssl"], 

66 ssl_cert_reqs=connection.extra_dejson["ssl_cert_reqs"], 

67 ssl_ca_certs=connection.extra_dejson["ssl_ca_certs"], 

68 ssl_keyfile=connection.extra_dejson["ssl_keyfile"], 

69 ssl_cert_file=connection.extra_dejson["ssl_cert_file"], 

70 ssl_check_hostname=connection.extra_dejson["ssl_check_hostname"], 

71 ) 

72 

73 def test_get_conn_password_stays_none(self): 

74 hook = RedisHook(redis_conn_id="redis_default") 

75 hook.get_conn() 

76 assert hook.password is None