Skip to content

Commit c542a42

Browse files
committed
refactor: remove testable mangled names
1 parent ea0693d commit c542a42

File tree

6 files changed

+71
-66
lines changed

6 files changed

+71
-66
lines changed

dbtmetabase/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
]
1010

1111
try:
12-
from ._version import __version__ as version # type: ignore
12+
from ._version import __version__ as version
1313

1414
__version__ = version
1515
except ModuleNotFoundError:

dbtmetabase/_exposures.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def dbname(details: Mapping) -> str:
8888
return details[key]
8989
return ""
9090

91-
ctx = self.__Context(
91+
ctx = _Context(
9292
model_refs={m.alias_path.lower(): m.ref for m in models if m.ref},
9393
database_names={
9494
d["id"]: dbname(d["details"]) for d in self.metabase.get_databases()
@@ -127,7 +127,7 @@ def dbname(details: Mapping) -> str:
127127
uid=collection["id"],
128128
models=("card", "dashboard"),
129129
):
130-
exposure = self.__Exposure(
130+
exposure = _Exposure(
131131
model=item["model"],
132132
uid=item["id"],
133133
label="Exposure [Unresolved Name]",
@@ -153,7 +153,7 @@ def dbname(details: Mapping) -> str:
153153
f"Visualization: {entity.get('display', 'Unknown').title()}"
154154
)
155155

156-
self.__exposure_card(ctx, exposure, entity)
156+
self._exposure_card(ctx, exposure, entity)
157157

158158
if average_query_time_ms := entity.get("average_query_time"):
159159
average_query_time_s = average_query_time_ms / 1000
@@ -179,7 +179,7 @@ def dbname(details: Mapping) -> str:
179179
continue
180180

181181
if card := self.metabase.find_card(uid=card["id"]):
182-
self.__exposure_card(ctx, exposure, card)
182+
self._exposure_card(ctx, exposure, card)
183183
else:
184184
_logger.warning("Unexpected collection item '%s'", item["model"])
185185
continue
@@ -236,7 +236,7 @@ def dbname(details: Mapping) -> str:
236236

237237
return exposures
238238

239-
def __exposure_card(self, ctx: __Context, exposure: __Exposure, card: Mapping):
239+
def _exposure_card(self, ctx: _Context, exposure: _Exposure, card: Mapping):
240240
"""Extracts exposures from Metabase questions."""
241241

242242
dataset_query = card.get("dataset_query", {})
@@ -248,7 +248,7 @@ def __exposure_card(self, ctx: __Context, exposure: __Exposure, card: Mapping):
248248
else:
249249
_logger.warning("Unsupported card type '%s'", card_type)
250250

251-
def __exposure_query(self, ctx: __Context, exposure: __Exposure, card: Mapping):
251+
def __exposure_query(self, ctx: _Context, exposure: _Exposure, card: Mapping):
252252
"""Extracts exposures from Metabase GUI queries."""
253253

254254
dataset_query = card.get("dataset_query", {})
@@ -259,7 +259,7 @@ def __exposure_query(self, ctx: __Context, exposure: __Exposure, card: Mapping):
259259
# Question based on another question
260260
source_card_uid = query_source.split("__")[-1]
261261
if source_card := self.metabase.find_card(uid=source_card_uid):
262-
self.__exposure_card(ctx, exposure, source_card)
262+
self._exposure_card(ctx, exposure, source_card)
263263

264264
elif isinstance(query_source, int) and query_source in ctx.table_names:
265265
# Question based on table
@@ -274,7 +274,7 @@ def __exposure_query(self, ctx: __Context, exposure: __Exposure, card: Mapping):
274274
# Question based on another question
275275
source_card_uid = join_source.split("__")[-1]
276276
if source_card := self.metabase.find_card(uid=source_card_uid):
277-
self.__exposure_card(ctx, exposure, source_card)
277+
self._exposure_card(ctx, exposure, source_card)
278278

279279
continue
280280

@@ -284,7 +284,7 @@ def __exposure_query(self, ctx: __Context, exposure: __Exposure, card: Mapping):
284284
exposure.depends.add(joined_table)
285285
_logger.info("Extracted model '%s' from join", joined_table)
286286

287-
def __exposure_native(self, ctx: __Context, exposure: __Exposure, card: Mapping):
287+
def __exposure_native(self, ctx: _Context, exposure: _Exposure, card: Mapping):
288288
"""Extracts exposures from Metabase native queries."""
289289

290290
dataset_query = card.get("dataset_query", {})
@@ -410,8 +410,8 @@ def __format_exposure(
410410

411411
return exposure
412412

413-
@staticmethod
414413
def __write_exposures(
414+
self,
415415
exposures: Iterable[Mapping],
416416
output_path: str,
417417
output_grouping: Optional[str],
@@ -448,24 +448,26 @@ def __write_exposures(
448448
stream=f,
449449
)
450450

451-
@dc.dataclass
452-
class __Context:
453-
model_refs: Mapping[str, str]
454-
database_names: Mapping[int, str]
455-
table_names: Mapping[int, str]
456-
457-
@dc.dataclass
458-
class __Exposure:
459-
model: str
460-
uid: str
461-
label: str
462-
name: str = ""
463-
description: str = ""
464-
created_at: str = ""
465-
header: str = ""
466-
creator_name: str = ""
467-
creator_email: str = ""
468-
average_query_time: Optional[str] = None
469-
last_used_at: Optional[str] = None
470-
native_query: Optional[str] = None
471-
depends: Set[str] = dc.field(default_factory=set)
451+
452+
@dc.dataclass
453+
class _Context:
454+
model_refs: Mapping[str, str]
455+
database_names: Mapping[int, str]
456+
table_names: Mapping[int, str]
457+
458+
459+
@dc.dataclass
460+
class _Exposure:
461+
model: str
462+
uid: str
463+
label: str
464+
name: str = ""
465+
description: str = ""
466+
created_at: str = ""
467+
header: str = ""
468+
creator_name: str = ""
469+
creator_email: str = ""
470+
average_query_time: Optional[str] = None
471+
last_used_at: Optional[str] = None
472+
native_query: Optional[str] = None
473+
depends: Set[str] = dc.field(default_factory=set)

dbtmetabase/_models.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
_logger = logging.getLogger(__name__)
1515

16+
_SYNC_PERIOD = 5
17+
1618

1719
class ModelsMixin(metaclass=ABCMeta):
1820
"""Abstraction for exporting models."""
1921

20-
__SYNC_PERIOD = 5
21-
2222
DEFAULT_MODELS_SYNC_TIMEOUT = 30
2323

2424
@property
@@ -57,7 +57,7 @@ def export_models(
5757
order_fields (bool, optional): Preserve column order in dbt project.
5858
"""
5959

60-
ctx = self.__Context()
60+
ctx = _Context()
6161
success = True
6262

6363
database = self.metabase.find_database(name=metabase_database)
@@ -77,9 +77,9 @@ def export_models(
7777
deadline = int(time.time()) + sync_timeout
7878
synced = False
7979
while not synced:
80-
time.sleep(self.__SYNC_PERIOD)
80+
time.sleep(_SYNC_PERIOD)
8181

82-
tables = self.__get_tables(database["id"])
82+
tables = self._get_metabase_tables(database["id"])
8383

8484
synced = True
8585
for model in models:
@@ -124,7 +124,7 @@ def export_models(
124124
raise MetabaseStateError("Unable to sync models with Metabase")
125125

126126
for model in models:
127-
success &= self.__export_model(
127+
success &= self._export_model(
128128
ctx=ctx,
129129
model=model,
130130
append_tags=append_tags,
@@ -159,9 +159,9 @@ def export_models(
159159
if not success:
160160
raise MetabaseStateError("Non-critical errors encountered, see above")
161161

162-
def __export_model(
162+
def _export_model(
163163
self,
164-
ctx: __Context,
164+
ctx: _Context,
165165
model: Model,
166166
append_tags: bool,
167167
docs_url: Optional[str],
@@ -250,7 +250,7 @@ def __export_model(
250250

251251
def __export_model_column_order(
252252
self,
253-
ctx: __Context,
253+
ctx: _Context,
254254
model: Model,
255255
api_table: Mapping,
256256
table_key: str,
@@ -303,7 +303,7 @@ def __export_model_column_order(
303303

304304
def __export_column(
305305
self,
306-
ctx: __Context,
306+
ctx: _Context,
307307
schema_name: str,
308308
model_name: str,
309309
column: Column,
@@ -430,7 +430,7 @@ def __export_column(
430430

431431
return success
432432

433-
def __get_tables(self, database_id: str) -> Mapping[str, MutableMapping]:
433+
def _get_metabase_tables(self, database_id: str) -> Mapping[str, MutableMapping]:
434434
tables = {}
435435

436436
metadata = self.metabase.get_database_metadata(database_id)
@@ -461,8 +461,8 @@ def __get_tables(self, database_id: str) -> Mapping[str, MutableMapping]:
461461

462462
return tables
463463

464-
@staticmethod
465464
def __filtered_models(
465+
self,
466466
models: Iterable[Model],
467467
database_filter: Optional[Filter],
468468
schema_filter: Optional[Filter],
@@ -479,25 +479,26 @@ def selected(m: Model) -> bool:
479479

480480
return list(filter(selected, models))
481481

482-
@dc.dataclass
483-
class __Context:
484-
tables: Mapping[str, MutableMapping] = dc.field(default_factory=dict)
485-
updates: MutableMapping[str, MutableMapping] = dc.field(default_factory=dict)
486482

487-
def get_field(self, table_key: str, field_key: str) -> MutableMapping:
488-
return self.tables.get(table_key, {}).get("fields", {}).get(field_key, {})
483+
@dc.dataclass
484+
class _Context:
485+
tables: Mapping[str, MutableMapping] = dc.field(default_factory=dict)
486+
updates: MutableMapping[str, MutableMapping] = dc.field(default_factory=dict)
487+
488+
def get_field(self, table_key: str, field_key: str) -> MutableMapping:
489+
return self.tables.get(table_key, {}).get("fields", {}).get(field_key, {})
489490

490-
def update(self, entity: MutableMapping, change: Mapping, label: str):
491-
entity.update(change)
491+
def update(self, entity: MutableMapping, change: Mapping, label: str):
492+
entity.update(change)
492493

493-
key = f"{entity['kind']}.{entity['id']}"
494-
update = self.updates.get(key, {})
495-
update["kind"] = entity["kind"]
496-
update["id"] = entity["id"]
497-
update["label"] = label
494+
key = f"{entity['kind']}.{entity['id']}"
495+
update = self.updates.get(key, {})
496+
update["kind"] = entity["kind"]
497+
update["id"] = entity["id"]
498+
update["label"] = label
498499

499-
body = update.get("body", {})
500-
body.update(change)
501-
update["body"] = body
500+
body = update.get("body", {})
501+
body.update(change)
502+
update["body"] = body
502503

503-
self.updates[key] = update
504+
self.updates[key] = update

tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import pytest
22

3+
import dbtmetabase._models
34
from tests._mocks import TMP_PATH, MockDbtMetabase, MockMetabase
45

56

67
@pytest.fixture(name="core")
78
def fixture_core() -> MockDbtMetabase:
89
c = MockDbtMetabase()
9-
c._ModelsMixin__SYNC_PERIOD = 1 # type: ignore
10+
dbtmetabase._models._SYNC_PERIOD = 1
1011
return c
1112

1213

tests/test_exposures.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
import yaml
66

7+
from dbtmetabase._exposures import _Context, _Exposure
78
from tests._mocks import FIXTURES_PATH, TMP_PATH, MockDbtMetabase
89

910

@@ -96,20 +97,20 @@ def test_extract_exposures_native_depends(
9697
query: str,
9798
expected: set,
9899
):
99-
ctx = MockDbtMetabase._ExposuresMixin__Context( # type: ignore
100+
ctx = _Context(
100101
model_refs={
101102
"database.schema.table0": "model0",
102103
"database.public.table1": "model1",
103104
},
104105
database_names={1: "database"},
105106
table_names={},
106107
)
107-
exposure = MockDbtMetabase._ExposuresMixin__Exposure( # type: ignore
108+
exposure = _Exposure(
108109
model="card",
109110
uid="",
110111
label="",
111112
)
112-
core._ExposuresMixin__exposure_card( # type: ignore
113+
core._exposure_card(
113114
ctx=ctx,
114115
exposure=exposure,
115116
card={

tests/test_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_build_lookups(core: MockDbtMetabase):
8080
"INVENTORY.SKUS": {"SKU_ID", "PRODUCT"},
8181
}
8282

83-
actual_tables = core._ModelsMixin__get_tables(database_id="2") # type: ignore
83+
actual_tables = core._get_metabase_tables(database_id="2")
8484

8585
assert set(actual_tables.keys()) == set(expected.keys())
8686

0 commit comments

Comments
 (0)