Skip to content

Commit

Permalink
Merge pull request #154 from prebid/unit_test_upgrade
Browse files Browse the repository at this point in the history
Unit test upgrade
  • Loading branch information
mkendall07 committed Jan 6, 2016
2 parents ffae99b + ab82f83 commit b052781
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 19 deletions.
62 changes: 47 additions & 15 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ var jscs = require('gulp-jscs');
var header = require('gulp-header');
var del = require('del');
var ecstatic = require('ecstatic');
var browserify = require('gulp-browserify');
var gulpBrowserify = require('gulp-browserify');
var gutil = require("gulp-util");
var gulpJsdoc2md = require("gulp-jsdoc-to-markdown");
var concat = require("gulp-concat");
var zip = require('gulp-zip');
var mocha = require('gulp-mocha');
var preprocessify = require('preprocessify');
/** for automated unit testing */
var browserSync = require("browser-sync"),
browserify = require("browserify"),
source = require("vinyl-source-stream"),
mochaPhantomJS = require("gulp-mocha-phantomjs");


var releaseDir = './dist/';
var csaSrcLocation = './src/prebid.js';
Expand Down Expand Up @@ -93,41 +99,35 @@ gulp.task('clean-dist', function(cb) {

});

//longer tests go here, to be run only when specfcifcally testing
gulp.task('runAdvancedTests', function() {

});

gulp.task('codeQuality', ['jshint', 'jscs'], function() {});

gulp.task('testAll', ['runBasicTests', 'runAdvancedTests'], 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'], function () {
gulp.src(['src/prebid.js'])
.pipe(browserify({
debug: false
}))
gulp.task('build-dev', ['jscs', 'clean-dist', 'browserify', 'unit-tests'], function () {
gulp.src(['src/prebid.js'])
.pipe(gulpBrowserify({
debug: false
}))
.pipe(header(banner, {
pkg: pkg
}))
.pipe(gulp.dest(releaseDir));
.pipe(gulp.dest(releaseDir));

});

gulp.task('build-minify', ['clean-dist', 'quality'], function(cb){

gulp.src(['src/prebid.js'])
.pipe(browserify({
.pipe(gulpBrowserify({
transform : preprocessify({ NODE_ENV: 'production'})
}

Expand Down Expand Up @@ -155,6 +155,7 @@ gulp.task('quality', ['jscs'], function(cb){

gulp.task('watch', function () {
gulp.watch(['src/**/*.js'], ['build-dev']);
//gulp.watch(["test/tests.js", "src/*"], ["browserify", "unit-tests"]);
});


Expand All @@ -179,3 +180,34 @@ gulp.task('zip', ['quality', 'clean-dist', 'build-minify'], function () {
.pipe(gulp.dest('./'));
});

gulp.task("browser-sync", function () {
"use strict";
browserSync({
server: {
//serve tests and the root as base dirs
baseDir: ["./test/", "./"],
//make tests.html the index file
index: "automatedRunnner.html"
}
});
});

//see http://fettblog.eu/gulp-browserify-multiple-bundles/
gulp.task("browserify", function() {
"use strict";
return browserify("./test/test.js")
.bundle()
.on("error", function (err) {
console.log(err.toString());
this.emit("end");
})
.pipe(source("tests-browserify.js"))
.pipe(gulp.dest("test/"))
.pipe(browserSync.reload({stream:true}));
});

gulp.task("unit-tests", function () {
"use strict";
return gulp.src("./test/automatedRunnner.html")
.pipe(mochaPhantomJS());
});
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"author": [],
"license": "NONE",
"devDependencies": {
"browser-sync": "^2.11.0",
"browserify": "^12.0.1",
"del": "^1.1.1",
"ecstatic": "^0.8.0",
"gulp": "^3.9.0",
Expand All @@ -19,6 +21,7 @@
"gulp-jsdoc-to-markdown": "^1.1.1",
"gulp-jshint": "^1.9.2",
"gulp-mocha": "^2.1.3",
"gulp-mocha-phantomjs": "^0.10.1",
"gulp-notify": "^2.2.0",
"gulp-rename": "^1.2.2",
"gulp-sourcemaps": "^1.5.0",
Expand All @@ -28,7 +31,10 @@
"gulp-zip": "^3.0.2",
"jsdoc-to-markdown": "^1.1.1",
"jshint-stylish": "^2.0.1",
"preprocessify": "0.0.6"
"mocha": "^2.3.4",
"mocha-phantomjs": "^4.0.2",
"preprocessify": "0.0.6",
"vinyl-source-stream": "^1.1.0"
},
"dependencies": {}
}
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
29 changes: 29 additions & 0 deletions test/automatedRunnner.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<!-- Test runner HTML file for executing tests with PhantomJS & displaying results -->
<html>
<head lang="en">
<meta charset="UTF-8">
<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" data-cover></script>

<script>

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

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

/* use this method to test individual files instead of the whole prebid.js project */

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

describe('replaceTokenInString', function(){

it('should replace all given tokens in a String', function() {
var tokensToReplace = {
'foo': 'bar',
'zap': 'quux'
};

var output = utils.replaceTokenInString("hello %FOO%, I am %ZAP%", tokensToReplace, "%");
assert.equal(output, "hello bar, I am quux");
});

it('should ignore tokens it does not see', function() {
var output = utils.replaceTokenInString("hello %FOO%", {}, "%");

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 b052781

Please sign in to comment.