cbrfWorker/internal/utils/currency_exchange_test.go

107 lines
1.9 KiB
Go
Raw Normal View History

2023-05-22 19:48:44 +00:00
package utils_test
import (
"fmt"
"testing"
2023-06-22 08:26:42 +00:00
"github.com/danilsolovyov/croupierCbrf/internal/dal"
"github.com/danilsolovyov/croupierCbrf/internal/utils"
2023-05-22 19:48:44 +00:00
"github.com/stretchr/testify/assert"
)
func TestCurrencyExchange(t *testing.T) {
currencies := map[string]dal.Quote{
"USD": {ID: "USD", Value: 79.9093},
"EUR": {ID: "EUR", Value: 86.2770},
"BYN": {ID: "BYN", Value: 27.3110},
}
testCases := []struct {
from string
to string
value int64
expected int64
isError bool
}{
{
from: "USD",
to: "RUB",
value: 10000,
expected: 799100,
},
{
from: "RUB",
to: "RUB",
value: 10000,
expected: 10000,
},
{
from: "BYN",
to: "RUB",
value: 10000,
expected: 273100,
},
{
from: "EUR",
to: "RUB",
value: 10000,
expected: 862800,
},
{
from: "RUB",
to: "USD",
value: 799100,
expected: 10000,
},
{
from: "RUB",
to: "BYN",
value: 273100,
expected: 10000,
},
{
from: "RUB",
to: "EUR",
value: 862800,
expected: 10000,
},
{
from: "BYN",
to: "EUR",
value: 10000,
expected: 3165,
},
{
from: "BYNNN",
to: "EURRR",
value: 19900,
expected: 0,
isError: true,
},
{
from: "BYN",
to: "EURRR",
value: 19900,
expected: 0,
isError: true,
},
}
for _, testCase := range testCases {
result, err := utils.CurrencyExchange(currencies, testCase.from, testCase.to, testCase.value)
if testCase.isError {
assert.Equal(t, testCase.expected, result)
assert.Error(t, err)
continue
}
assert.NoError(t, err)
assert.Equal(t,
testCase.expected,
result,
fmt.Sprintf("Неверный результат при переводе с %s на %s", testCase.from, testCase.to),
)
}
}