Files
query-orchestration/internal/database/config_test.go
T
Michael McGuinness 57764272c3 Merged in feature/unittests (pull request #9)
Feature/unittests

* addedmoretests

* easyquerytests

* fixotelinit

* preppedqueriestotestcontainer

* dbqueries

* splitintoqueryfiles

* covered the bases
2025-01-08 18:14:15 +00:00

85 lines
2.2 KiB
Go

package database
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMustGetConnectionString(t *testing.T) {
t.Setenv("DB_USER", "user")
t.Setenv("DB_PASS", "pass")
t.Setenv("DB_HOST", "host")
t.Setenv("DB_PORT", "port")
t.Setenv("DB_NAME", "name")
connStr := mustGetConnectionString()
assert.Equal(t, "postgres://user:pass@host:port/name?", connStr)
}
func TestMustGetConnectionStringNoSSL(t *testing.T) {
t.Setenv("DB_USER", "user")
t.Setenv("DB_PASS", "pass")
t.Setenv("DB_HOST", "host")
t.Setenv("DB_PORT", "port")
t.Setenv("DB_NAME", "name")
t.Setenv("DB_NOSSL", "1")
connStr := mustGetConnectionString()
assert.Equal(t, "postgres://user:pass@host:port/name?sslmode=disable", connStr)
}
func TestMustGetConnectionStringNoEnv(t *testing.T) {
t.Setenv("DB_USER", "")
t.Setenv("DB_PASS", "")
t.Setenv("DB_HOST", "")
t.Setenv("DB_PORT", "")
t.Setenv("DB_NAME", "")
assert.Panics(t, func() { mustGetConnectionString() })
}
func TestGetPoolConfig(t *testing.T) {
t.Setenv("DB_USER", "user")
t.Setenv("DB_PASS", "pass")
t.Setenv("DB_HOST", "host")
t.Setenv("DB_PORT", "5432")
t.Setenv("DB_NAME", "name")
config, err := getPoolConfig()
assert.Nil(t, err)
assert.Equal(t, config.ConnString(), mustGetConnectionString())
}
func TestGetPoolConfigInvalidPort(t *testing.T) {
t.Setenv("DB_USER", "user")
t.Setenv("DB_PASS", "pass")
t.Setenv("DB_HOST", "host")
t.Setenv("DB_PORT", "invalid_port")
t.Setenv("DB_NAME", "name")
_, err := getPoolConfig()
assert.EqualError(t, err, "cannot parse `postgres://user:xxxxxx@host:invalid_port/name?`: failed to parse as URL (invalid port \":invalid_port\" after host)")
}
func TestMustGetPoolConfig(t *testing.T) {
t.Setenv("DB_USER", "user")
t.Setenv("DB_PASS", "pass")
t.Setenv("DB_HOST", "host")
t.Setenv("DB_PORT", "5432")
t.Setenv("DB_NAME", "name")
config := mustGetPoolConfig()
assert.Equal(t, config.ConnString(), mustGetConnectionString())
}
func TestMustGetPoolConfigInvalidPort(t *testing.T) {
t.Setenv("DB_USER", "user")
t.Setenv("DB_PASS", "pass")
t.Setenv("DB_HOST", "host")
t.Setenv("DB_PORT", "invalid_port")
t.Setenv("DB_NAME", "name")
assert.Panics(t, func() { mustGetPoolConfig() })
}