-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update EnvConfigValue to support optional override from env
- Loading branch information
Showing
4 changed files
with
61 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,54 @@ | ||
import os | ||
from morpheus.utils.env_config_value import EnvConfigValue | ||
from unittest import mock | ||
import pytest | ||
|
||
class MyApi: | ||
class EnvDrivenValue(EnvConfigValue): | ||
_ENV_KEY = "DEFAULT" | ||
_ENV_KEY_OVERRIDE = "OVERRIDE" | ||
|
||
class BaseUri(EnvConfigValue): | ||
_CONFIG_NAME = "MY_API_BASE_URI" | ||
|
||
def __init__(self, base_uri: BaseUri): | ||
self._base_uri = str(base_uri) | ||
class EnvDriverValueNoOverride(EnvConfigValue): | ||
_ENV_KEY = "DEFAULT" | ||
|
||
|
||
from unittest import mock | ||
class EnvDrivenValueNoDefault(EnvConfigValue): | ||
_ENV_KEY_OVERRIDE = "OVERRIDE" | ||
|
||
|
||
def test_env_driven_value(): | ||
with mock.patch.dict(os.environ, clear=True, values={"DEFAULT": "default.api.com"}): | ||
assert str(EnvDrivenValue(None)) == "default.api.com" | ||
assert str(EnvDrivenValue("api.com")) == "api.com" | ||
|
||
with mock.patch.dict(os.environ, clear=True, values={"DEFAULT": "default.api.com", "OVERRIDE": "override.api.com"}): | ||
assert str(EnvDrivenValue("api.com")) == "override.api.com" | ||
assert str(EnvDrivenValue("api.com", use_env=False)) == "api.com" | ||
|
||
with pytest.raises(ValueError): | ||
EnvDrivenValue(None, use_env=False) | ||
|
||
|
||
def test_env_driven_value_no_override(): | ||
with mock.patch.dict(os.environ, clear=True, values={"DEFAULT": "default.api.com"}): | ||
assert str(EnvDriverValueNoOverride(None)) == "default.api.com" | ||
assert str(EnvDriverValueNoOverride("api.com")) == "api.com" | ||
|
||
with mock.patch.dict(os.environ, clear=True, values={"DEFAULT": "default.api.com", "OVERRIDE": "override.api.com"}): | ||
assert str(EnvDriverValueNoOverride("api.com")) == "api.com" | ||
assert str(EnvDriverValueNoOverride("api.com", use_env=False)) == "api.com" | ||
|
||
def test_os_config_value(): | ||
|
||
with mock.patch.dict(os.environ, clear=True, values={"MY_API_BASE_URI_DEFAULT": "default.api.com"}): | ||
assert str(MyApi.BaseUri(None)) == "default.api.com" | ||
def test_env_driven_value_no_default(): | ||
with mock.patch.dict(os.environ, clear=True, values={"DEFAULT": "default.api.com"}): | ||
with pytest.raises(ValueError): | ||
EnvDrivenValueNoDefault(None) | ||
|
||
with mock.patch.dict(os.environ, clear=True, values={"MY_API_BASE_URI_DEFAULT": "default.api.com"}): | ||
assert str(MyApi.BaseUri("api.com")) == "api.com" | ||
assert str(EnvDrivenValueNoDefault("api.com")) == "api.com" | ||
|
||
with mock.patch.dict(os.environ, clear=True, values={"MY_API_BASE_URI_DEFAULT": "default.api.com", "MY_API_BASE_URI_OVERRIDE": "override.api.com"}): | ||
assert str(MyApi.BaseUri("api.com")) == "override.api.com" | ||
with mock.patch.dict(os.environ, clear=True, values={"DEFAULT": "default.api.com", "OVERRIDE": "override.api.com"}): | ||
assert str(EnvDrivenValueNoDefault("api.com")) == "override.api.com" | ||
assert str(EnvDrivenValueNoDefault("api.com", use_env=False)) == "api.com" | ||
|
||
with mock.patch.dict(os.environ, clear=True, values={"MY_API_BASE_URI_DEFAULT": "default.api.com", "MY_API_BASE_URI_OVERRIDE": "override.api.com"}): | ||
assert str(MyApi.BaseUri("api.com", use_env=False)) == "api.com" | ||
with pytest.raises(ValueError): | ||
EnvDrivenValueNoDefault(None, use_env=False) |