github.com/prebid/prebid-server/v2@v2.18.0/openrtb_ext/bid_test.go (about)

     1  package openrtb_ext
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestBidderKey(t *testing.T) {
    10  	apnKey := HbpbConstantKey.BidderKey(BidderAppnexus, 50)
    11  	if apnKey != "hb_pb_appnexus" {
    12  		t.Errorf("Bad resolved targeting key. Expected hb_pb_appnexus, got %s", apnKey)
    13  	}
    14  }
    15  
    16  func TestTruncatedKey(t *testing.T) {
    17  	apnKey := HbpbConstantKey.BidderKey(BidderAppnexus, 8)
    18  	if apnKey != "hb_pb_ap" {
    19  		t.Errorf("Bad truncated targeting key. Expected hb_pb_ap, got %s", apnKey)
    20  	}
    21  }
    22  
    23  func TestTruncateKey(t *testing.T) {
    24  	testCases := []struct {
    25  		description          string
    26  		givenMaxLength       int
    27  		givenTargetingKey    TargetingKey
    28  		expectedTargetingKey string
    29  	}{
    30  		{
    31  			description:          "Targeting key is smaller than max length, expect targeting key to stay the same",
    32  			givenMaxLength:       15,
    33  			givenTargetingKey:    TargetingKey("hb_bidder_key"),
    34  			expectedTargetingKey: "hb_bidder_key",
    35  		},
    36  		{
    37  			description:          "Targeting key is larger than max length, expect targeting key to be truncated",
    38  			givenMaxLength:       9,
    39  			givenTargetingKey:    TargetingKey("hb_bidder_key"),
    40  			expectedTargetingKey: "hb_bidder",
    41  		},
    42  		{
    43  			description:          "Max length isn't greater than zero, expect targeting key to not be truncated",
    44  			givenMaxLength:       0,
    45  			givenTargetingKey:    TargetingKey("hb_bidder_key"),
    46  			expectedTargetingKey: "hb_bidder_key",
    47  		},
    48  	}
    49  
    50  	for _, test := range testCases {
    51  		truncatedKey := test.givenTargetingKey.TruncateKey(test.givenMaxLength)
    52  		assert.Equalf(t, test.expectedTargetingKey, truncatedKey, "The Targeting Key is incorrect: %s\n", test.description)
    53  	}
    54  }
    55  
    56  func TestBidParsing(t *testing.T) {
    57  	assertBidParse(t, "banner", BidTypeBanner)
    58  	assertBidParse(t, "video", BidTypeVideo)
    59  	assertBidParse(t, "audio", BidTypeAudio)
    60  	assertBidParse(t, "native", BidTypeNative)
    61  	parsed, err := ParseBidType("unknown")
    62  	if err == nil {
    63  		t.Errorf("ParseBidType did not return the expected error.")
    64  	}
    65  	if parsed != "" {
    66  		t.Errorf("ParseBidType should return an empty string on error. Instead got %s", parsed)
    67  	}
    68  }
    69  
    70  func assertBidParse(t *testing.T, s string, bidType BidType) {
    71  	t.Helper()
    72  
    73  	parsed, err := ParseBidType(s)
    74  	if err != nil {
    75  		t.Errorf("Bid parsing failed with error: %v", err)
    76  	}
    77  	if parsed != bidType {
    78  		t.Errorf("Bid types did not match. Expected %s, got %s", bidType, parsed)
    79  	}
    80  }