Skip to content

Commit

Permalink
refactor: run tests with PostgreSQL as well
Browse files Browse the repository at this point in the history
Fix #198.
  • Loading branch information
bfabio committed Jun 17, 2023
1 parent a073df9 commit d4f4a12
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 102 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/tests-postgresql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
tests-postgresql:
runs-on: ubuntu-latest

services:
db:
image: postgres:14
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: 1.18.x
- run: go test -race ./...
env:
DATABASE_DSN: "postgres://postgres:postgres@localhost:5432/test?sslmode=disable"
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ on:
branches: [main]

jobs:
tests:
tests-sqlite:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: 1.18.x
- run: go test -race ./...
env:
DATABASE_DSN: "file:/tmp/test.db"
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/gofiber/adaptor/v2 v2.1.25 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/lib/pq v1.10.9
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/mattn/go-sqlite3 v1.14.17
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/prometheus/client_golang v1.12.2 // indirect
Expand Down
52 changes: 39 additions & 13 deletions internal/database/database.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,55 @@
package database

import (
"fmt"
"log"
"strings"

"github.com/italia/developers-italia-api/internal/common"
"github.com/italia/developers-italia-api/internal/models"

"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)

type Database interface {
Init(dsn string) (*gorm.DB, error)
}
func NewDatabase(connection string) (*gorm.DB, error) {
var (
database *gorm.DB
err error
)

//nolintlint:ireturn
func NewDatabase(env common.Environment) Database {
if env.IsTest() {
switch {
case strings.HasPrefix(connection, "file:"):
log.Println("using SQLite database")

return &SQLiteDB{
dsn: env.Database,
}
database, err = gorm.Open(sqlite.Open(connection), &gorm.Config{TranslateError: true})
case strings.HasPrefix(connection, "postgres:"):
log.Println("using Postgres database")

database, err = gorm.Open(postgres.Open(connection), &gorm.Config{
TranslateError: true,
PrepareStmt: true,
// Disable logging in production
Logger: logger.Default.LogMode(logger.Silent),
})
}

log.Println("using Postgres database")
if err != nil {
return nil, fmt.Errorf("can't open database: %w", err)
}

return &PostgresDB{
dsn: env.Database,
if err = database.AutoMigrate(
&models.Publisher{},
&models.Event{},
&models.CodeHosting{},
&models.Log{},
&models.Software{},
&models.SoftwareURL{},
&models.Webhook{},
); err != nil {
return nil, fmt.Errorf("can't migrate database: %w", err)
}

return database, nil
}
42 changes: 0 additions & 42 deletions internal/database/postgres_database.go

This file was deleted.

36 changes: 0 additions & 36 deletions internal/database/sqlite_database.go

This file was deleted.

4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ func Setup() *fiber.App {
panic(err)
}

db := database.NewDatabase(common.EnvironmentConfig)

gormDB, err := db.Init(common.EnvironmentConfig.Database)
gormDB, err := database.NewDatabase(common.EnvironmentConfig.Database)
if err != nil {
panic(err)
}
Expand Down
27 changes: 22 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ import (
"github.com/go-testfixtures/testfixtures/v3"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"

_ "github.com/mattn/go-sqlite3"
_ "github.com/lib/pq"
)

const UUID_REGEXP = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"

var (
app *fiber.App
db *sql.DB
dbDriver string
goodToken = "Bearer v2.local.TwwHUQEi8hr2Eo881_Bs5vK9dHOR5BgEU24QRf-U7VmUwI1yOEA6mFT0EsXioMkFT_T-jjrtIJ_Nv8f6hR6ifJXUOuzWEkm9Ijq1mqSjQatD3aDqKMyjjBA"
badToken = "Bearer v2.local.UngfrCDNwGUw4pff2oBNoyxYvOErcbVVqLndl6nzONafUCzktaOeMSmoI7B0h62zoxXXLqTm_Phl"
)
Expand All @@ -43,23 +47,36 @@ type TestCase struct {
}

func init() {
_ = os.Remove("./test.db")
// Test on SQLite by default if DATABASE_DSN is not set
if _, exists := os.LookupEnv("DATABASE_DSN"); !exists {
_ = os.Setenv("DATABASE_DSN", "file:./test.db")
_ = os.Remove("./test.db")
}

_ = os.Setenv("DATABASE_DSN", "file:./test.db")
_ = os.Setenv("ENVIRONMENT", "test")

// echo -n 'test-paseto-key-dont-use-in-prod' | base64
_ = os.Setenv("PASETO_KEY", "dGVzdC1wYXNldG8ta2V5LWRvbnQtdXNlLWluLXByb2Q=")

dsn := os.Getenv("DATABASE_DSN")
switch {
case strings.HasPrefix(dsn, "postgres:"):
dbDriver = "postgres"
default:
dbDriver = "sqlite3"
}

var err error
db, err = sql.Open("sqlite3", os.Getenv("DATABASE_DSN"))
db, err = sql.Open(dbDriver, dsn)
if err != nil {
log.Fatal(err)
}

// This is needed, otherwise we get a database-locked error
// TODO: investigate the root cause
_, _ = db.Exec("PRAGMA journal_mode=WAL;")
if dbDriver == "sqlite3" {
_, _ = db.Exec("PRAGMA journal_mode=WAL;")
}

// Setup the app as it is done in the main function
app = Setup()
Expand All @@ -74,7 +91,7 @@ func TestMain(m *testing.M) {
func loadFixtures(t *testing.T) {
fixtures, err := testfixtures.New(
testfixtures.Database(db),
testfixtures.Dialect("sqlite"),
testfixtures.Dialect(dbDriver),
testfixtures.Directory("test/testdata/fixtures/"),
)
assert.Nil(t, err)
Expand Down

0 comments on commit d4f4a12

Please sign in to comment.