Skip to content

Commit

Permalink
Use isolated database for forms tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsapps committed Jan 7, 2025
1 parent e2331ea commit 004c388
Showing 1 changed file with 57 additions and 8 deletions.
65 changes: 57 additions & 8 deletions server/src/form_processor/process_forms_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package form_processor

import (
"database/sql"
"log"
"os"
"testing"

"github.com/dxe/adb/config"
"github.com/dxe/adb/model"
"github.com/jmoiron/sqlx"
)
Expand Down Expand Up @@ -40,7 +44,7 @@ func verifyFormWasMarkedAsProcessed(t *testing.T, db *sqlx.DB, query string) {
/* Form application tests */
func TestProcessFormApplicationForNoMatchingActivist(t *testing.T) {
/* Set up */
db := model.NewTestDB()
db := useTempDb()
defer db.Close()
_, err := db.Query(insertIntoFormApplicationQuery)
if err != nil {
Expand All @@ -56,7 +60,7 @@ func TestProcessFormApplicationForNoMatchingActivist(t *testing.T) {

func TestProcessFormApplicationForActivistMatchingOnName(t *testing.T) {
/* Set up */
db := model.NewTestDB()
db := useTempDb()
defer db.Close()

_, err := db.Exec(insertActivistQuery, "name1")
Expand All @@ -77,7 +81,7 @@ func TestProcessFormApplicationForActivistMatchingOnName(t *testing.T) {

func TestProcessFormApplicationForActivistMatchingOnEmail(t *testing.T) {
/* Set up */
db := model.NewTestDB()
db := useTempDb()
defer db.Close()

_, err := db.Exec(insertActivistQuery, "non-matching_name")
Expand All @@ -98,7 +102,7 @@ func TestProcessFormApplicationForActivistMatchingOnEmail(t *testing.T) {

func TestProcessFormApplicationForMultipleMatchingActivistsOnEmail(t *testing.T) {
/* Set up */
db := model.NewTestDB()
db := useTempDb()
defer db.Close()
_, err := db.Exec(insertActivistQuery, "non-matching_name1")
if err != nil {
Expand All @@ -122,7 +126,7 @@ func TestProcessFormApplicationForMultipleMatchingActivistsOnEmail(t *testing.T)
/* Form interest tests */
func TestProcessFormInterestForNoMatchingActivist(t *testing.T) {
/* Set up */
db := model.NewTestDB()
db := useTempDb()
defer db.Close()
_, err := db.Exec(insertIntoFormInterestQuery)
if err != nil {
Expand All @@ -138,7 +142,7 @@ func TestProcessFormInterestForNoMatchingActivist(t *testing.T) {

func TestProcessFormInterestForActivistMatchingOnName(t *testing.T) {
/* Set up */
db := model.NewTestDB()
db := useTempDb()
defer db.Close()
_, err := db.Exec(insertActivistQuery, "name1")
if err != nil {
Expand All @@ -158,7 +162,7 @@ func TestProcessFormInterestForActivistMatchingOnName(t *testing.T) {

func TestProcessFormInterestForActivistMatchingOnEmail(t *testing.T) {
/* Set up */
db := model.NewTestDB()
db := useTempDb()
defer db.Close()
_, err := db.Exec(insertActivistQuery, "non-matching_name")
if err != nil {
Expand All @@ -178,7 +182,7 @@ func TestProcessFormInterestForActivistMatchingOnEmail(t *testing.T) {

func TestProcessFormInterestForMultipleMatchingActivistsOnEmail(t *testing.T) {
/* Set up */
db := model.NewTestDB()
db := useTempDb()
defer db.Close()
_, err := db.Exec(insertActivistQuery, "non-matching_name1")
if err != nil {
Expand All @@ -198,3 +202,48 @@ func TestProcessFormInterestForMultipleMatchingActivistsOnEmail(t *testing.T) {

// For now, manually check error message "ERROR: 2 non-hidden activists associated"
}

// Do not use same DB as used by `models` package because go tests in different
// packages run in parallel.
const DB_NAME = "ADB_TEST_FORMS"

func createTempDb(name string) {
db, err := sql.Open("mysql", config.DataSourceBase+"/")
if err != nil {
log.Fatalf("Error connecting to database: %v", err)
}
defer db.Close()

// Create the database
createDBQuery := "CREATE DATABASE IF NOT EXISTS " + name
_, err = db.Exec(createDBQuery)
if err != nil {
log.Fatalf("Error creating database: %v", err)
}
}
func dropTempDb(name string) {
db, err := sql.Open("mysql", config.DataSourceBase+"/")
if err != nil {
log.Fatalf("Error connecting to database: %v", err)
}
defer db.Close()

// Create the database
createDBQuery := "DROP DATABASE " + name
_, err = db.Exec(createDBQuery)
if err != nil {
log.Fatalf("Error creating database: %v", err)
}
}
func useTempDb() *sqlx.DB {
db := model.NewDB(config.DataSourceBase + "/" + DB_NAME + "?parseTime=true")
model.WipeDatabase(db)
return db
}

func TestMain(m *testing.M) {
createTempDb(DB_NAME)
defer dropTempDb(DB_NAME)

os.Exit(m.Run())
}

0 comments on commit 004c388

Please sign in to comment.