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

Added docs #8

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 19 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
# dbt-testgen

dbt-testgen autogenerates dbt test yaml based on real data.
## About
`dbt-testgen` is a [dbt](https://github.com/dbt-labs/dbt) package that autogenerates dbt test YAML based on real data.

Inspired by [dbt-codegen]() and [deequ Constraint Suggestion](https://github.com/awslabs/deequ/blob/master/src/main/scala/com/amazon/deequ/examples/constraint_suggestion_example.md)
Inspired by [dbt-codegen](https://github.com/dbt-labs/dbt-codegen) and [deequ Constraint Suggestion](https://github.com/awslabs/deequ/blob/master/src/main/scala/com/amazon/deequ/examples/constraint_suggestion_example.md)

## Install
`dbt-testgen` currently supports `dbt 1.2.x` or higher.

Include in `packages.yml`:
```yaml
packages:
- git: https://github.com/kgmcquate/dbt-testgen
```

## Supported Databases
The following databases are supported:
- Snowflake
- RedShift
- Postgres
- DuckDB

## Usage
The DBT config YAML is generated by a Jinja macro, `get_test_suggestions`, which you can run like this:
Expand All @@ -23,12 +40,6 @@ models:
max_value: 30
```

## Supported Databases
The following databases are supported:
- Snowflake
- Postgres
- DuckDB

## Test types
dbt-testgen can generate these types of tests:
- [uniqueness](#uniqueness)
Expand All @@ -38,15 +49,3 @@ dbt-testgen can generate these types of tests:
- [accepted_values](#accepted-values)
- [freshness](#freshness)

### Uniqueness

### Not null

### String length

### Range

### Mean and stddev


###
4 changes: 1 addition & 3 deletions dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: 'testgen'
version: '0.0.1'

profile: postgres

require-dbt-version: [">=1.2.0", "<2.0.0"]
config-version: 2

target-path: "target"
Expand Down
1 change: 1 addition & 0 deletions docs/catalog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"metadata": {"dbt_schema_version": "https://schemas.getdbt.com/dbt/catalog/v1.json", "dbt_version": "1.7.4", "generated_at": "2024-01-03T04:59:27.467159Z", "invocation_id": "9da1e8ba-623e-4944-bd70-71b4be14443b", "env": {}}, "nodes": {"seed.testgen_integration_tests.colnames_with_spaces": {"metadata": {"type": "BASE TABLE", "schema": "main_integration_test_data", "name": "colnames_with_spaces", "database": "integration_test_data", "comment": null, "owner": null}, "columns": {"first name": {"type": "VARCHAR", "index": 1, "name": "first name", "comment": null}, "age (years)": {"type": "INTEGER", "index": 2, "name": "age (years)", "comment": null}, "current city": {"type": "VARCHAR", "index": 3, "name": "current city", "comment": null}}, "stats": {"has_stats": {"id": "has_stats", "label": "Has Stats?", "value": false, "include": false, "description": "Indicates whether there are statistics for this table"}}, "unique_id": "seed.testgen_integration_tests.colnames_with_spaces"}, "seed.testgen_integration_tests.users": {"metadata": {"type": "BASE TABLE", "schema": "main_integration_test_data", "name": "users", "database": "integration_test_data", "comment": null, "owner": null}, "columns": {"user_id": {"type": "INTEGER", "index": 1, "name": "user_id", "comment": null}, "username": {"type": "VARCHAR", "index": 2, "name": "username", "comment": null}, "email": {"type": "VARCHAR", "index": 3, "name": "email", "comment": null}, "age": {"type": "INTEGER", "index": 4, "name": "age", "comment": null}, "user_status": {"type": "VARCHAR", "index": 5, "name": "user_status", "comment": null}}, "stats": {"has_stats": {"id": "has_stats", "label": "Has Stats?", "value": false, "include": false, "description": "Indicates whether there are statistics for this table"}}, "unique_id": "seed.testgen_integration_tests.users"}}, "sources": {}, "errors": null}
102 changes: 102 additions & 0 deletions docs/index.html

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/manifest.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/run_results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"metadata": {"dbt_schema_version": "https://schemas.getdbt.com/dbt/run-results/v5.json", "dbt_version": "1.7.4", "generated_at": "2024-01-03T04:59:27.115354Z", "invocation_id": "9da1e8ba-623e-4944-bd70-71b4be14443b", "env": {}}, "results": [{"status": "success", "timing": [{"name": "compile", "started_at": "2024-01-03T04:59:25.546384Z", "completed_at": "2024-01-03T04:59:25.556604Z"}, {"name": "execute", "started_at": "2024-01-03T04:59:25.561393Z", "completed_at": "2024-01-03T04:59:25.561434Z"}], "thread_id": "Thread-1 (worker)", "execution_time": 0.02170872688293457, "adapter_response": {}, "message": null, "failures": null, "unique_id": "seed.testgen_integration_tests.colnames_with_spaces", "compiled": null, "compiled_code": null, "relation_name": null}, {"status": "success", "timing": [{"name": "compile", "started_at": "2024-01-03T04:59:25.573683Z", "completed_at": "2024-01-03T04:59:25.576952Z"}, {"name": "execute", "started_at": "2024-01-03T04:59:25.580572Z", "completed_at": "2024-01-03T04:59:25.580604Z"}], "thread_id": "Thread-1 (worker)", "execution_time": 0.014430999755859375, "adapter_response": {}, "message": null, "failures": null, "unique_id": "seed.testgen_integration_tests.users", "compiled": null, "compiled_code": null, "relation_name": null}, {"status": "success", "timing": [{"name": "compile", "started_at": "2024-01-03T04:59:25.593254Z", "completed_at": "2024-01-03T04:59:25.664443Z"}, {"name": "execute", "started_at": "2024-01-03T04:59:25.667966Z", "completed_at": "2024-01-03T04:59:25.668000Z"}], "thread_id": "Thread-1 (worker)", "execution_time": 0.08290576934814453, "adapter_response": {}, "message": null, "failures": null, "unique_id": "test.testgen_integration_tests.merge_dbt_configs_1", "compiled": true, "compiled_code": "\n\n\n\n\n\n\n\n\n\n\n\n select 'ok' limit 0\n\n\n", "relation_name": null}, {"status": "success", "timing": [{"name": "compile", "started_at": "2024-01-03T04:59:25.678237Z", "completed_at": "2024-01-03T04:59:25.699856Z"}, {"name": "execute", "started_at": "2024-01-03T04:59:25.703326Z", "completed_at": "2024-01-03T04:59:25.703356Z"}], "thread_id": "Thread-1 (worker)", "execution_time": 0.03134417533874512, "adapter_response": {}, "message": null, "failures": null, "unique_id": "test.testgen_integration_tests.merge_dbt_configs_2", "compiled": true, "compiled_code": "\n\n\n\n\n\n\n\n\n\n\n\n select 'ok' limit 0\n\n\n", "relation_name": null}, {"status": "success", "timing": [{"name": "compile", "started_at": "2024-01-03T04:59:25.713605Z", "completed_at": "2024-01-03T04:59:25.912815Z"}, {"name": "execute", "started_at": "2024-01-03T04:59:25.917043Z", "completed_at": "2024-01-03T04:59:25.917080Z"}], "thread_id": "Thread-1 (worker)", "execution_time": 0.22395873069763184, "adapter_response": {}, "message": null, "failures": null, "unique_id": "test.testgen_integration_tests.accepted_values_colnames_with_spaces", "compiled": true, "compiled_code": "\n\n\n\n\n\n\n select 'ok' limit 0\n\n\n", "relation_name": null}, {"status": "success", "timing": [{"name": "compile", "started_at": "2024-01-03T04:59:25.942087Z", "completed_at": "2024-01-03T04:59:26.140883Z"}, {"name": "execute", "started_at": "2024-01-03T04:59:26.144386Z", "completed_at": "2024-01-03T04:59:26.144415Z"}], "thread_id": "Thread-1 (worker)", "execution_time": 0.22037839889526367, "adapter_response": {}, "message": null, "failures": null, "unique_id": "test.testgen_integration_tests.range_colnames_with_spaces", "compiled": true, "compiled_code": "\n\n\n\n\n\n\n select 'ok' limit 0\n\n\n", "relation_name": null}, {"status": "success", "timing": [{"name": "compile", "started_at": "2024-01-03T04:59:26.166117Z", "completed_at": "2024-01-03T04:59:26.332584Z"}, {"name": "execute", "started_at": "2024-01-03T04:59:26.336373Z", "completed_at": "2024-01-03T04:59:26.336401Z"}], "thread_id": "Thread-1 (worker)", "execution_time": 0.189652681350708, "adapter_response": {}, "message": null, "failures": null, "unique_id": "test.testgen_integration_tests.uniqueness_colnames_with_spaces", "compiled": true, "compiled_code": "\n\n\n\n\n\n\n select 'ok' limit 0\n\n\n", "relation_name": null}, {"status": "success", "timing": [{"name": "compile", "started_at": "2024-01-03T04:59:26.360779Z", "completed_at": "2024-01-03T04:59:26.520275Z"}, {"name": "execute", "started_at": "2024-01-03T04:59:26.524486Z", "completed_at": "2024-01-03T04:59:26.524515Z"}], "thread_id": "Thread-1 (worker)", "execution_time": 0.18728899955749512, "adapter_response": {}, "message": null, "failures": null, "unique_id": "test.testgen_integration_tests.accepted_values_users", "compiled": true, "compiled_code": "\n\n\n\n\n\n\n select 'ok' limit 0\n\n\n", "relation_name": null}, {"status": "success", "timing": [{"name": "compile", "started_at": "2024-01-03T04:59:26.553028Z", "completed_at": "2024-01-03T04:59:26.669485Z"}, {"name": "execute", "started_at": "2024-01-03T04:59:26.674581Z", "completed_at": "2024-01-03T04:59:26.674606Z"}], "thread_id": "Thread-1 (worker)", "execution_time": 0.13959908485412598, "adapter_response": {}, "message": null, "failures": null, "unique_id": "test.testgen_integration_tests.range_users", "compiled": true, "compiled_code": "\n\n\n\n\n\n\n select 'ok' limit 0\n\n\n", "relation_name": null}, {"status": "success", "timing": [{"name": "compile", "started_at": "2024-01-03T04:59:26.696453Z", "completed_at": "2024-01-03T04:59:26.931429Z"}, {"name": "execute", "started_at": "2024-01-03T04:59:26.935102Z", "completed_at": "2024-01-03T04:59:26.935125Z"}], "thread_id": "Thread-1 (worker)", "execution_time": 0.25930023193359375, "adapter_response": {}, "message": null, "failures": null, "unique_id": "test.testgen_integration_tests.test_selection_users", "compiled": true, "compiled_code": "\n\n\n\n\n\n\n select 'ok' limit 0\n\n\n", "relation_name": null}, {"status": "success", "timing": [{"name": "compile", "started_at": "2024-01-03T04:59:26.961234Z", "completed_at": "2024-01-03T04:59:27.088522Z"}, {"name": "execute", "started_at": "2024-01-03T04:59:27.092184Z", "completed_at": "2024-01-03T04:59:27.092202Z"}], "thread_id": "Thread-1 (worker)", "execution_time": 0.15043139457702637, "adapter_response": {}, "message": null, "failures": null, "unique_id": "test.testgen_integration_tests.uniqueness_users", "compiled": true, "compiled_code": "\n\n\n\n\n\n\n select 'ok' limit 0\n\n\n", "relation_name": null}], "elapsed_time": 1.7102117538452148, "args": {"introspect": true, "partial_parse_file_diff": true, "printer_width": 80, "project_dir": "/run/dbt-testgen/integration_tests", "log_file_max_bytes": 10485760, "warn_error_options": {"include": [], "exclude": []}, "version_check": true, "indirect_selection": "eager", "quiet": false, "profiles_dir": "/run/dbt-testgen/integration_tests", "use_colors_file": true, "show_resource_report": false, "which": "generate", "log_format": "default", "compile": true, "log_level_file": "debug", "empty_catalog": false, "macro_debugging": false, "select": [], "vars": {}, "send_anonymous_usage_stats": false, "static": false, "defer": false, "print": true, "log_format_file": "debug", "favor_state": false, "populate_cache": true, "cache_selected_only": false, "static_parser": true, "invocation_command": "dbt docs generate", "strict_mode": false, "log_level": "info", "write_json": true, "exclude": [], "enable_legacy_logger": false, "log_path": "/run/dbt-testgen/integration_tests/logs", "partial_parse": true, "use_colors": true}}
7 changes: 7 additions & 0 deletions integration_tests/generate_docs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dbt docs generate

mkdir ../docs
cp target/catalog.json ../docs
cp target/index.html ../docs
cp target/manifest.json ../docs
cp target/run_results.json ../docs
11 changes: 9 additions & 2 deletions integration_tests/profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ integration_tests:
user: postgres
password: postgres
port: 5432
dbname: postgres # or database instead of dbname
dbname: postgres
schema: public
mysql:
type: mysql
host: mysql
username: mysql
password: mysql
port: 3306
schema: public
snowflake:
type: snowflake
Expand All @@ -43,7 +50,7 @@ integration_tests:
type: redshift
host: dbt-testgen.117819748843.us-east-1.redshift-serverless.amazonaws.com
user: dbt_testgen
password: mw*gXe9JMvp!0v%E #"{{ env_var('REDSHIFT_PASSWORD') }}"
password: "{{ env_var('REDSHIFT_PASSWORD') }}"
dbname: dbt_testgen
schema: dbt_testgen
port: 5439
4 changes: 3 additions & 1 deletion macros/test_generation/get_range_test_suggestions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
{% do min_max_exprs.append(
"SELECT '" ~ column.column ~ "' AS COLNAME, " ~
"MIN(" ~ adapter.quote(column.column) ~ ") as COL_MIN, " ~
"MAX(" ~ adapter.quote(column.column) ~ ") as COL_MAX " ~
"MAX(" ~ adapter.quote(column.column) ~ ") as COL_MAX, " ~
loop.index ~ " AS ORDERING " ~
"FROM " ~ table_relation
) %}
{% endfor %}
Expand All @@ -60,6 +61,7 @@
SELECT * FROM (
{{ min_max_exprs | join("\nUNION ALL\n") }}
) t1
ORDER BY ORDERING ASC
{% endset %}

{% set results = testgen.query_as_list(min_max_sql) %}
Expand Down