Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulyhedral committed Oct 26, 2024
1 parent af7d9fb commit 0a1166c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 18 deletions.
1 change: 1 addition & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const (
DB_HOST = "DB_HOST"
DB_NAME = "DB_NAME"
DB_OPTS = "DB_OPTS"
DB_PORT = "DB_PORT"
DB_PW = "DB_PW"
DB_SCHEME = "DB_SCHEME"
DB_URI = "DB_URI"
Expand Down
47 changes: 29 additions & 18 deletions database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/sweetrpg/catalog-api/constants"
"github.com/sweetrpg/catalog-api/logging"
"github.com/sweetrpg/catalog-api/util"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
Expand All @@ -17,28 +18,38 @@ import (
var Db *mongo.Database
var client *mongo.Client

func SetupDatabase() {
var dbUrl *url.URL
var err error
func buildDbURL() (*url.URL, string) {
dbUri, found := os.LookupEnv(constants.DB_URI)
dbName := os.Getenv(constants.DB_NAME)
if found {
dbUrl, err = url.Parse(dbUri)
} else {
dbScheme := os.Getenv(constants.DB_SCHEME)
dbUser := os.Getenv(constants.DB_USER)
dbPW := os.Getenv(constants.DB_PW)
dbHost := os.Getenv(constants.DB_HOST)
dbOpts := os.Getenv(constants.DB_OPTS)
dbUrl = &url.URL{
Scheme: dbScheme,
Host: dbHost,
User: url.UserPassword(dbUser, dbPW),
Path: dbName,
RawQuery: dbOpts,
ForceQuery: true,
dbUrl, err := url.Parse(dbUri)
if err != nil {
panic(err)
}

return dbUrl, dbUrl.Path[1:]
}

dbScheme := os.Getenv(constants.DB_SCHEME)
dbUser := os.Getenv(constants.DB_USER)
dbPW := os.Getenv(constants.DB_PW)
dbHost := os.Getenv(constants.DB_HOST)
dbPort := util.GetEnvInt(constants.DB_PORT, 27017)
dbOpts := os.Getenv(constants.DB_OPTS)
dbName := os.Getenv(constants.DB_NAME)
dbUrl := &url.URL{
Scheme: dbScheme,
Host: fmt.Sprintf("%s:%d", dbHost, dbPort),
User: url.UserPassword(dbUser, dbPW),
Path: dbName,
RawQuery: dbOpts,
ForceQuery: true,
}

return dbUrl, dbName
}

func SetupDatabase() {
dbUrl, dbName := buildDbURL()
logging.Logger.Info("Connecting to database", "url", dbUrl.Redacted())
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(dbUrl.String()))
if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions database/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package database

import (
"github.com/stretchr/testify/assert"
"github.com/sweetrpg/catalog-api/constants"
"os"
"testing"
)

func TestBuildURLFromURI(t *testing.T) {
os.Setenv(constants.DB_URI, "mongo://user:pass@host:12345/db?opts=these")
dbUrl, dbName := buildDbURL()
assert.Equal(t, dbUrl.Scheme, "mongo")
assert.Equal(t, dbUrl.User.Username(), "user")
// assert.Equal(t, dbUrl.User.Password(), "pass")
assert.Equal(t, dbUrl.Host, "host:12345")
assert.Equal(t, dbUrl.Query().Get("opts"), "these")
assert.Equal(t, dbName, "db")
}

func TestBuildURLFromParts(t *testing.T) {
os.Unsetenv(constants.DB_URI)
os.Setenv(constants.DB_NAME, "db")
os.Setenv(constants.DB_HOST, "host")
os.Setenv(constants.DB_SCHEME, "mongo")
os.Setenv(constants.DB_USER, "user")
os.Setenv(constants.DB_PW, "pass")
os.Setenv(constants.DB_PORT, "12345")
os.Setenv(constants.DB_OPTS, "opts=these")

dbUrl, dbName := buildDbURL()
assert.Equal(t, dbUrl.Scheme, "mongo")
assert.Equal(t, dbUrl.User.Username(), "user")
// assert.Equal(t, dbUrl.User.Password(), "pass")
assert.Equal(t, dbUrl.Host, "host:12345")
assert.Equal(t, dbUrl.Query().Get("opts"), "these")
assert.Equal(t, dbName, "db")
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/google/jsonapi v1.0.0
github.com/joho/godotenv v1.5.1
github.com/penglongli/gin-metrics v0.1.10
github.com/stretchr/testify v1.9.0
github.com/zerodha/logf v0.5.5
go.mongodb.org/mongo-driver v1.17.1
go.opentelemetry.io/otel v1.31.0
Expand All @@ -27,6 +28,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.6 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
Expand All @@ -52,6 +54,7 @@ require (
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
Expand Down

0 comments on commit 0a1166c

Please sign in to comment.