-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add rule to find mart references to source #6
- Loading branch information
Showing
4 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Staging models should have a single source. | ||
Copyright (C) 2020, Auto Trader UK | ||
Created 22. Dec 2020 08:40 | ||
""" | ||
|
||
from typing import List, Tuple | ||
|
||
from olivertwist.manifest import Manifest, Node | ||
from olivertwist.ruleengine.rule import rule | ||
from olivertwist.rules.utils import partition | ||
|
||
|
||
@rule(id="no-references-to-source-from-marts", name="No references to source from marts") | ||
def no_references_to_source_from_marts( | ||
manifest: Manifest, | ||
) -> Tuple[List[Node], List[Node]]: | ||
def mart_depends_on_source(node: Node): | ||
source_refs = [ | ||
p | ||
for p in manifest.graph.predecessors(node.id) | ||
if manifest.get_node(p).is_source | ||
] | ||
return node.is_mart and len(list(source_refs)) > 0 | ||
|
||
passes, failures = partition( | ||
mart_depends_on_source, manifest.nodes() | ||
) | ||
return list(passes), list(failures) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import pytest | ||
|
||
from olivertwist.manifest import Manifest | ||
from olivertwist.rules.no_references_to_source_from_marts import ( | ||
no_references_to_source_from_marts, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def manifest() -> Manifest: | ||
return Manifest( | ||
{ | ||
"nodes": { | ||
"source_1": { | ||
"unique_id": "source_1", | ||
"resource_type": "source", | ||
"fqn": ["foo", "bar"], | ||
}, | ||
"staging_1": { | ||
"unique_id": "staging_1", | ||
"resource_type": "model", | ||
"fqn": ["foo", "staging", "bar"], | ||
}, | ||
"mart_1": { | ||
"unique_id": "mart_1", | ||
"resource_type": "model", | ||
"fqn": ["foo", "marts", "bar"], | ||
}, | ||
}, | ||
"child_map": { | ||
"source_1": ["mart_1"], | ||
"staging_1": [], | ||
"mart_1": [], | ||
}, | ||
"disabled": [], | ||
"sources": { | ||
"source_1": {} | ||
}, | ||
} | ||
|
||
) | ||
|
||
|
||
def test_no_references_to_source_from_marts(manifest): | ||
passes, failures = no_references_to_source_from_marts(manifest) | ||
|
||
pass_ids = [p.id for p in passes] | ||
failure_ids = [f.id for f in failures] | ||
|
||
assert pass_ids == ["source_1", "staging_1"] | ||
assert failure_ids == ["mart_1"] |