Skip to content

Commit

Permalink
test: config ✅
Browse files Browse the repository at this point in the history
  • Loading branch information
kareemmahlees committed Apr 11, 2024
1 parent e59299f commit 0e13bbc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
9 changes: 4 additions & 5 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/go-sql-driver/mysql"
"github.com/lib/pq"
)

type Config interface {
Expand Down Expand Up @@ -42,17 +41,17 @@ func NewPGConfig(connUrl *string, pgConnParams *PgConnectionParams) *PgConfig {
}

func (pc PgConfig) DSN() string {
if *pc.ConnUrl != "" {
if pc.ConnUrl != nil {
return *pc.ConnUrl
} else {
cfg, _ := pq.ParseURL(fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s",
cfg := fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s",
pc.ConnParams.DBUsername,
pc.ConnParams.DBPassword,
pc.ConnParams.DBHost,
pc.ConnParams.DBPort,
pc.ConnParams.DBName,
pc.ConnParams.DBSslMode,
))
)
return cfg
}
}
Expand All @@ -75,7 +74,7 @@ func NewMySQLConfig(connUrl *string, pgConnParams *MySQLConnectionParams) *MySQL
}

func (mc *MySQLConfig) DSN() string {
if *mc.ConnUrl != "" {
if mc.ConnUrl != nil {
return *mc.ConnUrl
} else {
cfg := mysql.Config{
Expand Down
48 changes: 48 additions & 0 deletions utils/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package utils

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSQLiteConfig(t *testing.T) {
filePath := "any"
conf := NewSQLiteConfig(filePath)

assert.Equal(t, conf.DSN(), filePath)
}

func TestPostgresConfig(t *testing.T) {
connUrl := "any"

conf := NewPGConfig(&connUrl, nil)
assert.Equal(t, conf.DSN(), connUrl)

conf = NewPGConfig(nil, &PgConnectionParams{
DBUsername: "test",
DBPassword: "test",
DBHost: "localhost",
DBPort: 5432,
DBName: "test",
})

assert.Regexp(t, `postgres:\/\/\w+:\w+@.+:\d+\/\w+`, conf.DSN())
}

func TestMySQLConfig(t *testing.T) {
connUrl := "any"

conf := NewMySQLConfig(&connUrl, nil)
assert.Equal(t, conf.DSN(), connUrl)

conf = NewMySQLConfig(nil, &MySQLConnectionParams{
DBUsername: "test",
DBPassword: "test",
DBHost: "localhost",
DBPort: 5432,
DBName: "test",
})

assert.Regexp(t, `\w+:\w+@tcp\(.+:\d+\)/\w+`, conf.DSN())
}

0 comments on commit 0e13bbc

Please sign in to comment.