Config.go testing (#196)

More coverage for GetExchangeBankAccounts test
More coverage for UpdateExchangeBankAccounts test
Adding CheckClientBankAccounts test
Fixing bug in CheckClientBankAccounts
Adding CheckCommunicationsConfig test
Fixing bugs in CheckCommunicationsConfig
Adding CheckPairConsistency test
Removing superfluous commas
More coverage for GetForexProviderConfig test
More coverage for GetPrimaryForexProvider
This commit is contained in:
cranktakular
2018-10-30 15:55:50 +11:00
committed by Adrian Gallagher
parent 36b37b0571
commit 4a879bf1a1
2 changed files with 258 additions and 42 deletions

View File

@@ -324,25 +324,25 @@ func (c *Config) CheckClientBankAccounts() error {
return nil
}
for _, bank := range c.BankAccounts {
if bank.Enabled == true {
if bank.BankName == "" || bank.BankAddress == "" {
for i := range c.BankAccounts {
if c.BankAccounts[i].Enabled == true {
if c.BankAccounts[i].BankName == "" || c.BankAccounts[i].BankAddress == "" {
return fmt.Errorf("banking details for %s is enabled but variables not set correctly",
bank.BankName)
c.BankAccounts[i].BankName)
}
if bank.AccountName == "" || bank.AccountNumber == "" {
if c.BankAccounts[i].AccountName == "" || c.BankAccounts[i].AccountNumber == "" {
return fmt.Errorf("banking account details for %s variables not set correctly",
bank.BankName)
c.BankAccounts[i].BankName)
}
if bank.IBAN == "" && bank.SWIFTCode == "" && bank.BSBNumber == "" {
if c.BankAccounts[i].IBAN == "" && c.BankAccounts[i].SWIFTCode == "" && c.BankAccounts[i].BSBNumber == "" {
return fmt.Errorf("critical banking numbers not set for %s in %s account",
bank.BankName,
bank.AccountName)
c.BankAccounts[i].BankName,
c.BankAccounts[i].AccountName)
}
if bank.SupportedExchanges == "" {
bank.SupportedExchanges = "ALL"
if c.BankAccounts[i].SupportedExchanges == "" {
c.BankAccounts[i].SupportedExchanges = "ALL"
}
}
}
@@ -382,17 +382,33 @@ func (c *Config) CheckCommunicationsConfig() error {
}
if c.Communications.SMSGlobalConfig.Name == "" {
if c.SMS.Contacts != nil {
c.Communications.SMSGlobalConfig = SMSGlobalConfig{
Name: "SMSGlobal",
Enabled: c.SMS.Enabled,
Verbose: c.SMS.Verbose,
Username: c.SMS.Username,
Password: c.SMS.Password,
Contacts: c.SMS.Contacts,
if c.SMS != nil {
if c.SMS.Contacts != nil {
c.Communications.SMSGlobalConfig = SMSGlobalConfig{
Name: "SMSGlobal",
Enabled: c.SMS.Enabled,
Verbose: c.SMS.Verbose,
Username: c.SMS.Username,
Password: c.SMS.Password,
Contacts: c.SMS.Contacts,
}
// flush old SMS config
c.SMS = nil
} else {
c.Communications.SMSGlobalConfig = SMSGlobalConfig{
Name: "SMSGlobal",
Username: "main",
Password: "test",
Contacts: []SMSContact{
{
Name: "bob",
Number: "1234",
Enabled: false,
},
},
}
}
// flush old SMS config
c.SMS = nil
} else {
c.Communications.SMSGlobalConfig = SMSGlobalConfig{
Name: "SMSGlobal",
@@ -408,6 +424,7 @@ func (c *Config) CheckCommunicationsConfig() error {
},
}
}
} else {
if c.SMS != nil {
// flush old SMS config
@@ -441,7 +458,8 @@ func (c *Config) CheckCommunicationsConfig() error {
}
if c.Communications.SlackConfig.Enabled {
if c.Communications.SlackConfig.TargetChannel == "" ||
c.Communications.SlackConfig.VerificationToken == "" {
c.Communications.SlackConfig.VerificationToken == "" ||
c.Communications.SlackConfig.VerificationToken == "testtest" {
return errors.New("Slack enabled in config but variable data not set")
}
}
@@ -456,7 +474,7 @@ func (c *Config) CheckCommunicationsConfig() error {
if c.Communications.SMTPConfig.Host == "" ||
c.Communications.SMTPConfig.Port == "" ||
c.Communications.SMTPConfig.AccountName == "" ||
len(c.Communications.SMTPConfig.AccountName) == 0 {
c.Communications.SMTPConfig.AccountPassword == "" {
return errors.New("SMTP enabled in config but variable data not set")
}
}
@@ -503,6 +521,7 @@ func (c *Config) CheckPairConsistency(exchName string) error {
if len(pairs) == 0 {
exchCfg.EnabledPairs = pair.RandomPairFromPairs(availPairs).Pair().String()
log.Printf("Exchange %s: No enabled pairs found in available pairs, randomly added %v\n", exchName, exchCfg.EnabledPairs)
} else {
exchCfg.EnabledPairs = common.JoinStrings(pair.PairsToStringArray(pairs), ",")
}

View File

@@ -20,11 +20,15 @@ func TestGetExchangeBankAccounts(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. GetDepositBankAccounts LoadConfig error", err)
t.Error("Test failed. GetExchangeBankAccounts LoadConfig error", err)
}
_, err = cfg.GetExchangeBankAccounts("Bitfinex", "USD")
if err != nil {
t.Error("Test failed. GetDepositBankAccounts error", err)
t.Error("Test failed. GetExchangeBankAccounts error", err)
}
_, err = cfg.GetExchangeBankAccounts("Not an exchange", "Not a currency")
if err == nil {
t.Error("Test failed. GetExchangeBankAccounts, no error returned for invalid exchange")
}
}
@@ -32,13 +36,13 @@ func TestUpdateExchangeBankAccounts(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. UpdateDepositBankAccounts LoadConfig error", err)
t.Error("Test failed. UpdateExchangeBankAccounts LoadConfig error", err)
}
b := []BankAccount{{Enabled: false}}
err = cfg.UpdateExchangeBankAccounts("Bitfinex", b)
if err != nil {
t.Error("Test failed. UpdateDepositBankAccounts error", err)
t.Error("Test failed. UpdateExchangeBankAccounts error", err)
}
var count int
for _, exch := range cfg.Exchanges {
@@ -49,7 +53,12 @@ func TestUpdateExchangeBankAccounts(t *testing.T) {
}
}
if count != 1 {
t.Error("Test failed. UpdateDepositBankAccounts error")
t.Error("Test failed. UpdateExchangeBankAccounts error")
}
err = cfg.UpdateExchangeBankAccounts("Not an exchange", b)
if err == nil {
t.Error("Test failed. UpdateExchangeBankAccounts, no error returned for invalid exchange")
}
}
@@ -99,7 +108,54 @@ func TestUpdateClientBankAccounts(t *testing.T) {
}
}
if count != 1 {
t.Error("Test failed. UpdateDepositBankAccounts error")
t.Error("Test failed. UpdateClientBankAccounts error")
}
}
func TestCheckClientBankAccounts(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. CheckClientBankAccounts LoadConfig error", err)
}
cfg.BankAccounts = nil
err = cfg.CheckClientBankAccounts()
if err != nil || len(cfg.BankAccounts) == 0 {
t.Error("Test failed. CheckClientBankAccounts error:", err)
}
cfg.BankAccounts = nil
cfg.BankAccounts = append(cfg.BankAccounts, BankAccount{
Enabled: true,
BankName: "test",
})
err = cfg.CheckClientBankAccounts()
if err.Error() != "banking details for test is enabled but variables not set correctly" {
t.Error("Test failed. CheckClientBankAccounts unexpected error:", err)
}
cfg.BankAccounts[0].BankAddress = "test"
err = cfg.CheckClientBankAccounts()
if err.Error() != "banking account details for test variables not set correctly" {
t.Error("Test failed. CheckClientBankAccounts unexpected error:", err)
}
cfg.BankAccounts[0].AccountName = "Thrasher"
cfg.BankAccounts[0].AccountNumber = "1337"
err = cfg.CheckClientBankAccounts()
if err.Error() != "critical banking numbers not set for test in Thrasher account" {
t.Error("Test failed. CheckClientBankAccounts unexpected error:", err)
}
cfg.BankAccounts[0].IBAN = "12345678"
err = cfg.CheckClientBankAccounts()
if err != nil {
t.Error("Test failed. CheckClientBankAccounts error:", err)
}
if cfg.BankAccounts[0].SupportedExchanges == "" {
t.Error("Test failed. CheckClientBankAccounts SupportedExchanges unexpectedly nil, data:",
cfg.BankAccounts[0])
}
}
@@ -124,6 +180,141 @@ func TestUpdateCommunicationsConfig(t *testing.T) {
}
}
func TestCheckCommunicationsConfig(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. CheckCommunicationsConfig LoadConfig error", err)
}
cfg.Communications = CommunicationsConfig{}
err = cfg.CheckCommunicationsConfig()
if err != nil {
t.Error("Test failed. CheckCommunicationsConfig error:", err)
}
if cfg.Communications.SlackConfig.Name != "Slack" ||
cfg.Communications.SMSGlobalConfig.Name != "SMSGlobal" ||
cfg.Communications.SMTPConfig.Name != "SMTP" ||
cfg.Communications.TelegramConfig.Name != "Telegram" {
t.Error("Test failed. CheckCommunicationsConfig unexpected data:",
cfg.Communications)
}
cfg.SMS = &SMSGlobalConfig{}
cfg.Communications.SMSGlobalConfig.Name = ""
err = cfg.CheckCommunicationsConfig()
if err != nil || cfg.Communications.SMSGlobalConfig.Password != "test" {
t.Error("Test failed. CheckCommunicationsConfig error:", err)
}
cfg.SMS.Contacts = append(cfg.SMS.Contacts, SMSContact{
Name: "Bobby",
Number: "4321",
Enabled: false,
})
cfg.Communications.SMSGlobalConfig.Name = ""
err = cfg.CheckCommunicationsConfig()
if err != nil || cfg.Communications.SMSGlobalConfig.Contacts[0].Name != "Bobby" {
t.Error("Test failed. CheckCommunicationsConfig error:", err)
}
cfg.SMS = &SMSGlobalConfig{}
err = cfg.CheckCommunicationsConfig()
if err != nil {
t.Error("Test failed. CheckCommunicationsConfig error:", err)
}
if cfg.SMS != nil {
t.Error("Test failed. CheckCommunicationsConfig unexpected data:",
cfg.SMS)
}
cfg.Communications.SlackConfig.Name = "NOT Slack"
err = cfg.CheckCommunicationsConfig()
if err.Error() != "Communications config name/s not set correctly" {
t.Error("Test failed. CheckCommunicationsConfig unexpected error:", err)
}
cfg.Communications.SlackConfig.Name = "Slack"
cfg.Communications.SlackConfig.Enabled = true
err = cfg.CheckCommunicationsConfig()
if err.Error() != "Slack enabled in config but variable data not set" {
t.Error("Test failed. CheckCommunicationsConfig unexpected error:", err)
}
cfg.Communications.SlackConfig.Enabled = false
cfg.Communications.SMSGlobalConfig.Enabled = true
cfg.Communications.SMSGlobalConfig.Password = ""
err = cfg.CheckCommunicationsConfig()
if err.Error() != "SMSGlobal enabled in config but variable data not set" {
t.Error("Test failed. CheckCommunicationsConfig unexpected error:", err)
}
cfg.Communications.SMSGlobalConfig.Enabled = false
cfg.Communications.SMTPConfig.Enabled = true
cfg.Communications.SMTPConfig.AccountPassword = ""
err = cfg.CheckCommunicationsConfig()
if err.Error() != "SMTP enabled in config but variable data not set" {
t.Error("Test failed. CheckCommunicationsConfig unexpected error:", err)
}
cfg.Communications.SMTPConfig.Enabled = false
cfg.Communications.TelegramConfig.Enabled = true
cfg.Communications.TelegramConfig.VerificationToken = ""
err = cfg.CheckCommunicationsConfig()
if err.Error() != "Telegram enabled in config but variable data not set" {
t.Error("Test failed. CheckCommunicationsConfig unexpected error:", err)
}
}
func TestCheckPairConsistency(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Error("Test failed. CheckPairConsistency LoadConfig error", err)
}
err = cfg.CheckPairConsistency("asdf")
if err == nil {
t.Error("Test failed. CheckPairConsistency. Non-existent exchange returned nil error")
}
cfg.Exchanges = append(cfg.Exchanges, ExchangeConfig{
Name: "TestExchange",
Enabled: true,
AvailablePairs: "DOGE_USD,DOGE_AUD",
EnabledPairs: "DOGE_USD,DOGE_AUD,DOGE_BTC",
ConfigCurrencyPairFormat: &CurrencyPairFormatConfig{
Uppercase: true,
Delimiter: "_",
},
})
tec, err := cfg.GetExchangeConfig("TestExchange")
if err != nil {
t.Error("Test failed. CheckPairConsistency GetExchangeConfig error", err)
}
err = cfg.CheckPairConsistency("TestExchange")
if err != nil {
t.Error("Test failed. CheckPairConsistency error:", err)
}
// Calling again immediately to hit the if !update {return nil}
err = cfg.CheckPairConsistency("TestExchange")
if err != nil {
t.Error("Test failed. CheckPairConsistency error:", err)
}
tec.EnabledPairs = "DOGE_LTC,BTC_LTC"
err = cfg.UpdateExchangeConfig(tec)
if err != nil {
t.Error("Test failed. CheckPairConsistency Update config failed, error:", err)
}
err = cfg.CheckPairConsistency("TestExchange")
if err != nil {
t.Error("Test failed. CheckPairConsistency error:", err)
}
}
func TestSupportsPair(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(ConfigTestFile)
@@ -153,22 +344,19 @@ func TestGetAvailablePairs(t *testing.T) {
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf(
"Test failed. TestGetAvailablePairs. LoadConfig Error: %s", err.Error(),
)
"Test failed. TestGetAvailablePairs. LoadConfig Error: %s", err.Error())
}
_, err = cfg.GetAvailablePairs("asdf")
if err == nil {
t.Error(
"Test failed. TestGetAvailablePairs. Non-existent exchange returned nil error",
)
"Test failed. TestGetAvailablePairs. Non-existent exchange returned nil error")
}
_, err = cfg.GetAvailablePairs("Bitfinex")
if err != nil {
t.Errorf(
"Test failed. TestGetAvailablePairs. Incorrect values. Err: %s", err,
)
"Test failed. TestGetAvailablePairs. Incorrect values. Err: %s", err)
}
}
@@ -177,22 +365,19 @@ func TestGetEnabledPairs(t *testing.T) {
err := cfg.LoadConfig(ConfigTestFile)
if err != nil {
t.Errorf(
"Test failed. TestGetEnabledPairs. LoadConfig Error: %s", err.Error(),
)
"Test failed. TestGetEnabledPairs. LoadConfig Error: %s", err.Error())
}
_, err = cfg.GetEnabledPairs("asdf")
if err == nil {
t.Error(
"Test failed. TestGetEnabledPairs. Non-existent exchange returned nil error",
)
"Test failed. TestGetEnabledPairs. Non-existent exchange returned nil error")
}
_, err = cfg.GetEnabledPairs("Bitfinex")
if err != nil {
t.Errorf(
"Test failed. TestGetEnabledPairs. Incorrect values. Err: %s", err,
)
"Test failed. TestGetEnabledPairs. Incorrect values. Err: %s", err)
}
}
@@ -375,6 +560,11 @@ func TestGetForexProviderConfig(t *testing.T) {
if err != nil {
t.Error("Test failed. GetForexProviderConfig error", err)
}
_, err = cfg.GetForexProviderConfig("this is not a forex provider")
if err == nil {
t.Error("Test failed. GetForexProviderConfig no error for invalid provider")
}
}
func TestGetPrimaryForexProvider(t *testing.T) {
@@ -387,6 +577,14 @@ func TestGetPrimaryForexProvider(t *testing.T) {
if primary == "" {
t.Error("Test failed. GetPrimaryForexProvider error")
}
for i := range cfg.Currency.ForexProviders {
cfg.Currency.ForexProviders[i].PrimaryProvider = false
}
primary = cfg.GetPrimaryForexProvider()
if primary != "" {
t.Error("Test failed. GetPrimaryForexProvider error, expected nil got:", primary)
}
}
func TestUpdateExchangeConfig(t *testing.T) {
@@ -418,7 +616,6 @@ func TestUpdateExchangeConfig(t *testing.T) {
}
func TestCheckExchangeConfigValues(t *testing.T) {
t.Parallel()
checkExchangeConfigValues := Config{}
err := checkExchangeConfigValues.LoadConfig(ConfigTestFile)