diff --git a/config/.env.test b/config/.env.test index e69de29..ea2b2b0 100644 --- a/config/.env.test +++ b/config/.env.test @@ -0,0 +1,2 @@ +MINIMUM_CRYPTO_SELL_OPERATION=0.001 +MINIMUM_CRYPTO_BUY_OPERATION=0.001 \ No newline at end of file diff --git a/internal/operation-hub/application/config/load_env.go b/internal/operation-hub/application/config/load_env.go index ee90cc8..13721ac 100644 --- a/internal/operation-hub/application/config/load_env.go +++ b/internal/operation-hub/application/config/load_env.go @@ -2,7 +2,6 @@ package config import ( "github.com/joho/godotenv" - "log" "os" "regexp" ) @@ -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") } } @@ -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() } diff --git a/internal/operation-hub/application/properties/proprierties.go b/internal/operation-hub/application/properties/proprierties.go index 545e7a6..04eeea5 100644 --- a/internal/operation-hub/application/properties/proprierties.go +++ b/internal/operation-hub/application/properties/proprierties.go @@ -18,9 +18,10 @@ var propertiesInstance *properties func Properties() *properties { if propertiesInstance == nil { + propertiesLoaded := loadProperties() once.Do( func() { - propertiesInstance = loadProperties() + propertiesInstance = propertiesLoaded }) } @@ -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") diff --git a/internal/operation-hub/domain/exceptions/crypto_error.go b/internal/operation-hub/domain/exceptions/crypto_error.go deleted file mode 100644 index 5eecd55..0000000 --- a/internal/operation-hub/domain/exceptions/crypto_error.go +++ /dev/null @@ -1,7 +0,0 @@ -package exceptions - -import "github.com/brienze1/crypto-robot-operation-hub/pkg/custom_error" - -func CryptoError(err error, internalError string) *custom_error.BaseError { - return custom_error.NewBaseError(err, internalError, "Error while getting crypto value") -} diff --git a/test/internal/operation-hub/application/config/load_env_test.go b/test/internal/operation-hub/application/config/load_env_test.go new file mode 100644 index 0000000..285c47c --- /dev/null +++ b/test/internal/operation-hub/application/config/load_env_test.go @@ -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") +} diff --git a/test/internal/operation-hub/application/properties/properties_test.go b/test/internal/operation-hub/application/properties/properties_test.go new file mode 100644 index 0000000..cd01304 --- /dev/null +++ b/test/internal/operation-hub/application/properties/properties_test.go @@ -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") +}