From cdd091676ffff08e84709f61e10e439b06261bed Mon Sep 17 00:00:00 2001 From: Vuong Nguyen Date: Mon, 6 Mar 2023 20:09:24 +0000 Subject: [PATCH 1/2] Add acc test for `databricks_tables` data source --- catalog/data_tables.go | 2 +- catalog/data_tables_test.go | 35 ++++++---- internal/acceptance/data_tables_test.go | 87 +++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 internal/acceptance/data_tables_test.go diff --git a/catalog/data_tables.go b/catalog/data_tables.go index a40525735c..af8617d1ee 100644 --- a/catalog/data_tables.go +++ b/catalog/data_tables.go @@ -21,7 +21,7 @@ func DataSourceTables() *schema.Resource { } for _, v := range tables { if v.TableType != "VIEW" { - data.Ids = append(data.Ids, v.Name) + data.Ids = append(data.Ids, v.FullName) } } return nil diff --git a/catalog/data_tables_test.go b/catalog/data_tables_test.go index 3beee31bf3..ad2df34e5e 100644 --- a/catalog/data_tables_test.go +++ b/catalog/data_tables_test.go @@ -3,6 +3,7 @@ package catalog import ( "testing" + "github.com/databricks/databricks-sdk-go/service/unitycatalog" "github.com/databricks/terraform-provider-databricks/qa" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/stretchr/testify/assert" @@ -15,13 +16,15 @@ func TestTablesData(t *testing.T) { { Method: "GET", Resource: "/api/2.1/unity-catalog/tables?catalog_name=a&schema_name=b", - Response: Tables{ - Tables: []TableInfo{ + Response: unitycatalog.ListTablesResponse{ + Tables: []unitycatalog.TableInfo{ { - Name: "a.b.c", + FullName: "a.b.c", + Name: "c", }, { - Name: "a.b.d", + FullName: "a.b.d", + Name: "d", }, }, }, @@ -45,13 +48,15 @@ func TestTablesDataIssue1264(t *testing.T) { { Method: "GET", Resource: "/api/2.1/unity-catalog/tables?catalog_name=a&schema_name=b", - Response: Tables{ - Tables: []TableInfo{ + Response: unitycatalog.ListTablesResponse{ + Tables: []unitycatalog.TableInfo{ { - Name: "a", + Name: "a", + FullName: "a.b.a", }, { - Name: "b", + Name: "b", + FullName: "a.b.b", }, }, }, @@ -68,20 +73,22 @@ func TestTablesDataIssue1264(t *testing.T) { require.NoError(t, err) s := d.Get("ids").(*schema.Set) assert.Equal(t, 2, s.Len()) - assert.True(t, s.Contains("a")) + assert.True(t, s.Contains("a.b.a")) d, err = qa.ResourceFixture{ Fixtures: []qa.HTTPFixture{ { Method: "GET", Resource: "/api/2.1/unity-catalog/tables?catalog_name=a&schema_name=b", - Response: Tables{ - Tables: []TableInfo{ + Response: unitycatalog.ListTablesResponse{ + Tables: []unitycatalog.TableInfo{ { - Name: "c", + Name: "c", + FullName: "a.b.c", }, { - Name: "d", + Name: "d", + FullName: "a.b.d", }, }, }, @@ -98,7 +105,7 @@ func TestTablesDataIssue1264(t *testing.T) { require.NoError(t, err) s = d.Get("ids").(*schema.Set) assert.Equal(t, 2, s.Len()) - assert.True(t, s.Contains("c")) + assert.True(t, s.Contains("a.b.c")) } func TestTablesData_Error(t *testing.T) { diff --git a/internal/acceptance/data_tables_test.go b/internal/acceptance/data_tables_test.go new file mode 100644 index 0000000000..29adbe1524 --- /dev/null +++ b/internal/acceptance/data_tables_test.go @@ -0,0 +1,87 @@ +package acceptance + +import ( + "strconv" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func checkTablesDataSourcePopulated(t *testing.T) func(s *terraform.State) error { + return func(s *terraform.State) error { + _, ok := s.Modules[0].Resources["data.databricks_tables.this"] + require.True(t, ok, "data.databricks_shares.this has to be there") + num_tables, _ := strconv.Atoi(s.Modules[0].Outputs["tables"].Value.(string)) + assert.Equal(t, num_tables, 2) + return nil + } +} +func TestUcAccDataSourceTables(t *testing.T) { + unityWorkspaceLevel(t, step{ + Template: ` + resource "databricks_catalog" "sandbox" { + name = "sandbox{var.RANDOM}" + comment = "this catalog is managed by terraform" + properties = { + purpose = "testing" + } + } + + resource "databricks_schema" "things" { + catalog_name = databricks_catalog.sandbox.id + name = "things{var.RANDOM}" + comment = "this database is managed by terraform" + properties = { + kind = "various" + } + } + + resource "databricks_table" "mytable" { + catalog_name = databricks_catalog.sandbox.id + schema_name = databricks_schema.things.name + name = "bar" + table_type = "MANAGED" + data_source_format = "DELTA" + + column { + name = "id" + position = 0 + type_name = "INT" + type_text = "int" + type_json = "{\"name\":\"id\",\"type\":\"integer\",\"nullable\":true,\"metadata\":{}}" + } + } + + resource "databricks_table" "mytable_2" { + catalog_name = databricks_catalog.sandbox.id + schema_name = databricks_schema.things.name + name = "bar_2" + table_type = "MANAGED" + data_source_format = "DELTA" + + column { + name = "id" + position = 0 + type_name = "INT" + type_text = "int" + type_json = "{\"name\":\"id\",\"type\":\"integer\",\"nullable\":true,\"metadata\":{}}" + } + } + + data "databricks_tables" "this" { + catalog_name = databricks_catalog.sandbox.id + schema_name = databricks_schema.things.name + depends_on = [ + databricks_table.mytable, + databricks_table.mytable_2 + ] + } + output "tables" { + value = length(data.databricks_tables.this.ids) + } + `, + Check: checkTablesDataSourcePopulated(t), + }) +} From d799ee4209cbd770b108842fc7463bb5b874bba8 Mon Sep 17 00:00:00 2001 From: Vuong Nguyen Date: Mon, 6 Mar 2023 20:13:36 +0000 Subject: [PATCH 2/2] add more assertions --- internal/acceptance/data_tables_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/acceptance/data_tables_test.go b/internal/acceptance/data_tables_test.go index 29adbe1524..3e8069b961 100644 --- a/internal/acceptance/data_tables_test.go +++ b/internal/acceptance/data_tables_test.go @@ -11,8 +11,14 @@ import ( func checkTablesDataSourcePopulated(t *testing.T) func(s *terraform.State) error { return func(s *terraform.State) error { - _, ok := s.Modules[0].Resources["data.databricks_tables.this"] + r, ok := s.Modules[0].Resources["data.databricks_tables.this"] require.True(t, ok, "data.databricks_shares.this has to be there") + + attr := r.Primary.Attributes + + assert.Equal(t, s.Modules[0].Resources["databricks_table.mytable"].Primary.ID, attr["ids.0"]) + assert.Equal(t, s.Modules[0].Resources["databricks_table.mytable_2"].Primary.ID, attr["ids.1"]) + num_tables, _ := strconv.Atoi(s.Modules[0].Outputs["tables"].Value.(string)) assert.Equal(t, num_tables, 2) return nil