Skip to content

Commit 200bbb9

Browse files
authored
Add acc test for databricks_tables data source (#2075)
* Add acc test for `databricks_tables` data source * add more assertions
1 parent bd05be2 commit 200bbb9

File tree

3 files changed

+115
-15
lines changed

3 files changed

+115
-15
lines changed

catalog/data_tables.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func DataSourceTables() *schema.Resource {
2121
}
2222
for _, v := range tables {
2323
if v.TableType != "VIEW" {
24-
data.Ids = append(data.Ids, v.Name)
24+
data.Ids = append(data.Ids, v.FullName)
2525
}
2626
}
2727
return nil

catalog/data_tables_test.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package catalog
33
import (
44
"testing"
55

6+
"github.com/databricks/databricks-sdk-go/service/unitycatalog"
67
"github.com/databricks/terraform-provider-databricks/qa"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
89
"github.com/stretchr/testify/assert"
@@ -15,13 +16,15 @@ func TestTablesData(t *testing.T) {
1516
{
1617
Method: "GET",
1718
Resource: "/api/2.1/unity-catalog/tables?catalog_name=a&schema_name=b",
18-
Response: Tables{
19-
Tables: []TableInfo{
19+
Response: unitycatalog.ListTablesResponse{
20+
Tables: []unitycatalog.TableInfo{
2021
{
21-
Name: "a.b.c",
22+
FullName: "a.b.c",
23+
Name: "c",
2224
},
2325
{
24-
Name: "a.b.d",
26+
FullName: "a.b.d",
27+
Name: "d",
2528
},
2629
},
2730
},
@@ -45,13 +48,15 @@ func TestTablesDataIssue1264(t *testing.T) {
4548
{
4649
Method: "GET",
4750
Resource: "/api/2.1/unity-catalog/tables?catalog_name=a&schema_name=b",
48-
Response: Tables{
49-
Tables: []TableInfo{
51+
Response: unitycatalog.ListTablesResponse{
52+
Tables: []unitycatalog.TableInfo{
5053
{
51-
Name: "a",
54+
Name: "a",
55+
FullName: "a.b.a",
5256
},
5357
{
54-
Name: "b",
58+
Name: "b",
59+
FullName: "a.b.b",
5560
},
5661
},
5762
},
@@ -68,20 +73,22 @@ func TestTablesDataIssue1264(t *testing.T) {
6873
require.NoError(t, err)
6974
s := d.Get("ids").(*schema.Set)
7075
assert.Equal(t, 2, s.Len())
71-
assert.True(t, s.Contains("a"))
76+
assert.True(t, s.Contains("a.b.a"))
7277

7378
d, err = qa.ResourceFixture{
7479
Fixtures: []qa.HTTPFixture{
7580
{
7681
Method: "GET",
7782
Resource: "/api/2.1/unity-catalog/tables?catalog_name=a&schema_name=b",
78-
Response: Tables{
79-
Tables: []TableInfo{
83+
Response: unitycatalog.ListTablesResponse{
84+
Tables: []unitycatalog.TableInfo{
8085
{
81-
Name: "c",
86+
Name: "c",
87+
FullName: "a.b.c",
8288
},
8389
{
84-
Name: "d",
90+
Name: "d",
91+
FullName: "a.b.d",
8592
},
8693
},
8794
},
@@ -98,7 +105,7 @@ func TestTablesDataIssue1264(t *testing.T) {
98105
require.NoError(t, err)
99106
s = d.Get("ids").(*schema.Set)
100107
assert.Equal(t, 2, s.Len())
101-
assert.True(t, s.Contains("c"))
108+
assert.True(t, s.Contains("a.b.c"))
102109
}
103110

104111
func TestTablesData_Error(t *testing.T) {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package acceptance
2+
3+
import (
4+
"strconv"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func checkTablesDataSourcePopulated(t *testing.T) func(s *terraform.State) error {
13+
return func(s *terraform.State) error {
14+
r, ok := s.Modules[0].Resources["data.databricks_tables.this"]
15+
require.True(t, ok, "data.databricks_shares.this has to be there")
16+
17+
attr := r.Primary.Attributes
18+
19+
assert.Equal(t, s.Modules[0].Resources["databricks_table.mytable"].Primary.ID, attr["ids.0"])
20+
assert.Equal(t, s.Modules[0].Resources["databricks_table.mytable_2"].Primary.ID, attr["ids.1"])
21+
22+
num_tables, _ := strconv.Atoi(s.Modules[0].Outputs["tables"].Value.(string))
23+
assert.Equal(t, num_tables, 2)
24+
return nil
25+
}
26+
}
27+
func TestUcAccDataSourceTables(t *testing.T) {
28+
unityWorkspaceLevel(t, step{
29+
Template: `
30+
resource "databricks_catalog" "sandbox" {
31+
name = "sandbox{var.RANDOM}"
32+
comment = "this catalog is managed by terraform"
33+
properties = {
34+
purpose = "testing"
35+
}
36+
}
37+
38+
resource "databricks_schema" "things" {
39+
catalog_name = databricks_catalog.sandbox.id
40+
name = "things{var.RANDOM}"
41+
comment = "this database is managed by terraform"
42+
properties = {
43+
kind = "various"
44+
}
45+
}
46+
47+
resource "databricks_table" "mytable" {
48+
catalog_name = databricks_catalog.sandbox.id
49+
schema_name = databricks_schema.things.name
50+
name = "bar"
51+
table_type = "MANAGED"
52+
data_source_format = "DELTA"
53+
54+
column {
55+
name = "id"
56+
position = 0
57+
type_name = "INT"
58+
type_text = "int"
59+
type_json = "{\"name\":\"id\",\"type\":\"integer\",\"nullable\":true,\"metadata\":{}}"
60+
}
61+
}
62+
63+
resource "databricks_table" "mytable_2" {
64+
catalog_name = databricks_catalog.sandbox.id
65+
schema_name = databricks_schema.things.name
66+
name = "bar_2"
67+
table_type = "MANAGED"
68+
data_source_format = "DELTA"
69+
70+
column {
71+
name = "id"
72+
position = 0
73+
type_name = "INT"
74+
type_text = "int"
75+
type_json = "{\"name\":\"id\",\"type\":\"integer\",\"nullable\":true,\"metadata\":{}}"
76+
}
77+
}
78+
79+
data "databricks_tables" "this" {
80+
catalog_name = databricks_catalog.sandbox.id
81+
schema_name = databricks_schema.things.name
82+
depends_on = [
83+
databricks_table.mytable,
84+
databricks_table.mytable_2
85+
]
86+
}
87+
output "tables" {
88+
value = length(data.databricks_tables.this.ids)
89+
}
90+
`,
91+
Check: checkTablesDataSourcePopulated(t),
92+
})
93+
}

0 commit comments

Comments
 (0)