Best Python code snippet using avocado_python
test_config.py
Source:test_config.py
...19 # Our test fixture in conftest.py sets "chained_assignment"20 # to "raise" only after all test methods have been setup.21 # However, after this setup, there is no longer any22 # "chained_assignment" option, so re-register it.23 self.cf.register_option("chained_assignment", "raise")24 def teardown_method(self, method):25 setattr(self.cf, "_global_config", self.gc)26 setattr(self.cf, "_deprecated_options", self.do)27 setattr(self.cf, "_registered_options", self.ro)28 def test_api(self):29 # the pandas object exposes the user API30 assert hasattr(pd, "get_option")31 assert hasattr(pd, "set_option")32 assert hasattr(pd, "reset_option")33 assert hasattr(pd, "describe_option")34 def test_is_one_of_factory(self):35 v = self.cf.is_one_of_factory([None, 12])36 v(12)37 v(None)38 msg = r"Value must be one of None\|12"39 with pytest.raises(ValueError, match=msg):40 v(1.1)41 def test_register_option(self):42 self.cf.register_option("a", 1, "doc")43 # can't register an already registered option44 msg = "Option 'a' has already been registered"45 with pytest.raises(OptionError, match=msg):46 self.cf.register_option("a", 1, "doc")47 # can't register an already registered option48 msg = "Path prefix to option 'a' is already an option"49 with pytest.raises(OptionError, match=msg):50 self.cf.register_option("a.b.c.d1", 1, "doc")51 with pytest.raises(OptionError, match=msg):52 self.cf.register_option("a.b.c.d2", 1, "doc")53 # no python keywords54 msg = "for is a python keyword"55 with pytest.raises(ValueError, match=msg):56 self.cf.register_option("for", 0)57 with pytest.raises(ValueError, match=msg):58 self.cf.register_option("a.for.b", 0)59 # must be valid identifier (ensure attribute access works)60 msg = "oh my goddess! is not a valid identifier"61 with pytest.raises(ValueError, match=msg):62 self.cf.register_option("Oh my Goddess!", 0)63 # we can register options several levels deep64 # without predefining the intermediate steps65 # and we can define differently named options66 # in the same namespace67 self.cf.register_option("k.b.c.d1", 1, "doc")68 self.cf.register_option("k.b.c.d2", 1, "doc")69 def test_describe_option(self):70 self.cf.register_option("a", 1, "doc")71 self.cf.register_option("b", 1, "doc2")72 self.cf.deprecate_option("b")73 self.cf.register_option("c.d.e1", 1, "doc3")74 self.cf.register_option("c.d.e2", 1, "doc4")75 self.cf.register_option("f", 1)76 self.cf.register_option("g.h", 1)77 self.cf.register_option("k", 2)78 self.cf.deprecate_option("g.h", rkey="k")79 self.cf.register_option("l", "foo")80 # non-existent keys raise KeyError81 msg = r"No such keys\(s\)"82 with pytest.raises(OptionError, match=msg):83 self.cf.describe_option("no.such.key")84 # we can get the description for any key we registered85 assert "doc" in self.cf.describe_option("a", _print_desc=False)86 assert "doc2" in self.cf.describe_option("b", _print_desc=False)87 assert "precated" in self.cf.describe_option("b", _print_desc=False)88 assert "doc3" in self.cf.describe_option("c.d.e1", _print_desc=False)89 assert "doc4" in self.cf.describe_option("c.d.e2", _print_desc=False)90 # if no doc is specified we get a default message91 # saying "description not available"92 assert "vailable" in self.cf.describe_option("f", _print_desc=False)93 assert "vailable" in self.cf.describe_option("g.h", _print_desc=False)94 assert "precated" in self.cf.describe_option("g.h", _print_desc=False)95 assert "k" in self.cf.describe_option("g.h", _print_desc=False)96 # default is reported97 assert "foo" in self.cf.describe_option("l", _print_desc=False)98 # current value is reported99 assert "bar" not in self.cf.describe_option("l", _print_desc=False)100 self.cf.set_option("l", "bar")101 assert "bar" in self.cf.describe_option("l", _print_desc=False)102 def test_case_insensitive(self):103 self.cf.register_option("KanBAN", 1, "doc")104 assert "doc" in self.cf.describe_option("kanbaN", _print_desc=False)105 assert self.cf.get_option("kanBaN") == 1106 self.cf.set_option("KanBan", 2)107 assert self.cf.get_option("kAnBaN") == 2108 # gets of non-existent keys fail109 msg = r"No such keys\(s\): 'no_such_option'"110 with pytest.raises(OptionError, match=msg):111 self.cf.get_option("no_such_option")112 self.cf.deprecate_option("KanBan")113 assert self.cf._is_deprecated("kAnBaN")114 def test_get_option(self):115 self.cf.register_option("a", 1, "doc")116 self.cf.register_option("b.c", "hullo", "doc2")117 self.cf.register_option("b.b", None, "doc2")118 # gets of existing keys succeed119 assert self.cf.get_option("a") == 1120 assert self.cf.get_option("b.c") == "hullo"121 assert self.cf.get_option("b.b") is None122 # gets of non-existent keys fail123 msg = r"No such keys\(s\): 'no_such_option'"124 with pytest.raises(OptionError, match=msg):125 self.cf.get_option("no_such_option")126 def test_set_option(self):127 self.cf.register_option("a", 1, "doc")128 self.cf.register_option("b.c", "hullo", "doc2")129 self.cf.register_option("b.b", None, "doc2")130 assert self.cf.get_option("a") == 1131 assert self.cf.get_option("b.c") == "hullo"132 assert self.cf.get_option("b.b") is None133 self.cf.set_option("a", 2)134 self.cf.set_option("b.c", "wurld")135 self.cf.set_option("b.b", 1.1)136 assert self.cf.get_option("a") == 2137 assert self.cf.get_option("b.c") == "wurld"138 assert self.cf.get_option("b.b") == 1.1139 msg = r"No such keys\(s\): 'no.such.key'"140 with pytest.raises(OptionError, match=msg):141 self.cf.set_option("no.such.key", None)142 def test_set_option_empty_args(self):143 msg = "Must provide an even number of non-keyword arguments"144 with pytest.raises(ValueError, match=msg):145 self.cf.set_option()146 def test_set_option_uneven_args(self):147 msg = "Must provide an even number of non-keyword arguments"148 with pytest.raises(ValueError, match=msg):149 self.cf.set_option("a.b", 2, "b.c")150 def test_set_option_invalid_single_argument_type(self):151 msg = "Must provide an even number of non-keyword arguments"152 with pytest.raises(ValueError, match=msg):153 self.cf.set_option(2)154 def test_set_option_multiple(self):155 self.cf.register_option("a", 1, "doc")156 self.cf.register_option("b.c", "hullo", "doc2")157 self.cf.register_option("b.b", None, "doc2")158 assert self.cf.get_option("a") == 1159 assert self.cf.get_option("b.c") == "hullo"160 assert self.cf.get_option("b.b") is None161 self.cf.set_option("a", "2", "b.c", None, "b.b", 10.0)162 assert self.cf.get_option("a") == "2"163 assert self.cf.get_option("b.c") is None164 assert self.cf.get_option("b.b") == 10.0165 def test_validation(self):166 self.cf.register_option("a", 1, "doc", validator=self.cf.is_int)167 self.cf.register_option("d", 1, "doc", validator=self.cf.is_nonnegative_int)168 self.cf.register_option("b.c", "hullo", "doc2", validator=self.cf.is_text)169 msg = "Value must have type '<class 'int'>'"170 with pytest.raises(ValueError, match=msg):171 self.cf.register_option("a.b.c.d2", "NO", "doc", validator=self.cf.is_int)172 self.cf.set_option("a", 2) # int is_int173 self.cf.set_option("b.c", "wurld") # str is_str174 self.cf.set_option("d", 2)175 self.cf.set_option("d", None) # non-negative int can be None176 # None not is_int177 with pytest.raises(ValueError, match=msg):178 self.cf.set_option("a", None)179 with pytest.raises(ValueError, match=msg):180 self.cf.set_option("a", "ab")181 msg = "Value must be a nonnegative integer or None"182 with pytest.raises(ValueError, match=msg):183 self.cf.register_option(184 "a.b.c.d3", "NO", "doc", validator=self.cf.is_nonnegative_int185 )186 with pytest.raises(ValueError, match=msg):187 self.cf.register_option(188 "a.b.c.d3", -2, "doc", validator=self.cf.is_nonnegative_int189 )190 msg = r"Value must be an instance of <class 'str'>\|<class 'bytes'>"191 with pytest.raises(ValueError, match=msg):192 self.cf.set_option("b.c", 1)193 validator = self.cf.is_one_of_factory([None, self.cf.is_callable])194 self.cf.register_option("b", lambda: None, "doc", validator=validator)195 self.cf.set_option("b", "%.1f".format) # Formatter is callable196 self.cf.set_option("b", None) # Formatter is none (default)197 with pytest.raises(ValueError, match="Value must be a callable"):198 self.cf.set_option("b", "%.1f")199 def test_reset_option(self):200 self.cf.register_option("a", 1, "doc", validator=self.cf.is_int)201 self.cf.register_option("b.c", "hullo", "doc2", validator=self.cf.is_str)202 assert self.cf.get_option("a") == 1203 assert self.cf.get_option("b.c") == "hullo"204 self.cf.set_option("a", 2)205 self.cf.set_option("b.c", "wurld")206 assert self.cf.get_option("a") == 2207 assert self.cf.get_option("b.c") == "wurld"208 self.cf.reset_option("a")209 assert self.cf.get_option("a") == 1210 assert self.cf.get_option("b.c") == "wurld"211 self.cf.reset_option("b.c")212 assert self.cf.get_option("a") == 1213 assert self.cf.get_option("b.c") == "hullo"214 def test_reset_option_all(self):215 self.cf.register_option("a", 1, "doc", validator=self.cf.is_int)216 self.cf.register_option("b.c", "hullo", "doc2", validator=self.cf.is_str)217 assert self.cf.get_option("a") == 1218 assert self.cf.get_option("b.c") == "hullo"219 self.cf.set_option("a", 2)220 self.cf.set_option("b.c", "wurld")221 assert self.cf.get_option("a") == 2222 assert self.cf.get_option("b.c") == "wurld"223 self.cf.reset_option("all")224 assert self.cf.get_option("a") == 1225 assert self.cf.get_option("b.c") == "hullo"226 def test_deprecate_option(self):227 # we can deprecate non-existent options228 self.cf.deprecate_option("foo")229 assert self.cf._is_deprecated("foo")230 with warnings.catch_warnings(record=True) as w:231 warnings.simplefilter("always")232 with pytest.raises(KeyError, match="No such keys.s.: 'foo'"):233 self.cf.get_option("foo")234 assert len(w) == 1 # should have raised one warning235 assert "deprecated" in str(w[-1]) # we get the default message236 self.cf.register_option("a", 1, "doc", validator=self.cf.is_int)237 self.cf.register_option("b.c", "hullo", "doc2")238 self.cf.register_option("foo", "hullo", "doc2")239 self.cf.deprecate_option("a", removal_ver="nifty_ver")240 with warnings.catch_warnings(record=True) as w:241 warnings.simplefilter("always")242 self.cf.get_option("a")243 assert len(w) == 1 # should have raised one warning244 assert "eprecated" in str(w[-1]) # we get the default message245 assert "nifty_ver" in str(w[-1]) # with the removal_ver quoted246 msg = "Option 'a' has already been defined as deprecated"247 with pytest.raises(OptionError, match=msg):248 self.cf.deprecate_option("a")249 self.cf.deprecate_option("b.c", "zounds!")250 with warnings.catch_warnings(record=True) as w:251 warnings.simplefilter("always")252 self.cf.get_option("b.c")253 assert len(w) == 1 # should have raised one warning254 assert "zounds!" in str(w[-1]) # we get the custom message255 # test rerouting keys256 self.cf.register_option("d.a", "foo", "doc2")257 self.cf.register_option("d.dep", "bar", "doc2")258 assert self.cf.get_option("d.a") == "foo"259 assert self.cf.get_option("d.dep") == "bar"260 self.cf.deprecate_option("d.dep", rkey="d.a") # reroute d.dep to d.a261 with warnings.catch_warnings(record=True) as w:262 warnings.simplefilter("always")263 assert self.cf.get_option("d.dep") == "foo"264 assert len(w) == 1 # should have raised one warning265 assert "eprecated" in str(w[-1]) # we get the custom message266 with warnings.catch_warnings(record=True) as w:267 warnings.simplefilter("always")268 self.cf.set_option("d.dep", "baz") # should overwrite "d.a"269 assert len(w) == 1 # should have raised one warning270 assert "eprecated" in str(w[-1]) # we get the custom message271 with warnings.catch_warnings(record=True) as w:272 warnings.simplefilter("always")273 assert self.cf.get_option("d.dep") == "baz"274 assert len(w) == 1 # should have raised one warning275 assert "eprecated" in str(w[-1]) # we get the custom message276 def test_config_prefix(self):277 with self.cf.config_prefix("base"):278 self.cf.register_option("a", 1, "doc1")279 self.cf.register_option("b", 2, "doc2")280 assert self.cf.get_option("a") == 1281 assert self.cf.get_option("b") == 2282 self.cf.set_option("a", 3)283 self.cf.set_option("b", 4)284 assert self.cf.get_option("a") == 3285 assert self.cf.get_option("b") == 4286 assert self.cf.get_option("base.a") == 3287 assert self.cf.get_option("base.b") == 4288 assert "doc1" in self.cf.describe_option("base.a", _print_desc=False)289 assert "doc2" in self.cf.describe_option("base.b", _print_desc=False)290 self.cf.reset_option("base.a")291 self.cf.reset_option("base.b")292 with self.cf.config_prefix("base"):293 assert self.cf.get_option("a") == 1294 assert self.cf.get_option("b") == 2295 def test_callback(self):296 k = [None]297 v = [None]298 def callback(key):299 k.append(key)300 v.append(self.cf.get_option(key))301 self.cf.register_option("d.a", "foo", cb=callback)302 self.cf.register_option("d.b", "foo", cb=callback)303 del k[-1], v[-1]304 self.cf.set_option("d.a", "fooz")305 assert k[-1] == "d.a"306 assert v[-1] == "fooz"307 del k[-1], v[-1]308 self.cf.set_option("d.b", "boo")309 assert k[-1] == "d.b"310 assert v[-1] == "boo"311 del k[-1], v[-1]312 self.cf.reset_option("d.b")313 assert k[-1] == "d.b"314 def test_set_ContextManager(self):315 def eq(val):316 assert self.cf.get_option("a") == val317 self.cf.register_option("a", 0)318 eq(0)319 with self.cf.option_context("a", 15):320 eq(15)321 with self.cf.option_context("a", 25):322 eq(25)323 eq(15)324 eq(0)325 self.cf.set_option("a", 17)326 eq(17)327 def test_attribute_access(self):328 holder = []329 def f3(key):330 holder.append(True)331 self.cf.register_option("a", 0)332 self.cf.register_option("c", 0, cb=f3)333 options = self.cf.options334 assert options.a == 0335 with self.cf.option_context("a", 15):336 assert options.a == 15337 options.a = 500338 assert self.cf.get_option("a") == 500339 self.cf.reset_option("a")340 assert options.a == self.cf.get_option("a", 0)341 msg = "You can only set the value of existing options"342 with pytest.raises(OptionError, match=msg):343 options.b = 1344 with pytest.raises(OptionError, match=msg):345 options.display = 1346 # make sure callback kicks when using this form of setting347 options.c = 1348 assert len(holder) == 1349 def test_option_context_scope(self):350 # Ensure that creating a context does not affect the existing351 # environment as it is supposed to be used with the `with` statement.352 # See https://github.com/pandas-dev/pandas/issues/8514353 original_value = 60354 context_value = 10355 option_name = "a"356 self.cf.register_option(option_name, original_value)357 # Ensure creating contexts didn't affect the current context.358 ctx = self.cf.option_context(option_name, context_value)359 assert self.cf.get_option(option_name) == original_value360 # Ensure the correct value is available inside the context.361 with ctx:362 assert self.cf.get_option(option_name) == context_value363 # Ensure the current context is reset364 assert self.cf.get_option(option_name) == original_value365 def test_dictwrapper_getattr(self):366 options = self.cf.options367 # GH 19789368 with pytest.raises(OptionError, match="No such option"):369 options.bananas370 assert not hasattr(options, "bananas")
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!