Skip to content

Commit

Permalink
Minor code and unit tests refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Veronika Solovei committed Aug 24, 2020
1 parent 877aa76 commit e77ad3f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
6 changes: 3 additions & 3 deletions adapters/appnexus/appnexus.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func (a *AppNexusAdapter) MakeRequests(request *openrtb.BidRequest, reqInfo *ada
if isVIDEO == 1 {
podImps := groupByPods(imps)

requests := make([]*adapters.RequestData, 0, 0)
requests := make([]*adapters.RequestData, 0, len(podImps))
for _, podImps := range podImps {
reqExt.Appnexus.AdPodId = generatePodId()

Expand Down Expand Up @@ -396,7 +396,7 @@ func groupByPods(imps []openrtb.Imp) map[string]([]openrtb.Imp) {
return podImps
}

func requestHelper(request *openrtb.BidRequest, requestExtension appnexusReqExt, errs []error) {
func marshalAndSetRequestExt(request *openrtb.BidRequest, requestExtension appnexusReqExt, errs []error) {
var err error
request.Ext, err = json.Marshal(requestExtension)
if err != nil {
Expand All @@ -419,7 +419,7 @@ func splitRequests(imps []openrtb.Imp, request *openrtb.BidRequest, requestExten
headers.Add("Content-Type", "application/json;charset=utf-8")
headers.Add("Accept", "application/json")

requestHelper(request, requestExtension, errs)
marshalAndSetRequestExt(request, requestExtension, errs)

for impsLeft {

Expand Down
52 changes: 26 additions & 26 deletions adapters/appnexus/appnexus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ func TestVideoSinglePod(t *testing.T) {

result, err := a.MakeRequests(&req, &reqInfo)

assert.Equal(t, len(err), 0, "Errors array should be empty")
assert.Equal(t, len(result), 1, "Only one request should be returned")
assert.Empty(t, err, "Errors array should be empty")
assert.Len(t, result, 1, "Only one request should be returned")

var error error
var reqData *openrtb.BidRequest
error = json.Unmarshal(result[0].Body, &reqData)
assert.Nil(t, error, "Response body unmarshalling error should be nil")
assert.NoError(t, error, "Response body unmarshalling error should be nil")

var reqDataExt *appnexusReqExt
error = json.Unmarshal(reqData.Ext, &reqDataExt)
assert.Nil(t, error, "Response ext unmarshalling error should be nil")
assert.NoError(t, error, "Response ext unmarshalling error should be nil")

regMatch, matchErr := regexp.Match(`[0-9]19`, []byte(reqDataExt.Appnexus.AdPodId))
assert.Nil(t, matchErr, "Regex match error should be nil")
assert.NoError(t, matchErr, "Regex match error should be nil")
assert.True(t, regMatch, "AdPod id doesn't present in Appnexus extension or has incorrect format")
}

Expand Down Expand Up @@ -111,27 +111,27 @@ func TestVideoSinglePodManyImps(t *testing.T) {

res, err := a.MakeRequests(&req, &reqInfo)

assert.Equal(t, len(err), 0, "Errors array should be empty")
assert.Equal(t, len(res), 2, "Two requests should be returned")
assert.Empty(t, err, "Errors array should be empty")
assert.Len(t, res, 2, "Two requests should be returned")

var error error
var reqData1 *openrtb.BidRequest
error = json.Unmarshal(res[0].Body, &reqData1)
assert.Equal(t, error, nil, "Response body unmarshalling error should be nil")
assert.NoError(t, error, "Response body unmarshalling error should be nil")

var reqDataExt1 *appnexusReqExt
error = json.Unmarshal(reqData1.Ext, &reqDataExt1)
assert.Equal(t, error, nil, "Response ext unmarshalling error should be nil")
assert.NoError(t, error, "Response ext unmarshalling error should be nil")

adPodId1 := reqDataExt1.Appnexus.AdPodId

var reqData2 *openrtb.BidRequest
error = json.Unmarshal(res[1].Body, &reqData2)
assert.Equal(t, error, nil, "Response body unmarshalling error should be nil")
assert.NoError(t, error, "Response body unmarshalling error should be nil")

var reqDataExt2 *appnexusReqExt
error = json.Unmarshal(reqData2.Ext, &reqDataExt2)
assert.Equal(t, error, nil, "Response ext unmarshalling error should be nil")
assert.NoError(t, error, "Response ext unmarshalling error should be nil")

adPodId2 := reqDataExt2.Appnexus.AdPodId

Expand Down Expand Up @@ -163,27 +163,27 @@ func TestVideoTwoPods(t *testing.T) {

res, err := a.MakeRequests(&req, &reqInfo)

assert.Equal(t, len(err), 0, "Errors array should be empty")
assert.Equal(t, len(res), 2, "Two request should be returned")
assert.Empty(t, err, "Errors array should be empty")
assert.Len(t, res, 2, "Two request should be returned")

var error error
var reqData1 *openrtb.BidRequest
error = json.Unmarshal(res[0].Body, &reqData1)
assert.Equal(t, error, nil, "Response body unmarshalling error should be nil")
assert.NoError(t, error, "Response body unmarshalling error should be nil")

var reqDataExt1 *appnexusReqExt
error = json.Unmarshal(reqData1.Ext, &reqDataExt1)
assert.Equal(t, error, nil, "Response ext unmarshalling error should be nil")
assert.NoError(t, error, "Response ext unmarshalling error should be nil")

adPodId1 := reqDataExt1.Appnexus.AdPodId

var reqData2 *openrtb.BidRequest
error = json.Unmarshal(res[1].Body, &reqData2)
assert.Equal(t, error, nil, "Response body unmarshalling error should be nil")
assert.NoError(t, error, "Response body unmarshalling error should be nil")

var reqDataExt2 *appnexusReqExt
error = json.Unmarshal(reqData2.Ext, &reqDataExt2)
assert.Equal(t, error, nil, "Response ext unmarshalling error should be nil")
assert.NoError(t, error, "Response ext unmarshalling error should be nil")

adPodId2 := reqDataExt2.Appnexus.AdPodId

Expand Down Expand Up @@ -227,33 +227,33 @@ func TestVideoTwoPodsManyImps(t *testing.T) {

res, err := a.MakeRequests(&req, &reqInfo)

assert.Equal(t, len(err), 0, "Errors array should be empty")
assert.Equal(t, len(res), 3, "Three requests should be returned")
assert.Len(t, err, 0, "Errors array should be empty")
assert.Len(t, res, 3, "Three requests should be returned")

var error error
var reqData1 *openrtb.BidRequest
error = json.Unmarshal(res[0].Body, &reqData1)
assert.Equal(t, error, nil, "Response body unmarshalling error should be nil")
assert.NoError(t, error, "Response body unmarshalling error should be nil")

var reqDataExt1 *appnexusReqExt
error = json.Unmarshal(reqData1.Ext, &reqDataExt1)
assert.Equal(t, error, nil, "Response ext unmarshalling error should be nil")
assert.NoError(t, error, "Response ext unmarshalling error should be nil")

var reqData2 *openrtb.BidRequest
error = json.Unmarshal(res[1].Body, &reqData2)
assert.Equal(t, error, nil, "Response body unmarshalling error should be nil")
assert.NoError(t, error, "Response body unmarshalling error should be nil")

var reqDataExt2 *appnexusReqExt
error = json.Unmarshal(reqData2.Ext, &reqDataExt2)
assert.Equal(t, error, nil, "Response ext unmarshalling error should be nil")
assert.NoError(t, error, "Response ext unmarshalling error should be nil")

var reqData3 *openrtb.BidRequest
error = json.Unmarshal(res[2].Body, &reqData3)
assert.Equal(t, error, nil, "Response body unmarshalling error should be nil")
assert.NoError(t, error, "Response body unmarshalling error should be nil")

var reqDataExt3 *appnexusReqExt
error = json.Unmarshal(reqData3.Ext, &reqDataExt3)
assert.Equal(t, error, nil, "Response ext unmarshalling error should be nil")
assert.NoError(t, error, "Response ext unmarshalling error should be nil")

adPodId1 := reqDataExt1.Appnexus.AdPodId
adPodId2 := reqDataExt2.Appnexus.AdPodId
Expand All @@ -264,7 +264,7 @@ func TestVideoTwoPodsManyImps(t *testing.T) {
podIds[adPodId2] = podIds[adPodId2] + 1
podIds[adPodId3] = podIds[adPodId3] + 1

assert.Equal(t, len(podIds), 2, "Incorrect number of unique pod ids")
assert.Len(t, podIds, 2, "Incorrect number of unique pod ids")
}

// ----------------------------------------------------------------------------
Expand Down

0 comments on commit e77ad3f

Please sign in to comment.