Skip to content

Commit c19d74d

Browse files
committed
Fixed based on the suggestions
1 parent 0f4e94c commit c19d74d

File tree

4 files changed

+64
-30
lines changed

4 files changed

+64
-30
lines changed

pyatlan/cache/template_config_cache.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pyatlan.client.atlan import AtlanClient
1313

1414

15-
class TemplateConfigCache:
15+
class DQTemplateConfigCache:
1616
"""
1717
Lazily-loaded cache for DQ rule template configurations to avoid multiple API calls.
1818
"""
@@ -60,7 +60,8 @@ def _refresh_cache(self) -> None:
6060
)
6161
).to_request()
6262

63-
for result in self.client.asset.search(request):
63+
results = self.client.asset.search(request)
64+
for result in results:
6465
template_config = {
6566
"name": result.name,
6667
"qualified_name": result.qualified_name,

pyatlan/client/atlan.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from pyatlan.cache.group_cache import GroupCache
3838
from pyatlan.cache.role_cache import RoleCache
3939
from pyatlan.cache.source_tag_cache import SourceTagCache
40-
from pyatlan.cache.template_config_cache import TemplateConfigCache
40+
from pyatlan.cache.template_config_cache import DQTemplateConfigCache
4141
from pyatlan.cache.user_cache import UserCache
4242
from pyatlan.client.admin import AdminClient
4343
from pyatlan.client.asset import A, AssetClient, IndexSearchResults, LineageListResults
@@ -175,7 +175,9 @@ class AtlanClient(BaseSettings):
175175
_custom_metadata_cache: Optional[CustomMetadataCache] = PrivateAttr(default=None)
176176
_connection_cache: Optional[ConnectionCache] = PrivateAttr(default=None)
177177
_source_tag_cache: Optional[SourceTagCache] = PrivateAttr(default=None)
178-
_template_config_cache: Optional[TemplateConfigCache] = PrivateAttr(default=None)
178+
_dq_template_config_cache: Optional[DQTemplateConfigCache] = PrivateAttr(
179+
default=None
180+
)
179181

180182
class Config:
181183
env_prefix = "atlan_"
@@ -350,10 +352,10 @@ def source_tag_cache(self) -> SourceTagCache:
350352
return self._source_tag_cache
351353

352354
@property
353-
def template_config_cache(self) -> TemplateConfigCache:
354-
if self._template_config_cache is None:
355-
self._template_config_cache = TemplateConfigCache(client=self)
356-
return self._template_config_cache
355+
def dq_template_config_cache(self) -> DQTemplateConfigCache:
356+
if self._dq_template_config_cache is None:
357+
self._dq_template_config_cache = DQTemplateConfigCache(client=self)
358+
return self._dq_template_config_cache
357359

358360
@classmethod
359361
def from_token_guid(cls, guid: str) -> AtlanClient:

pyatlan/model/assets/core/alpha__d_q_rule.py

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def custom_sql_creator(
6161
) -> alpha_DQRule:
6262
validate_required_fields(
6363
[
64+
"client",
6465
"rule_name",
6566
"asset",
6667
"threshold_compare_operator",
@@ -70,6 +71,7 @@ def custom_sql_creator(
7071
"custom_sql",
7172
],
7273
[
74+
client,
7375
rule_name,
7476
asset,
7577
threshold_compare_operator,
@@ -110,13 +112,15 @@ def table_level_rule_creator(
110112
) -> alpha_DQRule:
111113
validate_required_fields(
112114
[
115+
"client",
113116
"rule_type",
114117
"asset",
115118
"threshold_compare_operator",
116119
"threshold_value",
117120
"alert_priority",
118121
],
119122
[
123+
client,
120124
rule_type,
121125
asset,
122126
threshold_compare_operator,
@@ -159,6 +163,7 @@ def column_level_rule_creator(
159163
) -> alpha_DQRule:
160164
validate_required_fields(
161165
[
166+
"client",
162167
"rule_type",
163168
"asset",
164169
"column",
@@ -167,6 +172,7 @@ def column_level_rule_creator(
167172
"alert_priority",
168173
],
169174
[
175+
client,
170176
rule_type,
171177
asset,
172178
column,
@@ -212,8 +218,8 @@ def updater(
212218
from pyatlan.model.fluent_search import FluentSearch
213219

214220
validate_required_fields(
215-
["qualified_name"],
216-
[qualified_name],
221+
["client", "qualified_name"],
222+
[client, qualified_name],
217223
)
218224
request = (
219225
FluentSearch()
@@ -235,6 +241,11 @@ def updater(
235241

236242
results = client.asset.search(request)
237243

244+
if results.count != 1:
245+
raise ValueError(
246+
f"Expected exactly 1 asset for qualified_name: {qualified_name}, "
247+
f"but found: {results.count}"
248+
)
238249
search_result = results.current_page()[0]
239250

240251
retrieved_custom_sql = search_result.alpha_dq_rule_custom_s_q_l # type: ignore[attr-defined]
@@ -246,9 +257,27 @@ def updater(
246257
retrieved_asset = search_result.alpha_dq_rule_base_dataset # type: ignore[attr-defined]
247258
retrieved_template_rule_name = search_result.alpha_dq_rule_template_name # type: ignore[attr-defined]
248259
retrieved_template = search_result.alpha_dq_rule_template # type: ignore[attr-defined]
249-
retrieved_threshold_compare_operator = search_result.alpha_dq_rule_config_arguments.alpha_dq_rule_threshold_object.alpha_dq_rule_threshold_compare_operator # type: ignore[attr-defined]
250-
retrieved_threshold_value = search_result.alpha_dq_rule_config_arguments.alpha_dq_rule_threshold_object.alpha_dq_rule_threshold_value # type: ignore[attr-defined]
251-
retrieved_threshold_unit = search_result.alpha_dq_rule_config_arguments.alpha_dq_rule_threshold_object.alpha_dq_rule_threshold_unit # type: ignore[attr-defined]
260+
retrieved_threshold_compare_operator = (
261+
search_result.alpha_dq_rule_config_arguments.alpha_dq_rule_threshold_object.alpha_dq_rule_threshold_compare_operator # type: ignore[attr-defined]
262+
if search_result.alpha_dq_rule_config_arguments is not None # type: ignore[attr-defined]
263+
and search_result.alpha_dq_rule_config_arguments.alpha_dq_rule_threshold_object # type: ignore[attr-defined]
264+
is not None
265+
else None
266+
)
267+
retrieved_threshold_value = (
268+
search_result.alpha_dq_rule_config_arguments.alpha_dq_rule_threshold_object.alpha_dq_rule_threshold_value # type: ignore[attr-defined]
269+
if search_result.alpha_dq_rule_config_arguments is not None # type: ignore[attr-defined]
270+
and search_result.alpha_dq_rule_config_arguments.alpha_dq_rule_threshold_object # type: ignore[attr-defined]
271+
is not None
272+
else None
273+
) # type: ignore[attr-defined]
274+
retrieved_threshold_unit = (
275+
search_result.alpha_dq_rule_config_arguments.alpha_dq_rule_threshold_object.alpha_dq_rule_threshold_unit # type: ignore[attr-defined]
276+
if search_result.alpha_dq_rule_config_arguments is not None # type: ignore[attr-defined]
277+
and search_result.alpha_dq_rule_config_arguments.alpha_dq_rule_threshold_object # type: ignore[attr-defined]
278+
is not None
279+
else None
280+
) # type: ignore[attr-defined]
252281

253282
config_arguments_raw = alpha_DQRule.Attributes._generate_config_arguments_raw(
254283
is_alert_enabled=True,
@@ -1053,30 +1082,32 @@ def creator(
10531082
custom_sql: Optional[str] = None,
10541083
description: Optional[str] = None,
10551084
) -> alpha_DQRule.Attributes:
1056-
template_config = client.template_config_cache.get_template_config(
1085+
template_config = client.dq_template_config_cache.get_template_config(
10571086
rule_type
10581087
)
10591088

10601089
if template_config is None:
10611090
raise ErrorCode.DQ_RULE_NOT_FOUND.exception_with_parameters(rule_type)
10621091

1063-
template_rule_name = template_config["name"]
1064-
template_qualified_name = template_config["qualified_name"]
1092+
template_rule_name = template_config.get("name")
1093+
template_qualified_name = template_config.get("qualified_name")
10651094

10661095
if dimension is None:
1067-
dimension = template_config["dimension"]
1096+
dimension = template_config.get("dimension")
10681097

10691098
if threshold_unit is None:
1070-
threashold_object = template_config[
1071-
"config"
1072-
].alpha_dq_rule_template_config_threshold_object
1073-
threashold_object_json = json.loads(threashold_object)
1074-
properties = threashold_object_json.get("properties", {})
1075-
threshold_unit_field = properties.get(
1076-
"alpha_dqRuleTemplateConfigThresholdUnit", {}
1077-
)
1078-
default_value = threshold_unit_field.get("default")
1079-
threshold_unit = default_value
1099+
config = template_config.get("config")
1100+
if config is not None:
1101+
threashold_object = (
1102+
config.alpha_dq_rule_template_config_threshold_object
1103+
)
1104+
threashold_object_json = json.loads(threashold_object)
1105+
properties = threashold_object_json.get("properties", {})
1106+
threshold_unit_field = properties.get(
1107+
"alpha_dqRuleTemplateConfigThresholdUnit", {}
1108+
)
1109+
default_value = threshold_unit_field.get("default")
1110+
threshold_unit = default_value
10801111

10811112
config_arguments_raw = (
10821113
alpha_DQRule.Attributes._generate_config_arguments_raw(
@@ -1112,7 +1143,7 @@ def creator(
11121143
alpha_dq_rule_dimension=dimension,
11131144
alpha_dq_rule_template_name=template_rule_name,
11141145
alpha_dq_rule_template=alpha_DQRuleTemplate.ref_by_qualified_name(
1115-
qualified_name=template_qualified_name
1146+
qualified_name=template_qualified_name # type: ignore
11161147
),
11171148
)
11181149

tests/unit/model/alpha__d_q_rule_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
@pytest.fixture
2727
def mock_client():
2828
client = Mock()
29-
client.template_config_cache = Mock()
29+
client.dq_template_config_cache = Mock()
3030

3131
# Create a proper config object with a JSON string for threshold_object
3232
config = Mock()
@@ -40,7 +40,7 @@ def mock_client():
4040
}
4141
)
4242

43-
client.template_config_cache.get_template_config.return_value = {
43+
client.dq_template_config_cache.get_template_config.return_value = {
4444
"name": "Test Template",
4545
"qualified_name": "test/template/123",
4646
"dimension": alpha_DQDimension.COMPLETENESS,

0 commit comments

Comments
 (0)