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 acc test for databricks_tables data source #2075

Merged
merged 2 commits into from
Mar 7, 2023
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: 1 addition & 1 deletion catalog/data_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're changing the expected output - customers expect name instead of full name for tables data

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because #2068 changed FullName -> Name, this fixes it by changing it back 😅

}
}
return nil
Expand Down
35 changes: 21 additions & 14 deletions catalog/data_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
},
},
},
Expand All @@ -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",
},
},
},
Expand All @@ -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",
},
},
},
Expand All @@ -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) {
Expand Down
93 changes: 93 additions & 0 deletions internal/acceptance/data_tables_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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 {
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
}
}
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),
})
}