Skip to content
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

Convert docs_generate_tests to new framework #5058

Merged
merged 8 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/dbt/tests/fixtures/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ def dbt_profile_target():
}


@pytest.fixture(scope="class")
def profile_user(dbt_profile_target):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which adapter need this change and why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a way to get the "owner" in the catalog. It's different for different adapters. Some of them use the "user" in the profile, some use other things. So this is special fixture to help with that. We didn't have a standard way to get the profile's user before.

return dbt_profile_target["user"]


# This fixture can be overridden in a project. The data provided in this
# fixture will be merged into the default project dictionary via a python 'update'.
@pytest.fixture(scope="class")
Expand Down
50 changes: 49 additions & 1 deletion core/dbt/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@
# get_relation_columns
# update_rows
# generate_update_clause

#
# Classes for comparing fields in dictionaries
# AnyFloat
# AnyInteger
# AnyString
# AnyStringWith
# =============================================================================


Expand Down Expand Up @@ -430,3 +435,46 @@ def check_table_does_not_exist(adapter, name):
def check_table_does_exist(adapter, name):
columns = get_relation_columns(adapter, name)
assert len(columns) > 0


# Utility classes for enabling comparison of dictionaries


class AnyFloat:
"""Any float. Use this in assert calls"""

def __eq__(self, other):
return isinstance(other, float)


class AnyInteger:
"""Any Integer. Use this in assert calls"""

def __eq__(self, other):
return isinstance(other, int)


class AnyString:
"""Any string. Use this in assert calls"""

def __eq__(self, other):
return isinstance(other, str)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The three classes here looks very similar, why we are doing it as 3 individual instead of having one that you can maybe pass in a specific type? Also, think we can achieve AnyString by not passing in contains for AnyStringWith?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're separate classes because they were copied from existing classes in the integration test suite. You do have a point that a single class that takes a type would work here.



class AnyStringWith:
"""AnyStringWith("AUTO")"""

def __init__(self, contains=None):
self.contains = contains

def __eq__(self, other):
if not isinstance(other, str):
return False

if self.contains is None:
return True

return self.contains in other

def __repr__(self):
return "AnyStringWith<{!r}>".format(self.contains)
105 changes: 66 additions & 39 deletions tests/adapter/dbt/tests/adapter/basic/expected_catalog.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from dbt.tests.util import AnyInteger


def no_stats():
return {
"has_stats": {
Expand All @@ -12,6 +15,7 @@ def no_stats():

def base_expected_catalog(
project,
role,
id_type,
text_type,
time_type,
Expand All @@ -21,7 +25,6 @@ def base_expected_catalog(
seed_stats=None,
case=None,
case_columns=False,
model_database=None,
):

if case is None:
Expand All @@ -34,40 +37,38 @@ def case(x):
if seed_stats is None:
seed_stats = model_stats

if model_database is None:
model_database = project.database
my_schema_name = project.test_schema
role = "root"
alternate_schema = project.test_schema + "_test"
model_database = project.database
my_schema_name = case(project.test_schema)
alternate_schema = case(project.test_schema + "_test")

expected_cols = {
col_case("id"): {
"name": col_case("id"),
"index": 1,
"index": AnyInteger(),
"type": id_type,
"comment": None,
},
col_case("first_name"): {
"name": col_case("first_name"),
"index": 2,
"index": AnyInteger(),
"type": text_type,
"comment": None,
},
col_case("email"): {
"name": col_case("email"),
"index": 3,
"index": AnyInteger(),
"type": text_type,
"comment": None,
},
col_case("ip_address"): {
"name": col_case("ip_address"),
"index": 4,
"index": AnyInteger(),
"type": text_type,
"comment": None,
},
col_case("updated_at"): {
"name": col_case("updated_at"),
"index": 5,
"index": AnyInteger(),
"type": time_type,
"comment": None,
},
Expand Down Expand Up @@ -132,55 +133,81 @@ def case(x):
}


def expected_references_catalog(project):
def expected_references_catalog(
project,
role,
id_type,
text_type,
time_type,
view_type,
table_type,
model_stats,
bigint_type=None,
seed_stats=None,
case=None,
case_columns=False,
view_summary_stats=None,
):
if case is None:

def case(x):
return x

col_case = case if case_columns else lambda x: x

if seed_stats is None:
seed_stats = model_stats

if view_summary_stats is None:
view_summary_stats = model_stats

model_database = project.database
my_schema_name = project.test_schema
role = "root"
stats = no_stats()
my_schema_name = case(project.test_schema)

summary_columns = {
"first_name": {
"name": "first_name",
"index": 1,
"type": "text",
"type": text_type,
"comment": None,
},
"ct": {
"name": "ct",
"index": 2,
"type": "bigint",
"type": bigint_type,
"comment": None,
},
}

seed_columns = {
"id": {
"name": "id",
"name": col_case("id"),
"index": 1,
"type": "integer",
"type": id_type,
"comment": None,
},
"first_name": {
"name": "first_name",
"name": col_case("first_name"),
"index": 2,
"type": "text",
"type": text_type,
"comment": None,
},
"email": {
"name": "email",
"name": col_case("email"),
"index": 3,
"type": "text",
"type": text_type,
"comment": None,
},
"ip_address": {
"name": "ip_address",
"name": col_case("ip_address"),
"index": 4,
"type": "text",
"type": text_type,
"comment": None,
},
"updated_at": {
"name": "updated_at",
"name": col_case("updated_at"),
"index": 5,
"type": "timestamp without time zone",
"type": time_type,
"comment": None,
},
}
Expand All @@ -191,38 +218,38 @@ def expected_references_catalog(project):
"metadata": {
"schema": my_schema_name,
"database": project.database,
"name": "seed",
"type": "BASE TABLE",
"name": case("seed"),
"type": table_type,
"comment": None,
"owner": role,
},
"stats": stats,
"stats": seed_stats,
"columns": seed_columns,
},
"model.test.ephemeral_summary": {
"unique_id": "model.test.ephemeral_summary",
"metadata": {
"schema": my_schema_name,
"database": model_database,
"name": "ephemeral_summary",
"type": "BASE TABLE",
"name": case("ephemeral_summary"),
"type": table_type,
"comment": None,
"owner": role,
},
"stats": stats,
"stats": model_stats,
"columns": summary_columns,
},
"model.test.view_summary": {
"unique_id": "model.test.view_summary",
"metadata": {
"schema": my_schema_name,
"database": model_database,
"name": "view_summary",
"type": "VIEW",
"name": case("view_summary"),
"type": view_type,
"comment": None,
"owner": role,
},
"stats": stats,
"stats": view_summary_stats,
"columns": summary_columns,
},
},
Expand All @@ -232,12 +259,12 @@ def expected_references_catalog(project):
"metadata": {
"schema": my_schema_name,
"database": project.database,
"name": "seed",
"type": "BASE TABLE",
"name": case("seed"),
"type": table_type,
"comment": None,
"owner": role,
},
"stats": stats,
"stats": seed_stats,
"columns": seed_columns,
},
},
Expand Down
Loading