Skip to content

Commit 4f2e00f

Browse files
committed
up
1 parent ed4e186 commit 4f2e00f

File tree

11 files changed

+193
-29
lines changed

11 files changed

+193
-29
lines changed

cli/pkg/util/string_util.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ func ParseFloat(s string) (float64, error) {
1515

1616
func ParseIPCA(data []map[string]interface{}) (float64, error) {
1717
var ipca float64
18+
19+
if len(data) == 0 {
20+
return 0, fmt.Errorf("nenhum dado retornado")
21+
}
1822

1923
for _, entry := range data {
2024
valorStr, ok := entry["valor"].(string)

tests/mock/fetcher_mock.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package mock

tests/mock/util_mock.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package mock

tests/pkg/api/api_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package api_test
2+
3+
import (
4+
"testing"
5+
6+
"profitability/cli/pkg/api"
7+
)
8+
9+
func TestGetUrl(t *testing.T) {
10+
// Teste para um índice válido
11+
validIndex := "ipca"
12+
expectedValidURL := "https://api.bcb.gov.br/dados/serie/bcdata.sgs.433/dados/ultimos/12?formato=json"
13+
validURL := api.GetUrl(validIndex)
14+
if validURL != expectedValidURL {
15+
t.Errorf("Para o índice válido '%s', esperava-se '%s', mas obteve '%s'", validIndex, expectedValidURL, validURL)
16+
}
17+
18+
// Teste para um índice inválido
19+
invalidIndex := "inexistente"
20+
expectedInvalidURL := ""
21+
invalidURL := api.GetUrl(invalidIndex)
22+
if invalidURL != expectedInvalidURL {
23+
t.Errorf("Para o índice inválido '%s', esperava-se uma string vazia, mas obteve '%s'", invalidIndex, invalidURL)
24+
}
25+
}

tests/pkg/calculus/calc_prop_test.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

tests/pkg/calculus/ipca_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package calculus_test

tests/pkg/calculus/pos_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package calculus_test

tests/pkg/calculus/calc_pre_test.go renamed to tests/pkg/calculus/pre_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package calculus_test
22

33
import (
44
"testing"
5-
"math"
65

76
"profitability/cli/pkg/calculus"
87
)
@@ -21,8 +20,7 @@ func TestPre(t *testing.T) {
2120
}
2221

2322
// Define o valor esperado com base nos parâmetros de entrada
24-
expectedResult := rate * (1 - 0.225)
25-
expectedResult = math.Round(expectedResult*100) / 100
23+
expectedResult := 10.0 * (1 - 0.225)
2624

2725
// Compara o resultado retornado pela função com o resultado esperado
2826
if result.ResultPre != expectedResult {

tests/pkg/calculus/prop_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package calculus_test

tests/pkg/util/string_util_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package util_test
2+
3+
import (
4+
"testing"
5+
"profitability/cli/pkg/util"
6+
)
7+
8+
func TestParseFloat(t *testing.T) {
9+
// Teste com valor válido
10+
result, err := util.ParseFloat("10.5")
11+
if err != nil {
12+
t.Fatalf("Erro inesperado: %v", err)
13+
}
14+
15+
expectedResult := 10.5
16+
if result != expectedResult {
17+
t.Errorf("Resultado inesperado. Esperado: %f, Obtido: %f", expectedResult, result)
18+
}
19+
20+
// Teste com valor inválido
21+
_, err = util.ParseFloat("abc")
22+
if err == nil {
23+
t.Error("Esperava um erro para string inválida, mas nenhum foi retornado.")
24+
}
25+
}
26+
27+
func TestParseIPCA(t *testing.T) {
28+
// Teste com dados válidos
29+
data := []map[string]interface{}{
30+
{"valor": "2.5"},
31+
{"valor": "3.0"},
32+
{"valor": "1.8"},
33+
}
34+
35+
result, err := util.ParseIPCA(data)
36+
if err != nil {
37+
t.Fatalf("Erro inesperado: %v", err)
38+
}
39+
40+
expectedResult := 7.3
41+
if result != expectedResult {
42+
t.Errorf("Resultado inesperado. Esperado: %f, Obtido: %f", expectedResult, result)
43+
}
44+
45+
// Teste com valor inválido
46+
_, err = util.ParseIPCA([]map[string]interface{}{{"valor": "abc"}})
47+
if err == nil {
48+
t.Error("Esperava um erro para string inválida, mas nenhum foi retornado.")
49+
}
50+
51+
// Teste com dados vazios
52+
_, err = util.ParseIPCA([]map[string]interface{}{})
53+
if err == nil {
54+
t.Error("Esperava um erro para dados vazios, mas nenhum foi retornado.")
55+
}
56+
}
57+
58+
func TestParseSelic(t *testing.T) {
59+
// Teste com dados válidos
60+
data := []map[string]interface{}{
61+
{"valor": "5.2"},
62+
}
63+
64+
result, err := util.ParseSelic(data)
65+
if err != nil {
66+
t.Fatalf("Erro inesperado: %v", err)
67+
}
68+
69+
expectedResult := 5.2
70+
if result != expectedResult {
71+
t.Errorf("Resultado inesperado. Esperado: %f, Obtido: %f", expectedResult, result)
72+
}
73+
74+
// Teste com valor inválido
75+
_, err = util.ParseSelic([]map[string]interface{}{{"valor": "abc"}})
76+
if err == nil {
77+
t.Error("Esperava um erro para string inválida, mas nenhum foi retornado.")
78+
}
79+
80+
// Teste com dados vazios
81+
_, err = util.ParseSelic([]map[string]interface{}{})
82+
if err == nil {
83+
t.Error("Esperava um erro para dados vazios, mas nenhum foi retornado.")
84+
}
85+
}

0 commit comments

Comments
 (0)