def test_template_dir(): dut = AssetManager() base_path = os.path.join(os.path.dirname(__file__), 'resources') dut.base_path = base_path assert str(dut.templates_dir()) == os.path.join(base_path, 'assets/templates') with modified_environ(TEMPLATES_DIR=base_path): dut.base_path = None assert str(dut.templates_dir()) == os.path.join(base_path) with modified_environ(TEMPLATES_DIR="i do not exist"): with pytest.raises(TemplateDirectoryNotFoundError): dut.templates_dir()
def test_asset_dir(): dut = AssetManager() base_path = os.path.join(os.path.dirname(__file__), 'resources') dut.base_path = base_path assert str(dut.assets_dir()) == os.path.join(base_path, 'assets') with modified_environ(ASSETS_DIR=base_path): dut.base_path = None assert str(dut.assets_dir()) == os.path.join(base_path) with modified_environ(ASSETS_DIR="i do not exist"): with pytest.raises(AssetDirectoryNotFoundError): dut.assets_dir()
def test_secret(): dut = AssetManager() with modified_environ('token', 'not_here', TOKEN='the_token'): assert dut.secret('token') == 'the_token' assert dut.secret('TOKEN') == 'the_token' with pytest.raises(SecretNotFoundError, match="The secret 'not_here' could not be resolved."): dut.secret('not_here')
def test_environment_resolver_for_unset_var(): class _Test: @EnvironmentResolver() def work(self, my_var): return my_var with modified_environ('MY_VAR'): with pytest.raises(TypeError) as err: _Test().work() assert "work() missing 1 required positional argument: 'my_var'" in str( err)
def test_yaml_tag_env_with_envvar_set(): dut = YamlConfigLoader() with modified_environ(REPEAT_STR="Repeat me!"): config = dut.load_config(path_to_config('yaml_tag_env/config.yaml')) assert config is not None task_wo_default = config.tasks.get('env_tag_wo_default') assert task_wo_default assert str(task_wo_default.pull.instance) == str( Repeat("Repeat me!", name="env_tag_wo_default_pull", interval="1s")) task_w_default = config.tasks.get('env_tag_with_default') assert task_w_default assert str(task_w_default.pull.instance) == str( Repeat("the_default", name="env_tag_with_default_pull", interval="1s"))
# We inject arguments from the environment variables scope # to an instance __init__. # We use a `prefix` to minimize clashes with other components that have a username / password. # Argument username will have a correponding DB_USERNAME, same for password and database from argresolver import Environment from argresolver.utils import modified_environ # We use it to alter the environment variables class Connection: @Environment(prefix='DB') def __init__(self, username, password, database='default'): self.username = username self.password = password self.database = database def __str__(self): # Hint: In a real world example you won't put your password in here ;-) return "Connection(username='******', password='******'"\ ", database='{self.database}')".format(self=self) with modified_environ(DB_USERNAME='******', DB_PASSWORD='******'): conn = Connection() print(str(conn) ) # Connection(username='******', password='******', database='default')
def test_bundled_config(config_file): with modified_environ(**ENV): app = Application.from_file(config_file) assert app is not None assert str(app) assert repr(app)
# We inject arguments from the environment variables scope to a simple function # We use a `prefix` to minimize clashes with other components # username will have a correponding DB_USERNAME, same for password and database from argresolver import Environment from argresolver.utils import modified_environ # We use it to alter the environment variables @Environment() def connect(host, user, password): print("Connecting: {user}:{password}@{host}".format(**locals())) with modified_environ(PASSWORD='******'): connect('localhost', 'admin') # Prints: Connecting: admin:my_pass@localhost