Skip to content

Commit

Permalink
Add unit tests for bidmanager.getKeyValueTargetingPairs
Browse files Browse the repository at this point in the history
  • Loading branch information
mkendall07 committed Jan 6, 2016
1 parent 3a436e2 commit ab82f83
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 7 deletions.
4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ gulp.task('codeQuality', ['jshint', 'jscs'], function() {});

gulp.task('default', ['build'], function() {});

gulp.task('serve', ['build-dev', 'watch'], function () {
gulp.task('serve', ['build-dev', 'watch', 'browser-sync'], function () {
var port = 9999;
require('http').createServer(ecstatic({
root: __dirname
})).listen(port);
console.log('Server started at http://localhost:' + port + '/');
});

gulp.task('build-dev', ['jscs', 'clean-dist', 'browserify', 'browser-sync', 'unit-tests'], function () {
gulp.task('build-dev', ['jscs', 'clean-dist', 'browserify', 'unit-tests'], function () {
gulp.src(['src/prebid.js'])
.pipe(gulpBrowserify({
debug: false
Expand Down
6 changes: 3 additions & 3 deletions src/bidmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ exports.addBidResponse = function(adUnitCode, bid) {
//if there is any key value pairs to map do here
var keyValues = {};
if (bid.bidderCode && bid.cpm !== 0) {
keyValues = getKeyValueTargetingPairs(bid.bidderCode, bid);
keyValues = this.getKeyValueTargetingPairs(bid.bidderCode, bid);
bid.adserverTargeting = keyValues;
}

Expand Down Expand Up @@ -213,7 +213,7 @@ exports.createEmptyBidResponseObj = function() {
};
};

function getKeyValueTargetingPairs(bidderCode, custBidObj) {
exports.getKeyValueTargetingPairs = function(bidderCode, custBidObj) {
//retrive key value settings
var keyValues = {};
var bidder_settings = pbjs.bidderSettings || {};
Expand Down Expand Up @@ -260,7 +260,7 @@ function getKeyValueTargetingPairs(bidderCode, custBidObj) {
}

return keyValues;
}
};

function setKeys(keyValues, bidderSettings, custBidObj) {
var targeting = bidderSettings[CONSTANTS.JSON_MAPPING.ADSERVER_TARGETING];
Expand Down
11 changes: 9 additions & 2 deletions test/automatedRunnner.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@
<html>
<head lang="en">
<meta charset="UTF-8">
<title>TextChanger tests</title>
<title>Unit Tests for Prebid.js</title>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css"/>
</head>
<body>
<div id="mocha"></div>
<script src="../node_modules/mocha/mocha.js"></script>

<!-- <script src="lib/blanket.js"></script> -->


<script>mocha.setup('bdd')</script>
<script src="tests-browserify.js"></script>
<script src="tests-browserify.js" data-cover></script>

<script>

if (window.mochaPhantomJS) {
mochaPhantomJS.run();
} else {
mocha.run();
}

</script>
</body>
</html>
156 changes: 156 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var assert = require("assert");

//TODO refactor to use the spec files
var utils = require('../src/utils');
var bidmanager = require('../src/bidmanager');

describe('replaceTokenInString', function(){

Expand All @@ -22,4 +23,159 @@ var utils = require('../src/utils');

assert.equal(output, "hello %FOO%");
});
});


describe('bidmanager.js', function(){



describe('getKeyValueTargetingPairs', function(){
var bid = {};
var bidPriceCpm = 5.578;
var bidPbLg = 5.50;
var bidPbMg = 5.50;
var bidPbHg = 5.57;
var adUnitCode = '12345';
var bidderCode = 'appnexus';
var size = '300x250';
var adId = '1adId';

before(function() {
console.log(pbjs);
bid.cpm = bidPriceCpm;
bid.pbLg = bidPbLg;
bid.pbMg = bidPbMg;
bid.pbHg = bidPbHg;
bid.height = 300;
bid.width = 250;
bid.adUnitCode = adUnitCode;
bid.getSize = function(){
return this.height + 'x' + this.width;
};
bid.bidderCode = bidderCode;
bid.adId = adId;

});


it('No bidder level configuration defined - default', function() {
var expected = {"hb_bidder": bidderCode, "hb_adid": adId,"hb_pb": bidPbMg,"hb_size": size};
var response = bidmanager.getKeyValueTargetingPairs(bidderCode, bid);
assert.deepEqual(response, expected);

});

it('Custom configuration for all bidders', function() {
pbjs.bidderSettings =
{
standard: {
adserverTargeting: [{
key: "hb_bidder",
val: function(bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: "hb_adid",
val: function(bidResponse) {
return bidResponse.adId;
}
}, {
key: "hb_pb",
val: function(bidResponse) {
//change default here
return bidResponse.pbHg;
}
}, {
key: "hb_size",
val: function(bidResponse) {
return bidResponse.size;

}
}]

}
};

var expected = {"hb_bidder": bidderCode, "hb_adid": adId,"hb_pb": bidPbHg,"hb_size": size};
var response = bidmanager.getKeyValueTargetingPairs(bidderCode, bid);
assert.deepEqual(response, expected);

});

it('Custom configuration for one bidder', function() {
pbjs.bidderSettings =
{
appnexus: {
adserverTargeting: [{
key: "hb_bidder",
val: function(bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: "hb_adid",
val: function(bidResponse) {
return bidResponse.adId;
}
}, {
key: "hb_pb",
val: function(bidResponse) {
//change default here
return bidResponse.pbHg;
}
}, {
key: "hb_size",
val: function(bidResponse) {
return bidResponse.size;

}
}]

}
};

var expected = {"hb_bidder": bidderCode, "hb_adid": adId,"hb_pb": bidPbHg,"hb_size": size};
var response = bidmanager.getKeyValueTargetingPairs(bidderCode, bid);
assert.deepEqual(response, expected);

});

it('Custom configuration for one bidder - not matched', function() {
pbjs.bidderSettings =
{
nonExistentBidder: {
adserverTargeting: [{
key: "hb_bidder",
val: function(bidResponse) {
return bidResponse.bidderCode;
}
}, {
key: "hb_adid",
val: function(bidResponse) {
return bidResponse.adId;
}
}, {
key: "hb_pb",
val: function(bidResponse) {
//change default here
return bidResponse.pbHg;
}
}, {
key: "hb_size",
val: function(bidResponse) {
return bidResponse.size;

}
}]

}
};

var expected = {"hb_bidder": bidderCode, "hb_adid": adId,"hb_pb": bidPbMg,"hb_size": size};
var response = bidmanager.getKeyValueTargetingPairs(bidderCode, bid);
assert.deepEqual(response, expected);

});

});
});

0 comments on commit ab82f83

Please sign in to comment.