@@ -981,3 +981,119 @@ def test_resolve_host_metadata_does_not_overwrite_token_audience(mocker):
981981 token_audience = "custom-audience" ,
982982 )
983983 assert config .token_audience == "custom-audience"
984+
985+
986+ # ---------------------------------------------------------------------------
987+ # HostType.from_api_value tests
988+ # ---------------------------------------------------------------------------
989+
990+
991+ @pytest .mark .parametrize (
992+ "api_value,expected" ,
993+ [
994+ ("workspace" , HostType .WORKSPACE ),
995+ ("Workspace" , HostType .WORKSPACE ),
996+ ("WORKSPACE" , HostType .WORKSPACE ),
997+ ("account" , HostType .ACCOUNTS ),
998+ ("Account" , HostType .ACCOUNTS ),
999+ ("ACCOUNT" , HostType .ACCOUNTS ),
1000+ ("unified" , HostType .UNIFIED ),
1001+ ("Unified" , HostType .UNIFIED ),
1002+ ("UNIFIED" , HostType .UNIFIED ),
1003+ ("unknown" , None ),
1004+ ("" , None ),
1005+ (None , None ),
1006+ ],
1007+ )
1008+ def test_host_type_from_api_value (api_value , expected ):
1009+ assert HostType .from_api_value (api_value ) == expected
1010+
1011+
1012+ # ---------------------------------------------------------------------------
1013+ # HostMetadata.from_dict with host_type field
1014+ # ---------------------------------------------------------------------------
1015+
1016+
1017+ def test_host_metadata_from_dict_with_host_type ():
1018+ """HostMetadata.from_dict parses the host_type field."""
1019+ d = {
1020+ "oidc_endpoint" : "https://host/oidc" ,
1021+ "account_id" : "acc-1" ,
1022+ "host_type" : "workspace" ,
1023+ }
1024+ meta = HostMetadata .from_dict (d )
1025+ assert meta .host_type == "workspace"
1026+
1027+
1028+ def test_host_metadata_from_dict_without_host_type ():
1029+ """HostMetadata.from_dict returns None for missing host_type."""
1030+ d = {"oidc_endpoint" : "https://host/oidc" }
1031+ meta = HostMetadata .from_dict (d )
1032+ assert meta .host_type is None
1033+
1034+
1035+ # ---------------------------------------------------------------------------
1036+ # _resolve_host_metadata populates _resolved_host_type
1037+ # ---------------------------------------------------------------------------
1038+
1039+
1040+ def test_resolve_host_metadata_populates_resolved_host_type (mocker ):
1041+ """_resolved_host_type is populated from metadata host_type."""
1042+ mocker .patch (
1043+ "databricks.sdk.config.get_host_metadata" ,
1044+ return_value = HostMetadata .from_dict (
1045+ {
1046+ "oidc_endpoint" : f"{ _DUMMY_WS_HOST } /oidc" ,
1047+ "host_type" : "unified" ,
1048+ }
1049+ ),
1050+ )
1051+ config = Config (host = _DUMMY_WS_HOST , token = "t" )
1052+ assert config ._resolved_host_type == HostType .UNIFIED
1053+
1054+
1055+ def test_resolve_host_metadata_does_not_overwrite_existing_resolved_host_type (mocker ):
1056+ """An already-set _resolved_host_type is not overwritten by metadata."""
1057+ mocker .patch (
1058+ "databricks.sdk.config.get_host_metadata" ,
1059+ return_value = HostMetadata .from_dict (
1060+ {
1061+ "oidc_endpoint" : f"{ _DUMMY_WS_HOST } /oidc" ,
1062+ "host_type" : "account" ,
1063+ }
1064+ ),
1065+ )
1066+ config = Config (host = _DUMMY_WS_HOST , token = "t" )
1067+ # Manually set resolved host type then re-resolve
1068+ config ._resolved_host_type = HostType .UNIFIED
1069+ config ._resolve_host_metadata ()
1070+ assert config ._resolved_host_type == HostType .UNIFIED
1071+
1072+
1073+ def test_resolve_host_metadata_resolved_host_type_none_when_missing (mocker ):
1074+ """_resolved_host_type stays None when metadata has no host_type."""
1075+ mocker .patch (
1076+ "databricks.sdk.config.get_host_metadata" ,
1077+ return_value = HostMetadata .from_dict (
1078+ {
1079+ "oidc_endpoint" : f"{ _DUMMY_WS_HOST } /oidc" ,
1080+ }
1081+ ),
1082+ )
1083+ config = Config (host = _DUMMY_WS_HOST , token = "t" )
1084+ assert config ._resolved_host_type is None
1085+
1086+
1087+ def test_resolve_host_metadata_resolved_host_type_unknown_string (mocker ):
1088+ """_resolved_host_type stays None for unrecognized host_type strings."""
1089+ mocker .patch (
1090+ "databricks.sdk.config.get_host_metadata" ,
1091+ return_value = HostMetadata .from_dict (
1092+ {
1093+ "oidc_endpoint" : f"{ _DUMMY_WS_HOST } /oidc" ,
1094+ "host_type" : "some_future_type" ,
1095+ }
1096+ ),
1097+ )
1098+ config = Config (host = _DUMMY_WS_HOST , token = "t" )
1099+ assert config ._resolved_host_type is None
0 commit comments