From af3c3f4cbe0d306a5e663c86c99bbfc30158a671 Mon Sep 17 00:00:00 2001 From: Joshua Carp Date: Mon, 8 Mar 2021 15:39:02 -0500 Subject: [PATCH] Add tests for bigquery label sanitize helper. --- .../bigquery/dbt/adapters/bigquery/connections.py | 4 ++-- test/unit/test_bigquery_adapter.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/plugins/bigquery/dbt/adapters/bigquery/connections.py b/plugins/bigquery/dbt/adapters/bigquery/connections.py index 69ae2178532..acbecfa84c2 100644 --- a/plugins/bigquery/dbt/adapters/bigquery/connections.py +++ b/plugins/bigquery/dbt/adapters/bigquery/connections.py @@ -596,7 +596,7 @@ def _is_retryable(error): def _sanitize_label(value: str, max_length: int = 63) -> str: """Return a legal value for a BigQuery label.""" - value = value.lower() + value = value.strip().lower() value = _SANITIZE_LABEL_PATTERN.sub("_", value) - value = value[: max_length - 1] + value = value[: max_length] return value diff --git a/test/unit/test_bigquery_adapter.py b/test/unit/test_bigquery_adapter.py index 7cc71c6e459..5f317376191 100644 --- a/test/unit/test_bigquery_adapter.py +++ b/test/unit/test_bigquery_adapter.py @@ -16,12 +16,14 @@ from dbt.adapters.bigquery import BigQueryRelation from dbt.adapters.bigquery import Plugin as BigQueryPlugin from dbt.adapters.bigquery.connections import BigQueryConnectionManager +from dbt.adapters.bigquery.connections import _sanitize_label from dbt.adapters.base.query_headers import MacroQueryStringSetter from dbt.clients import agate_helper import dbt.exceptions from dbt.logger import GLOBAL_LOGGER as logger # noqa from dbt.context.providers import RuntimeConfigObject +import pytest import google.cloud.bigquery from .utils import config_from_parts_or_dicts, inject_adapter, TestAdapterConversions @@ -939,3 +941,16 @@ def test_convert_time_type(self): expected = ['time', 'time', 'time'] for col_idx, expect in enumerate(expected): assert BigQueryAdapter.convert_time_type(agate_table, col_idx) == expect + + +@pytest.mark.parametrize( + ["input", "output"], + [ + ("a" * 64, "a" * 63), + ("ABC", "abc"), + ("a c", "a_c"), + ("a ", "a"), + ], +) +def test_sanitize_label(input, output): + assert _sanitize_label(input) == output