Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from gumo-py/20190705-refactoring-configuratio…
…n-dependencies Refactoring configuration dependencies
- Loading branch information
Showing
8 changed files
with
161 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,8 @@ | ||
import dataclasses | ||
from typing import Optional | ||
# backward compatibility | ||
|
||
from gumo.datastore.infrastructure.configuration import DatastoreConfiguration | ||
|
||
@dataclasses.dataclass(frozen=True) | ||
class DatastoreConfiguration: | ||
use_local_emulator: bool = False | ||
emulator_host: Optional[str] = None | ||
namespace: Optional[str] = None | ||
|
||
def __post_init__(self): | ||
if self.use_local_emulator and self.emulator_host is None: | ||
raise ValueError(f'If the emulator enabled, then emulator_host must be present.') | ||
__all__ = [ | ||
DatastoreConfiguration.__name__ | ||
] |
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import os | ||
import dataclasses | ||
import threading | ||
|
||
from typing import Optional | ||
from typing import ClassVar | ||
from typing import Union | ||
|
||
from gumo.core import GoogleCloudProjectID | ||
|
||
from google.cloud import datastore | ||
|
||
|
||
@dataclasses.dataclass(frozen=False) | ||
class DatastoreConfiguration: | ||
google_cloud_project: Union[GoogleCloudProjectID, str, None] = None | ||
use_local_emulator: bool = False | ||
emulator_host: Optional[str] = None | ||
namespace: Optional[str] = None | ||
client: Optional[datastore.Client] = None | ||
|
||
_ENV_KEY_GOOGLE_CLOUD_PROJECT: ClassVar = 'GOOGLE_CLOUD_PROJECT' | ||
_ENV_KEY_DATASTORE_EMULATOR_HOST: ClassVar = 'DATASTORE_EMULATOR_HOST' | ||
|
||
_lock: ClassVar = threading.Lock() | ||
|
||
def __post_init__(self): | ||
with self._lock: | ||
self._set_google_cloud_project() | ||
self._set_emulator_config() | ||
self._set_client() | ||
|
||
def _set_google_cloud_project(self): | ||
if isinstance(self.google_cloud_project, str): | ||
self.google_cloud_project = GoogleCloudProjectID(self.google_cloud_project) | ||
if isinstance(self.google_cloud_project, GoogleCloudProjectID): | ||
if self.google_cloud_project.value != os.environ.get(self._ENV_KEY_GOOGLE_CLOUD_PROJECT): | ||
raise RuntimeError(f'Env-var "{self._ENV_KEY_GOOGLE_CLOUD_PROJECT}" is invalid or undefined.' | ||
f'Please set value "{self.google_cloud_project.value}" to env-vars.') | ||
|
||
if self.google_cloud_project is None: | ||
if self._ENV_KEY_GOOGLE_CLOUD_PROJECT in os.environ: | ||
self.google_cloud_project = GoogleCloudProjectID(os.environ[self._ENV_KEY_GOOGLE_CLOUD_PROJECT]) | ||
else: | ||
raise RuntimeError(f'Env-var "{self._ENV_KEY_GOOGLE_CLOUD_PROJECT}" is undefined, please set it.') | ||
|
||
def _set_emulator_config(self): | ||
emulator_not_configured = not self.use_local_emulator and self.emulator_host is None | ||
if emulator_not_configured and os.environ.get(self._ENV_KEY_DATASTORE_EMULATOR_HOST): | ||
self.use_local_emulator = True | ||
self.emulator_host = os.environ[self._ENV_KEY_DATASTORE_EMULATOR_HOST] | ||
|
||
if not self.use_local_emulator: | ||
return | ||
|
||
if os.environ.get(self._ENV_KEY_DATASTORE_EMULATOR_HOST) is None: | ||
raise RuntimeError( | ||
f'If the emulator enabled, then env-var "{self._ENV_KEY_DATASTORE_EMULATOR_HOST}" must be present.' | ||
) | ||
|
||
if os.environ.get(self._ENV_KEY_DATASTORE_EMULATOR_HOST) != self.emulator_host: | ||
host = os.environ.get(self._ENV_KEY_DATASTORE_EMULATOR_HOST) | ||
raise RuntimeError( | ||
f'Env-var "{self._ENV_KEY_DATASTORE_EMULATOR_HOST}" and self.emulator_host do not match. ' | ||
f'env["{self._ENV_KEY_DATASTORE_EMULATOR_HOST}"]={host}, self.emulator_host={self.emulator_host}' | ||
) | ||
|
||
def _set_client(self): | ||
if isinstance(self.client, datastore.Client): | ||
return | ||
|
||
self.client = datastore.Client( | ||
project=self.google_cloud_project.value, | ||
namespace=self.namespace, | ||
) |
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,27 +1,76 @@ | ||
import pytest | ||
import os | ||
|
||
from gumo.datastore._configuration import ConfigurationFactory | ||
from gumo.datastore.domain.configuration import DatastoreConfiguration | ||
from gumo.datastore.infrastructure.configuration import DatastoreConfiguration | ||
|
||
from google.cloud import datastore | ||
|
||
def test_configuration_factory_build(): | ||
o = ConfigurationFactory.build( | ||
use_local_emulator='yes', | ||
emulator_host='localhost:8080', | ||
namespace='test', | ||
) | ||
|
||
assert o == DatastoreConfiguration( | ||
use_local_emulator=True, | ||
emulator_host='localhost:8080', | ||
namespace='test', | ||
) | ||
class TestDatastoreConfiguration: | ||
def setup_method(self, method): | ||
self.env_vars = {} | ||
for k, v in os.environ.items(): | ||
self.env_vars[k] = v | ||
|
||
def teardown_method(self, method): | ||
for k in os.environ.keys(): | ||
if k not in self.env_vars: | ||
del os.environ[k] | ||
|
||
def test_configuration_factory_build_failed(): | ||
with pytest.raises(ValueError): | ||
ConfigurationFactory.build( | ||
use_local_emulator='yes', | ||
emulator_host=None, | ||
namespace='test', | ||
) | ||
for k, v in self.env_vars.items(): | ||
os.environ[k] = v | ||
|
||
def test_build_success_with_standard_configuration(self): | ||
if 'DATASTORE_EMULATOR_HOST' in os.environ: | ||
del os.environ['DATASTORE_EMULATOR_HOST'] | ||
assert os.environ['GOOGLE_CLOUD_PROJECT'] is not None | ||
assert 'DATASTORE_EMULATOR_HOST' not in os.environ | ||
|
||
o = DatastoreConfiguration() | ||
|
||
assert o.google_cloud_project.value == os.environ['GOOGLE_CLOUD_PROJECT'] | ||
assert not o.use_local_emulator | ||
assert o.emulator_host is None | ||
assert o.namespace is None | ||
assert isinstance(o.client, datastore.Client) | ||
|
||
def test_build_success_with_emulator_configuration(self): | ||
assert os.environ['GOOGLE_CLOUD_PROJECT'] is not None | ||
assert os.environ['DATASTORE_EMULATOR_HOST'] is not None | ||
|
||
o = DatastoreConfiguration() | ||
|
||
assert o.google_cloud_project.value == os.environ['GOOGLE_CLOUD_PROJECT'] | ||
assert o.use_local_emulator | ||
assert o.emulator_host == os.environ['DATASTORE_EMULATOR_HOST'] | ||
assert o.namespace is None | ||
assert isinstance(o.client, datastore.Client) | ||
|
||
def test_build_failure_without_google_cloud_project_env_vars(self): | ||
if 'GOOGLE_CLOUD_PROJECT' in os.environ: | ||
del os.environ['GOOGLE_CLOUD_PROJECT'] | ||
|
||
with pytest.raises(RuntimeError, match='"GOOGLE_CLOUD_PROJECT" is undefined'): | ||
DatastoreConfiguration() | ||
|
||
def test_build_failure_with_emulator_configuration_and_without_emulator_env_vars(self): | ||
if 'DATASTORE_EMULATOR_HOST' in os.environ: | ||
del os.environ['DATASTORE_EMULATOR_HOST'] | ||
assert os.environ['GOOGLE_CLOUD_PROJECT'] is not None | ||
assert 'DATASTORE_EMULATOR_HOST' not in os.environ | ||
|
||
with pytest.raises(RuntimeError, match='env-var "DATASTORE_EMULATOR_HOST" must be present'): | ||
DatastoreConfiguration( | ||
use_local_emulator=True | ||
) | ||
|
||
def test_build_failure_with_emulator_host_mismatched(self): | ||
assert os.environ['GOOGLE_CLOUD_PROJECT'] is not None | ||
assert os.environ['DATASTORE_EMULATOR_HOST'] is not None | ||
|
||
with pytest.raises(RuntimeError, | ||
match='Env-var "DATASTORE_EMULATOR_HOST" and self.emulator_host do not match.'): | ||
DatastoreConfiguration( | ||
use_local_emulator=True, | ||
emulator_host='example.localhost:12345' | ||
) |