Skip to content

Commit

Permalink
Add basic unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Wei-Chun, Chang <[email protected]>
  • Loading branch information
wcchang1115 committed Sep 1, 2023
1 parent 4b3df7d commit 0f20229
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
2 changes: 1 addition & 1 deletion piperider_cli/dbtutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def find_derived_time_grains(manifest: Dict, metric: Dict):
return time_grains


def get_dbt_state_metrics_16(dbt_state_dir: str, dbt_tag: str, dbt_resources: Optional[dict] = None):
def get_dbt_state_metrics_16(dbt_state_dir: str, dbt_tag: Optional[str] = None, dbt_resources: Optional[dict] = None):
manifest = _get_state_manifest(dbt_state_dir)

if not is_dbt_schema_version_16(manifest):
Expand Down

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions tests/test_dbt_integeration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Dict, List
from unittest import TestCase

import pytest
from packaging import version

from piperider_cli.dbt import dbt_version
Expand All @@ -13,6 +14,7 @@
load_manifest,
)
from piperider_cli.dbt.changeset import GraphDataChangeSet
from tests.test_dbt_manifest_compatible import _load_manifest


class _BaseDbtTest(TestCase):
Expand Down Expand Up @@ -125,6 +127,59 @@ def test_list_all_resources(self):

self.assertDbtResources(expected, all_results)

@pytest.mark.skipif(dbt_version < 'v1.6', reason='skip manifest test before dbt-core 1.6')
def test_list_all_resources_16(self):
expected = [
"metric.jaffle_shop.average_order_amount",
"metric.jaffle_shop.expenses",
"metric.jaffle_shop.profit",
"metric.jaffle_shop.revenue",
"model.jaffle_shop.int_customer_order_history_joined",
"model.jaffle_shop.int_order_payments_pivoted",
"model.jaffle_shop.metricflow_time_spine",
"model.jaffle_shop.orders",
"model.jaffle_shop.stg_customers",
"model.jaffle_shop.stg_orders",
"model.jaffle_shop.stg_payments",
"seed.jaffle_shop.raw_customers",
"seed.jaffle_shop.raw_orders",
"seed.jaffle_shop.raw_payments",
"test.jaffle_shop.accepted_values_int_order_payments_pivoted_status__placed__shipped__completed__return_pending__returned.0ccdff53e8",
"test.jaffle_shop.accepted_values_orders_status__placed__shipped__completed__return_pending__returned.be6b5b5ec3",
"test.jaffle_shop.accepted_values_stg_orders_status__placed__shipped__completed__return_pending__returned.080fb20aad",
"test.jaffle_shop.accepted_values_stg_payments_payment_method__credit_card__coupon__bank_transfer__gift_card.3c3820f278",
"test.jaffle_shop.not_null_int_customer_order_history_joined_customer_id.5eeb8cdf92",
"test.jaffle_shop.not_null_int_order_payments_pivoted_amount.b7598e0e3b",
"test.jaffle_shop.not_null_int_order_payments_pivoted_bank_transfer_amount.1a9e62933b",
"test.jaffle_shop.not_null_int_order_payments_pivoted_coupon_amount.2532b538c2",
"test.jaffle_shop.not_null_int_order_payments_pivoted_credit_card_amount.ae9c42d967",
"test.jaffle_shop.not_null_int_order_payments_pivoted_customer_id.3db59c6de4",
"test.jaffle_shop.not_null_int_order_payments_pivoted_gift_card_amount.710d789cc0",
"test.jaffle_shop.not_null_int_order_payments_pivoted_order_id.787ba994a8",
"test.jaffle_shop.not_null_orders_amount.106140f9fd",
"test.jaffle_shop.not_null_orders_bank_transfer_amount.7743500c49",
"test.jaffle_shop.not_null_orders_coupon_amount.ab90c90625",
"test.jaffle_shop.not_null_orders_credit_card_amount.d3ca593b59",
"test.jaffle_shop.not_null_orders_customer_id.c5f02694af",
"test.jaffle_shop.not_null_orders_gift_card_amount.413a0d2d7a",
"test.jaffle_shop.not_null_orders_order_id.cf6c17daed",
"test.jaffle_shop.not_null_stg_customers_customer_id.e2cfb1f9aa",
"test.jaffle_shop.not_null_stg_orders_order_id.81cfe2fe64",
"test.jaffle_shop.not_null_stg_payments_payment_id.c19cc50075",
"test.jaffle_shop.relationships_int_order_payments_pivoted_customer_id__customer_id__ref_int_customer_order_history_joined_.654a1aa35d",
"test.jaffle_shop.unique_int_customer_order_history_joined_customer_id.995635f7d9",
"test.jaffle_shop.unique_int_order_payments_pivoted_order_id.34a0f3307d",
"test.jaffle_shop.unique_orders_order_id.fed79b3a6e",
"test.jaffle_shop.unique_stg_customers_customer_id.c7614daada",
"test.jaffle_shop.unique_stg_orders_order_id.e3b841c71a",
"test.jaffle_shop.unique_stg_payments_payment_id.3744510712",
]

