|
| 1 | +import pytest |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +from dbt.tests.adapter.unit_testing.test_pseudocolumns import ( |
| 5 | + BasePseudocolumnUnitTest, |
| 6 | + external_table_csv, |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +# Model that uses BigQuery's _FILE_NAME pseudocolumn |
| 11 | +my_model_sql = """ |
| 12 | +select |
| 13 | + id, |
| 14 | + _FILE_NAME as file_name |
| 15 | +from {{ source('test_source', 'external_table') }} |
| 16 | +""" |
| 17 | + |
| 18 | +# Source definition for external table |
| 19 | +schema_sources_yml = """ |
| 20 | +sources: |
| 21 | + - name: test_source |
| 22 | + schema: "{{ target.schema }}" |
| 23 | + tables: |
| 24 | + - name: external_table |
| 25 | + columns: |
| 26 | + - name: id |
| 27 | +""" |
| 28 | + |
| 29 | +# Unit test that includes _FILE_NAME pseudocolumn in the fixture |
| 30 | +test_my_model_yml = """ |
| 31 | +unit_tests: |
| 32 | + - name: test_bigquery_file_name_pseudocolumn |
| 33 | + model: my_model |
| 34 | + given: |
| 35 | + - input: source('test_source', 'external_table') |
| 36 | + rows: |
| 37 | + - {id: 1, _FILE_NAME: 'gs://bucket/file1.csv'} |
| 38 | + - {id: 2, _FILE_NAME: 'gs://bucket/file2.csv'} |
| 39 | + expect: |
| 40 | + rows: |
| 41 | + - {id: 1, file_name: 'gs://bucket/file1.csv'} |
| 42 | + - {id: 2, file_name: 'gs://bucket/file2.csv'} |
| 43 | +""" |
| 44 | + |
| 45 | + |
| 46 | +class TestBigQueryPseudocolumns(BasePseudocolumnUnitTest): |
| 47 | + """Test BigQuery's _FILE_NAME pseudocolumn support in unit tests. |
| 48 | +
|
| 49 | + Uses BigQuery-specific SQL (_FILE_NAME as file_name) and validates that the |
| 50 | + pseudocolumn framework works with BigQueryColumn types. |
| 51 | + """ |
| 52 | + |
| 53 | + @pytest.fixture(scope="class") |
| 54 | + def seeds(self): |
| 55 | + """Seed data.""" |
| 56 | + return { |
| 57 | + "external_table.csv": external_table_csv, |
| 58 | + } |
| 59 | + |
| 60 | + @pytest.fixture(scope="class") |
| 61 | + def models(self): |
| 62 | + """Models using BigQuery's _FILE_NAME pseudocolumn.""" |
| 63 | + return { |
| 64 | + "my_model.sql": my_model_sql, |
| 65 | + "sources.yml": schema_sources_yml + test_my_model_yml, |
| 66 | + } |
| 67 | + |
| 68 | + @pytest.fixture(scope="class") |
| 69 | + def setup_pseudocolumn_override(self, project): |
| 70 | + """Patch BigQueryAdapter at the class level to return columns + _FILE_NAME for external_table. |
| 71 | +
|
| 72 | + Instance-level patching (project.adapter.X) doesn't work because dbtRunner |
| 73 | + creates its own adapter instance. Class-level patching affects all instances. |
| 74 | + """ |
| 75 | + from dbt.adapters.bigquery.column import BigQueryColumn |
| 76 | + from dbt.adapters.bigquery.impl import BigQueryAdapter |
| 77 | + |
| 78 | + original_method = BigQueryAdapter.get_columns_and_pseudocolumns_for_relation |
| 79 | + |
| 80 | + def mock_columns_and_pseudocolumns(_self, relation): |
| 81 | + if relation.identifier == "external_table": |
| 82 | + return [BigQueryColumn("id", "INTEGER"), BigQueryColumn("_FILE_NAME", "STRING")] |
| 83 | + # Fall through to the real method for temp relations (model output schema lookup) |
| 84 | + return original_method(_self, relation) |
| 85 | + |
| 86 | + mock_columns_and_pseudocolumns._is_available_ = True |
| 87 | + mock_columns_and_pseudocolumns._parse_replacement_ = lambda *a, **k: [] |
| 88 | + |
| 89 | + with patch.object( |
| 90 | + BigQueryAdapter, |
| 91 | + "get_columns_and_pseudocolumns_for_relation", |
| 92 | + mock_columns_and_pseudocolumns, |
| 93 | + ): |
| 94 | + yield |
0 commit comments