github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/utils/annotations_test.py (about) 1 # Licensed to the Apache Software Foundation (ASF) under one or more 2 # contributor license agreements. See the NOTICE file distributed with 3 # this work for additional information regarding copyright ownership. 4 # The ASF licenses this file to You under the Apache License, Version 2.0 5 # (the "License"); you may not use this file except in compliance with 6 # the License. You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 # 16 17 # pytype: skip-file 18 19 import unittest 20 import warnings 21 22 from apache_beam.utils.annotations import BeamDeprecationWarning 23 from apache_beam.utils.annotations import deprecated 24 25 26 class AnnotationTests(unittest.TestCase): 27 # Note: use different names for each of the functions decorated 28 # so that a warning is produced for each of them. 29 30 def setUp(self): 31 # This is necessary to ensure warnings are captured in all 32 # platforms (Windows/Unix). 33 warnings.resetwarnings() 34 35 def test_deprecated_with_since_current_message(self): 36 with warnings.catch_warnings(record=True) as w: 37 38 @deprecated(since='v.1', current='multiply', extra_message='Do this') 39 def fnc_test_deprecated_with_since_current_message(): 40 return 'lol' 41 42 fnc_test_deprecated_with_since_current_message() 43 self.check_annotation( 44 warning=w, 45 warning_type=BeamDeprecationWarning, 46 obj_name='fnc_test_deprecated_with_since_current_message', 47 annotation_type='deprecated', 48 label_check_list=[('since', True), ('instead', True), 49 ('Do this', True)]) 50 51 def test_deprecated_with_since_current(self): 52 with warnings.catch_warnings(record=True) as w: 53 54 @deprecated(since='v.1', current='multiply') 55 def fnc_test_deprecated_with_since_current(): 56 return 'lol' 57 58 fnc_test_deprecated_with_since_current() 59 self.check_annotation( 60 warning=w, 61 warning_type=BeamDeprecationWarning, 62 obj_name='fnc_test_deprecated_with_since_current', 63 annotation_type='deprecated', 64 label_check_list=[('since', True), ('instead', True)]) 65 66 def test_deprecated_without_current(self): 67 with warnings.catch_warnings(record=True) as w: 68 69 @deprecated(since='v.1') 70 def fnc_test_deprecated_without_current(): 71 return 'lol' 72 73 fnc_test_deprecated_without_current() 74 self.check_annotation( 75 warning=w, 76 warning_type=BeamDeprecationWarning, 77 obj_name='fnc_test_deprecated_without_current', 78 annotation_type='deprecated', 79 label_check_list=[('since', True), ('instead', False)]) 80 81 def test_deprecated_without_since_should_fail(self): 82 with warnings.catch_warnings(record=True) as w: 83 with self.assertRaises(TypeError): 84 85 @deprecated() 86 def fnc_test_deprecated_without_since_should_fail(): 87 return 'lol' 88 89 fnc_test_deprecated_without_since_should_fail() 90 assert not w 91 92 def test_deprecated_without_since_custom_should_fail(self): 93 with warnings.catch_warnings(record=True) as w: 94 with self.assertRaises(TypeError): 95 96 @deprecated(custom_message='Test %since%') 97 def fnc_test_deprecated_without_since_custom_should_fail(): 98 return 'lol' 99 100 fnc_test_deprecated_without_since_custom_should_fail() 101 assert not w 102 103 def test_deprecated_custom_no_replacements(self): 104 """Tests if custom message prints an empty string 105 for each replacement token when only the 106 custom_message and since parameter are given.""" 107 with warnings.catch_warnings(record=True) as w: 108 strSince = 'v1' 109 strCustom = 'Replacement:%since%%current%%extra%' 110 111 @deprecated(since=strSince, custom_message=strCustom) 112 def fnc_test_experimental_custom_no_replacements(): 113 return 'lol' 114 115 fnc_test_experimental_custom_no_replacements() 116 self.check_custom_annotation( 117 warning=w, 118 warning_type=BeamDeprecationWarning, 119 obj_name='fnc_test_experimental_custom_no_\ 120 replacements', 121 annotation_type='experimental', 122 intended_message=strCustom.replace('%since%', strSince).replace( 123 '%current%', '').replace('%extra%', '')) 124 125 def test_enforce_custom_since_deprecated_must_fail(self): 126 """Tests since replacement token inclusion on the 127 custom message for the decapreted string. If no 128 since replacement token is given, the annotation must fail""" 129 with warnings.catch_warnings(record=True) as w: 130 with self.assertRaises(TypeError): 131 strSince = 'v1' 132 strCustom = 'Replacement:' 133 134 @deprecated(since=strSince, custom_message=strCustom) 135 def fnc_test_experimental_custom_no_replacements(): 136 return 'lol' 137 138 fnc_test_experimental_custom_no_replacements() 139 assert not w 140 141 def test_deprecated_with_since_current_message_custom(self): 142 with warnings.catch_warnings(record=True) as w: 143 strSince = 'v.1' 144 strCurrent = 'multiply' 145 strExtra = 'Do this' 146 strCustom = "%name% Will be deprecated from %since%. \ 147 Please use %current% insted. Will %extra%" 148 149 @deprecated( 150 since=strSince, 151 current=strCurrent, 152 extra_message=strExtra, 153 custom_message=strCustom) 154 def fnc_test_deprecated_with_since_current_message_custom(): 155 return 'lol' 156 157 strName = fnc_test_deprecated_with_since_current_message_custom.__name__ 158 fnc_test_deprecated_with_since_current_message_custom() 159 self.check_custom_annotation( 160 warning=w, 161 warning_type=BeamDeprecationWarning, 162 obj_name='fnc_test_deprecated_with_since_\ 163 current_message_custom', 164 annotation_type='deprecated', 165 intended_message=strCustom.replace('%name%', strName).replace( 166 '%since%', 167 strSince).replace('%current%', 168 strCurrent).replace('%extra%', strExtra)) 169 170 def test_deprecated_with_since_current_message_class(self): 171 with warnings.catch_warnings(record=True) as w: 172 173 @deprecated(since='v.1', current='multiply', extra_message='Do this') 174 class Class_test_deprecated_with_since_current_message(object): 175 fooo = 'lol' 176 177 def __init__(self): 178 pass 179 180 def foo(self): 181 return 'lol' 182 183 foo = Class_test_deprecated_with_since_current_message() 184 strName = Class_test_deprecated_with_since_current_message.__name__ 185 foo.foo() 186 self.check_annotation( 187 warning=w, 188 warning_type=BeamDeprecationWarning, 189 obj_name=strName, 190 annotation_type='deprecated', 191 label_check_list=[('since', True), ('instead', True), 192 ('Do this', True)]) 193 194 # helper function 195 def check_annotation( 196 self, warning, warning_type, obj_name, annotation_type, label_check_list): 197 self.assertTrue(issubclass(warning[-1].category, warning_type)) 198 self.assertIn(obj_name + ' is ' + annotation_type, str(warning[-1].message)) 199 for label in label_check_list: 200 if label[1] is True: 201 self.assertIn(label[0], str(warning[-1].message)) 202 else: 203 self.assertNotIn(label[0], str(warning[-1].message)) 204 205 # Helper function for custom messages 206 def check_custom_annotation( 207 self, warning, warning_type, obj_name, annotation_type, intended_message): 208 self.assertTrue(issubclass(warning[-1].category, warning_type)) 209 self.assertIn(intended_message, str(warning[-1].message)) 210 211 212 if __name__ == '__main__': 213 unittest.main()