Skip to content

Commit

Permalink
Fix: Add .dbt-checkpoint.yaml to disable hook execution tracking in m…
Browse files Browse the repository at this point in the history
…ixpanel (#139)

* add .dbt-checkpoint.yaml file

* update .pre-commit-config.yaml

* ignore my hook failures

* add valid_config to the list of input arguments

---------

Co-authored-by: Madhu <[email protected]>
  • Loading branch information
mbhoopathy and Madhu committed Jul 10, 2023
1 parent b0bdb75 commit c57c8dd
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 76 deletions.
2 changes: 2 additions & 0 deletions .dbt-checkpoint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version: 1
disable-tracking: true
71 changes: 36 additions & 35 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-docstring-first
- id: check-added-large-files
- id: debug-statements
- id: name-tests-test
args: ["--django"]
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
hooks:
- id: flake8
additional_dependencies:
- flake8-absolute-import
- flake8-black
- flake8-comprehensions
- repo: https://github.com/asottile/reorder_python_imports
rev: v2.3.6
hooks:
- id: reorder-python-imports
args: [--py3-plus]
- repo: https://github.com/asottile/setup-cfg-fmt
rev: v1.16.0
hooks:
- id: setup-cfg-fmt
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.790
hooks:
- id: mypy
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-docstring-first
- id: check-added-large-files
- id: debug-statements
- id: name-tests-test
args: ["--django"]
- repo: https://github.com/PyCQA/flake8
rev: 3.8.4
hooks:
- id: flake8
additional_dependencies:
- flake8-absolute-import
- flake8-black
- flake8-comprehensions
- repo: https://github.com/asottile/reorder_python_imports
rev: v2.3.6
hooks:
- id: reorder-python-imports
args: [--py3-plus]
- repo: https://github.com/asottile/setup-cfg-fmt
rev: v1.16.0
hooks:
- id: setup-cfg-fmt
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.940
hooks:
- id: mypy
additional_dependencies: ["types-PyYAML"]
29 changes: 15 additions & 14 deletions dbt_checkpoint/check_model_has_description.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import argparse
import os
import time
from typing import Any, Dict, Optional, Sequence
from typing import Any
from typing import Dict
from typing import Optional
from typing import Sequence

from dbt_checkpoint.tracking import dbtCheckpointTracking
from dbt_checkpoint.utils import (
JsonOpenError,
add_default_args,
get_dbt_manifest,
get_filenames,
get_missing_file_paths,
get_model_schemas,
get_model_sqls,
get_models,
red,
)
from dbt_checkpoint.utils import add_default_args
from dbt_checkpoint.utils import get_dbt_manifest
from dbt_checkpoint.utils import get_filenames
from dbt_checkpoint.utils import get_missing_file_paths
from dbt_checkpoint.utils import get_model_schemas
from dbt_checkpoint.utils import get_model_sqls
from dbt_checkpoint.utils import get_models
from dbt_checkpoint.utils import JsonOpenError
from dbt_checkpoint.utils import red


def has_description(
paths: Sequence[str], manifest: Dict[str, Any], exclude_pattern: str
) -> Dict[str, Any]:
paths = get_missing_file_paths(
paths = get_missing_file_paths( # type: ignore
paths, manifest, extensions=[".yml", ".yaml"], exclude_pattern=exclude_pattern
)

Expand Down Expand Up @@ -82,7 +83,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
},
)

return hook_properties.get("status_code")
return hook_properties.get("status_code") # type: ignore


if __name__ == "__main__":
Expand Down
22 changes: 15 additions & 7 deletions dbt_checkpoint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
import subprocess
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Dict, Generator, List, Optional, Sequence, Set, Text, Union
from typing import Any
from typing import Dict
from typing import Generator
from typing import List
from typing import Optional
from typing import Sequence
from typing import Set
from typing import Text
from typing import Union

from yaml import safe_load

