Skip to content

Commit

Permalink
Fixes #7551: Create add_from_artifact to populate state_relation
Browse files Browse the repository at this point in the history
…field of nodes
  • Loading branch information
aranke committed Jun 6, 2023
1 parent d0ddb96 commit f1f3f45
Showing 1 changed file with 48 additions and 20 deletions.
68 changes: 48 additions & 20 deletions test/unit/test_manifest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os
import unittest
from unittest import mock

from argparse import Namespace
import copy
from collections import namedtuple
from itertools import product
from datetime import datetime
from itertools import product
from unittest import mock
from copy import deepcopy

import freezegun
import pytest

import dbt.flags
Expand All @@ -27,22 +27,17 @@
Group,
RefArgs,
)

from dbt.contracts.graph.unparsed import (
ExposureType,
Owner,
MaturityType,
MetricFilter,
MetricTime,
)

from dbt.events.functions import reset_metadata_vars
from dbt.exceptions import AmbiguousResourceNameRefError
from dbt.flags import set_from_args

from dbt.node_types import NodeType
import freezegun

from .utils import (
MockMacro,
MockDocumentation,
Expand All @@ -53,7 +48,6 @@
inject_plugin,
)


REQUIRED_PARSED_NODE_KEYS = frozenset(
{
"alias",
Expand Down Expand Up @@ -103,7 +97,6 @@
| {"compiled", "extra_ctes_injected", "extra_ctes", "compiled_code", "relation_name"}
)


ENV_KEY_NAME = "KEY" if os.name == "nt" else "key"


Expand Down Expand Up @@ -408,7 +401,7 @@ def test__no_nodes(self):

@freezegun.freeze_time("2018-02-14T09:15:13Z")
def test__nested_nodes(self):
nodes = copy.copy(self.nested_nodes)
nodes = deepcopy(self.nested_nodes)
manifest = Manifest(
nodes=nodes,
sources={},
Expand Down Expand Up @@ -463,11 +456,11 @@ def test__nested_nodes(self):
self.assertEqual(child_map["model.snowplow.events"], [])

def test__build_flat_graph(self):
exposures = copy.copy(self.exposures)
metrics = copy.copy(self.metrics)
groups = copy.copy(self.groups)
nodes = copy.copy(self.nested_nodes)
sources = copy.copy(self.sources)
exposures = deepcopy(self.exposures)
metrics = deepcopy(self.metrics)
groups = deepcopy(self.groups)
nodes = deepcopy(self.nested_nodes)
sources = deepcopy(self.sources)
manifest = Manifest(
nodes=nodes,
sources=sources,
Expand Down Expand Up @@ -588,7 +581,7 @@ def test_get_resource_fqns_empty(self):
self.assertEqual(manifest.get_resource_fqns(), {})

def test_get_resource_fqns(self):
nodes = copy.copy(self.nested_nodes)
nodes = deepcopy(self.nested_nodes)
nodes["seed.root.seed"] = SeedNode(
name="seed",
database="dbt",
Expand Down Expand Up @@ -663,6 +656,41 @@ def test__deepcopy_copies_flat_graph(self):
copy = original.deepcopy()
self.assertEqual(original.flat_graph, copy.flat_graph)

def test__add_from_artifact(self):
original_nodes = deepcopy(self.nested_nodes)
other_nodes = deepcopy(self.nested_nodes)

nested2 = other_nodes.pop("model.root.nested")
nested2.name = "nested2"
nested2.alias = "nested2"
nested2.fqn = ["root", "nested2"]

other_nodes["model.root.nested2"] = nested2

for k, v in other_nodes.items():
v.database = "other_" + v.database
v.schema = "other_" + v.schema
v.alias = "other_" + v.alias

other_nodes[k] = v

original_manifest = Manifest(nodes=original_nodes)
other_manifest = Manifest(nodes=other_nodes)
original_manifest.add_from_artifact(other_manifest.writable_manifest())

# new node added should not be in original manifest
assert "model.root.nested2" not in original_manifest.nodes

# old node removed should not have state relation in original manifest
assert original_manifest.nodes["model.root.nested"].state_relation is None

# for all other nodes, check that state relation is updated
for k, v in original_manifest.nodes.items():
if k not in ["model.root.nested", "model.root.nested2"]:
self.assertEqual("other_" + v.database, v.state_relation.database)
self.assertEqual("other_" + v.schema, v.state_relation.schema)
self.assertEqual("other_" + v.alias, v.state_relation.alias)


class MixedManifestTest(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -912,7 +940,7 @@ def test__no_nodes(self):

@freezegun.freeze_time("2018-02-14T09:15:13Z")
def test__nested_nodes(self):
nodes = copy.copy(self.nested_nodes)
nodes = deepcopy(self.nested_nodes)
manifest = Manifest(
nodes=nodes,
sources={},
Expand Down Expand Up @@ -964,7 +992,7 @@ def test__nested_nodes(self):
self.assertEqual(child_map["model.snowplow.events"], [])

def test__build_flat_graph(self):
nodes = copy.copy(self.nested_nodes)
nodes = deepcopy(self.nested_nodes)
manifest = Manifest(
nodes=nodes,
sources={},
Expand Down

0 comments on commit f1f3f45

Please sign in to comment.