-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
add a simple set of graph unit tests for models only #270
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
08842e5
add a simple set of graph unit tests for models only
3579cc9
fix tests
efb204f
this is called the graph, not dependencies
a76b5b9
thanks drew!
072ae94
fix tests
233182f
use six for py2/3 compat
1278434
windows :(
da82757
i think this will actually fix windows
ca85dc3
restore module functions after mocking them in setUp
8a53b58
Merge branch 'development' into graph-unit-tests
drewbanin 20701ba
add materialization unit tests for the graph
drewbanin 9997afb
test topological sort + depenedency list
drewbanin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM python:3.5 | ||
FROM python:3.6 | ||
|
||
RUN apt-get update | ||
|
||
|
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
Empty file.
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,53 @@ | ||
import fnmatch | ||
import os | ||
import os.path | ||
|
||
|
||
def find_matching(root_path, | ||
relative_paths_to_search, | ||
file_pattern): | ||
""" | ||
Given an absolute `root_path`, a list of relative paths to that | ||
absolute root path (`relative_paths_to_search`), and a `file_pattern` | ||
like '*.sql', returns information about the files. For example: | ||
|
||
> find_matching('/root/path', 'models', '*.sql') | ||
|
||
[ { 'absolute_path': '/root/path/models/model_one.sql', | ||
'relative_path': 'models/model_one.sql', | ||
'searched_path': 'models' }, | ||
{ 'absolute_path': '/root/path/models/subdirectory/model_two.sql', | ||
'relative_path': 'models/subdirectory/model_two.sql', | ||
'searched_path': 'models' } ] | ||
""" | ||
matching = [] | ||
|
||
for relative_path_to_search in relative_paths_to_search: | ||
absolute_path_to_search = os.path.join( | ||
root_path, relative_path_to_search) | ||
walk_results = os.walk(absolute_path_to_search) | ||
|
||
for current_path, subdirectories, local_files in walk_results: | ||
for local_file in local_files: | ||
absolute_path = os.path.join(current_path, local_file) | ||
relative_path = os.path.relpath( | ||
absolute_path, absolute_path_to_search) | ||
|
||
if fnmatch.fnmatch(local_file, file_pattern): | ||
matching.append({ | ||
'searched_path': relative_path_to_search, | ||
'absolute_path': absolute_path, | ||
'relative_path': relative_path, | ||
}) | ||
|
||
return matching | ||
|
||
|
||
def load_file_contents(path, strip=True): | ||
with open(path, 'rb') as handle: | ||
to_return = handle.read().decode('utf-8') | ||
|
||
if strip: | ||
to_return = to_return.strip() | ||
|
||
return to_return |
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 |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
from dbt.model import Model, Analysis, TestModel, SchemaFile, Csv, Macro, \ | ||
ArchiveModel, DataTest | ||
|
||
import dbt.clients.system | ||
|
||
|
||
class Source(object): | ||
def __init__(self, project, own_project=None): | ||
|
@@ -15,72 +17,91 @@ def __init__(self, project, own_project=None): | |
self.own_project_root = self.own_project['project-root'] | ||
self.own_project_name = self.own_project['name'] | ||
|
||
def find(self, source_paths, file_pattern): | ||
"""returns abspath, relpath, filename of files matching file_regex in | ||
source_paths""" | ||
found = [] | ||
|
||
if type(source_paths) not in (list, tuple): | ||
source_paths = [source_paths] | ||
|
||
for source_path in source_paths: | ||
root_path = os.path.join(self.own_project_root, source_path) | ||
for root, dirs, files in os.walk(root_path): | ||
for filename in files: | ||
abs_path = os.path.join(root, filename) | ||
rel_path = os.path.relpath(abs_path, root_path) | ||
|
||
if fnmatch.fnmatch(filename, file_pattern): | ||
found.append( | ||
(self.project, | ||
source_path, | ||
rel_path, | ||
self.own_project) | ||
) | ||
return found | ||
def build_models_from_file_matches( | ||
self, | ||
to_build, | ||
file_matches, | ||
extra_args=[]): | ||
|
||
build_args = [[self.project, | ||
file_match.get('searched_path'), | ||
file_match.get('relative_path'), | ||
self.own_project] + extra_args | ||
for file_match in file_matches] | ||
|
||
return [to_build(*args) for args in build_args] | ||
|
||
def get_models(self, model_dirs, create_template): | ||
pattern = "[!.#~]*.sql" | ||
models = [Model(*model + (create_template,)) | ||
for model in self.find(model_dirs, pattern)] | ||
return models | ||
file_matches = dbt.clients.system.find_matching( | ||
self.own_project_root, | ||
model_dirs, | ||
"[!.#~]*.sql") | ||
|
||
return self.build_models_from_file_matches( | ||
Model, | ||
file_matches, | ||
[create_template]) | ||
|
||
def get_test_models(self, model_dirs, create_template): | ||
pattern = "[!.#~]*.sql" | ||
models = [TestModel(*model + (create_template,)) | ||
for model in self.find(model_dirs, pattern)] | ||
return models | ||
file_matches = dbt.clients.system.find_matching( | ||
self.own_project_root, | ||
model_dirs, | ||
"[!.#~]*.sql") | ||
|
||
return self.build_models_from_file_matches( | ||
TestModel, | ||
file_matches, | ||
[create_template]) | ||
|
||
def get_analyses(self, analysis_dirs): | ||
pattern = "[!.#~]*.sql" | ||
models = [Analysis(*analysis) | ||
for analysis in self.find(analysis_dirs, pattern)] | ||
return models | ||
|
||
def get_schemas(self, model_dirs): | ||
"Get schema.yml files" | ||
pattern = "[!.#~]*.yml" | ||
schemas = [SchemaFile(*schema) | ||
for schema in self.find(model_dirs, pattern)] | ||
return schemas | ||
file_matches = dbt.clients.system.find_matching( | ||
self.own_project_root, | ||
analysis_dirs, | ||
"[!.#~]*.sql") | ||
|
||
return self.build_models_from_file_matches( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is so good There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think we can clean it up even more if we drop the extreme OOP-iness in |
||
Analysis, | ||
file_matches) | ||
|
||
def get_schemas(self, schema_dirs): | ||
file_matches = dbt.clients.system.find_matching( | ||
self.own_project_root, | ||
schema_dirs, | ||
"[!.#~]*.yml") | ||
|
||
return self.build_models_from_file_matches( | ||
SchemaFile, | ||
file_matches) | ||
|
||
def get_tests(self, test_dirs): | ||
"Get custom test files" | ||
pattern = "[!.#~]*.sql" | ||
tests = [DataTest(*test) for test in self.find(test_dirs, pattern)] | ||
return tests | ||
file_matches = dbt.clients.system.find_matching( | ||
self.own_project_root, | ||
test_dirs, | ||
"[!.#~]*.sql") | ||
|
||
return self.build_models_from_file_matches( | ||
DataTest, | ||
file_matches) | ||
|
||
def get_csvs(self, csv_dirs): | ||
"Get CSV files" | ||
pattern = "[!.#~]*.csv" | ||
csvs = [Csv(*csv) for csv in self.find(csv_dirs, pattern)] | ||
return csvs | ||
file_matches = dbt.clients.system.find_matching( | ||
self.own_project_root, | ||
csv_dirs, | ||
"[!.#~]*.csv") | ||
|
||
return self.build_models_from_file_matches( | ||
Csv, | ||
file_matches) | ||
|
||
def get_macros(self, macro_dirs): | ||
"Get Macro files" | ||
pattern = "[!.#~]*.sql" | ||
macros = [Macro(*macro) for macro in self.find(macro_dirs, pattern)] | ||
return macros | ||
file_matches = dbt.clients.system.find_matching( | ||
self.own_project_root, | ||
macro_dirs, | ||
"[!.#~]*.sql") | ||
|
||
return self.build_models_from_file_matches( | ||
Macro, | ||
file_matches) | ||
|
||
def get_archives(self, create_template): | ||
"Get Archive models defined in project config" | ||
|
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
nose>=1.3.7 | ||
nosy>=1.1.2 | ||
mock>=1.3.0 | ||
pep8>=1.6.2 | ||
bumpversion==0.5.3 | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