Expand Down Expand Up @@ -557,11 +565,11 @@ def add_related_ymls(
paths_with_missing.add(yml_as_string)


def _discover_sql_files(node):
def _discover_sql_files(node): # type: ignore
return Path().glob(f"**/{node.get('original_file_path')}")


def _discover_prop_files(model_path):
def _discover_prop_files(model_path): # type: ignore
return Path().glob(f"**/{model_path}")


Expand All @@ -585,7 +593,7 @@ def get_missing_file_paths(
continue
if exclude_pattern:
exclude_re = re.compile(exclude_pattern)
paths_with_missing = [
paths_with_missing = [ # type: ignore
filename
for filename in paths_with_missing
if not exclude_re.search(filename)
Expand All @@ -604,12 +612,12 @@ def yellow(string: Optional[Any]) -> str:
def extend_dbt_project_dir_flag(
cmd: List[str], cmd_flags: List[str], dbt_project_dir: str = ""
) -> List[str]:
if dbt_project_dir and not "--project-dir" in cmd_flags:
if dbt_project_dir and not "--project-dir" in cmd_flags: # noqa
cmd.extend(["--project-dir", dbt_project_dir])
return cmd


def get_dbt_manifest(args):
def get_dbt_manifest(args): # type: ignore
"""
Get dbt manifest following the new config file approach. Precedence:
- custom `--manifest` flag
Expand All @@ -627,7 +635,7 @@ def get_dbt_manifest(args):
return get_json(manifest_path)


def get_dbt_catalog(args):
def get_dbt_catalog(args): # type: ignore
"""
Get dbt catalog following the new config file approach
"""
Expand Down
25 changes: 20 additions & 5 deletions tests/unit/test_check_model_columns_have_desc.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from unittest.mock import mock_open, patch
from unittest.mock import mock_open
from unittest.mock import patch

import pytest

from dbt_checkpoint.check_model_columns_have_desc import check_column_desc, main
from dbt_checkpoint.check_model_columns_have_desc import check_column_desc
from dbt_checkpoint.check_model_columns_have_desc import main

# Input args, valid manifest, expected return value
TESTS = (
Expand All @@ -21,6 +23,7 @@
]
},
True,
True,
0,
),
(
Expand All @@ -38,6 +41,7 @@
]
},
False,
True,
1,
),
(
Expand All @@ -55,6 +59,7 @@
]
},
True,
True,
1,
),
(
Expand All @@ -72,23 +77,33 @@
]
},
True,
True,
1,
),
)


@pytest.mark.parametrize(
("input_args", "schema", "valid_manifest", "expected_status_code"), TESTS
("input_args", "schema", "valid_manifest", "valid_config", "expected_status_code"),
TESTS,
)
def test_check_model_columns_have_desc(
input_args, schema, valid_manifest, expected_status_code, manifest_path_str
input_args,
schema,
valid_manifest,
valid_config,
expected_status_code,
manifest_path_str,
config_path_str,
):
if valid_manifest:
input_args.extend(["--manifest", manifest_path_str])
if valid_config:
input_args.extend(["--config", config_path_str])
with patch("builtins.open", mock_open(read_data="data")):
with patch("dbt_checkpoint.utils.safe_load") as mock_safe_load:
mock_safe_load.return_value = schema
status_code = main(input_args)
status_code = main(input_args)
assert status_code == expected_status_code


Expand Down
29 changes: 18 additions & 11 deletions tests/unit/test_check_model_has_description.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from unittest.mock import mock_open, patch
from unittest.mock import mock_open
from unittest.mock import patch

import pytest

Expand All @@ -10,41 +11,47 @@
["aa/bb/with_description.sql"],
{"models": [{"name": "with_description", "description": "test description"}]},
True,
True,
0,
),
(
["aa/bb/with_description.sql"],
{"models": [{"name": "with_description", "description": "test description"}]},
False,
True,
1,
),
(
["aa/bb/without_description.sql"],
{
"models": [
{
"name": "without_description",
}
]
},
{"models": [{"name": "without_description"}]},
True,
False,
1,
),
)


@pytest.mark.parametrize(
("input_args", "schema", "valid_manifest", "expected_status_code"), TESTS
("input_args", "schema", "valid_manifest", "valid_config", "expected_status_code"),
TESTS,
)
def test_check_model_description(
input_args, schema, valid_manifest, expected_status_code, manifest_path_str
input_args,
schema,
valid_manifest,
valid_config,
expected_status_code,
manifest_path_str,
config_path_str,
):
if valid_manifest:
input_args.extend(["--manifest", manifest_path_str])
if valid_config:
input_args.extend(["--config", config_path_str])
with patch("builtins.open", mock_open(read_data="data")):
with patch("dbt_checkpoint.utils.safe_load") as mock_safe_load:
mock_safe_load.return_value = schema
status_code = main(input_args)
status_code = main(input_args)
assert status_code == expected_status_code


Expand Down

0 comments on commit c57c8dd

Please sign in to comment.