|
15 | 15 | # specific language governing permissions and limitations |
16 | 16 | # under the License. |
17 | 17 | # pylint: disable=redefined-outer-name,unused-argument |
| 18 | +import os |
| 19 | +from unittest import mock |
18 | 20 | from uuid import UUID |
19 | 21 |
|
20 | 22 | import pytest |
21 | 23 | from requests_mock import Mocker |
22 | 24 |
|
23 | 25 | import pyiceberg |
24 | | -from pyiceberg.catalog import PropertiesUpdateSummary, Table |
| 26 | +from pyiceberg.catalog import PropertiesUpdateSummary, Table, load_catalog |
25 | 27 | from pyiceberg.catalog.rest import RestCatalog |
| 28 | +from pyiceberg.utils.config import Config |
26 | 29 | from pyiceberg.exceptions import ( |
27 | 30 | NamespaceAlreadyExistsError, |
28 | 31 | NoSuchNamespaceError, |
@@ -943,3 +946,39 @@ def test_request_session_with_ssl_client_cert() -> None: |
943 | 946 | # Missing namespace |
944 | 947 | RestCatalog("rest", **catalog_properties) # type: ignore |
945 | 948 | assert "Could not find the TLS certificate file, invalid path: path_to_client_cert" in str(e.value) |
| 949 | + |
| 950 | + |
| 951 | +EXAMPLE_ENV = {"PYICEBERG_CATALOG__PRODUCTION__URI": TEST_URI} |
| 952 | + |
| 953 | + |
| 954 | +@mock.patch.dict(os.environ, EXAMPLE_ENV) |
| 955 | +@mock.patch("pyiceberg.catalog.Config.get_catalog_config") |
| 956 | +def test_catalog_from_environment_variables(catalog_config_mock, rest_mock) -> None: |
| 957 | + catalog_config_mock.return_value = Config._from_environment_variables({}).get("catalog").get("production") |
| 958 | + catalog = load_catalog("production") |
| 959 | + assert catalog.uri == TEST_URI |
| 960 | + |
| 961 | + |
| 962 | +@mock.patch.dict(os.environ, EXAMPLE_ENV) |
| 963 | +@mock.patch("pyiceberg.catalog._ENV_CONFIG.get_catalog_config") |
| 964 | +def test_catalog_from_environment_variables_override(catalog_config_mock, rest_mock) -> None: |
| 965 | + rest_mock.get( |
| 966 | + "https://other-service.io/api/v1/config", |
| 967 | + json={"defaults": {}, "overrides": {}}, |
| 968 | + status_code=200, |
| 969 | + ) |
| 970 | + |
| 971 | + catalog_config_mock.return_value = Config._from_environment_variables({}).get("catalog").get("production") |
| 972 | + catalog = load_catalog("production", uri="https://other-service.io/api") |
| 973 | + assert catalog.uri == "https://other-service.io/api" |
| 974 | + |
| 975 | + |
| 976 | +def test_catalog_from_parameters_empty_env(rest_mock) -> None: |
| 977 | + rest_mock.get( |
| 978 | + f"https://other-service.io/api/v1/config", |
| 979 | + json={"defaults": {}, "overrides": {}}, |
| 980 | + status_code=200, |
| 981 | + ) |
| 982 | + |
| 983 | + catalog = load_catalog("production", uri="https://other-service.io/api") |
| 984 | + assert catalog.uri == "https://other-service.io/api" |
0 commit comments