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

Add column rename example #19

Merged
merged 21 commits into from
Jul 22, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Add example with column rename macro ([PR](https://github.com/godatadriven/pytest-dbt-core/pull/19))

## [0.1.0] - 2022-07-22

- Run test examples from docs ([issue](https://github.com/godatadriven/pytest-dbt-core/issues/14), [PR](https://github.com/godatadriven/pytest-dbt-core/pull/17))
Expand Down
18 changes: 18 additions & 0 deletions docs/source/_static/dbt_project/macros/normalize_column_names.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% macro normalize_column_names(column_names) %}
{%- set re = modules.re -%}

{%- for column_name in column_names -%}

{%- set normalized_column_name = re.sub('[!?@#$()%&]', '', column_name).strip().replace(' ', '_').replace('.', '_').rstrip('_').lower() -%}

{# Columns should not start with digits #}
{%- if normalized_column_name[0].isdigit() -%}
{% set normalized_column_name = '_' + normalized_column_name-%}
{% endif -%}

`{{ column_name }}` as {{ normalized_column_name }}
{%- if not loop.last -%}, {% endif -%}

{%- endfor -%}

{% endmacro %}
2 changes: 2 additions & 0 deletions docs/source/_static/dbt_project/packages.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
packages:
- package: dbt-labs/spark_utils
version: 0.3.0
- package: dbt-labs/dbt_utils
version: 0.8.6
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
from dbt.clients.jinja import MacroGenerator
from pyspark.sql import SparkSession


@pytest.mark.parametrize(
"macro_generator",
["macro.dbt_project.normalize_column_names"],
indirect=True,
)
@pytest.mark.parametrize(
"column_name,expected_column_name",
[
("unit", "unit"),
("column with spaces", "column_with_spaces"),
("c!h?a#r$a(c)t%e&rs", "characters"),
("trailing white spaces ", "trailing_white_spaces"),
("column.with.periods", "column_with_periods"),
("9leading number", "_9leading_number"),
("UPPERCASE", "uppercase"),
],
)
def test_normalize_column_names(
spark_session: SparkSession,
macro_generator: MacroGenerator,
column_name: str,
expected_column_name: str,
) -> None:
"""Test normalize column names with different scenarios."""
normalized_column_names = macro_generator([column_name])
out = spark_session.sql(
f"SELECT {normalized_column_names} FROM (SELECT True AS `{column_name}`)"
)
assert out.columns[0] == expected_column_name, normalized_column_names
21 changes: 2 additions & 19 deletions docs/source/dbt_spark.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,8 @@ Usage

Use the `spark_session` fixture to set-up the unit test for your macro:

.. code-block:: python

from __future__ import annotations

import pytest
from dbt.clients.jinja import MacroGenerator
from pyspark.sql import SparkSession


@pytest.mark.parametrize(
"macro_generator", ["macro.spark_utils.get_tables"], indirect=True
)
def test_create_table(
spark_session: SparkSession, macro_generator: MacroGenerator
) -> None:
expected_table = "default.example"
spark_session.sql(f"CREATE TABLE {expected_table} (id int) USING parquet")
tables = macro_generator()
assert tables == [expected_table]
.. literalinclude :: _static/dbt_project/tests/test_spark_get_tables.py
:language: python

Test
****
Expand Down
7 changes: 6 additions & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ Install `pytest-dbt-core` via pip with
Usage
******

Create a macro:

.. literalinclude :: _static/dbt_project/macros/normalize_column_names.sql
:language: jinja

Unit test a macro:

.. literalinclude :: _static/dbt_project/tests/test_spark_get_tables.py
.. literalinclude :: _static/dbt_project/tests/test_normalize_column_names.py
:language: python

.. toctree::
Expand Down