Skip to content

Commit

Permalink
Adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pubmatic-Dhruv-Sonone committed Jun 12, 2024
1 parent 46cce13 commit 1e98f26
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 14 deletions.
8 changes: 8 additions & 0 deletions adapters/ortbbidder/resolver/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package resolver

const (
mtypeKey = "mtype"
bidTypeKey = "BidType"
currencyKey = "Currency"
curKey = "cur"
)
4 changes: 2 additions & 2 deletions adapters/ortbbidder/resolver/currency_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type currencyResolver struct {
}

func (r *currencyResolver) getFromORTBObject(ortbResponse map[string]any) (any, bool) {
if curr, ok := ortbResponse["cur"]; !ok && curr != "" {
if curr, ok := ortbResponse[curKey]; !ok && curr != "" {
return curr, true
}
return nil, false
Expand All @@ -17,5 +17,5 @@ func (r *currencyResolver) autoDetect(bid map[string]any) (any, bool) {
}

func (r *currencyResolver) setValue(adapterBid map[string]any, value any) {
adapterBid["Currency"] = value
adapterBid[currencyKey] = value
}
4 changes: 2 additions & 2 deletions adapters/ortbbidder/resolver/mediatype_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type mtypeResolver struct {
}

func (r *mtypeResolver) getFromORTBObject(bid map[string]any) (any, bool) {
mtype, ok := bid["mtype"].(float64)
mtype, ok := bid[mtypeKey].(float64)
if !ok && mtype == 0 {
return nil, false
}
Expand All @@ -23,5 +23,5 @@ func (r *mtypeResolver) autoDetect(bid map[string]any) (any, bool) {
}

func (r *mtypeResolver) setValue(adapterBid map[string]any, value any) {
adapterBid["BidType"] = value
adapterBid[bidTypeKey] = value
}
2 changes: 1 addition & 1 deletion adapters/ortbbidder/response_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (rb *responseBuilder) buildResponse() error {
// If the parameter exists in the response parameters, resolve it.
for _, paramName := range resolver.TypeBidFields {
if paramMapper, ok := rb.responseParams[paramName]; ok {
path := util.GetPath(paramMapper.Location, []int{seatIndex, bidIndex})
path := util.ReplaceLocationMacro(paramMapper.Location, []int{seatIndex, bidIndex})
paramResolver.Resolve(bid, typeBid, path, paramName)
}
}
Expand Down
19 changes: 10 additions & 9 deletions adapters/ortbbidder/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
)

const (
impKey = "imp"
extKey = "ext"
bidderKey = "bidder"
appsiteKey = "appsite"
siteKey = "site"
appKey = "app"
owOrtbPrefix = "owortb_"
impKey = "imp"
extKey = "ext"
bidderKey = "bidder"
appsiteKey = "appsite"
siteKey = "site"
appKey = "app"
owOrtbPrefix = "owortb_"
locationMacro = "#"
)

/*
Expand Down Expand Up @@ -139,11 +140,11 @@ func GetValueFromLocation(val interface{}, path string) (interface{}, bool) {
return next, true
}

func GetPath(path string, array []int) string {
func ReplaceLocationMacro(path string, array []int) string {
parts := strings.Split(path, ".")
j := 0
for i, part := range parts {
if part == "#" {
if part == locationMacro {
if j >= len(array) {
break
}
Expand Down
164 changes: 164 additions & 0 deletions adapters/ortbbidder/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package util
import (
"testing"

"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/openrtb_ext"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -405,3 +407,165 @@ func TestGetNode(t *testing.T) {
})
}
}

func TestReplaceLocationMacro(t *testing.T) {
tests := []struct {
name string
path string
array []int
expectedValuePath string
}{
{
name: "Empty path",
path: "",
array: []int{0, 1},
expectedValuePath: "",
},
{
name: "Replace # in path with array",
path: "seatbid.#.bid.#.ext.mtype",
array: []int{0, 1},
expectedValuePath: "seatbid.0.bid.1.ext.mtype",
},
{
name: "Array length less than # count in path",
path: "seatbid.#.bid.#.ext.mtype",
array: []int{0},
expectedValuePath: "seatbid.0.bid.#.ext.mtype",
},
{
name: "No # in path",
path: "seatbid.bid.ext.mtype",
array: []int{0, 1},
expectedValuePath: "seatbid.bid.ext.mtype",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ReplaceLocationMacro(tt.path, tt.array)
assert.Equal(t, tt.expectedValuePath, result)
})
}
}

func TestGetValueFromLocation(t *testing.T) {
node := map[string]interface{}{
"seatbid": []interface{}{
map[string]interface{}{
"bid": []interface{}{
map[string]interface{}{
"ext": map[string]interface{}{
"mtype": "video",
},
},
},
},
},
}

tests := []struct {
name string
node interface{}
path string
expectedValue interface{}
ok bool
}{
{
name: "Node is empty",
node: nil,
path: "seatbid.0.bid.0.ext.mtype",
expectedValue: nil,
ok: false,
},
{
name: "Path is empty",
node: node,
path: "",
expectedValue: nil,
ok: false,
},
{
name: "Value is present in node",
node: node,
path: "seatbid.0.bid.0.ext.mtype",
expectedValue: "video",
ok: true,
},
{
name: "Value is not present in node",
node: node,
path: "seatbid.0.bid.0.ext.mtype1",
expectedValue: nil,
ok: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, ok := GetValueFromLocation(tt.node, tt.path)
assert.Equal(t, tt.ok, ok)
assert.Equal(t, tt.expectedValue, result)
})
}
}

func TestGetMediaType(t *testing.T) {
tests := []struct {
name string
mtype openrtb2.MarkupType
expected openrtb_ext.BidType
}{
{
name: "MarkupBanner",
mtype: openrtb2.MarkupBanner,
expected: openrtb_ext.BidTypeBanner,
},
{
name: "MarkupVideo",
mtype: openrtb2.MarkupVideo,
expected: openrtb_ext.BidTypeVideo,
},
{
name: "MarkupAudio",
mtype: openrtb2.MarkupAudio,
expected: openrtb_ext.BidTypeAudio,
},
{
name: "MarkupNative",
mtype: openrtb2.MarkupNative,
expected: openrtb_ext.BidTypeNative,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := GetMediaType(tt.mtype)
assert.Equal(t, tt.expected, result)
})
}
}

func TestIsORTBBidder(t *testing.T) {
tests := []struct {
name string
bidder string
expected bool
}{
{
name: "ORTB bidder",
bidder: "owortb_test",
expected: true,
},
{
name: "Non-ORTB bidder",
bidder: "test",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := IsORTBBidder(tt.bidder)
assert.Equal(t, tt.expected, result)
})
}
}

0 comments on commit 1e98f26

Please sign in to comment.