Skip to content

Commit

Permalink
Small fixes. New + updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jtcohen6 committed May 8, 2021
1 parent feb93ef commit 33a607c
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
{% if flags.STORE_FAILURES %}

{% set identifier = model['name'] %}
{% set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) %}
{% set target_relation = api.Relation.create(
identifier=identifier, schema=schema, database=database, type='table') -%} %}

{% do adapter.drop_relation(target_relation) %}
{{ log(old_relation, info = true) }}
{{ log(database + '.' + schema + '.' + identifier, info = true) }}

{% if old_relation %}
{% do adapter.drop_relation(old_relation) %}
{% endif %}

{% call statement(auto_begin=True) %}
{{ create_table_as(False, target_relation, sql) }}
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/task/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def print_run_result_error(
logger.info("")
msg = f"select * from {result.node.relation_name}"
border = '-' * len(msg)
logger.info(f" See all failures:\n {border}\n {msg}\n {border}")
logger.info(f" See test failures:\n {border}\n {msg}\n {border}")

elif result.message is not None:
first = True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ def rendered_tst_config(self, **updates):
'severity': 'ERROR',
'full_refresh': None,
'database': None,
'schema': None,
'schema': 'dbt_test__audit',
'alias': None,
}
result.update(updates)
Expand Down Expand Up @@ -1065,6 +1065,7 @@ def expected_seeded_manifest(self, model_database=None, quote_model=False):
snapshot_path = self.dir(os.path.join('snapshot', 'snapshot_seed.sql'))

my_schema_name = self.unique_schema()
test_audit_schema = my_schema_name + '_dbt_test__audit'

