Skip to content

Commit

Permalink
Add production database connectoin test
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Aug 5, 2024
1 parent 0b1d362 commit e591ea7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 22 deletions.
1 change: 1 addition & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/go-playground/validator/v10 v10.22.0
github.com/gorilla/websocket v1.5.0
github.com/graph-gophers/dataloader/v7 v7.1.0
github.com/joho/godotenv v1.5.1
github.com/labstack/echo/v4 v4.12.0
github.com/labstack/gommon v0.4.2
github.com/m-mizutani/goerr v0.1.14
Expand Down
2 changes: 2 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.16.0 h1:iULayQNOReoYUe+1qtKOqw9CwJv3aNQu8ivo7lw1HU4=
Expand Down
13 changes: 12 additions & 1 deletion backend/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package main

import (
"backend/pkg/config"
"backend/pkg/repository"
"backend/web/server"
)

var migrationFilePath = "./db/migrations"

func main() {
server.StartServer(migrationFilePath)
dbConfig := repository.DBConfig{
Host: config.Cfg.PGHost,
User: config.Cfg.PGUser,
Password: config.Cfg.PGPassword,
DBName: config.Cfg.PGDBName,
Port: config.Cfg.PGPort,
SSLMode: config.Cfg.PGSSLMode,
MigrationFilePath: migrationFilePath,
}
server.StartServer(dbConfig)
}
29 changes: 19 additions & 10 deletions backend/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,31 @@ import (
"backend/web/server"
"context"
"fmt"
"github.com/caarlos0/env/v11"
"github.com/joho/godotenv"
"github.com/m-mizutani/goerr"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"log"
)

func init() {
// Load .env file
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}

// Parse environment variables into the config.Cfg
if err := env.Parse(&config.Cfg); err != nil {
log.Fatalf("Failed to parse environment variables: %+v", err)
}
}

func setupTestDB(t *testing.T) (*httptest.Server, func()) {
t.Helper()

Expand All @@ -34,15 +50,6 @@ func setupTestDB(t *testing.T) (*httptest.Server, func()) {
t.Fatalf("failed to run migrations: %+v", err)
}

// Override the config values for testing
config.Cfg.PGHost = pg.GetConfig().Host
config.Cfg.PGUser = pg.GetConfig().User
config.Cfg.PGPassword = pg.GetConfig().Password
config.Cfg.PGDBName = pg.GetConfig().DBName
config.Cfg.PGPort = pg.GetConfig().Port
config.Cfg.PGSSLMode = "disable"
config.Cfg.Port = 8080

// Initialize the Echo router using the NewRouter function
e := server.NewRouter(pg.GetDB())

Expand All @@ -64,7 +71,7 @@ func setupProdDB() (*gorm.DB, error) {
config.Cfg.PGSSLMode)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
return nil, err
return nil, goerr.Wrap(err)
}
return db, nil
}
Expand Down Expand Up @@ -103,6 +110,8 @@ func TestMainSmoke(t *testing.T) {
})

t.Run("Test with Production Database", func(t *testing.T) {
// Comment out this when you want to do a smoke test against production database.
t.SkipNow()
ts, cleanup := setupProdServer(t)
defer cleanup()

Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Config struct {
PGPassword string `env:"PG_PASSWORD" envDefault:"testpassword"`
PGDBName string `env:"PG_DBNAME" envDefault:"flamingodb"`
PGPort string `env:"PG_PORT" envDefault:"5432"`
PGSSLMode string `env:"PG_SSLMODE" envDefault:"disable"`
PGSSLMode string `env:"PG_SSLMODE" envDefault:"allow"`
}

// Cfg is the package-level variable that holds the parsed configuration
Expand Down
20 changes: 10 additions & 10 deletions backend/web/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ func NewRouter(db *gorm.DB) *echo.Echo {
return e
}

func StartServer(migrationFilePath string) {
func StartServer(dbConfig repository.DBConfig) {
// Initialize the database
dbConfig := repository.DBConfig{
Host: config.Cfg.PGHost,
User: config.Cfg.PGUser,
Password: config.Cfg.PGPassword,
DBName: config.Cfg.PGDBName,
Port: config.Cfg.PGPort,
SSLMode: config.Cfg.PGSSLMode,
MigrationFilePath: migrationFilePath,
}
//dbConfig := repository.DBConfig{
// Host: config.Cfg.PGHost,
// User: config.Cfg.PGUser,
// Password: config.Cfg.PGPassword,
// DBName: config.Cfg.PGDBName,
// Port: config.Cfg.PGPort,
// SSLMode: config.Cfg.PGSSLMode,
// MigrationFilePath: migrationFilePath,
//}
db := repository.InitializeDatabase(dbConfig)

router := NewRouter(db)
Expand Down

0 comments on commit e591ea7

Please sign in to comment.