|
| 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