|  | 
|  | 1 | +import unittest | 
|  | 2 | + | 
|  | 3 | +from dbt.adapters.bigquery.relation_configs._options import BigQueryOptionsConfig | 
|  | 4 | + | 
|  | 5 | + | 
|  | 6 | +class TestBigQueryOptionsConfig(unittest.TestCase): | 
|  | 7 | +    """Unit tests for BigQuery options config with tags""" | 
|  | 8 | + | 
|  | 9 | +    def test_from_dict_with_tags(self): | 
|  | 10 | +        """Test creating BigQueryOptionsConfig from dict with tags""" | 
|  | 11 | +        config_dict = { | 
|  | 12 | +            "enable_refresh": True, | 
|  | 13 | +            "refresh_interval_minutes": 45, | 
|  | 14 | +            "description": "Test materialized view", | 
|  | 15 | +            "labels": {"env": "dev", "version": "1.0"}, | 
|  | 16 | +            "tags": {"test-project/team": "data", "test-project/cost_center": "analytics"}, | 
|  | 17 | +        } | 
|  | 18 | + | 
|  | 19 | +        options = BigQueryOptionsConfig.from_dict(config_dict) | 
|  | 20 | + | 
|  | 21 | +        # Verify all fields are correctly set | 
|  | 22 | +        self.assertTrue(options.enable_refresh) | 
|  | 23 | +        self.assertEqual(options.refresh_interval_minutes, 45) | 
|  | 24 | +        self.assertEqual(options.description, "Test materialized view") | 
|  | 25 | +        self.assertEqual(options.labels, {"env": "dev", "version": "1.0"}) | 
|  | 26 | +        self.assertEqual( | 
|  | 27 | +            options.tags, {"test-project/team": "data", "test-project/cost_center": "analytics"} | 
|  | 28 | +        ) | 
|  | 29 | + | 
|  | 30 | +    def test_as_ddl_dict_with_tags(self): | 
|  | 31 | +        """Test generating DDL dict with tags""" | 
|  | 32 | +        options = BigQueryOptionsConfig( | 
|  | 33 | +            enable_refresh=True, | 
|  | 34 | +            refresh_interval_minutes=30, | 
|  | 35 | +            description="Test MV", | 
|  | 36 | +            labels={"env": "test"}, | 
|  | 37 | +            tags={"test-project/team": "data"}, | 
|  | 38 | +        ) | 
|  | 39 | + | 
|  | 40 | +        ddl_dict = options.as_ddl_dict() | 
|  | 41 | + | 
|  | 42 | +        # Verify the output format | 
|  | 43 | +        self.assertTrue(ddl_dict["enable_refresh"]) | 
|  | 44 | +        self.assertEqual(ddl_dict["refresh_interval_minutes"], 30) | 
|  | 45 | +        self.assertEqual(ddl_dict["description"], '"""Test MV"""') | 
|  | 46 | + | 
|  | 47 | +        # Check labels and tags are properly formatted as arrays of tuples | 
|  | 48 | +        expected_labels = [("env", "test")] | 
|  | 49 | +        expected_tags = [("test-project/team", "data")] | 
|  | 50 | + | 
|  | 51 | +        self.assertEqual(ddl_dict["labels"], expected_labels) | 
|  | 52 | +        self.assertEqual(ddl_dict["tags"], expected_tags) | 
|  | 53 | + | 
|  | 54 | +    def test_tags_none_not_included_in_ddl(self): | 
|  | 55 | +        """Test that None tags are not included in DDL dict""" | 
|  | 56 | +        options = BigQueryOptionsConfig(enable_refresh=True, tags=None) | 
|  | 57 | + | 
|  | 58 | +        ddl_dict = options.as_ddl_dict() | 
|  | 59 | + | 
|  | 60 | +        # tags should not be in the output when None | 
|  | 61 | +        self.assertNotIn("tags", ddl_dict) | 
|  | 62 | +        self.assertIn("enable_refresh", ddl_dict) | 
|  | 63 | + | 
|  | 64 | +    def test_parse_relation_config_with_resource_tags(self): | 
|  | 65 | +        """Test parse_relation_config method handles resource_tags correctly""" | 
|  | 66 | +        from unittest.mock import Mock | 
|  | 67 | + | 
|  | 68 | +        # Mock relation config with resource_tags | 
|  | 69 | +        mock_relation_config = Mock() | 
|  | 70 | +        mock_config = Mock() | 
|  | 71 | +        mock_config.extra = { | 
|  | 72 | +            "enable_refresh": True, | 
|  | 73 | +            "refresh_interval_minutes": 30, | 
|  | 74 | +            "description": "Test view", | 
|  | 75 | +            "labels": {"env": "test"}, | 
|  | 76 | +            "resource_tags": {"test-project/team": "data", "test-project/project": "analytics"}, | 
|  | 77 | +        } | 
|  | 78 | +        mock_config.persist_docs = True | 
|  | 79 | +        mock_relation_config.config = mock_config | 
|  | 80 | + | 
|  | 81 | +        config_dict = BigQueryOptionsConfig.parse_relation_config(mock_relation_config) | 
|  | 82 | + | 
|  | 83 | +        # Verify resource_tags gets mapped to tags | 
|  | 84 | +        self.assertEqual( | 
|  | 85 | +            config_dict["tags"], {"test-project/team": "data", "test-project/project": "analytics"} | 
|  | 86 | +        ) | 
|  | 87 | +        self.assertEqual(config_dict["labels"], {"env": "test"}) | 
|  | 88 | +        self.assertEqual(config_dict["description"], "Test view") | 
|  | 89 | +        self.assertTrue(config_dict["enable_refresh"]) | 
|  | 90 | + | 
|  | 91 | +    def test_parse_relation_config_without_resource_tags(self): | 
|  | 92 | +        """Test parse_relation_config when resource_tags is not present""" | 
|  | 93 | +        from unittest.mock import Mock | 
|  | 94 | + | 
|  | 95 | +        # Mock relation config without resource_tags | 
|  | 96 | +        mock_relation_config = Mock() | 
|  | 97 | +        mock_config = Mock() | 
|  | 98 | +        mock_config.extra = {"enable_refresh": False, "labels": {"env": "prod"}} | 
|  | 99 | +        mock_config.persist_docs = True | 
|  | 100 | +        mock_relation_config.config = mock_config | 
|  | 101 | + | 
|  | 102 | +        config_dict = BigQueryOptionsConfig.parse_relation_config(mock_relation_config) | 
|  | 103 | + | 
|  | 104 | +        # Verify tags is not in the config_dict when resource_tags is not present | 
|  | 105 | +        self.assertNotIn("tags", config_dict) | 
|  | 106 | +        self.assertEqual(config_dict["labels"], {"env": "prod"}) | 
|  | 107 | +        self.assertFalse(config_dict["enable_refresh"]) | 
|  | 108 | + | 
|  | 109 | + | 
|  | 110 | +if __name__ == "__main__": | 
|  | 111 | +    unittest.main() | 
0 commit comments