Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arzavj committed Apr 11, 2021
1 parent 62e53a6 commit 940862c
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 1 deletion.
4 changes: 4 additions & 0 deletions test/integration/065_postgres_index_tests/data/seed.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
country_code,country_name
US,United States
CA,Canada
GB,United Kingdom
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{
config(
materialized = "table",
indexes=[
{'columns': 'column_a, column_b'},
]
)
}}

select 1 as column_a, 2 as column_b
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{
config(
materialized = "table",
indexes=[
{'columns': ['column_a'], 'type': 'non_existent_type'},
]
)
}}

select 1 as column_a, 2 as column_b
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{
config(
materialized = "table",
indexes=[
{'columns': ['column_a'], 'unique': 'yes'},
]
)
}}

select 1 as column_a, 2 as column_b
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{
config(
materialized = "table",
indexes=[
{'unique': True},
]
)
}}

select 1 as column_a, 2 as column_b
18 changes: 18 additions & 0 deletions test/integration/065_postgres_index_tests/models/incremental.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{
config(
materialized = "incremental",
indexes=[
{'columns': ['column_a'], 'type': 'hash'},
{'columns': ['column_a', 'column_b'], 'unique': True},
]
)
}}

select *
from (
select 1 as column_a, 2 as column_b
) t

{% if is_incremental() %}
where column_a > (select max(column_a) from {{this}})
{% endif %}
29 changes: 29 additions & 0 deletions test/integration/065_postgres_index_tests/snapshots/colors.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{% snapshot colors %}

{{
config(
target_database=database,
target_schema=schema,
unique_key='id',
strategy='check',
check_cols=['color'],
indexes=[
{'columns': ['id'], 'type': 'hash'},
{'columns': ['id', 'color'], 'unique': True},
]
)
}}

{% if var('version') == 1 %}

select 1 as id, 'red' as color union all
select 2 as id, 'green' as color

{% else %}

select 1 as id, 'blue' as color union all
select 2 as id, 'green' as color

{% endif %}

{% endsnapshot %}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,25 @@ def schema(self):
def models(self):
return "models"

@property
def project_config(self):
return {
'config-version': 2,
'seeds': {
'quote_columns': False,
'indexes': [
{'columns': ['country_code'], 'unique': False, 'type': 'hash'},
{'columns': ['country_code', 'country_name'], 'unique': True},
],
},
'vars': {
'version': 1
},
}

@use_profile('postgres')
def test__postgres__table(self):
results = self.run_dbt()
results = self.run_dbt(['run', '--models', 'table'])
self.assertEqual(len(results), 1)

indexes = self.get_indexes('table')
Expand All @@ -31,6 +47,51 @@ def test__postgres__table(self):
]
)

@use_profile('postgres')
def test__postgres__incremental(self):
for additional_argument in [[], [], ['--full-refresh']]:
results = self.run_dbt(['run', '--models', 'incremental'] + additional_argument)
self.assertEqual(len(results), 1)

indexes = self.get_indexes('incremental')
self.assertCountEqual(
indexes,
[
{'columns': 'column_a', 'unique': False, 'type': 'hash'},
{'columns': 'column_a, column_b', 'unique': True, 'type': 'btree'},
]
)

@use_profile('postgres')
def test__postgres__seed(self):
for additional_argument in [[], [], ['--full-refresh']]:
results = self.run_dbt(["seed"] + additional_argument)
self.assertEqual(len(results), 1)

indexes = self.get_indexes('seed')
self.assertCountEqual(
indexes,
[
{'columns': 'country_code', 'unique': False, 'type': 'hash'},
{'columns': 'country_code, country_name', 'unique': True, 'type': 'btree'},
]
)

@use_profile('postgres')
def test__postgres__snapshot(self):
for version in [1, 2]:
results = self.run_dbt(["snapshot", '--vars', 'version: {}'.format(version)])
self.assertEqual(len(results), 1)

indexes = self.get_indexes('colors')
self.assertCountEqual(
indexes,
[
{'columns': 'id', 'unique': False, 'type': 'hash'},
{'columns': 'id, color', 'unique': True, 'type': 'btree'},
]
)

def get_indexes(self, table_name):
sql = """
SELECT
Expand All @@ -53,3 +114,21 @@ def parse_index_definition(self, index_definition):
is_unique = 'unique' in index_definition
m = INDEX_DEFINITION_PATTERN.search(index_definition)
return {'columns': m.group(2), 'unique': is_unique, 'type': m.group(1)}

class TestPostgresInvalidIndex(DBTIntegrationTest):
@property
def schema(self):
return "postgres_index_065"

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

@use_profile('postgres')
def test__postgres__invalid_index_configs(self):
results, output = self.run_dbt_and_capture(expect_pass=False)
self.assertEqual(len(results), 4)
self.assertRegex(output, r'columns.*is not of type \'array\'')
self.assertRegex(output, r'unique.*is not of type \'boolean\'')
self.assertRegex(output, r'\'columns\' is a required property')
self.assertRegex(output, r'Database Error in model invalid_type')

0 comments on commit 940862c

Please sign in to comment.