Skip to content

Commit

Permalink
Adding tests for config fetcher with sample rates
Browse files Browse the repository at this point in the history
  • Loading branch information
rajagopal28 committed Jul 31, 2021
1 parent 0919b25 commit b518178
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
55 changes: 55 additions & 0 deletions test/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package config

import (
"log"
"testing"
"reflect"
"pricingengine/service/config"
)


func TestConfigFetchSuccess(t *testing.T){
fetcher := config.ConfigFetcher {Path: "/../test_configs/"}
var temp []map[string]interface{}
res, err := fetcher.ReadFileAndGetAsObject("test-base-rate.json" , temp)
if err != nil {
log.Println("error reading the config file:", err)
t.Errorf("got error: %q", err)
}
result := res.([]map[string]interface{})
println("result length =", len(result))
if len(result) != 2 {
t.Errorf("Expected 2 records, found %q", len(result))
}
expected := [2]map[string]interface{}{
map[string]interface{}{
"time":1800,
"label":"0.5 hours",
"rate":273,
},
map[string]interface{}{
"time":345600,
"label":"96 hours / 4 days",
"rate":5204,
},
}
AssertEqual(result[0]["time"], float64(1800), t)
AssertEqual(result[0]["label"], "0.5 hours", t)
AssertEqual(result[0]["rate"], float64(273), t)


AssertEqual(result[1]["time"], float64(345600), t)
AssertEqual(result[1]["label"], "96 hours / 4 days", t)
AssertEqual(result[1]["rate"], float64(5204), t)
log.Printf("expected : %+v", expected)
log.Printf("List : %+v", result)
}



func AssertEqual(a interface{}, b interface{}, t *testing.T) {
if a == b || reflect.DeepEqual(a, b) {
return
}
t.Errorf("Received %v (type %v), expected %v (type %v)", a, reflect.TypeOf(a), b, reflect.TypeOf(b))
}
12 changes: 12 additions & 0 deletions test/test_configs/test-base-rate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"time":1800,
"label":"0.5 hours",
"rate":273
},
{
"time":345600,
"label":"96 hours / 4 days",
"rate":5204
}
]

0 comments on commit b518178

Please sign in to comment.