-
Notifications
You must be signed in to change notification settings - Fork 0
/
currc_test.go
56 lines (48 loc) · 1.14 KB
/
currc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"strconv"
"testing"
)
var currency string = "USD_HRK"
var flag_t string = "fd"
var amount string = "10"
func TestRetrieveRatesFromAPI(t *testing.T) {
a := retrieveRatesFromAPI(currency)
if a == nil {
t.Error("Response is empty.")
}
}
func TestUnmarshalAPIRates(t *testing.T) {
response := retrieveRatesFromAPI(currency)
data, err := unmarshalAPIRates(response)
if err != nil {
t.Error(err)
}
if data == nil {
t.Error("Data is empty.")
}
if data[currency] == nil {
t.Error("Multiplier is nonexistent.")
}
if data[currency]["val"] == 0 {
t.Error("Multiplies is zero.")
}
}
func TestConvertCurrency(t *testing.T) {
amountFloat, err := strconv.ParseFloat(amount, 64)
if err != nil {
panic(err)
}
data, err := unmarshalAPIRates(retrieveRatesFromAPI(currency))
if err != nil {
t.Error(err)
}
calculatedAmount := amountFloat * data[currency]["val"]
convertedAmount := convertCurrency(flag_t, amount)
if convertedAmount == 0 {
t.Error("Converted amount is zero.")
}
if convertedAmount != calculatedAmount {
t.Errorf("Converted amount is wrong, expected %f, got %f", calculatedAmount, convertedAmount)
}
}