Skip to content

Commit

Permalink
[TEST] - added more coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Brienze <lfbrienze@gmail.com>
  • Loading branch information
brienze1 committed Aug 24, 2022
1 parent ebd6341 commit 465812e
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 15 deletions.
2 changes: 2 additions & 0 deletions config/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MINIMUM_CRYPTO_SELL_OPERATION=0.001
MINIMUM_CRYPTO_BUY_OPERATION=0.001
8 changes: 2 additions & 6 deletions internal/operation-hub/application/config/load_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"github.com/joho/godotenv"
"log"
"os"
"regexp"
)
Expand All @@ -27,7 +26,7 @@ func load(file string) {

err := godotenv.Load(string(rootPath) + configDirPath + file)
if err != nil {
log.Fatal("Error loading .env file")
panic("Error loading .env file")
}
}

Expand All @@ -38,10 +37,7 @@ const (
)

func LoadTestEnv() {
err := os.Setenv("OPERATION_HUB_ENV", string(test))
if err != nil {
panic("error while trying to set env variable")
}
_ = os.Setenv("OPERATION_HUB_ENV", string(test))

LoadEnv()
}
5 changes: 3 additions & 2 deletions internal/operation-hub/application/properties/proprierties.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ var propertiesInstance *properties

func Properties() *properties {
if propertiesInstance == nil {
propertiesLoaded := loadProperties()
once.Do(
func() {
propertiesInstance = loadProperties()
propertiesInstance = propertiesLoaded
})
}

Expand All @@ -34,7 +35,7 @@ func loadProperties() *properties {
}
minimumCryptoBuyOperation, err := strconv.ParseFloat(os.Getenv("MINIMUM_CRYPTO_BUY_OPERATION"), 64)
if err != nil {
panic("Failed to load property \"MINIMUM_CRYPTO_SELL_OPERATION\" from environment")
panic("Failed to load property \"MINIMUM_CRYPTO_BUY_OPERATION\" from environment")
}
binanceCryptoSymbolPriceTickerUrl := os.Getenv("BINANCE_CRYPTO_SYMBOL_PRICE_TICKER_URL")

Expand Down
7 changes: 0 additions & 7 deletions internal/operation-hub/domain/exceptions/crypto_error.go

This file was deleted.

31 changes: 31 additions & 0 deletions test/internal/operation-hub/application/config/load_env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package config

import (
"github.com/brienze1/crypto-robot-operation-hub/internal/operation-hub/application/config"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

func TestLoadEnvSuccess(t *testing.T) {
err := os.Setenv("OPERATION_HUB_ENV", "test")
assert.Nil(t, err)

defer func() {
r := recover()

assert.Nil(t, r)
}()

config.LoadEnv()
}

func TestLoadEnvFailure(t *testing.T) {
err := os.Setenv("OPERATION_HUB_ENV", uuid.NewString())
assert.Nil(t, err)

panicFunction := func() { config.LoadEnv() }

assert.Panicsf(t, panicFunction, "Should panic")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package properties

import (
"github.com/brienze1/crypto-robot-operation-hub/internal/operation-hub/application/properties"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

func setup() {
_ = os.Setenv("MINIMUM_CRYPTO_SELL_OPERATION", "0.1")
_ = os.Setenv("MINIMUM_CRYPTO_BUY_OPERATION", "0.1")
}

func TestPropertiesMinimumCryptoSellOperationFailure(t *testing.T) {
setup()

_ = os.Setenv("MINIMUM_CRYPTO_SELL_OPERATION", uuid.NewString())

panicFunction := func() { properties.Properties() }

assert.Panicsf(t, panicFunction, "Should panic")
}

func TestPropertiesMinimumCryptoBuyOperationFailure(t *testing.T) {
setup()

_ = os.Setenv("MINIMUM_CRYPTO_BUY_OPERATION", uuid.NewString())

panicFunction := func() { properties.Properties() }

assert.Panicsf(t, panicFunction, "Should panic")
}

func TestPropertiesSuccess(t *testing.T) {
setup()

panicFunction := func() { properties.Properties() }

assert.NotPanicsf(t, panicFunction, "Should not panic")
}

0 comments on commit 465812e

Please sign in to comment.