Skip to content

Commit

Permalink
Fix up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gouline committed Dec 15, 2023
1 parent 9a03742 commit e5bcf76
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions tests/test_metabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import json
import logging
import unittest
from operator import itemgetter
from pathlib import Path

import yaml

from dbtmetabase.dbt import MetabaseColumn, MetabaseModel, ModelType
from dbtmetabase.metabase import MetabaseClient, _ExportModelsJob
from dbtmetabase.metabase import MetabaseClient, _ExportModelsJob, _ExtractExposuresJob

FIXTURES_PATH = Path("tests") / "fixtures"

MODELS = [
MetabaseModel(
Expand Down Expand Up @@ -245,9 +248,8 @@

class MockMetabaseClient(MetabaseClient):
def api(self, method: str, path: str, critical: bool = True, **kwargs):
BASE_PATH = "tests/fixtures/mock_api/"
if method == "get":
json_path = Path(f"{BASE_PATH}/{path.lstrip('/')}.json")
json_path = FIXTURES_PATH / "mock_api" / f"{path.lstrip('/')}.json"
if json_path.exists():
with open(json_path, encoding="utf-8") as f:
return json.load(f)
Expand All @@ -264,39 +266,38 @@ def setUp(self):
logging.basicConfig(level=logging.DEBUG)

def test_exposures(self):
mbc = self.client
mbc.extract_exposures(
MODELS,
output_name="unittest_exposures",
output_path="tests/fixtures/exposure/",
output_path = FIXTURES_PATH / "exposure"
output_name = "unittest_exposures"
job = _ExtractExposuresJob(
client=self.client,
models=MODELS,
output_path=str(output_path),
output_name=output_name,
include_personal_collections=False,
collection_excludes=None,
)
# Baseline in SCM
with open(
"tests/fixtures/exposure/baseline_test_exposures.yml", "r", encoding="utf-8"
) as f:
baseline = yaml.safe_load(f)
# Load from YAML and tear down
with open(
"tests/fixtures/exposure/unittest_exposures.yml", "r", encoding="utf-8"
) as f:
sample = yaml.safe_load(f)
job.execute()

with open(output_path / "baseline_test_exposures.yml", encoding="utf-8") as f:
expected = yaml.safe_load(f)
with open(output_path / f"{output_name}.yml", encoding="utf-8") as f:
actual = yaml.safe_load(f)

baseline_exposures = sorted(baseline["exposures"], key=lambda ele: ele["name"])
sample_exposures = sorted(sample["exposures"], key=lambda ele: ele["name"])
expected_exposures = sorted(expected["exposures"], key=itemgetter("name"))
actual_exposures = sorted(actual["exposures"], key=itemgetter("name"))

self.assertEqual(baseline_exposures, sample_exposures)
self.assertEqual(expected_exposures, actual_exposures)

def test_build_lookups(self):
client = self.client
job = _ExportModelsJob(
client=client,
client=self.client,
database="unit_testing",
models=[],
exclude_sources=True,
sync_timeout=0,
)

baseline_tables = [
expected_tables = [
"PUBLIC.CUSTOMERS",
"PUBLIC.ORDERS",
"PUBLIC.RAW_CUSTOMERS",
Expand All @@ -306,9 +307,10 @@ def test_build_lookups(self):
"PUBLIC.STG_ORDERS",
"PUBLIC.STG_PAYMENTS",
]
tables = job._load_tables(database_id="2")
self.assertEqual(baseline_tables, list(tables.keys()))
baseline_columns = [
actual_tables = job._load_tables(database_id="2")
self.assertEqual(expected_tables, list(actual_tables.keys()))

expected_columns = [
[
"CUSTOMER_ID",
"FIRST_NAME",
Expand Down Expand Up @@ -336,5 +338,5 @@ def test_build_lookups(self):
["ORDER_ID", "CUSTOMER_ID", "ORDER_DATE", "STATUS"],
["PAYMENT_ID", "ORDER_ID", "PAYMENT_METHOD", "AMOUNT"],
]
for table, columns in zip(baseline_tables, baseline_columns):
self.assertEqual(columns, list(tables[table]["fields"].keys()))
for table, columns in zip(expected_tables, expected_columns):
self.assertEqual(columns, list(actual_tables[table]["fields"].keys()))

0 comments on commit e5bcf76

Please sign in to comment.