Skip to content

Commit

Permalink
Added a few unit tests, and added fixtures for shared data structures.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbemiller committed Jun 22, 2017
1 parent 33c6707 commit 15e6db4
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/bidmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ exports.addBidResponse = function (adUnitCode, bid) {
if (bid.adUnitCode && bidsBackAdUnit(bid.adUnitCode)) {
triggerAdUnitCallbacks(bid.adUnitCode);
}

if (bidsBackAll()) {
exports.executeCallback();
}
}

// Video bids may fail if the cache is down, or there's trouble on the network.
Expand All @@ -172,7 +176,7 @@ exports.addBidResponse = function (adUnitCode, bid) {
if (error) {
utils.logWarn(`Failed to save to the video cache: ${error}. Video bid must be discarded.`);
} else {
bid.videoCacheKey = cacheIds[0];
bid.videoCacheKey = cacheIds[0].cacheId;
addBidToAuction(bid);
}
});
Expand All @@ -186,10 +190,6 @@ exports.addBidResponse = function (adUnitCode, bid) {
} else {
addBidToAuction(bid);
}

if (bidsBackAll()) {
exports.executeCallback();
}
}
};

Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/video/adUnit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"code": "video1",
"sizes": [640,480],
"mediaType": "video",
"bids": [
{
"bidder": "appnexusAst",
"params": {
"placementId": "9333431",
"video": {
"skipppable": false,
"playback_methods": ["auto_play_sound_off"]
}
}
}
]
}
26 changes: 26 additions & 0 deletions test/fixtures/video/bidRequest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"auctionStart": 1462918897459,
"bidderCode": "appnexusAst",
"bidderRequestId": "2946b569352ef2",
"bids": [
{
"bidder": "appnexusAst",
"params": {
"placementId": "9333431",
"video": {
"skipppable": false,
"playback_methods": ["auto_play_sound_off"]
}
},
"placementCode": "video1",
"sizes": [640,480],
"bidId": "392b5a6b05d648",
"bidderRequestId": "2946b569352ef2",
"requestId": "6172477f-987f-4523-a967-fa6d7a434ddf",
"startTime": 1462918897462
}
],
"requestId": "6172477f-987f-4523-a967-fa6d7a434ddf",
"start": 1462918897460,
"timeout": 5000
}
13 changes: 13 additions & 0 deletions test/fixtures/video/bidResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"adUnitCode": "video1",
"bidder": "appnexusAst",
"bidderCode": "appnexusAst",
"code": "appnexusAst",
"dealId": "foo",
"cpm": 0.1,
"height": 480,
"mediaType": "video",
"requestId": "6172477f-987f-4523-a967-fa6d7a434ddf",
"vastUrl": "www.myVastUrl.com",
"width": 640
}
34 changes: 34 additions & 0 deletions test/mocks/videoCacheStub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as videoCache from 'src/video-cache';

/**
* Function which can be called from unit tests to stub out the video cache.
*
* @param {Object} responses
* @param {} responses.store If this is an Error, we'll stub out the store function so that it fails.
* If it's anything else, the store function will succeed, sending that value into the callback.
*
* @return {function} A function which returns the current stubs for the mocked functions.
*/
export default function useVideoCacheStub(responses) {
let storeStub;

beforeEach(() => {
storeStub = sinon.stub(videoCache, 'store');

if (responses.store instanceof Error) {
storeStub.callsArgWith(1, responses.store);
} else {
storeStub.callsArgWith(1, null, responses.store);
}
});

afterEach(() => {
videoCache.store.restore();
});

return function() {
return {
store: storeStub
};
}
}
91 changes: 91 additions & 0 deletions test/spec/unit/bidmanager_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { expect } from 'chai';
import * as bidManager from 'src/bidmanager';
import useVideoCacheStubs from 'test/mocks/videoCacheStub';
import adUnit from 'test/fixtures/video/adUnit';
import bidRequest from 'test/fixtures/video/bidRequest';
import bidResponse from 'test/fixtures/video/bidResponse';

describe('The Bid Manager', () => {
describe('addBidResponse() function', () => {
before(() => {
$$PREBID_GLOBAL$$.cbTimeout = 5000;
$$PREBID_GLOBAL$$.timeoutBuffer = 50;
});

useVideoCacheStubs({
store: [{ cacheId: 'FAKE_UUID' }],
});

describe('when more bids are expected after this one', () => {
beforeEach(() => {
const tweakedBidRequest = JSON.parse(JSON.stringify(bidRequest));
const tweakedBidRequestBid = Object.assign({}, bidRequest.bids[0], { placementCode: 'video2' });
tweakedBidRequest.bids.push(tweakedBidRequestBid);
tweakedBidRequest.start = new Date().getTime();

$$PREBID_GLOBAL$$.adUnits = [adUnit, Object.assign({}, adUnit, { code: 'video2' })];
$$PREBID_GLOBAL$$._bidsRequested = [tweakedBidRequest];
$$PREBID_GLOBAL$$._bidsReceived = [];
$$PREBID_GLOBAL$$._adUnitCodes = $$PREBID_GLOBAL$$.adUnits.map(unit => unit.code);
});

it('should add video bids, but *not* call the end-of-auction callbacks', () => {
const mockResponse = Object.assign({}, bidResponse);
const spy = sinon.spy();
bidManager.addOneTimeCallback(spy);

mockResponse.getSize = function() {
return `${this.height}x${this.width}`
};
bidManager.addBidResponse(adUnit.code, mockResponse);

expect($$PREBID_GLOBAL$$._bidsReceived.length).to.equal(1);

const bid = $$PREBID_GLOBAL$$._bidsReceived[0];
expect(bid.vastUrl).to.equal('www.myVastUrl.com');
expect(bid.videoCacheKey).to.equal('FAKE_UUID');

expect(spy.called).to.equal(false);
});

it("shouldn't add bids which arrive after the auction times out", () => {
expect(true).to.equal(true);
});
});

describe('when this is the last bid expected in the auction', () => {
beforeEach(() => {
$$PREBID_GLOBAL$$.adUnits = [adUnit];
$$PREBID_GLOBAL$$._bidsRequested = [bidRequest];
$$PREBID_GLOBAL$$._bidsReceived = [];
$$PREBID_GLOBAL$$._adUnitCodes = $$PREBID_GLOBAL$$.adUnits.map(unit => unit.code);
});

it("shouldn't add invalid bids", () => {
bidManager.addBidResponse('', { });
bidManager.addBidResponse('testCode', { mediaType: 'video' });
bidManager.addBidResponse('testCode', { mediaType: 'native' });
expect($$PREBID_GLOBAL$$._bidsReceived.length).to.equal(0);
});

it('should add valid video bids and then execute the callbacks signaling the end of the auction', () => {
const mockResponse = Object.assign({}, bidResponse);
const spy = sinon.spy();
bidManager.addOneTimeCallback(spy);

mockResponse.getSize = function() {
return `${this.height}x${this.width}`
};
bidManager.addBidResponse(adUnit.code, mockResponse);

expect($$PREBID_GLOBAL$$._bidsReceived.length).to.equal(1);

const bid = $$PREBID_GLOBAL$$._bidsReceived[0];
expect(bid.vastUrl).to.equal('www.myVastUrl.com');
expect(bid.videoCacheKey).to.equal('FAKE_UUID');

expect(spy.calledOnce).to.equal(true);
});
});
});
});

0 comments on commit 15e6db4

Please sign in to comment.