Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/drc 953 test case enhance unit test to prevent drc 935 wont be #537

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tests/data/config/recce.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Preset Checks
# Please see https://datarecce.io/docs/features/preset-checks/
checks:
- name: Row count diff
description: Check the row count diff for all table models.
type: row_count_diff
params:
select: state:modified,config.materialized:table
- name: Schema diff
description: Check the schema diff for all nodes.
type: schema_diff
46 changes: 46 additions & 0 deletions tests/tasks/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import os
from unittest import TestCase
from unittest.mock import MagicMock

import pytest

from recce.adapter.dbt_adapter import load_manifest, load_catalog, DbtAdapter
from recce.core import RecceContext, set_default_context
from recce.run import schema_diff_should_be_approved


def test_validator():
from recce.tasks.schema import SchemaDiffCheckValidator
Expand Down Expand Up @@ -48,3 +56,41 @@ def validate(params: dict):
validate({
'view_mode': 'abc',
})


test_root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


class TestSchemaDiffAutoApprove(TestCase):

def setUp(self):
self.default_context = MagicMock(spec=RecceContext)
manifest = load_manifest(path=os.path.join(test_root_path, 'manifest.json'))
catalog = load_catalog(path=os.path.join(test_root_path, 'catalog.json'))
dbt_adapter = DbtAdapter(curr_manifest=manifest, curr_catalog=catalog)
self.default_context.adapter = dbt_adapter

dbt_adapter.select_nodes = MagicMock()
# Base and Current will be the same
self.default_context.get_lineage.return_value = dbt_adapter.get_lineage()
set_default_context(self.default_context)

def test_schema_diff_should_be_approved(self):
# Node_id is string
is_approved = schema_diff_should_be_approved({
'node_id': 'model.jaffle_shop.customers',
})
assert is_approved is True

# Node_id is list
is_approved = schema_diff_should_be_approved({
'node_id': ['model.jaffle_shop.customers'],
})
assert is_approved is True

# Select all models
self.default_context.adapter.select_nodes.return_value = ['model.jaffle_shop.customers']
is_approved = schema_diff_should_be_approved({
'select': 'customers',
})
assert is_approved is True
37 changes: 37 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os.path
from unittest import TestCase
from unittest.mock import patch

from recce.config import RecceConfig
from recce.util import SingletonMeta

test_root_path = os.path.dirname(os.path.abspath(__file__))


class RecceConfigTestCase(TestCase):
def setUp(self):
self.recce_config_path = os.path.join(test_root_path, 'data', 'config', 'recce.yml')
pass

def tearDown(self):
# Reset the SingletonMeta instances due to RecceConfig is a singleton
SingletonMeta._instances = {}

def test_load_recce_config(self):
config = RecceConfig(self.recce_config_path)

# Test data contains 2 checks
preset_checks = config.config.get('checks')
self.assertIsNotNone(preset_checks)
self.assertIsInstance(preset_checks, list)
self.assertEqual(len(preset_checks), 2)

@patch('recce.config.RecceConfig.save')
def test_recce_config_not_found(self, mock_save):
default_config = RecceConfig('NOT_EXISTING_FILE')
assert mock_save.called is True
# Default config should be generated
preset_checks = default_config.config.get('checks')
self.assertIsNotNone(default_config.config)
self.assertIsInstance(preset_checks, list)
self.assertEqual(len(preset_checks), 2)
Loading