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

Change from unittest to pytest for manifestLoader #9960

Merged
merged 5 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20240416-154405.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Move ManifestLoader test and change it to pytest
time: 2024-04-16T15:44:05.659973-07:00
custom:
Author: ChenyuLInx
Issue: "9960"
93 changes: 93 additions & 0 deletions tests/unit/parser/test_manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import pytest
from unittest.mock import patch, MagicMock
from argparse import Namespace


from dbt.contracts.graph.manifest import Manifest
from dbt.parser.manifest import ManifestLoader
from dbt.config import RuntimeConfig
from dbt.flags import set_from_args


@pytest.fixture
def mock_project():
mock_project = MagicMock(RuntimeConfig)
mock_project.cli_vars = ""
mock_project.args = MagicMock()
mock_project.args.profile = "test"
mock_project.args.target = "test"
mock_project.project_env_vars = {}
mock_project.profile_env_vars = {}
mock_project.project_target_path = "mock_target_path"
mock_project.credentials = MagicMock()
return mock_project


class TestPartialParse:
@patch("dbt.parser.manifest.ManifestLoader.build_manifest_state_check")
@patch("dbt.parser.manifest.os.path.exists")
@patch("dbt.parser.manifest.open")
def test_partial_parse_file_path(self, patched_open, patched_os_exist, patched_state_check):
mock_project = MagicMock(RuntimeConfig)
mock_project.project_target_path = "mock_target_path"
patched_os_exist.return_value = True
set_from_args(Namespace(), {})
ManifestLoader(mock_project, {})
# by default we use the project_target_path
patched_open.assert_called_with("mock_target_path/partial_parse.msgpack", "rb")
set_from_args(Namespace(partial_parse_file_path="specified_partial_parse_path"), {})
ManifestLoader(mock_project, {})
# if specified in flags, we use the specified path
patched_open.assert_called_with("specified_partial_parse_path", "rb")

def test_profile_hash_change(self, mock_project):
# This test validate that the profile_hash is updated when the connection keys change
profile_hash = "750bc99c1d64ca518536ead26b28465a224be5ffc918bf2a490102faa5a1bcf5"
mock_project.credentials.connection_info.return_value = "test"
set_from_args(Namespace(), {})
manifest = ManifestLoader(mock_project, {})
assert manifest.manifest.state_check.profile_hash.checksum == profile_hash
mock_project.credentials.connection_info.return_value = "test1"
manifest = ManifestLoader(mock_project, {})
assert manifest.manifest.state_check.profile_hash.checksum != profile_hash


class TestFailedPartialParse:
@patch("dbt.tracking.track_partial_parser")
@patch("dbt.tracking.active_user")
@patch("dbt.parser.manifest.PartialParsing")
@patch("dbt.parser.manifest.ManifestLoader.read_manifest_for_partial_parse")
@patch("dbt.parser.manifest.ManifestLoader.build_manifest_state_check")
def test_partial_parse_safe_update_project_parser_files_partially(
self,
patched_state_check,
patched_read_manifest_for_partial_parse,
patched_partial_parsing,
patched_active_user,
patched_track_partial_parser,
):
mock_instance = MagicMock()
mock_instance.skip_parsing.return_value = False
mock_instance.get_parsing_files.side_effect = KeyError("Whoopsie!")
patched_partial_parsing.return_value = mock_instance

mock_project = MagicMock(RuntimeConfig)
mock_project.project_target_path = "mock_target_path"

mock_saved_manifest = MagicMock(Manifest)
mock_saved_manifest.files = {}
patched_read_manifest_for_partial_parse.return_value = mock_saved_manifest

set_from_args(Namespace(), {})
loader = ManifestLoader(mock_project, {})
loader.safe_update_project_parser_files_partially({})

patched_track_partial_parser.assert_called_once()
exc_info = patched_track_partial_parser.call_args[0][0]
assert "traceback" in exc_info
assert "exception" in exc_info
assert "code" in exc_info
assert "location" in exc_info
assert "full_reparse_reason" in exc_info
assert "KeyError: 'Whoopsie!'" == exc_info["exception"]
assert isinstance(exc_info["code"], str) or isinstance(exc_info["code"], type(None))
189 changes: 0 additions & 189 deletions tests/unit/test_parse_manifest.py

This file was deleted.