-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename
get_api_key
to get_environment_variable
Renames the `get_api_key` function in `coreutils.py` to `get_environment_variable` to make it more generic and applicable to a wider range of use cases.
- Loading branch information
Showing
4 changed files
with
88 additions
and
37 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env python | ||
|
||
"""Tests for `coreutils` module.""" | ||
import os | ||
import sys | ||
import unittest | ||
from unittest import mock | ||
|
||
from geemap import coreutils | ||
|
||
|
||
class FakeSecretNotFoundError(Exception): | ||
"""google.colab.userdata.SecretNotFoundError fake.""" | ||
|
||
|
||
class FakeNotebookAccessError(Exception): | ||
"""google.colab.userdata.NotebookAccessError fake.""" | ||
|
||
|
||
class TestCoreUtils(unittest.TestCase): | ||
"""Tests for core utilss.""" | ||
|
||
def test_get_environment_invalid_key(self): | ||
"""Verifies None is returned if keys are invalid.""" | ||
self.assertIsNone(coreutils.get_environment_variable(None)) | ||
self.assertIsNone(coreutils.get_environment_variable("")) | ||
|
||
@mock.patch.dict(os.environ, {"key": "value"}) | ||
def test_get_environment_variable_unknown_key(self): | ||
"""Verifies None is returned if the environment variable could not be found.""" | ||
self.assertIsNone(coreutils.get_environment_variable("unknown-key")) | ||
|
||
@mock.patch.dict(os.environ, {"key": "value"}) | ||
def test_get_environment_variable_from_env(self): | ||
"""Verifies environment variables are read from environment variables.""" | ||
self.assertEqual(coreutils.get_environment_variable("key"), "value") | ||
|
||
@mock.patch.dict("sys.modules", {"google.colab": mock.Mock()}) | ||
def test_get_environment_variable_from_colab(self): | ||
"""Verifies environment variables are read from Colab secrets.""" | ||
mock_colab = sys.modules["google.colab"] | ||
mock_colab.userdata.get.return_value = "colab-value" | ||
|
||
self.assertEqual(coreutils.get_environment_variable("key"), "colab-value") | ||
mock_colab.userdata.get.assert_called_once_with("key") | ||
|
||
@mock.patch.dict(os.environ, {"key": "environ-value"}) | ||
@mock.patch.dict("sys.modules", {"google.colab": mock.Mock()}) | ||
def test_get_environment_variable_colab_fails_fallback_to_env(self): | ||
"""Verifies environment variables are read if a Colab secret read fails.""" | ||
mock_colab = sys.modules["google.colab"] | ||
mock_colab.userdata.SecretNotFoundError = FakeSecretNotFoundError | ||
mock_colab.userdata.NotebookAccessError = FakeNotebookAccessError | ||
mock_colab.userdata.get.side_effect = FakeNotebookAccessError() | ||
|
||
self.assertEqual(coreutils.get_environment_variable("key"), "environ-value") |