Skip to content

Commit

Permalink
adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
authtestprod committed Jul 15, 2016
1 parent 01d9442 commit c064a32
Show file tree
Hide file tree
Showing 5 changed files with 574 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/compute/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ Region.prototype.rule = function(name) {
* These methods can be used with either a callback or as a readable object
* stream. `streamRouter` is used to add this dual behavior.
*/
streamRouter.extend(Region, ['getAddresses', 'getOperations', 'getRules', 'getSubnetworks']);
streamRouter.extend(Region,
['getAddresses', 'getOperations', 'getRules', 'getSubnetworks']);

module.exports = Region;
108 changes: 108 additions & 0 deletions test/compute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function FakeOperation() {
function FakeRegion() {
this.calledWith_ = slice.call(arguments);
this.address = function() { return {}; };
this.subnetwork = function() { return {}; };
}

function FakeRule() {
Expand Down Expand Up @@ -1140,6 +1141,113 @@ describe('Compute', function() {
});
});

describe('getSubnetworks', function() {
it('should accept only a callback', function(done) {
compute.request = function(reqOpts) {
assert.deepEqual(reqOpts.qs, {});
done();
};

compute.getSubnetworks(assert.ifError);
});

it('should make the correct API request', function(done) {
var options = {};

compute.request = function(reqOpts) {
assert.strictEqual(reqOpts.uri, '/aggregated/subnetworks');
assert.strictEqual(reqOpts.qs, options);
done();
};

compute.getSubnetworks(options, assert.ifError);
});

describe('error', function() {
var error = new Error('Error.');
var apiResponse = { a: 'b', c: 'd' };

beforeEach(function() {
compute.request = function(reqOpts, callback) {
callback(error, apiResponse);
};
});

it('should execute callback with error & API response', function(done) {
compute.getSubnetworks({}, function(err, subnetworks, nextQuery, resp) {
assert.strictEqual(err, error);
assert.strictEqual(subnetworks, null);
assert.strictEqual(nextQuery, null);
assert.strictEqual(resp, apiResponse);

done();
});
});
});

describe('success', function() {
var REGION_NAME = 'region-1';
var FULL_REGION_NAME = 'regions/' + REGION_NAME;

var subnetwork = { name: 'subnetwork-1' };
var apiResponse = {
items: {}
};

apiResponse.items[FULL_REGION_NAME] = {
subnetworks: [subnetwork]
};

beforeEach(function() {
compute.request = function(reqOpts, callback) {
callback(null, apiResponse);
};
});

it('should create Subnetwork objects from the response', function(done) {
var region = {};

compute.region = function(name) {
assert.strictEqual(name, REGION_NAME);
return region;
};

region.subnetwork = function(name) {
assert.strictEqual(name, subnetwork.name);
setImmediate(done);
return subnetwork;
};

compute.getSubnetworks({}, assert.ifError);
});

it('should build a nextQuery if necessary', function(done) {
var apiResponseWithNextPageToken = extend({}, apiResponse, {
nextPageToken: 'next-page-token'
});

var query = { a: 'b', c: 'd' };
var originalQuery = extend({}, query);

compute.request = function(reqOpts, callback) {
callback(null, apiResponseWithNextPageToken);
};

compute.getSubnetworks(query, function(err, subnetworks, nextQuery) {
assert.ifError(err);

assert.deepEqual(query, originalQuery);

assert.deepEqual(nextQuery, extend({}, query, {
pageToken: apiResponseWithNextPageToken.nextPageToken
}));

done();
});
});
});
});

describe('getFirewalls', function() {
it('should accept only a callback', function(done) {
compute.request = function(reqOpts) {
Expand Down
66 changes: 66 additions & 0 deletions test/compute/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,26 @@ describe('Network', function() {
});
});

describe('createSubnetwork', function() {
it('should make the correct call to Region', function(done) {
var name = 'subnetwork-name';
var config = { a: 'b', c: 'd', region: 'region1'};
var expectedConfig = extend({}, config, {
network: network.formattedName,
name: name
});
delete expectedConfig.region;

network.compute.request = function(config_) {
assert.strictEqual(config_.json.name, name);
assert.deepEqual(config_.json, expectedConfig);
done();
};

network.createSubnetwork(name, config, done);
});
});

describe('delete', function() {
it('should call ServiceObject.delete', function(done) {
FakeServiceObject.prototype.delete = function() {
Expand Down Expand Up @@ -235,6 +255,52 @@ describe('Network', function() {
});
});

describe('getSubnetworks', function() {
it('should make the correct call to Compute', function(done) {
var options = { a: 'b', c: 'd' };
var expectedOptions = extend({}, options, {
filter: 'network eq .*' + network.formattedName
});

network.compute.getSubnetworks = function(options, callback) {
assert.deepEqual(options, expectedOptions);
callback();
};

network.getSubnetworks(options, done);
});

it('should not require options', function(done) {
network.compute.getSubnetworks = function(options, callback) {
callback();
};

network.getSubnetworks(done);
});

it('should not require any arguments', function(done) {
network.compute.getSubnetworks = function(options, callback) {
assert.deepEqual(options, {
filter: 'network eq .*' + network.formattedName
});
assert.strictEqual(typeof callback, 'undefined');
done();
};

network.getSubnetworks();
});

it('should return the result of calling Compute', function() {
var resultOfgetSubnetworks = {};

network.compute.getSubnetworks = function() {
return resultOfgetSubnetworks;
};

assert.strictEqual(network.getSubnetworks(), resultOfgetSubnetworks);
});
});

describe('getFirewalls', function() {
it('should make the correct call to Compute', function(done) {
var options = { a: 'b', c: 'd' };
Expand Down
Loading

0 comments on commit c064a32

Please sign in to comment.