Best Python code snippet using autotest_python
test_config.py
Source:test_config.py
1import threading2import asdf_standard.integration3import pytest4import asdf5from asdf import get_config, resource6from asdf.extension import BuiltinExtension, ExtensionProxy7from asdf.resource import ResourceMappingProxy8def test_config_context():9 assert get_config().validate_on_read is True10 with asdf.config_context() as config:11 config.validate_on_read = False12 assert get_config().validate_on_read is False13 assert get_config().validate_on_read is True14def test_config_context_nested():15 assert get_config().validate_on_read is True16 with asdf.config_context() as config1:17 config1.validate_on_read = False18 with asdf.config_context() as config2:19 config2.validate_on_read = True20 with asdf.config_context() as config3:21 config3.validate_on_read = False22 assert get_config().validate_on_read is False23 assert get_config().validate_on_read is True24def test_config_context_threaded():25 assert get_config().validate_on_read is True26 thread_value = None27 def worker():28 nonlocal thread_value29 thread_value = get_config().validate_on_read30 with asdf.config_context() as config:31 config.validate_on_read = False32 with asdf.config_context() as config:33 config.validate_on_read = False34 thread = threading.Thread(target=worker)35 thread.start()36 thread.join()37 assert thread_value is True38 assert get_config().validate_on_read is True39def test_global_config():40 assert get_config().validate_on_read is True41 get_config().validate_on_read = False42 assert get_config().validate_on_read is False43 with asdf.config_context() as config:44 assert config.validate_on_read is False45 config.validate_on_read = True46 assert get_config().validate_on_read is True47 assert get_config().validate_on_read is False48 # Global config is reset to defaults by autouse49 # fixture in asdf/tests/conftest.py.50def test_validate_on_read():51 with asdf.config_context() as config:52 assert config.validate_on_read == asdf.config.DEFAULT_VALIDATE_ON_READ53 config.validate_on_read = False54 assert get_config().validate_on_read is False55 config.validate_on_read = True56 assert get_config().validate_on_read is True57def test_default_version():58 with asdf.config_context() as config:59 assert config.default_version == asdf.config.DEFAULT_DEFAULT_VERSION60 assert "1.2.0" != asdf.config.DEFAULT_DEFAULT_VERSION61 config.default_version = "1.2.0"62 assert config.default_version == "1.2.0"63 with pytest.raises(ValueError):64 config.default_version = "0.1.5"65def test_legacy_fill_schema_defaults():66 with asdf.config_context() as config:67 assert config.legacy_fill_schema_defaults == asdf.config.DEFAULT_LEGACY_FILL_SCHEMA_DEFAULTS68 config.legacy_fill_schema_defaults = False69 assert get_config().legacy_fill_schema_defaults is False70 config.legacy_fill_schema_defaults = True71 assert get_config().legacy_fill_schema_defaults is True72def test_array_inline_threshold():73 with asdf.config_context() as config:74 assert config.array_inline_threshold == asdf.config.DEFAULT_ARRAY_INLINE_THRESHOLD75 config.array_inline_threshold = 1076 assert get_config().array_inline_threshold == 1077 config.array_inline_threshold = None78 assert get_config().array_inline_threshold is None79def test_resource_mappings():80 with asdf.config_context() as config:81 core_mappings = resource.get_json_schema_resource_mappings() + asdf_standard.integration.get_resource_mappings()82 default_mappings = config.resource_mappings83 assert len(default_mappings) >= len(core_mappings)84 new_mapping = {"http://somewhere.org/schemas/foo-1.0.0": b"foo"}85 config.add_resource_mapping(new_mapping)86 assert len(config.resource_mappings) == len(default_mappings) + 187 assert any(m for m in config.resource_mappings if m.delegate is new_mapping)88 # Adding a mapping should be idempotent:89 config.add_resource_mapping(new_mapping)90 # ... even if wrapped:91 config.add_resource_mapping(ResourceMappingProxy(new_mapping))92 assert len(config.resource_mappings) == len(default_mappings) + 193 # Adding a mapping should place it at the front of the line:94 front_mapping = {"http://somewhere.org/schemas/baz-1.0.0": b"baz"}95 config.add_resource_mapping(front_mapping)96 assert len(config.resource_mappings) == len(default_mappings) + 297 assert config.resource_mappings[0].delegate is front_mapping98 # ... even if the mapping is already in the list:99 config.add_resource_mapping(new_mapping)100 assert len(config.resource_mappings) == len(default_mappings) + 2101 assert config.resource_mappings[0].delegate is new_mapping102 # Reset should get rid of any additions:103 config.reset_resources()104 assert len(config.resource_mappings) == len(default_mappings)105 # Should be able to remove a mapping:106 config.add_resource_mapping(new_mapping)107 config.remove_resource_mapping(new_mapping)108 assert len(config.resource_mappings) == len(default_mappings)109 # ... even if wrapped:110 config.add_resource_mapping(new_mapping)111 config.remove_resource_mapping(ResourceMappingProxy(new_mapping))112 assert len(config.resource_mappings) == len(default_mappings)113 # ... and also by the name of the package the mappings came from:114 config.add_resource_mapping(ResourceMappingProxy(new_mapping, package_name="foo"))115 config.add_resource_mapping(116 ResourceMappingProxy({"http://somewhere.org/schemas/bar-1.0.0": b"bar"}, package_name="foo")117 )118 config.remove_resource_mapping(package="foo")119 assert len(config.resource_mappings) == len(default_mappings)120 # Can combine the package and mapping filters when removing:121 config.add_resource_mapping(ResourceMappingProxy(new_mapping, package_name="foo"))122 config.remove_resource_mapping(new_mapping, package="foo")123 assert len(config.resource_mappings) == len(default_mappings)124 # But not omit both:125 with pytest.raises(ValueError):126 config.remove_resource_mapping()127 # Removing a mapping should be idempotent:128 config.add_resource_mapping(new_mapping)129 config.remove_resource_mapping(new_mapping)130 config.remove_resource_mapping(new_mapping)131 assert len(config.resource_mappings) == len(default_mappings)132def test_resource_manager():133 with asdf.config_context() as config:134 # Initial resource manager should contain just the entry points resources:135 assert "http://stsci.edu/schemas/asdf/core/asdf-1.1.0" in config.resource_manager136 assert (137 b"http://stsci.edu/schemas/asdf/core/asdf-1.1.0"138 in config.resource_manager["http://stsci.edu/schemas/asdf/core/asdf-1.1.0"]139 )140 assert "http://somewhere.org/schemas/foo-1.0.0" not in config.resource_manager141 # Add a mapping and confirm that the manager now contains it:142 new_mapping = {"http://somewhere.org/schemas/foo-1.0.0": b"foo"}143 config.add_resource_mapping(new_mapping)144 assert "http://stsci.edu/schemas/asdf/core/asdf-1.1.0" in config.resource_manager145 assert (146 b"http://stsci.edu/schemas/asdf/core/asdf-1.1.0"147 in config.resource_manager["http://stsci.edu/schemas/asdf/core/asdf-1.1.0"]148 )149 assert "http://somewhere.org/schemas/foo-1.0.0" in config.resource_manager150 assert config.resource_manager["http://somewhere.org/schemas/foo-1.0.0"] == b"foo"151 # Remove a mapping and confirm that the manager no longer contains it:152 config.remove_resource_mapping(new_mapping)153 assert "http://stsci.edu/schemas/asdf/core/asdf-1.1.0" in config.resource_manager154 assert (155 b"http://stsci.edu/schemas/asdf/core/asdf-1.1.0"156 in config.resource_manager["http://stsci.edu/schemas/asdf/core/asdf-1.1.0"]157 )158 assert "http://somewhere.org/schemas/foo-1.0.0" not in config.resource_manager159 # Reset and confirm that the manager no longer contains the custom mapping:160 config.add_resource_mapping(new_mapping)161 config.reset_resources()162 assert "http://stsci.edu/schemas/asdf/core/asdf-1.1.0" in config.resource_manager163 assert (164 b"http://stsci.edu/schemas/asdf/core/asdf-1.1.0"165 in config.resource_manager["http://stsci.edu/schemas/asdf/core/asdf-1.1.0"]166 )167 assert "http://somewhere.org/schemas/foo-1.0.0" not in config.resource_manager168def test_extensions():169 with asdf.config_context() as config:170 original_extensions = config.extensions171 assert any(isinstance(e.delegate, BuiltinExtension) for e in original_extensions)172 class FooExtension:173 types = []174 tag_mapping = []175 url_mapping = []176 new_extension = FooExtension()177 class BarExtension:178 extension_uri = "asdf://somewhere.org/extensions/bar-1.0"179 types = []180 tag_mapping = []181 url_mapping = []182 uri_extension = BarExtension()183 # Add an extension:184 config.add_extension(new_extension)185 assert len(config.extensions) == len(original_extensions) + 1186 assert any(e for e in config.extensions if e.delegate is new_extension)187 # Adding an extension should be idempotent:188 config.add_extension(new_extension)189 assert len(config.extensions) == len(original_extensions) + 1190 # Even when wrapped:191 config.add_extension(ExtensionProxy(new_extension))192 assert len(config.extensions) == len(original_extensions) + 1193 # Remove an extension:194 config.remove_extension(new_extension)195 assert len(config.extensions) == len(original_extensions)196 # Removing should work when wrapped:197 config.add_extension(new_extension)198 config.remove_extension(ExtensionProxy(new_extension))199 assert len(config.extensions) == len(original_extensions)200 # And also by URI:201 config.add_extension(uri_extension)202 config.remove_extension(uri_extension.extension_uri)203 assert len(config.extensions) == len(original_extensions)204 # And also by URI pattern:205 config.add_extension(uri_extension)206 config.remove_extension("asdf://somewhere.org/extensions/*")207 assert len(config.extensions) == len(original_extensions)208 # Remove by the name of the extension's package:209 config.add_extension(ExtensionProxy(new_extension, package_name="foo"))210 config.add_extension(ExtensionProxy(uri_extension, package_name="foo"))211 config.remove_extension(package="foo")212 assert len(config.extensions) == len(original_extensions)213 # Can combine remove filters:214 config.add_extension(ExtensionProxy(new_extension, package_name="foo"))215 config.add_extension(ExtensionProxy(uri_extension, package_name="foo"))216 config.remove_extension(uri_extension.extension_uri, package="foo")217 assert len(config.extensions) == len(original_extensions) + 1218 # ... but not omit both:219 with pytest.raises(ValueError):220 config.remove_extension()221 # Removing an extension should be idempotent:222 config.add_extension(new_extension)223 config.remove_extension(new_extension)224 config.remove_extension(new_extension)225 assert len(config.extensions) == len(original_extensions)226 # Resetting should get rid of any additions:227 config.add_extension(new_extension)228 config.add_extension(FooExtension())229 config.reset_extensions()230 assert len(config.extensions) == len(original_extensions)231def test_config_repr():232 with asdf.config_context() as config:233 config.validate_on_read = True234 config.default_version = "1.5.0"235 config.io_block_size = 9999236 config.legacy_fill_schema_defaults = False237 config.array_inline_threshold = 14238 assert "validate_on_read: True" in repr(config)239 assert "default_version: 1.5.0" in repr(config)240 assert "io_block_size: 9999" in repr(config)241 assert "legacy_fill_schema_defaults: False" in repr(config)...
core.py
Source:core.py
1#-*- coding: utf8 -*-2# This line ensures that frames from this file will not be shown in tracebacks3__unittest = 14import inspect5import os6import yaml7from freshen.context import *8from freshen.parser import parse_steps, parse_file9class StepsRunner(object):10 11 def __init__(self, step_registry):12 self.step_registry = step_registry13 14 def run_steps_from_string(self, spec, language_name='en'):15 """ Called from within step definitions to run other steps. """16 17 caller = inspect.currentframe().f_back18 line = caller.f_lineno - 119 fname = caller.f_code.co_filename20 21 steps = parse_steps(spec, fname, line, load_language(language_name))22 for s in steps:23 self.run_step(s)24 25 def run_step(self, step):26 step_impl, args = self.step_registry.find_step_impl(step)27 if step.arg is not None:28 return step_impl.run(step.arg, *args)29 else:30 return step_impl.run(*args)31class TagMatcher(object):32 33 def __init__(self, tags):34 self.include_tags = set(t.lstrip("@") for t in tags if not t.startswith("~"))35 self.exclude_tags = set(t.lstrip("~@") for t in tags if t.startswith("~"))36 def check_match(self, tagset):37 tagset = set(t.lstrip("@") for t in tagset)38 if tagset & self.exclude_tags:39 return False40 41 return not self.include_tags or (tagset & self.include_tags)42class Language(object):43 def __init__(self, mappings, default_mappings=None):44 self.mappings = mappings45 self.default_mappings = default_mappings46 47 def words(self, key):48 """49 Give all the synonymns of a word in the requested language 50 (or the default language if no word is available).51 """52 if self.default_mappings is not None and key not in self.mappings:53 return self.default_mappings[key].encode('utf').split("|")54 else:55 return self.mappings[key].encode('utf').split("|")56def load_feature(fname, language):57 """ Load and parse a feature file. """58 fname = os.path.abspath(fname)59 feat = parse_file(fname, language)60 return feat61def load_language(language_name, default_language_name="en"):62 directory, _f = os.path.split(os.path.abspath(__file__))63 language_path = os.path.join(directory, 'languages.yml')64 languages = yaml.load(open(language_path))65 if language_name not in languages:66 return None67 return Language(languages[language_name], languages[default_language_name])68def run_steps(spec, language="en"):69 """ Can be called by the user from within a step definition to execute other steps. """70 # The way this works is a little exotic, but I couldn't think of a better way to work around71 # the fact that this has to be a global function and therefore cannot know about which step72 # runner to use (other than making step runner global)73 74 # Find the step runner that is currently running and use it to run the given steps75 fr = inspect.currentframe()76 while fr:77 if "self" in fr.f_locals:78 f_self = fr.f_locals['self']79 if isinstance(f_self, StepsRunner):80 return f_self.run_steps_from_string(spec, language)...
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!!