107 lines
1.8 KiB
Go
107 lines
1.8 KiB
Go
package utils_test
|
||
|
||
import (
|
||
"fmt"
|
||
"testing"
|
||
|
||
"gitea.pena/PenaSide/cbrfWorker/internal/dal"
|
||
"gitea.pena/PenaSide/cbrfWorker/internal/utils"
|
||
"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),
|
||
)
|
||
}
|
||
}
|