Skip to content

Commit

Permalink
Updating config fetcher scenarios test + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rajagopal28 committed Jul 31, 2021
1 parent 6d39fa0 commit fc65cc3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
10 changes: 5 additions & 5 deletions service/config/config_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ type ConfigFetcher struct{


func (c *ConfigFetcher) ReadFileAndGetAsObject(filename string, class interface{}) (interface{}, error) {
log.Println("Entering ReadFileAndGetAsObject")
pwd, _ := os.Getwd()
log.Print("Current Working Directory: ", pwd)
log.Println("Current Working Directory: ", pwd)
jsonFile, err := os.Open(pwd+c.Path+filename)
// txt, _ := ioutil.ReadFile(pwd+"/path/to/file.txt")
// if we os.Open returns an error then handle it

if err != nil {
log.Fatalf("error opening file: %v", err)
log.Printf("error opening file: %v", err)
return nil, err
}
log.Print("Successfully Opened ", filename)
log.Println("Successfully Opened ", filename)
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)


command := reflect.New(reflect.TypeOf(class))
json.Unmarshal([]byte(byteValue), command.Interface())
result := command.Elem().Interface()
println(result)
log.Println("Leaving ReadFileAndGetAsObject")
return result, nil
}
46 changes: 28 additions & 18 deletions test/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,42 @@ package config
import (
"log"
"testing"
"strings"

"pricingengine/service/config"
"pricingengine/test/util"
)


func TestConfigFetchSuccess(t *testing.T){
func TestConfigFetch(tp *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))
}
util.AssertEqual(result[0]["time"], float64(1800), t)
util.AssertEqual(result[0]["label"], "0.5 hours", t)
util.AssertEqual(result[0]["rate"], float64(273), t)
tp.Run("TestConfigFetchSuccess", func(t *testing.T) {
res, err := fetcher.ReadFileAndGetAsObject("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{})
log.Println("result length =", len(result))
util.AssertEqual(2, len(result), t)
util.AssertEqual(result[0]["time"], float64(1800), t)
util.AssertEqual(result[0]["label"], "0.5 hours", t)
util.AssertEqual(result[0]["rate"], float64(273), t)


util.AssertEqual(result[1]["time"], float64(345600), t)
util.AssertEqual(result[1]["label"], "96 hours / 4 days", t)
util.AssertEqual(result[1]["rate"], float64(5204), t)
log.Printf("List : %+v", result)
util.AssertEqual(result[1]["time"], float64(345600), t)
util.AssertEqual(result[1]["label"], "96 hours / 4 days", t)
util.AssertEqual(result[1]["rate"], float64(5204), t)
log.Printf("List : %+v", result)
})
tp.Run("TestConfigFetchFileNotFoundError", func(t *testing.T) {
_,err := fetcher.ReadFileAndGetAsObject("some-base-rate.json" , temp)
log.Println("Getting result:", err)
if err == nil || strings.Contains(err.Error(), "error opening file: open"){
log.Println("Should not come here!")
t.Errorf("should get error")
}
log.Println("Coming here!")
})
}

0 comments on commit fc65cc3

Please sign in to comment.