Skip to content

Commit e30c690

Browse files
committed
swap preference in merge_config
1 parent 4a876c2 commit e30c690

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

pyiceberg/utils/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def merge_config(lhs: RecursiveDict, rhs: RecursiveDict) -> RecursiveDict:
4343
new_config[rhs_key] = merge_config(lhs_value, rhs_value)
4444
else:
4545
# Take the non-null value, with precedence on rhs
46-
new_config[rhs_key] = lhs_value or rhs_value
46+
new_config[rhs_key] = rhs_value or lhs_value
4747
else:
4848
# New key
4949
new_config[rhs_key] = rhs_value

tests/__init__.py

Whitespace-only changes.

tests/catalog/test_rest.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717
# pylint: disable=redefined-outer-name,unused-argument
18+
import os
19+
from unittest import mock
1820
from uuid import UUID
1921

2022
import pytest
2123
from requests_mock import Mocker
2224

2325
import pyiceberg
24-
from pyiceberg.catalog import PropertiesUpdateSummary, Table
26+
from pyiceberg.catalog import PropertiesUpdateSummary, Table, load_catalog
2527
from pyiceberg.catalog.rest import RestCatalog
28+
from pyiceberg.utils.config import Config
2629
from pyiceberg.exceptions import (
2730
NamespaceAlreadyExistsError,
2831
NoSuchNamespaceError,
@@ -943,3 +946,39 @@ def test_request_session_with_ssl_client_cert() -> None:
943946
# Missing namespace
944947
RestCatalog("rest", **catalog_properties) # type: ignore
945948
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"

tests/utils/test_config.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import pytest
2121
from strictyaml import as_document
2222

23-
from pyiceberg.utils.config import Config, _lowercase_dictionary_keys
23+
from pyiceberg.utils.config import Config, _lowercase_dictionary_keys, merge_config
2424

2525
EXAMPLE_ENV = {"PYICEBERG_CATALOG__PRODUCTION__URI": "https://service.io/api"}
2626

@@ -54,3 +54,10 @@ def test_lowercase_dictionary_keys() -> None:
5454
uppercase_keys = {"UPPER": {"NESTED_UPPER": {"YES"}}}
5555
expected = {"upper": {"nested_upper": {"YES"}}}
5656
assert _lowercase_dictionary_keys(uppercase_keys) == expected # type: ignore
57+
58+
def test_merge_config() -> None:
59+
lhs = {"common_key": "abc123"}
60+
rhs = {"common_key": "xyz789"}
61+
result = merge_config(lhs, rhs)
62+
assert result["common_key"] == rhs["common_key"]
63+

0 commit comments

Comments
 (0)