github.com/prebid/prebid-server/v2@v2.18.0/adservertargeting/requestcache_test.go (about)

     1  package adservertargeting
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/prebid/openrtb/v20/openrtb2"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestRequestImpCache(t *testing.T) {
    12  	testCases := []struct {
    13  		description     string
    14  		inputRequest    json.RawMessage
    15  		expectedReqSize int
    16  		expectedImpsNum int
    17  		expectedError   bool
    18  	}{
    19  		{
    20  			description:     "valid request with 2 imps",
    21  			inputRequest:    json.RawMessage(reqValid),
    22  			expectedReqSize: 355,
    23  			expectedImpsNum: 2,
    24  			expectedError:   false,
    25  		},
    26  		{
    27  			description:     "invalid request ",
    28  			inputRequest:    json.RawMessage(reqInvalid),
    29  			expectedReqSize: 88,
    30  			expectedImpsNum: 0,
    31  			expectedError:   true,
    32  		},
    33  		{
    34  			description:     "valid request with no imps",
    35  			inputRequest:    json.RawMessage(reqNoImps),
    36  			expectedReqSize: 52,
    37  			expectedImpsNum: 0,
    38  			expectedError:   true,
    39  		},
    40  	}
    41  
    42  	for _, test := range testCases {
    43  		reqImpCache := requestCache{resolvedReq: test.inputRequest}
    44  
    45  		actualReq := reqImpCache.GetReqJson()
    46  		assert.Len(t, actualReq, test.expectedReqSize, "incorrect request returned")
    47  		actualImps, err := reqImpCache.GetImpsData()
    48  		assert.Len(t, actualImps, test.expectedImpsNum, "incorrect number of impressions returned")
    49  
    50  		if test.expectedError {
    51  			assert.Error(t, err, "expected error not returned")
    52  		} else {
    53  			assert.NoError(t, err, "unexpected error returned")
    54  		}
    55  	}
    56  }
    57  
    58  func TestBidsCache(t *testing.T) {
    59  
    60  	testCases := []struct {
    61  		description      string
    62  		inputBidder      string
    63  		inputBidId       string
    64  		inputBid         openrtb2.Bid
    65  		expectedBidBytes []byte
    66  		expectedError    bool
    67  	}{
    68  		{
    69  			description:      "valid bid not in cache for existing bidder",
    70  			inputBidder:      "bidderA",
    71  			inputBidId:       "bid3",
    72  			inputBid:         openrtb2.Bid{ID: "test_bid3"},
    73  			expectedBidBytes: []byte(`{"id":"test_bid3","impid":"","price":0}`),
    74  			expectedError:    false,
    75  		},
    76  		{
    77  			description:      "valid bid and not existing bidder",
    78  			inputBidder:      "bidderB",
    79  			inputBidId:       "bid1",
    80  			inputBid:         openrtb2.Bid{ID: "test_bid1"},
    81  			expectedBidBytes: []byte(`{"id":"test_bid1","impid":"","price":0}`),
    82  			expectedError:    false,
    83  		},
    84  		{
    85  			description:      "valid bid in cache",
    86  			inputBidder:      "bidderA",
    87  			inputBidId:       "bid2",
    88  			inputBid:         openrtb2.Bid{},
    89  			expectedBidBytes: []byte(`{"bidid":"test_bid2"}`),
    90  			expectedError:    false,
    91  		},
    92  	}
    93  
    94  	bCache := bidsCache{bids: map[string]map[string][]byte{
    95  		"bidderA": {
    96  			"bid1": []byte(`{"bidid":"test_bid1"}`),
    97  			"bid2": []byte(`{"bidid":"test_bid2"}`),
    98  		},
    99  	}}
   100  
   101  	for _, test := range testCases {
   102  		bidBytes, err := bCache.GetBid(test.inputBidder, test.inputBidId, test.inputBid)
   103  		if test.expectedError {
   104  			assert.Error(t, err, "expected error not returned")
   105  		} else {
   106  			assert.NoError(t, err, "unexpected error returned")
   107  			assert.Equal(t, test.expectedBidBytes, bidBytes, "incorrect bid returned")
   108  		}
   109  	}
   110  }