diff --git a/fetcher.go b/fetcher.go index 44812bd..0e898f1 100644 --- a/fetcher.go +++ b/fetcher.go @@ -1,7 +1,6 @@ package cache import ( - "errors" "fmt" ) @@ -33,7 +32,7 @@ func (f *fetcher) Register(keyPrefix string, cbf callbackFunc) bool { func (f *fetcher) Execute(key string) (interface{}, error) { if len(key) < f.prefixLen { - return nil, errors.New("Invalid Key " + key) + return nil, fmt.Errorf(errInvalidKey, key) } keyPrefix := key[:f.prefixLen] cbf, ok := f.cb[keyPrefix] diff --git a/fetcher_test.go b/fetcher_test.go index 55af0bd..81a7ae3 100644 --- a/fetcher_test.go +++ b/fetcher_test.go @@ -5,6 +5,63 @@ import ( "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) { type fields struct { cb map[string]callbackFunc @@ -54,15 +111,17 @@ func Test_fetcher_Execute(t *testing.T) { { name: "Valid Key Execution", fields: fields{ - cb: make(map[string]callbackFunc), + cb: map[string]callbackFunc{ + "AAG00": CbGetAdUnitConfig, + "AAA00": nil, + "AAB00": nil, + }, prefixLen: 5, }, args: args{ key: "AAG00_5890", }, - want: f.cb{ - "afewv", - }, + want: "AdUnitConfig", wantErr: false, }, } @@ -74,10 +133,7 @@ func Test_fetcher_Execute(t *testing.T) { prefixLen: tt.fields.prefixLen, } 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 { t.Errorf("fetcher.Execute() error = %v, wantErr %v", err, tt.wantErr) 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 + +}