From d3f5c06f5022a9e8fb7e6f7d560f440160017083 Mon Sep 17 00:00:00 2001 From: Gus Carreon Date: Fri, 20 Dec 2019 16:20:35 -0500 Subject: [PATCH 1/8] currencies/rate_converter_test.go test case --- currencies/rate_converter_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/currencies/rate_converter_test.go b/currencies/rate_converter_test.go index a1807a63ffb..f9a14895347 100644 --- a/currencies/rate_converter_test.go +++ b/currencies/rate_converter_test.go @@ -379,6 +379,7 @@ func TestRates(t *testing.T) { {from: "", to: "EUR", expectedRate: 0, hasError: true}, {from: "CNY", to: "", expectedRate: 0, hasError: true}, {from: "", to: "", expectedRate: 0, hasError: true}, + {from: "USD", to: "USD", expectedRate: 1, hasError: false}, } mockedHttpServer := httptest.NewServer(http.HandlerFunc( From a099405ed2b4b190357c9cce82b1a8598fca699e Mon Sep 17 00:00:00 2001 From: Gus Carreon Date: Fri, 20 Dec 2019 19:57:31 -0500 Subject: [PATCH 2/8] Solution and test case update --- exchange/exchange.go | 1 + exchange/exchange_test.go | 217 ++++++++++++++++++++++++++++++++------ 2 files changed, 187 insertions(+), 31 deletions(-) diff --git a/exchange/exchange.go b/exchange/exchange.go index 071b4f8d771..3d9055ca8a6 100644 --- a/exchange/exchange.go +++ b/exchange/exchange.go @@ -324,6 +324,7 @@ func (e *exchange) buildBidResponse(ctx context.Context, liveAdapters []openrtb_ if adapterBids[a] != nil && len(adapterBids[a].bids) > 0 { sb := e.makeSeatBid(adapterBids[a], a, adapterExtra, auc) seatBids = append(seatBids, *sb) + bidResponse.Cur = adapterBids[a].currency } } diff --git a/exchange/exchange_test.go b/exchange/exchange_test.go index 6718eb7df80..8848f955c63 100644 --- a/exchange/exchange_test.go +++ b/exchange/exchange_test.go @@ -314,53 +314,208 @@ func TestGetBidCacheInfo(t *testing.T) { assert.Equal(t, expCacheURL, cacheURL, "[TestGetBidCacheInfo] cacheId field in ext should equal \"%s\" \n", expCacheURL) } -func buildBidResponseParams(bidderName openrtb_ext.BidderName, bidRequest *openrtb.BidRequest) ([]openrtb_ext.BidderName, map[openrtb_ext.BidderName]*pbsOrtbSeatBid, json.RawMessage, map[openrtb_ext.BidderName]*seatResponseExtra) { +//func TestBidResponseCurrency(t *testing.T) { +// /* Init parameters */ +// auc := &auction{ +// cacheIds: map[*openrtb.Bid]string{ +// bids[0]: testUUID, +// }, +// } +// liveAdapters, adapterBids, resolvedRequest, adapterExtra := buildBidResponseParams() +// +// /* List test cases */ +// +// sampleBidRequest := &openrtb.BidRequest{ +// ID: "some-request-id", +// Imp: []openrtb.Imp{{ +// ID: "some-impression-id", +// Banner: &openrtb.Banner{Format: []openrtb.Format{{W: 300, H: 250}, {W: 300, H: 600}}}, +// Ext: json.RawMessage(`{"appnexus": {"placementId": 10433394}}`), +// }}, +// Site: &openrtb.Site{Page: "prebid.org", Ext: json.RawMessage(`{"amp":0}`)}, +// Device: &openrtb.Device{UA: "curl/7.54.0", IP: "::1"}, +// AT: 1, +// TMax: 500, +// Ext: json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 10433394}}}],"tmax": 500}`), +// } +// /* Execute tests */ +// for test := range testCases { +// /* 4) Build bid response */ +// adapterBids.currency = test.currency +// //sampleBidRequest.Cur = testCurrencyArray +// +// bid_resp, err := e.buildBidResponse(context.Background(), liveAdapters, adapterBids, sampleBidRequest, resolvedRequest, adapterExtra, auc, errList) +// } +// +// assert.NoError(t, err, "[TestBidResponseCurrency] buildBidResponse() threw an error") +// +// expectedBidResponse := &openrtb.BidResponse{ +// SeatBid: []openrtb.SeatBid{ +// { +// Seat: string(bidderName), +// Bid: []openrtb.Bid{ +// { +// Ext: json.RawMessage(`{ "prebid": { "cache": { "bids": { "cacheId": "` + testUUID + `", "url": "` + testExternalCacheHost + `/` + testExternalCachePath + `?uuid=` + testUUID + `" }, "key": "", "url": "" }`), +// }, +// }, +// }, +// }, +// } +//} +func TestBidResponseCurrency2(t *testing.T) { + /* 1) Adapter with a '& char in its endpoint property */ + /* https://github.com/prebid/prebid-server/issues/465 */ + cfg := &config.Configuration{ + Adapters: make(map[string]config.Adapter, 1), + } + cfg.Adapters["appnexus"] = config.Adapter{ + Endpoint: "http://ib.adnxs.com/openrtb2?query1&query2", //Note the '&' character in there + } + + /* 2) Init new exchange with said configuration */ + //Other parameters also needed to create exchange + handlerNoBidServer := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(204) } + server := httptest.NewServer(http.HandlerFunc(handlerNoBidServer)) + defer server.Close() + + e := NewExchange(server.Client(), nil, cfg, pbsmetrics.NewMetrics(metrics.NewRegistry(), openrtb_ext.BidderList(), config.DisabledMetrics{}), adapters.ParseBidderInfos(cfg.Adapters, "../static/bidder-info", openrtb_ext.BidderList()), gdpr.AlwaysAllow{}, currencies.NewRateConverterDefault()).(*exchange) + + /* 3) Build all the parameters e.buildBidResponse(ctx.Background(), liveA... ) needs */ //liveAdapters []openrtb_ext.BidderName, - liveAdapters := []openrtb_ext.BidderName{bidderName} + liveAdapters := make([]openrtb_ext.BidderName, 1) + liveAdapters[0] = "appnexus" //adapterBids map[openrtb_ext.BidderName]*pbsOrtbSeatBid, - adapterBids := map[openrtb_ext.BidderName]*pbsOrtbSeatBid{ - bidderName: { - bids: []*pbsOrtbBid{ - { - bid: &openrtb.Bid{ - ID: "some-imp-id", - Price: 9.517803, - W: 300, - H: 250, - }, - bidType: openrtb_ext.BidTypeBanner, - bidTargets: map[string]string{ - "pricegranularity": "med", - "includewinners": "true", - "includebidderkeys": "false", - }, - }, + aPbsOrtbBidArr := []*pbsOrtbBid{ + { + bid: &openrtb.Bid{ + ID: "some-imp-id", + ImpID: "", + Price: 9.517803, + NURL: "", + BURL: "", + LURL: "", + AdM: "", + AdID: "", + ADomain: nil, + Bundle: "", + IURL: "", + CID: "", + CrID: "", + Tactic: "", + Cat: nil, + Attr: nil, + API: 0, + Protocol: 0, + QAGMediaRating: 0, + Language: "", + DealID: "", + W: 300, + H: 250, + WRatio: 0, + HRatio: 0, + Exp: 0, + Ext: nil, }, + bidType: openrtb_ext.BidTypeBanner, + }, + } + adapterBids := map[openrtb_ext.BidderName]*pbsOrtbSeatBid{ + openrtb_ext.BidderName("appnexus"): { + bids: aPbsOrtbBidArr, currency: "USD", - //ext: bidRequest.Ext, }, } + //An openrtb.BidRequest struct as specified in https://github.com/prebid/prebid-server/issues/465 + bidRequest := &openrtb.BidRequest{ + ID: "some-request-id", + Imp: []openrtb.Imp{{ + ID: "some-impression-id", + Banner: &openrtb.Banner{Format: []openrtb.Format{{W: 300, H: 250}, {W: 300, H: 600}}}, + Ext: json.RawMessage(`{"appnexus": {"placementId": 10433394}}`), + }}, + Site: &openrtb.Site{Page: "prebid.org", Ext: json.RawMessage(`{"amp":0}`)}, + Device: &openrtb.Device{UA: "curl/7.54.0", IP: "::1"}, + AT: 1, + TMax: 500, + Ext: json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 10433394}}}],"tmax": 500}`), + } + //resolvedRequest json.RawMessage resolvedRequest := json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 10433394}}}],"tmax": 500}`) //adapterExtra map[openrtb_ext.BidderName]*seatResponseExtra, - adapterExtra := map[openrtb_ext.BidderName]*seatResponseExtra{ - bidderName: { - ResponseTimeMillis: 5, - Errors: []openrtb_ext.ExtBidderError{ - { - Code: 999, - Message: "Post ib.adnxs.com/openrtb2?query1&query2: unsupported protocol scheme \"\"", - }, - }, - }, + adapterExtra := make(map[openrtb_ext.BidderName]*seatResponseExtra, 1) + adapterExtra["appnexus"] = &seatResponseExtra{ + ResponseTimeMillis: 5, + Errors: []openrtb_ext.ExtBidderError{{Code: 999, Message: "Post ib.adnxs.com/openrtb2?query1&query2: unsupported protocol scheme \"\""}}, } - return liveAdapters, adapterBids, resolvedRequest, adapterExtra + //errList []error + var errList []error + + /* 4) Build bid response */ + bidResp, err := e.buildBidResponse(context.Background(), liveAdapters, adapterBids, bidRequest, resolvedRequest, adapterExtra, nil, errList) + + /* 5) Assert we have no errors and one '&' character as we are supposed to */ + if err != nil { + t.Errorf("exchange.buildBidResponse returned unexpected error: %v", err) + } + if len(errList) > 0 { + t.Errorf("exchange.buildBidResponse returned %d errors", len(errList)) + } + if bytes.Contains(bidResp.Ext, []byte("u0026")) { + t.Errorf("exchange.buildBidResponse() did not correctly print the '&' characters %s", string(bidResp.Ext)) + } } +//func buildBidResponseParams(bidRequest *openrtb.BidRequest) ([]openrtb_ext.BidderName, map[openrtb_ext.BidderName]*pbsOrtbSeatBid, json.RawMessage, map[openrtb_ext.BidderName]*seatResponseExtra) { +// //liveAdapters []openrtb_ext.BidderName, +// liveAdapters := []openrtb_ext.BidderName{openrtb_ext.BidderName("appnexus")} +// +// //adapterBids map[openrtb_ext.BidderName]*pbsOrtbSeatBid, +// adapterBids := map[openrtb_ext.BidderName]*pbsOrtbSeatBid{ +// bidderName: { +// bids: []*pbsOrtbBid{ +// { +// bid: &openrtb.Bid{ +// ID: "some-imp-id", +// Price: 9.517803, +// W: 300, +// H: 250, +// }, +// bidType: openrtb_ext.BidTypeBanner, +// bidTargets: map[string]string{ +// "pricegranularity": "med", +// "includewinners": "true", +// "includebidderkeys": "false", +// }, +// }, +// }, +// currency: "", +// }, +// } +// +// //resolvedRequest json.RawMessage +// resolvedRequest := json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 10433394}}}],"tmax": 500}`) +// +// //adapterExtra map[openrtb_ext.BidderName]*seatResponseExtra, +// adapterExtra := map[openrtb_ext.BidderName]*seatResponseExtra{ +// bidderName: { +// ResponseTimeMillis: 5, +// Errors: []openrtb_ext.ExtBidderError{ +// { +// Code: 999, +// Message: "Post ib.adnxs.com/openrtb2?query1&query2: unsupported protocol scheme \"\"", +// }, +// }, +// }, +// } +// +// return liveAdapters, adapterBids, resolvedRequest, adapterExtra +//} + // TestRaceIntegration runs an integration test using all the sample params from // adapters/{bidder}/{bidder}test/params/race/*.json files. // From 69425e61c8b7e5e25b731499fdae834ae28a4af6 Mon Sep 17 00:00:00 2001 From: Gus Carreon Date: Fri, 20 Dec 2019 21:25:31 -0500 Subject: [PATCH 3/8] exchange/exchange_test.go ready --- exchange/exchange_test.go | 275 +++++++++++++++----------------------- 1 file changed, 109 insertions(+), 166 deletions(-) diff --git a/exchange/exchange_test.go b/exchange/exchange_test.go index 8848f955c63..639569f6e62 100644 --- a/exchange/exchange_test.go +++ b/exchange/exchange_test.go @@ -314,120 +314,20 @@ func TestGetBidCacheInfo(t *testing.T) { assert.Equal(t, expCacheURL, cacheURL, "[TestGetBidCacheInfo] cacheId field in ext should equal \"%s\" \n", expCacheURL) } -//func TestBidResponseCurrency(t *testing.T) { -// /* Init parameters */ -// auc := &auction{ -// cacheIds: map[*openrtb.Bid]string{ -// bids[0]: testUUID, -// }, -// } -// liveAdapters, adapterBids, resolvedRequest, adapterExtra := buildBidResponseParams() -// -// /* List test cases */ -// -// sampleBidRequest := &openrtb.BidRequest{ -// ID: "some-request-id", -// Imp: []openrtb.Imp{{ -// ID: "some-impression-id", -// Banner: &openrtb.Banner{Format: []openrtb.Format{{W: 300, H: 250}, {W: 300, H: 600}}}, -// Ext: json.RawMessage(`{"appnexus": {"placementId": 10433394}}`), -// }}, -// Site: &openrtb.Site{Page: "prebid.org", Ext: json.RawMessage(`{"amp":0}`)}, -// Device: &openrtb.Device{UA: "curl/7.54.0", IP: "::1"}, -// AT: 1, -// TMax: 500, -// Ext: json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 10433394}}}],"tmax": 500}`), -// } -// /* Execute tests */ -// for test := range testCases { -// /* 4) Build bid response */ -// adapterBids.currency = test.currency -// //sampleBidRequest.Cur = testCurrencyArray -// -// bid_resp, err := e.buildBidResponse(context.Background(), liveAdapters, adapterBids, sampleBidRequest, resolvedRequest, adapterExtra, auc, errList) -// } -// -// assert.NoError(t, err, "[TestBidResponseCurrency] buildBidResponse() threw an error") -// -// expectedBidResponse := &openrtb.BidResponse{ -// SeatBid: []openrtb.SeatBid{ -// { -// Seat: string(bidderName), -// Bid: []openrtb.Bid{ -// { -// Ext: json.RawMessage(`{ "prebid": { "cache": { "bids": { "cacheId": "` + testUUID + `", "url": "` + testExternalCacheHost + `/` + testExternalCachePath + `?uuid=` + testUUID + `" }, "key": "", "url": "" }`), -// }, -// }, -// }, -// }, -// } -//} -func TestBidResponseCurrency2(t *testing.T) { - /* 1) Adapter with a '& char in its endpoint property */ - /* https://github.com/prebid/prebid-server/issues/465 */ - cfg := &config.Configuration{ - Adapters: make(map[string]config.Adapter, 1), - } - cfg.Adapters["appnexus"] = config.Adapter{ - Endpoint: "http://ib.adnxs.com/openrtb2?query1&query2", //Note the '&' character in there - } +func TestBidResponseCurrency(t *testing.T) { + // Init objects + cfg := &config.Configuration{Adapters: make(map[string]config.Adapter, 1)} + cfg.Adapters["appnexus"] = config.Adapter{Endpoint: "http://ib.adnxs.com"} - /* 2) Init new exchange with said configuration */ - //Other parameters also needed to create exchange handlerNoBidServer := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(204) } server := httptest.NewServer(http.HandlerFunc(handlerNoBidServer)) defer server.Close() e := NewExchange(server.Client(), nil, cfg, pbsmetrics.NewMetrics(metrics.NewRegistry(), openrtb_ext.BidderList(), config.DisabledMetrics{}), adapters.ParseBidderInfos(cfg.Adapters, "../static/bidder-info", openrtb_ext.BidderList()), gdpr.AlwaysAllow{}, currencies.NewRateConverterDefault()).(*exchange) - /* 3) Build all the parameters e.buildBidResponse(ctx.Background(), liveA... ) needs */ - //liveAdapters []openrtb_ext.BidderName, liveAdapters := make([]openrtb_ext.BidderName, 1) liveAdapters[0] = "appnexus" - //adapterBids map[openrtb_ext.BidderName]*pbsOrtbSeatBid, - aPbsOrtbBidArr := []*pbsOrtbBid{ - { - bid: &openrtb.Bid{ - ID: "some-imp-id", - ImpID: "", - Price: 9.517803, - NURL: "", - BURL: "", - LURL: "", - AdM: "", - AdID: "", - ADomain: nil, - Bundle: "", - IURL: "", - CID: "", - CrID: "", - Tactic: "", - Cat: nil, - Attr: nil, - API: 0, - Protocol: 0, - QAGMediaRating: 0, - Language: "", - DealID: "", - W: 300, - H: 250, - WRatio: 0, - HRatio: 0, - Exp: 0, - Ext: nil, - }, - bidType: openrtb_ext.BidTypeBanner, - }, - } - adapterBids := map[openrtb_ext.BidderName]*pbsOrtbSeatBid{ - openrtb_ext.BidderName("appnexus"): { - bids: aPbsOrtbBidArr, - currency: "USD", - }, - } - - //An openrtb.BidRequest struct as specified in https://github.com/prebid/prebid-server/issues/465 bidRequest := &openrtb.BidRequest{ ID: "some-request-id", Imp: []openrtb.Imp{{ @@ -442,80 +342,123 @@ func TestBidResponseCurrency2(t *testing.T) { Ext: json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 10433394}}}],"tmax": 500}`), } - //resolvedRequest json.RawMessage resolvedRequest := json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 10433394}}}],"tmax": 500}`) - //adapterExtra map[openrtb_ext.BidderName]*seatResponseExtra, - adapterExtra := make(map[openrtb_ext.BidderName]*seatResponseExtra, 1) - adapterExtra["appnexus"] = &seatResponseExtra{ - ResponseTimeMillis: 5, - Errors: []openrtb_ext.ExtBidderError{{Code: 999, Message: "Post ib.adnxs.com/openrtb2?query1&query2: unsupported protocol scheme \"\""}}, + adapterExtra := map[openrtb_ext.BidderName]*seatResponseExtra{ + "appnexus": &seatResponseExtra{ResponseTimeMillis: 5}, } - //errList []error var errList []error - /* 4) Build bid response */ - bidResp, err := e.buildBidResponse(context.Background(), liveAdapters, adapterBids, bidRequest, resolvedRequest, adapterExtra, nil, errList) + sampleBid := &openrtb.Bid{ + ID: "some-imp-id", + Price: 9.517803, + W: 300, + H: 250, + Ext: nil, + } + aPbsOrtbBidArr := []*pbsOrtbBid{ + { + bid: sampleBid, + bidType: openrtb_ext.BidTypeBanner, + }, + } + sampleSeatBid := []openrtb.SeatBid{ + { + Seat: string("appnexus"), + Bid: []openrtb.Bid{ + { + ID: "some-imp-id", + Price: 9.517803, + W: 300, + H: 250, + Ext: json.RawMessage(`{"prebid":{"type":"banner"}}`), + }, + }, + }, + } - /* 5) Assert we have no errors and one '&' character as we are supposed to */ - if err != nil { - t.Errorf("exchange.buildBidResponse returned unexpected error: %v", err) + // Test cases + type aTest struct { + description string + adapterBids map[openrtb_ext.BidderName]*pbsOrtbSeatBid + expectedBidResponse *openrtb.BidResponse } - if len(errList) > 0 { - t.Errorf("exchange.buildBidResponse returned %d errors", len(errList)) + testCases := []aTest{ + { + description: "1) Non-empty currency, non-empty bid array", + adapterBids: map[openrtb_ext.BidderName]*pbsOrtbSeatBid{ + openrtb_ext.BidderName("appnexus"): { + bids: aPbsOrtbBidArr, + currency: "USD", + }, + }, + expectedBidResponse: &openrtb.BidResponse{ + ID: "some-request-id", + SeatBid: sampleSeatBid, + Cur: "USD", + Ext: json.RawMessage(`{"responsetimemillis":{"appnexus":5},"tmaxrequest":500} +`), + }, + }, + { + description: "2) Non-empty currency, empty bid array", + adapterBids: map[openrtb_ext.BidderName]*pbsOrtbSeatBid{ + openrtb_ext.BidderName("appnexus"): { + bids: nil, + currency: "USD", + }, + }, + expectedBidResponse: &openrtb.BidResponse{ + ID: "some-request-id", + SeatBid: []openrtb.SeatBid{}, + Cur: "", + Ext: json.RawMessage(`{"responsetimemillis":{"appnexus":5},"tmaxrequest":500} +`), + }, + }, + { + description: "3) Empty currency, non-empty bid array", + adapterBids: map[openrtb_ext.BidderName]*pbsOrtbSeatBid{ + openrtb_ext.BidderName("appnexus"): { + bids: aPbsOrtbBidArr, + currency: "", + }, + }, + expectedBidResponse: &openrtb.BidResponse{ + ID: "some-request-id", + SeatBid: sampleSeatBid, + Cur: "", + Ext: json.RawMessage(`{"responsetimemillis":{"appnexus":5},"tmaxrequest":500} +`), + }, + }, + { + description: "4) Empty currency, empty bid array", + adapterBids: map[openrtb_ext.BidderName]*pbsOrtbSeatBid{ + openrtb_ext.BidderName("appnexus"): { + bids: nil, + currency: "USD", + }, + }, + expectedBidResponse: &openrtb.BidResponse{ + ID: "some-request-id", + SeatBid: []openrtb.SeatBid{}, + Cur: "", + Ext: json.RawMessage(`{"responsetimemillis":{"appnexus":5},"tmaxrequest":500} +`), + }, + }, } - if bytes.Contains(bidResp.Ext, []byte("u0026")) { - t.Errorf("exchange.buildBidResponse() did not correctly print the '&' characters %s", string(bidResp.Ext)) + + // Run tests + for i := range testCases { + actualBidResp, err := e.buildBidResponse(context.Background(), liveAdapters, testCases[i].adapterBids, bidRequest, resolvedRequest, adapterExtra, nil, errList) + assert.NoError(t, err, fmt.Sprintf("[TEST_FAILED] e.buildBidResponse resturns error in test: %s Error message: %s \n", testCases[i].description, err)) + assert.Equalf(t, testCases[i].expectedBidResponse, actualBidResp, fmt.Sprintf("[TEST_FAILED] Objects must be equal for test: %s \n Expected: >>%s<< \n Actual: >>%s<< ", testCases[i].description, testCases[i].expectedBidResponse.Ext, actualBidResp.Ext)) } } -//func buildBidResponseParams(bidRequest *openrtb.BidRequest) ([]openrtb_ext.BidderName, map[openrtb_ext.BidderName]*pbsOrtbSeatBid, json.RawMessage, map[openrtb_ext.BidderName]*seatResponseExtra) { -// //liveAdapters []openrtb_ext.BidderName, -// liveAdapters := []openrtb_ext.BidderName{openrtb_ext.BidderName("appnexus")} -// -// //adapterBids map[openrtb_ext.BidderName]*pbsOrtbSeatBid, -// adapterBids := map[openrtb_ext.BidderName]*pbsOrtbSeatBid{ -// bidderName: { -// bids: []*pbsOrtbBid{ -// { -// bid: &openrtb.Bid{ -// ID: "some-imp-id", -// Price: 9.517803, -// W: 300, -// H: 250, -// }, -// bidType: openrtb_ext.BidTypeBanner, -// bidTargets: map[string]string{ -// "pricegranularity": "med", -// "includewinners": "true", -// "includebidderkeys": "false", -// }, -// }, -// }, -// currency: "", -// }, -// } -// -// //resolvedRequest json.RawMessage -// resolvedRequest := json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 10433394}}}],"tmax": 500}`) -// -// //adapterExtra map[openrtb_ext.BidderName]*seatResponseExtra, -// adapterExtra := map[openrtb_ext.BidderName]*seatResponseExtra{ -// bidderName: { -// ResponseTimeMillis: 5, -// Errors: []openrtb_ext.ExtBidderError{ -// { -// Code: 999, -// Message: "Post ib.adnxs.com/openrtb2?query1&query2: unsupported protocol scheme \"\"", -// }, -// }, -// }, -// } -// -// return liveAdapters, adapterBids, resolvedRequest, adapterExtra -//} - // TestRaceIntegration runs an integration test using all the sample params from // adapters/{bidder}/{bidder}test/params/race/*.json files. // From 5915a784247dc43b411f3c83d9827f24e2c4205f Mon Sep 17 00:00:00 2001 From: Gus Carreon Date: Fri, 20 Dec 2019 22:39:38 -0500 Subject: [PATCH 4/8] Added test on the adform adapter itself --- adapters/adform/adform_test.go | 80 ++++++++++++++++++++++++++++++++++ exchange/exchange_test.go | 2 +- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/adapters/adform/adform_test.go b/adapters/adform/adform_test.go index e59bd951858..edfe98a2625 100644 --- a/adapters/adform/adform_test.go +++ b/adapters/adform/adform_test.go @@ -21,6 +21,8 @@ import ( "github.com/prebid/prebid-server/adapters" "github.com/prebid/prebid-server/config" "github.com/prebid/prebid-server/openrtb_ext" + + "github.com/stretchr/testify/assert" ) func TestJsonSamples(t *testing.T) { @@ -597,3 +599,81 @@ func TestPriceTypeUrlParameterCreation(t *testing.T) { } } } + +// Asserts that toOpenRtbBidResponse() creates a *adapters.BidderResponse with +// the currency of the last valid []*adformBid element and the expected number of bids +func TestToOpenRtbBidResponse(t *testing.T) { + // expected values + expectedBids := 3 + lastCurrency, anotherCurrency, emptyCurrency := "EUR", "USD", "" + + // init `toOpenRtbBidResponse` parameters + request := &openrtb.BidRequest{ + ID: "test-request-id", + Imp: []openrtb.Imp{ + { //1 + ID: "banner-imp-no1", + Ext: json.RawMessage(`{"bidder1": { "mid": "32341" }}`), + Banner: &openrtb.Banner{}, + }, + { //2 + ID: "banner-imp-no2", + Ext: json.RawMessage(`{"bidder1": { "mid": "32342" }}`), + Banner: &openrtb.Banner{}, + }, + { //3 + ID: "banner-imp-no3", + Ext: json.RawMessage(`{"bidder1": { "mid": "32343" }}`), + Banner: &openrtb.Banner{}, + }, + { //4 + ID: "banner-imp-no4", + Ext: json.RawMessage(`{"bidder1": { "mid": "32344" }}`), + Banner: &openrtb.Banner{}, + }, + }, + Device: &openrtb.Device{UA: "ua", IP: "ip"}, + User: &openrtb.User{BuyerUID: "buyerUID"}, + } + + testAdformBids := []*adformBid{ + { //1 + ResponseType: "banner", + Banner: "banner-content1", + Price: 1.23, + Currency: anotherCurrency, + Width: 300, + Height: 200, + DealId: "dealId1", + CreativeId: "creativeId1", + }, + {}, //2 + { //3 + ResponseType: "banner", + Banner: "banner-content2", + Price: 1.24, + Currency: emptyCurrency, + Width: 300, + Height: 200, + DealId: "dealId2", + CreativeId: "creativeId2", + }, + { //4 + ResponseType: "banner", + Banner: "banner-content3", + Price: 1.25, + Currency: lastCurrency, + Width: 300, + Height: 200, + DealId: "dealId3", + CreativeId: "creativeId3", + }, + } + + // Execute `toOpenRtbBidResponse` + actualBidResponse := toOpenRtbBidResponse(testAdformBids, request) + + // Assert number of bids and currency + assert.Equalf(t, expectedBids, len(actualBidResponse.Bids), fmt.Sprintf("[TestToOpenRtbBidResponse] actualBidResponse lenght differs\n")) + assert.Equalf(t, lastCurrency, actualBidResponse.Currency, fmt.Sprintf("[TestToOpenRtbBidResponse] actualBidResponse lenght differs\n")) +} diff --git a/exchange/exchange_test.go b/exchange/exchange_test.go index 639569f6e62..1d0fa99ed27 100644 --- a/exchange/exchange_test.go +++ b/exchange/exchange_test.go @@ -345,7 +345,7 @@ func TestBidResponseCurrency(t *testing.T) { resolvedRequest := json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 10433394}}}],"tmax": 500}`) adapterExtra := map[openrtb_ext.BidderName]*seatResponseExtra{ - "appnexus": &seatResponseExtra{ResponseTimeMillis: 5}, + "appnexus": {ResponseTimeMillis: 5}, } var errList []error From e2116e2839e010cdbad931a3fa70c3ad8e039eac Mon Sep 17 00:00:00 2001 From: Gus Carreon Date: Fri, 20 Dec 2019 22:51:11 -0500 Subject: [PATCH 5/8] added comments to exchange_test.go --- exchange/exchange_test.go | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/exchange/exchange_test.go b/exchange/exchange_test.go index 1d0fa99ed27..c76c8c53c60 100644 --- a/exchange/exchange_test.go +++ b/exchange/exchange_test.go @@ -357,12 +357,7 @@ func TestBidResponseCurrency(t *testing.T) { H: 250, Ext: nil, } - aPbsOrtbBidArr := []*pbsOrtbBid{ - { - bid: sampleBid, - bidType: openrtb_ext.BidTypeBanner, - }, - } + aPbsOrtbBidArr := []*pbsOrtbBid{{bid: sampleBid, bidType: openrtb_ext.BidTypeBanner}} sampleSeatBid := []openrtb.SeatBid{ { Seat: string("appnexus"), @@ -377,6 +372,7 @@ func TestBidResponseCurrency(t *testing.T) { }, }, } + emptySeatBid := []openrtb.SeatBid{} // Test cases type aTest struct { @@ -386,7 +382,7 @@ func TestBidResponseCurrency(t *testing.T) { } testCases := []aTest{ { - description: "1) Non-empty currency, non-empty bid array", + description: "1) Adapter to bids map comes with a non-empty currency field and non-empty bid array", adapterBids: map[openrtb_ext.BidderName]*pbsOrtbSeatBid{ openrtb_ext.BidderName("appnexus"): { bids: aPbsOrtbBidArr, @@ -402,7 +398,7 @@ func TestBidResponseCurrency(t *testing.T) { }, }, { - description: "2) Non-empty currency, empty bid array", + description: "2) Adapter to bids map comes with a non-empty currency field but an empty bid array", adapterBids: map[openrtb_ext.BidderName]*pbsOrtbSeatBid{ openrtb_ext.BidderName("appnexus"): { bids: nil, @@ -411,14 +407,14 @@ func TestBidResponseCurrency(t *testing.T) { }, expectedBidResponse: &openrtb.BidResponse{ ID: "some-request-id", - SeatBid: []openrtb.SeatBid{}, + SeatBid: emptySeatBid, Cur: "", Ext: json.RawMessage(`{"responsetimemillis":{"appnexus":5},"tmaxrequest":500} `), }, }, { - description: "3) Empty currency, non-empty bid array", + description: "3) Adapter to bids map comes with an empty currency string and a non-empty bid array", adapterBids: map[openrtb_ext.BidderName]*pbsOrtbSeatBid{ openrtb_ext.BidderName("appnexus"): { bids: aPbsOrtbBidArr, @@ -434,16 +430,16 @@ func TestBidResponseCurrency(t *testing.T) { }, }, { - description: "4) Empty currency, empty bid array", + description: "4) Adapter to bids map comes with an empty currency string and an empty bid array", adapterBids: map[openrtb_ext.BidderName]*pbsOrtbSeatBid{ openrtb_ext.BidderName("appnexus"): { bids: nil, - currency: "USD", + currency: "", }, }, expectedBidResponse: &openrtb.BidResponse{ ID: "some-request-id", - SeatBid: []openrtb.SeatBid{}, + SeatBid: emptySeatBid, Cur: "", Ext: json.RawMessage(`{"responsetimemillis":{"appnexus":5},"tmaxrequest":500} `), From a82cc58c98b219add7b67957f0fa419e2603b888 Mon Sep 17 00:00:00 2001 From: Gus Carreon Date: Tue, 7 Jan 2020 14:42:52 -0500 Subject: [PATCH 6/8] Corrections to address PR comments --- adapters/adform/adform_test.go | 36 +++++++++++++++------------------- exchange/exchange_test.go | 2 +- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/adapters/adform/adform_test.go b/adapters/adform/adform_test.go index edfe98a2625..63646f5f7f5 100644 --- a/adapters/adform/adform_test.go +++ b/adapters/adform/adform_test.go @@ -603,30 +603,28 @@ func TestPriceTypeUrlParameterCreation(t *testing.T) { // Asserts that toOpenRtbBidResponse() creates a *adapters.BidderResponse with // the currency of the last valid []*adformBid element and the expected number of bids func TestToOpenRtbBidResponse(t *testing.T) { - // expected values expectedBids := 3 lastCurrency, anotherCurrency, emptyCurrency := "EUR", "USD", "" - // init `toOpenRtbBidResponse` parameters request := &openrtb.BidRequest{ ID: "test-request-id", Imp: []openrtb.Imp{ - { //1 + { ID: "banner-imp-no1", Ext: json.RawMessage(`{"bidder1": { "mid": "32341" }}`), Banner: &openrtb.Banner{}, }, - { //2 + { ID: "banner-imp-no2", Ext: json.RawMessage(`{"bidder1": { "mid": "32342" }}`), Banner: &openrtb.Banner{}, }, - { //3 + { ID: "banner-imp-no3", Ext: json.RawMessage(`{"bidder1": { "mid": "32343" }}`), Banner: &openrtb.Banner{}, }, - { //4 + { ID: "banner-imp-no4", Ext: json.RawMessage(`{"bidder1": { "mid": "32344" }}`), Banner: &openrtb.Banner{}, @@ -637,7 +635,7 @@ func TestToOpenRtbBidResponse(t *testing.T) { } testAdformBids := []*adformBid{ - { //1 + { ResponseType: "banner", Banner: "banner-content1", Price: 1.23, @@ -647,33 +645,31 @@ func TestToOpenRtbBidResponse(t *testing.T) { DealId: "dealId1", CreativeId: "creativeId1", }, - {}, //2 - { //3 + {}, + { ResponseType: "banner", - Banner: "banner-content2", + Banner: "banner-content3", Price: 1.24, Currency: emptyCurrency, Width: 300, Height: 200, - DealId: "dealId2", - CreativeId: "creativeId2", + DealId: "dealId3", + CreativeId: "creativeId3", }, - { //4 + { ResponseType: "banner", - Banner: "banner-content3", + Banner: "banner-content4", Price: 1.25, Currency: lastCurrency, Width: 300, Height: 200, - DealId: "dealId3", - CreativeId: "creativeId3", + DealId: "dealId4", + CreativeId: "creativeId4", }, } - // Execute `toOpenRtbBidResponse` actualBidResponse := toOpenRtbBidResponse(testAdformBids, request) - // Assert number of bids and currency - assert.Equalf(t, expectedBids, len(actualBidResponse.Bids), fmt.Sprintf("[TestToOpenRtbBidResponse] actualBidResponse lenght differs\n")) - assert.Equalf(t, lastCurrency, actualBidResponse.Currency, fmt.Sprintf("[TestToOpenRtbBidResponse] actualBidResponse lenght differs\n")) + assert.Equalf(t, expectedBids, len(actualBidResponse.Bids), "bid count") + assert.Equalf(t, lastCurrency, actualBidResponse.Currency, "currency") } diff --git a/exchange/exchange_test.go b/exchange/exchange_test.go index c76c8c53c60..e87197461d3 100644 --- a/exchange/exchange_test.go +++ b/exchange/exchange_test.go @@ -360,7 +360,7 @@ func TestBidResponseCurrency(t *testing.T) { aPbsOrtbBidArr := []*pbsOrtbBid{{bid: sampleBid, bidType: openrtb_ext.BidTypeBanner}} sampleSeatBid := []openrtb.SeatBid{ { - Seat: string("appnexus"), + Seat: "appnexus", Bid: []openrtb.Bid{ { ID: "some-imp-id", From 158bc22f2c40164295984637dcc52465c1cafbf5 Mon Sep 17 00:00:00 2001 From: Gus Carreon Date: Thu, 9 Jan 2020 17:40:26 -0500 Subject: [PATCH 7/8] Correct conflict --- exchange/exchange_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exchange/exchange_test.go b/exchange/exchange_test.go index e87197461d3..1f633d80b28 100644 --- a/exchange/exchange_test.go +++ b/exchange/exchange_test.go @@ -342,7 +342,7 @@ func TestBidResponseCurrency(t *testing.T) { Ext: json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 10433394}}}],"tmax": 500}`), } - resolvedRequest := json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 10433394}}}],"tmax": 500}`) + resolvedRequest := json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 1}}}],"tmax": 500}`) adapterExtra := map[openrtb_ext.BidderName]*seatResponseExtra{ "appnexus": {ResponseTimeMillis: 5}, From c57b31a21eb381dd7f16c9b8b620fa67048b0178 Mon Sep 17 00:00:00 2001 From: Gus Carreon Date: Thu, 9 Jan 2020 18:03:33 -0500 Subject: [PATCH 8/8] Correct some more conflicts --- exchange/exchange_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/exchange/exchange_test.go b/exchange/exchange_test.go index 1f633d80b28..31dddae4c74 100644 --- a/exchange/exchange_test.go +++ b/exchange/exchange_test.go @@ -103,17 +103,17 @@ func TestCharacterEscape(t *testing.T) { Imp: []openrtb.Imp{{ ID: "some-impression-id", Banner: &openrtb.Banner{Format: []openrtb.Format{{W: 300, H: 250}, {W: 300, H: 600}}}, - Ext: json.RawMessage(`{"appnexus": {"placementId": 10433394}}`), + Ext: json.RawMessage(`{"appnexus": {"placementId": 1}}`), }}, Site: &openrtb.Site{Page: "prebid.org", Ext: json.RawMessage(`{"amp":0}`)}, Device: &openrtb.Device{UA: "curl/7.54.0", IP: "::1"}, AT: 1, TMax: 500, - Ext: json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 10433394}}}],"tmax": 500}`), + Ext: json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 1}}}],"tmax": 500}`), } //resolvedRequest json.RawMessage - resolvedRequest := json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 10433394}}}],"tmax": 500}`) + resolvedRequest := json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 1}}}],"tmax": 500}`) //adapterExtra map[openrtb_ext.BidderName]*seatResponseExtra, adapterExtra := make(map[openrtb_ext.BidderName]*seatResponseExtra, 1) @@ -231,7 +231,7 @@ func TestGetBidCacheInfo(t *testing.T) { } //resolvedRequest json.RawMessage - resolvedRequest := json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 10433394}}}],"tmax": 500}`) + resolvedRequest := json.RawMessage(`{"id": "some-request-id","site": {"page": "prebid.org"},"imp": [{"id": "some-impression-id","banner": {"format": [{"w": 300,"h": 250},{"w": 300,"h": 600}]},"ext": {"appnexus": {"placementId": 1}}}],"tmax": 500}`) //adapterExtra map[openrtb_ext.BidderName]*seatResponseExtra, adapterExtra := map[openrtb_ext.BidderName]*seatResponseExtra{ @@ -259,7 +259,7 @@ func TestGetBidCacheInfo(t *testing.T) { "siteId": 113932, "zoneId": 535510 }, - "appnexus": { "placementId": 10433394 }, + "appnexus": { "placementId": 1 }, "pubmatic": { "publisherId": "156209", "adSlot": "pubmatic_test2@300x250" }, "pulsepoint": { "cf": "300X250", "cp": 512379, "ct": 486653 }, "conversant": { "site_id": "108060" },