Skip to content

Commit

Permalink
Add acc test for databricks_tables data source (#2075)
Browse files Browse the repository at this point in the history
* Add acc test for `databricks_tables` data source

* add more assertions
  • Loading branch information
nkvuong authored Mar 7, 2023
1 parent bd05be2 commit 200bbb9
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 15 deletions.
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)
}
}
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),
})
}

0 comments on commit 200bbb9

Please sign in to comment.