UOE-7910 Updated Fetcher Module

This commit is contained in:
saurabh-narkhede 2022-07-25 17:59:17 +05:30
parent 477f2fc5ea
commit 0f75ead0ff
2 changed files with 78 additions and 10 deletions

View File

@ -1,7 +1,6 @@
package cache package cache
import ( import (
"errors"
"fmt" "fmt"
) )
@ -33,7 +32,7 @@ func (f *fetcher) Register(keyPrefix string, cbf callbackFunc) bool {
func (f *fetcher) Execute(key string) (interface{}, error) { func (f *fetcher) Execute(key string) (interface{}, error) {
if len(key) < f.prefixLen { if len(key) < f.prefixLen {
return nil, errors.New("Invalid Key " + key) return nil, fmt.Errorf(errInvalidKey, key)
} }
keyPrefix := key[:f.prefixLen] keyPrefix := key[:f.prefixLen]
cbf, ok := f.cb[keyPrefix] cbf, ok := f.cb[keyPrefix]

View File

@ -5,6 +5,63 @@ import (
"testing" "testing"
) )
func Test_fetcher_Register(t *testing.T) {
type fields struct {
cb map[string]callbackFunc
prefixLen int
}
type args struct {
keyPrefix string
cbf callbackFunc
}
tests := []struct {
name string
fields fields
args args
want bool
}{
// TODO: Add test cases.
{
name: "Registering Invalid KeyPrefix",
fields: fields(*NewFetcher(5)),
args: args{
keyPrefix: "AAG0022222",
cbf: nil,
},
want: false,
},
{
name: "Registering Empty KeyPrefix",
fields: fields(*NewFetcher(5)),
args: args{
keyPrefix: "",
cbf: nil,
},
want: false,
},
{
name: "Registration Valid KeyPrefix",
fields: fields(*NewFetcher(5)),
args: args{
keyPrefix: "AAG00",
cbf: CbGetAdUnitConfig,
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &fetcher{
cb: tt.fields.cb,
prefixLen: tt.fields.prefixLen,
}
if got := f.Register(tt.args.keyPrefix, tt.args.cbf); got != tt.want {
t.Errorf("fetcher.Register() = %v, want %v", got, tt.want)
}
})
}
}
func Test_fetcher_Execute(t *testing.T) { func Test_fetcher_Execute(t *testing.T) {
type fields struct { type fields struct {
cb map[string]callbackFunc cb map[string]callbackFunc
@ -54,15 +111,17 @@ func Test_fetcher_Execute(t *testing.T) {
{ {
name: "Valid Key Execution", name: "Valid Key Execution",
fields: fields{ fields: fields{
cb: make(map[string]callbackFunc), cb: map[string]callbackFunc{
"AAG00": CbGetAdUnitConfig,
"AAA00": nil,
"AAB00": nil,
},
prefixLen: 5, prefixLen: 5,
}, },
args: args{ args: args{
key: "AAG00_5890", key: "AAG00_5890",
}, },
want: f.cb{ want: "AdUnitConfig",
"afewv",
},
wantErr: false, wantErr: false,
}, },
} }
@ -74,10 +133,7 @@ func Test_fetcher_Execute(t *testing.T) {
prefixLen: tt.fields.prefixLen, prefixLen: tt.fields.prefixLen,
} }
got, err := f.Execute(tt.args.key) got, err := f.Execute(tt.args.key)
// if errors.Is(err, tt.wantErr) {
// t.Errorf("fetcher.Execute() error = %v, wantErr %v", err, tt.wantErr)
// return
// }
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("fetcher.Execute() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("fetcher.Execute() error = %v, wantErr %v", err, tt.wantErr)
return return
@ -88,3 +144,16 @@ func Test_fetcher_Execute(t *testing.T) {
}) })
} }
} }
func CbGetAdUnitConfig(key string) (interface{}, error) {
//Spliting Key to Call Respective DB call
//info := strings.Split(key, "_")
//profileID, _ := strconv.Atoi(info[1])
//displayVersionID, _ := strconv.Atoi(info[2])
data := "AdUnitConfig"
return data, nil
}