Skip to content

Commit b00f211

Browse files
add a test
1 parent cfbe786 commit b00f211

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

postgresql/resource_postgresql_script_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package postgresql
22

33
import (
4+
"fmt"
45
"regexp"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
810
)
911

1012
func TestAccPostgresqlScript_basic(t *testing.T) {
@@ -227,3 +229,85 @@ func TestAccPostgresqlScript_timeout(t *testing.T) {
227229
},
228230
})
229231
}
232+
233+
func TestAccPostgresqlScript_withDatabase(t *testing.T) {
234+
config := `
235+
resource "postgresql_database" "test_db" {
236+
name = "test_script_db"
237+
}
238+
239+
resource "postgresql_script" "test" {
240+
database = postgresql_database.test_db.name
241+
commands = [
242+
"CREATE TABLE test_table (id INT);",
243+
"INSERT INTO test_table VALUES (1);"
244+
]
245+
depends_on = [postgresql_database.test_db]
246+
}
247+
`
248+
249+
resource.Test(t, resource.TestCase{
250+
PreCheck: func() { testAccPreCheck(t) },
251+
Providers: testAccProviders,
252+
Steps: []resource.TestStep{
253+
{
254+
Config: config,
255+
Check: resource.ComposeTestCheckFunc(
256+
resource.TestCheckResourceAttr("postgresql_script.test", "database", "test_script_db"),
257+
resource.TestCheckResourceAttr("postgresql_script.test", "commands.0", "CREATE TABLE test_table (id INT);"),
258+
resource.TestCheckResourceAttr("postgresql_script.test", "commands.1", "INSERT INTO test_table VALUES (1);"),
259+
testAccCheckTableExistsInDatabase("test_script_db", "test_table"),
260+
testAccCheckTableHasRecords("test_script_db", "test_table", 1),
261+
),
262+
},
263+
},
264+
})
265+
}
266+
267+
func testAccCheckTableExistsInDatabase(dbName, tableName string) resource.TestCheckFunc {
268+
return func(s *terraform.State) error {
269+
client := testAccProvider.Meta().(*Client)
270+
dbClient := client.config.NewClient(dbName)
271+
db, err := dbClient.Connect()
272+
if err != nil {
273+
return fmt.Errorf("Error connecting to database %s: %s", dbName, err)
274+
}
275+
276+
var exists bool
277+
query := "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = $1)"
278+
err = db.QueryRow(query, tableName).Scan(&exists)
279+
if err != nil {
280+
return fmt.Errorf("Error checking if table %s exists: %s", tableName, err)
281+
}
282+
283+
if !exists {
284+
return fmt.Errorf("Table %s does not exist in database %s", tableName, dbName)
285+
}
286+
287+
return nil
288+
}
289+
}
290+
291+
func testAccCheckTableHasRecords(dbName, tableName string, expectedCount int) resource.TestCheckFunc {
292+
return func(s *terraform.State) error {
293+
client := testAccProvider.Meta().(*Client)
294+
dbClient := client.config.NewClient(dbName)
295+
db, err := dbClient.Connect()
296+
if err != nil {
297+
return fmt.Errorf("Error connecting to database %s: %s", dbName, err)
298+
}
299+
300+
var count int
301+
query := fmt.Sprintf("SELECT COUNT(*) FROM %s", tableName)
302+
err = db.QueryRow(query).Scan(&count)
303+
if err != nil {
304+
return fmt.Errorf("Error counting records in table %s: %s", tableName, err)
305+
}
306+
307+
if count != expectedCount {
308+
return fmt.Errorf("Expected %d records but got %d in table %s", expectedCount, count, tableName)
309+
}
310+
311+
return nil
312+
}
313+
}

0 commit comments

Comments
 (0)