|
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 typing import cast |
| 20 | +from unittest import mock |
18 | 21 | from uuid import UUID
|
19 | 22 |
|
20 | 23 | import pytest
|
21 | 24 | from requests_mock import Mocker
|
22 | 25 |
|
23 | 26 | import pyiceberg
|
24 |
| -from pyiceberg.catalog import PropertiesUpdateSummary, Table |
| 27 | +from pyiceberg.catalog import PropertiesUpdateSummary, Table, load_catalog |
25 | 28 | from pyiceberg.catalog.rest import RestCatalog
|
26 | 29 | from pyiceberg.exceptions import (
|
27 | 30 | NamespaceAlreadyExistsError,
|
|
38 | 41 | from pyiceberg.table.snapshots import Operation, Snapshot, Summary
|
39 | 42 | from pyiceberg.table.sorting import SortField, SortOrder
|
40 | 43 | from pyiceberg.transforms import IdentityTransform, TruncateTransform
|
| 44 | +from pyiceberg.typedef import RecursiveDict |
41 | 45 | from pyiceberg.types import (
|
42 | 46 | BooleanType,
|
43 | 47 | IntegerType,
|
44 | 48 | NestedField,
|
45 | 49 | StringType,
|
46 | 50 | )
|
| 51 | +from pyiceberg.utils.config import Config |
47 | 52 |
|
48 | 53 | TEST_URI = "https://iceberg-test-catalog/"
|
49 | 54 | TEST_CREDENTIALS = "client:secret"
|
@@ -943,3 +948,41 @@ def test_request_session_with_ssl_client_cert() -> None:
|
943 | 948 | # Missing namespace
|
944 | 949 | RestCatalog("rest", **catalog_properties) # type: ignore
|
945 | 950 | assert "Could not find the TLS certificate file, invalid path: path_to_client_cert" in str(e.value)
|
| 951 | + |
| 952 | + |
| 953 | +EXAMPLE_ENV = {"PYICEBERG_CATALOG__PRODUCTION__URI": TEST_URI} |
| 954 | + |
| 955 | + |
| 956 | +@mock.patch.dict(os.environ, EXAMPLE_ENV) |
| 957 | +@mock.patch("pyiceberg.catalog.Config.get_catalog_config") |
| 958 | +def test_catalog_from_environment_variables(catalog_config_mock: mock.Mock, rest_mock: Mocker) -> None: |
| 959 | + env_config: RecursiveDict = Config._from_environment_variables({}) |
| 960 | + catalog_config_mock.return_value = cast(RecursiveDict, env_config.get("catalog")).get("production") |
| 961 | + catalog = cast(RestCatalog, load_catalog("production")) |
| 962 | + assert catalog.uri == TEST_URI |
| 963 | + |
| 964 | + |
| 965 | +@mock.patch.dict(os.environ, EXAMPLE_ENV) |
| 966 | +@mock.patch("pyiceberg.catalog._ENV_CONFIG.get_catalog_config") |
| 967 | +def test_catalog_from_environment_variables_override(catalog_config_mock: mock.Mock, rest_mock: Mocker) -> None: |
| 968 | + rest_mock.get( |
| 969 | + "https://other-service.io/api/v1/config", |
| 970 | + json={"defaults": {}, "overrides": {}}, |
| 971 | + status_code=200, |
| 972 | + ) |
| 973 | + env_config: RecursiveDict = Config._from_environment_variables({}) |
| 974 | + |
| 975 | + catalog_config_mock.return_value = cast(RecursiveDict, env_config.get("catalog")).get("production") |
| 976 | + catalog = cast(RestCatalog, load_catalog("production", uri="https://other-service.io/api")) |
| 977 | + assert catalog.uri == "https://other-service.io/api" |
| 978 | + |
| 979 | + |
| 980 | +def test_catalog_from_parameters_empty_env(rest_mock: Mocker) -> None: |
| 981 | + rest_mock.get( |
| 982 | + "https://other-service.io/api/v1/config", |
| 983 | + json={"defaults": {}, "overrides": {}}, |
| 984 | + status_code=200, |
| 985 | + ) |
| 986 | + |
| 987 | + catalog = cast(RestCatalog, load_catalog("production", uri="https://other-service.io/api")) |
| 988 | + assert catalog.uri == "https://other-service.io/api" |
0 commit comments