manifest = load_manifest(_load_manifest('dbt-duckdb-1.6.0-manifest.json'))
all_results = list_resources_unique_id_from_manifest(manifest)

self.assertDbtResources(expected, all_results)

def test_compare_with_manifests(self):
without_downstream = compare_models_between_manifests(
self.base_manifest(), self.target_manifest()
Expand Down
11 changes: 11 additions & 0 deletions tests/test_dbt_manifest_compatible.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def manifest_from_1_5():
return _load_manifest('dbt-duckdb-1.5.1-manifest.json')


@pytest.fixture()
def manifest_from_1_6():
return _load_manifest('dbt-duckdb-1.6.0-manifest.json')


def check_manifest_type(m):
from dbt.contracts.graph.manifest import Manifest, WritableManifest
assert isinstance(m, Manifest) or isinstance(m, WritableManifest)
Expand All @@ -51,6 +56,12 @@ def test_load_manifest_v9(manifest_from_1_5):
check_manifest_type(m)


@pytest.mark.skipif(dbt_version < version.parse('v1.6'), reason='skip manifest test before dbt-core 1.6')
def test_load_manifest_10(manifest_from_1_6):
m = load_manifest(manifest_from_1_6)
check_manifest_type(m)


def test_log_functions():
with disable_dbt_compile_stats():
# make sure this not breaking in all dbt versions
Expand Down
30 changes: 30 additions & 0 deletions tests/test_dbt_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from pathlib import Path
from unittest import TestCase, mock, skip

import pytest

import piperider_cli.dbtutil as dbtutil
from piperider_cli.datasource.sqlite import SqliteDataSource
from piperider_cli.dbt import dbt_version
Expand Down Expand Up @@ -222,6 +224,34 @@ def test_get_dbt_state_metrics_only_resources(self):
self.assertEqual(len(metrics), 5)
self.assertEqual(metrics[0].name, 'total_events')

@pytest.mark.skipif(dbt_version < '1.6', reason="only for dbt 1.6")
@mock.patch('piperider_cli.dbtutil._get_state_manifest')
def test_get_dbt_state_metrics_16(self, _get_state_manifest):
_get_state_manifest.return_value = _load_manifest('dbt-duckdb-1.6.0-manifest.json')
metrics = dbtutil.get_dbt_state_metrics_16(self.dbt_state_dir, dbt_tag=None, dbt_resources=None)

self.assertEqual(len(metrics), 4)
# expenses
self.assertEqual(metrics[0].name, 'expenses')

# revenue
self.assertEqual(metrics[1].calculation_method, 'sum')

# average_order_amount
self.assertEqual(metrics[2].model.table, 'orders')
self.assertEqual(metrics[2].model.timestamp, 'order_date')
self.assertEqual(metrics[2].model.expression, 'amount')

# profit
self.assertEqual(metrics[3].model, None)
self.assertEqual(metrics[3].calculation_method, 'derived')
self.assertEqual(metrics[3].expression, 'revenue - expenses')

# skip metric < 1.6
_get_state_manifest.return_value = _load_manifest('dbt-duckdb-1.5.1-manifest.json')
metrics = dbtutil.get_dbt_state_metrics_16(self.dbt_state_dir, dbt_tag=None, dbt_resources=None)
self.assertEqual(len(metrics), 0)

@mock.patch('pathlib.Path.cwd',
return_value=Path(os.path.join(os.path.dirname(__file__), 'mock_dbt_project', 'dir_1', 'dir_2')))
def test_search_dbt_project_path(self, *args):
Expand Down

0 comments on commit 0f20229

Please sign in to comment.