if model_database is None:
model_database = self.alternative_database
Expand Down Expand Up @@ -1357,7 +1358,7 @@ def expected_seeded_manifest(self, model_database=None, quote_model=False):
'relation_name': None,
'resource_type': 'test',
'root_path': self.test_root_realpath,
'schema': my_schema_name,
'schema': test_audit_schema,
'database': self.default_database,
'tags': ['schema'],
'meta': {},
Expand Down Expand Up @@ -1444,7 +1445,7 @@ def expected_seeded_manifest(self, model_database=None, quote_model=False):
'relation_name': None,
'resource_type': 'test',
'root_path': self.test_root_realpath,
'schema': my_schema_name,
'schema': test_audit_schema,
'database': self.default_database,
'tags': ['schema'],
'meta': {},
Expand Down Expand Up @@ -1488,7 +1489,7 @@ def expected_seeded_manifest(self, model_database=None, quote_model=False):
'relation_name': None,
'resource_type': 'test',
'root_path': self.test_root_realpath,
'schema': my_schema_name,
'schema': test_audit_schema,
'database': self.default_database,
'tags': ['schema'],
'meta': {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class TestSelectionExpansion(DBTIntegrationTest):
@property
def schema(self):
return "test_selection_expansion_065"
return "test_selection_expansion_066"

@property
def models(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
id,first_name,last_name,email,gender,ip_address
1,Jack,Hunter,jhunter0@pbs.org,Male,59.80.20.168
2,Kathryn,Walker,kwalker1@ezinearticles.com,Female,194.121.179.35
3,Gerald,Ryan,gryan2@com.com,Male,11.3.212.243
4,Bonnie,Spencer,bspencer3@ameblo.jp,Female,216.32.196.175
5,Harold,Taylor,htaylor4@people.com.cn,Male,253.10.246.136
6,Jacqueline,Griffin,jgriffin5@t.co,Female,16.13.192.220
7,Wanda,Arnold,warnold6@google.nl,Female,232.116.150.64
8,Craig,Ortiz,cortiz7@sciencedaily.com,Male,199.126.106.13
9,Gary,Day,gday8@nih.gov,Male,35.81.68.186
10,Rose,Wright,rwright9@yahoo.co.jp,Female,236.82.178.100
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,first_name,last_name,email,gender,ip_address
,Gerald,Ryan,gryan2@com.com,Male,11.3.212.243
,Bonnie,Spencer,bspencer3@ameblo.jp,Female,216.32.196.175
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
value,num
2,2
1,2
11 changes: 11 additions & 0 deletions test/integration/067_store_test_failures_tests/data/people.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
id,first_name,last_name,email,gender,ip_address
1,Jack,Hunter,jhunter0@pbs.org,Male,59.80.20.168
2,Kathryn,Walker,kwalker1@ezinearticles.com,Female,194.121.179.35
3,Gerald,Ryan,gryan2@com.com,Male,11.3.212.243
4,Bonnie,Spencer,bspencer3@ameblo.jp,Female,216.32.196.175
5,Harold,Taylor,htaylor4@people.com.cn,Male,253.10.246.136
6,Jacqueline,Griffin,jgriffin5@t.co,Female,16.13.192.220
7,Wanda,Arnold,warnold6@google.nl,Female,232.116.150.64
8,Craig,Ortiz,cortiz7@sciencedaily.com,Male,199.126.106.13
9,Gary,Day,gday8@nih.gov,Male,35.81.68.186
10,Rose,Wright,rwright9@yahoo.co.jp,Female,236.82.178.100
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select * from {{ ref('people') }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
select * from {{ ref('people') }}

union all

select * from {{ ref('people') }}
where id in (1,2)

union all

select null as id, first_name, last_name, email, gender, ip_address from {{ ref('people') }}
where id in (3,4)
17 changes: 17 additions & 0 deletions test/integration/067_store_test_failures_tests/models/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: 2

models:

- name: fine_model
columns:
- name: id
tests:
- unique
- not_null

- name: problematic_model
columns:
- name: id
tests:
- unique
- not_null
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from test.integration.base import DBTIntegrationTest, FakeArgs, use_profile

from dbt.task.test import TestTask
from dbt.task.list import ListTask


class TestStoreTestFailures(DBTIntegrationTest):
@property
def schema(self):
return "test_store_test_failures_067"

@property
def models(self):
return "models"

@property
def project_config(self):
return {
"config-version": 2,
"test-paths": ["tests"],
"seeds": {
"quote_columns": False,
"test": {
"expected": self.column_type_overrides()
},
},
}

def column_type_overrides(self):
return {}

def run_tests_store_failures_and_assert(self):
test_audit_schema = self.unique_schema() + "_dbt_test__audit"

self.run_dbt(["seed"])
self.run_dbt(["run"])
# make sure this works idempotently
self.run_dbt(["test", "--store-failures"], expect_pass=False)
results = self.run_dbt(["test", "--store-failures"], expect_pass=False)

# compare test results
actual = [(r.status, r.message) for r in results]
expected = [('pass', 0), ('fail', 10), ('pass', 0), ('fail', 2), ('pass', 0), ('fail', 2)]
self.assertEqual(sorted(actual), sorted(expected))

# compare test results stored in database
self.assertTablesEqual("failing_test", "expected_failing_test", test_audit_schema)
self.assertTablesEqual("not_null_problematic_model_id", "expected_not_null_problematic_model_id", test_audit_schema)
self.assertTablesEqual("unique_problematic_model_id", "expected_unique_problematic_model_id", test_audit_schema)

class PostgresTestStoreTestFailures(TestStoreTestFailures):
@property
def schema(self):
return "067" # otherwise too long + truncated

def column_type_overrides(self):
return {
"expected_unique_problematic_model_id": {
"+column_types": {
"num": "bigint",
},
},
}

@use_profile('postgres')
def test__postgres__store_and_assert(self):
self.run_tests_store_failures_and_assert()

class RedshiftTestStoreTestFailures(TestStoreTestFailures):
def column_type_overrides(self):
return {
"expected_not_null_problematic_model_id": {
"+column_types": {
"email": "varchar(26)",
"first_name": "varchar(10)",
},
},
"expected_unique_problematic_model_id": {
"+column_types": {
"num": "bigint",
},
},
}

@use_profile('redshift')
def test__redshift__store_and_assert(self):
self.run_tests_store_failures_and_assert()

class SnowflakeTestStoreTestFailures(TestStoreTestFailures):
@use_profile('snowflake')
def test__snowflake__store_and_assert(self):
self.run_tests_store_failures_and_assert()

class BigQueryTestStoreTestFailures(TestStoreTestFailures):
@use_profile('bigquery')
def test__bigquery__store_and_assert(self):
self.run_tests_store_failures_and_assert()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select * from {{ ref('fine_model') }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select * from {{ ref('fine_model') }}
where false
12 changes: 7 additions & 5 deletions test/unit/test_contracts_graph_compiled.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def minimal_schema_test_dict():
'unique_id': 'model.test.foo',
'fqn': ['test', 'models', 'foo'],
'database': 'test_db',
'schema': 'test_schema',
'schema': 'dbt_test__audit',
'alias': 'bar',
'test_metadata': {
'name': 'foo',
Expand Down Expand Up @@ -347,7 +347,7 @@ def basic_uncompiled_schema_test_node():
depends_on=DependsOn(),
description='',
database='test_db',
schema='test_schema',
schema='dbt_test__audit',
alias='bar',
tags=[],
config=TestConfig(),
Expand Down Expand Up @@ -379,7 +379,7 @@ def basic_compiled_schema_test_node():
deferred=False,
description='',
database='test_db',
schema='test_schema',
schema='dbt_test__audit',
alias='bar',
tags=[],
config=TestConfig(severity='warn'),
Expand Down Expand Up @@ -414,7 +414,7 @@ def basic_uncompiled_schema_test_dict():
'depends_on': {'macros': [], 'nodes': []},
'database': 'test_db',
'description': '',
'schema': 'test_schema',
'schema': 'dbt_test__audit',
'alias': 'bar',
'tags': [],
'config': {
Expand All @@ -428,6 +428,7 @@ def basic_uncompiled_schema_test_dict():
'tags': [],
'vars': {},
'severity': 'ERROR',
'schema': 'dbt_test__audit',
},
'deferred': False,
'docs': {'show': True},
Expand Down Expand Up @@ -463,7 +464,7 @@ def basic_compiled_schema_test_dict():
'deferred': False,
'database': 'test_db',
'description': '',
'schema': 'test_schema',
'schema': 'dbt_test__audit',
'alias': 'bar',
'tags': [],
'config': {
Expand All @@ -477,6 +478,7 @@ def basic_compiled_schema_test_dict():
'tags': [],
'vars': {},
'severity': 'warn',
'schema': 'dbt_test__audit',
},

'docs': {'show': True},
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_contracts_graph_parsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ def basic_parsed_schema_test_object():
depends_on=DependsOn(),
description='',
database='test_db',
schema='test_schema',
schema='test_schema_dbt_test__audit',
alias='bar',
tags=[],
meta={},
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ def test_basic(self):
alias='test_1',
name='test_1',
database='test',
schema='analytics',
schema='dbt_test__audit',
resource_type=NodeType.Test,
unique_id='test.snowplow.test_1',
fqn=['snowplow', 'data_test', 'test_1'],
Expand Down

0 comments on commit 33a607c

Please sign in to comment.