-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
89 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,95 @@ | ||
import logging | ||
import pytest | ||
import mock | ||
|
||
from google.api_core.client_logging import setup_logging | ||
from google.api_core.client_logging import ( | ||
setup_logging, | ||
parse_logging_scopes, | ||
initialize_logging, | ||
) | ||
|
||
# TODO: We should not be testing against the "google" logger | ||
# and should mock `base_logger` instead. | ||
|
||
def reset_logger(scope): | ||
logger = logging.getLogger(scope) | ||
logger.handlers = [] | ||
logger.setLevel(logging.NOTSET) | ||
logger.propagate = True | ||
|
||
|
||
|
||
def test_setup_logging_w_no_scopes(): | ||
setup_logging() | ||
base_logger = logging.getLogger("google") | ||
assert base_logger.handlers == [] | ||
assert base_logger.propagate == False | ||
assert base_logger.level == logging.NOTSET | ||
with mock.patch("google.api_core.client_logging._BASE_LOGGER_NAME", "foo"): | ||
setup_logging() | ||
base_logger = logging.getLogger("foo") | ||
assert base_logger.handlers == [] | ||
assert base_logger.propagate == False | ||
assert base_logger.level == logging.NOTSET | ||
|
||
reset_logger("google") | ||
|
||
|
||
def test_setup_logging_w_base_scope(): | ||
setup_logging("google") | ||
base_logger = logging.getLogger("google") | ||
with mock.patch("google.api_core.client_logging._BASE_LOGGER_NAME", "foo"): | ||
setup_logging("foo") | ||
base_logger = logging.getLogger("foo") | ||
assert isinstance(base_logger.handlers[0], logging.StreamHandler) | ||
assert base_logger.propagate == False | ||
assert base_logger.level == logging.DEBUG | ||
|
||
reset_logger("google") | ||
|
||
|
||
def test_setup_logging_w_module_scope(): | ||
setup_logging("google.foo") | ||
|
||
base_logger = logging.getLogger("google") | ||
with mock.patch("google.api_core.client_logging._BASE_LOGGER_NAME", "foo"): | ||
setup_logging("foo.bar") | ||
|
||
base_logger = logging.getLogger("foo") | ||
assert base_logger.handlers == [] | ||
assert base_logger.propagate == False | ||
assert base_logger.level == logging.NOTSET | ||
|
||
module_logger = logging.getLogger("google.foo") | ||
module_logger = logging.getLogger("foo.bar") | ||
assert isinstance(module_logger.handlers[0], logging.StreamHandler) | ||
assert module_logger.propagate == False | ||
assert module_logger.level == logging.DEBUG | ||
|
||
|
||
reset_logger("google") | ||
reset_logger("google.foo") | ||
|
||
|
||
def test_setup_logging_w_incorrect_scope(): | ||
setup_logging("foo") | ||
|
||
base_logger = logging.getLogger("google") | ||
with mock.patch("google.api_core.client_logging._BASE_LOGGER_NAME", "foo"): | ||
setup_logging("abc") | ||
|
||
base_logger = logging.getLogger("foo") | ||
assert base_logger.handlers == [] | ||
assert base_logger.propagate == False | ||
assert base_logger.level == logging.NOTSET | ||
|
||
# TODO(https://github.com/googleapis/python-api-core/issues/759): update test once we add logic to ignore an incorrect scope. | ||
logger = logging.getLogger("foo") | ||
logger = logging.getLogger("abc") | ||
assert isinstance(logger.handlers[0], logging.StreamHandler) | ||
assert logger.propagate == False | ||
assert logger.level == logging.DEBUG | ||
|
||
reset_logger("google") | ||
reset_logger("foo") | ||
|
||
|
||
def test_initialize_logging(): | ||
|
||
with mock.patch("os.getenv", return_value="foo.bar"): | ||
with mock.patch("google.api_core.client_logging._BASE_LOGGER_NAME", "foo"): | ||
initialize_logging() | ||
|
||
base_logger = logging.getLogger("foo") | ||
assert base_logger.handlers == [] | ||
assert base_logger.propagate == False | ||
assert base_logger.level == logging.NOTSET | ||
|
||
module_logger = logging.getLogger("foo.bar") | ||
assert isinstance(module_logger.handlers[0], logging.StreamHandler) | ||
assert module_logger.propagate == False | ||
assert module_logger.level == logging.DEBUG | ||
|
||
reset_logger("google") | ||
reset_logger("google.foo") |