Skip to content

Commit

Permalink
fix: failing tests
Browse files Browse the repository at this point in the history
Signed-off-by: Azanul <azanulhaque@gmail.com>
  • Loading branch information
Azanul committed Jun 8, 2024
1 parent 10e6425 commit 2fe650e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
35 changes: 29 additions & 6 deletions notification/internal/repository/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package postgres
import (
"context"
"database/sql"
"log"
"os"
"strings"
"testing"

_ "github.com/lib/pq"
Expand All @@ -27,17 +27,40 @@ func setupTestDB() (*sql.DB, error) {
}

sql := string(sqlBytes)
_, err = db.Exec(sql)
if err != nil {
return nil, err
statements := strings.Split(sql, ";")
for _, statement := range statements {
if strings.TrimSpace(statement) == "" {
continue
}
_, err := db.Exec(statement)
if err != nil && !strings.Contains(err.Error(), "already exists") {
return nil, err
}
}

return db, nil
}

func teardownTestDB(db *sql.DB) {
func teardownTestDB(db *sql.DB) error {
// Close the database connection
if err := db.Close(); err != nil {
log.Fatalf("Error closing test database: %v\n", err)
return err
}

// Reopen the database connection to perform cleanup operations
db, err := sql.Open("postgres", "postgresql://postgres:postgres@localhost:5432/postgres?sslmode=disable")
if err != nil {
return err
}
defer db.Close()

// Execute SQL statements to clear the database
_, err = db.Exec("DROP SCHEMA public CASCADE; CREATE SCHEMA public;")
if err != nil {
return err
}

return nil
}

func TestRepository(t *testing.T) {
Expand Down
40 changes: 37 additions & 3 deletions user/internal/repository/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package postgres
import (
"context"
"database/sql"
"log"
"os"
"strings"
"testing"

_ "github.com/lib/pq"
Expand All @@ -19,13 +20,46 @@ func setupTestDB() (*sql.DB, error) {
if err := db.Ping(); err != nil {
return nil, err
}
sqlBytes, err := os.ReadFile("../../../pkg/model/user.sql")
if err != nil {
return nil, err
}

sql := string(sqlBytes)
statements := strings.Split(sql, ";")
for _, statement := range statements {
if strings.TrimSpace(statement) == "" {
continue
}
_, err := db.Exec(statement)
if err != nil && !strings.Contains(err.Error(), "already exists") {
return nil, err
}
}

return db, nil
}

func teardownTestDB(db *sql.DB) {
func teardownTestDB(db *sql.DB) error {
// Close the database connection
if err := db.Close(); err != nil {
log.Fatalf("Error closing test database: %v\n", err)
return err
}

// Reopen the database connection to perform cleanup operations
db, err := sql.Open("postgres", "postgresql://postgres:postgres@localhost:5432/postgres?sslmode=disable")
if err != nil {
return err
}
defer db.Close()

// Execute SQL statements to clear the database
_, err = db.Exec("DROP SCHEMA public CASCADE; CREATE SCHEMA public;")
if err != nil {
return err
}

return nil
}

func TestUserRepository(t *testing.T) {
Expand Down

0 comments on commit 2fe650e

Please sign in to comment.