Skip to content

Commit c064a32

Browse files
committed
adding unit tests
1 parent 01d9442 commit c064a32

5 files changed

Lines changed: 574 additions & 2 deletions

File tree

lib/compute/region.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ Region.prototype.rule = function(name) {
813813
* These methods can be used with either a callback or as a readable object
814814
* stream. `streamRouter` is used to add this dual behavior.
815815
*/
816-
streamRouter.extend(Region, ['getAddresses', 'getOperations', 'getRules', 'getSubnetworks']);
816+
streamRouter.extend(Region,
817+
['getAddresses', 'getOperations', 'getRules', 'getSubnetworks']);
817818

818819
module.exports = Region;

test/compute/index.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ function FakeOperation() {
7979
function FakeRegion() {
8080
this.calledWith_ = slice.call(arguments);
8181
this.address = function() { return {}; };
82+
this.subnetwork = function() { return {}; };
8283
}
8384

8485
function FakeRule() {
@@ -1140,6 +1141,113 @@ describe('Compute', function() {
11401141
});
11411142
});
11421143

1144+
describe('getSubnetworks', function() {
1145+
it('should accept only a callback', function(done) {
1146+
compute.request = function(reqOpts) {
1147+
assert.deepEqual(reqOpts.qs, {});
1148+
done();
1149+
};
1150+
1151+
compute.getSubnetworks(assert.ifError);
1152+
});
1153+
1154+
it('should make the correct API request', function(done) {
1155+
var options = {};
1156+
1157+
compute.request = function(reqOpts) {
1158+
assert.strictEqual(reqOpts.uri, '/aggregated/subnetworks');
1159+
assert.strictEqual(reqOpts.qs, options);
1160+
done();
1161+
};
1162+
1163+
compute.getSubnetworks(options, assert.ifError);
1164+
});
1165+
1166+
describe('error', function() {
1167+
var error = new Error('Error.');
1168+
var apiResponse = { a: 'b', c: 'd' };
1169+
1170+
beforeEach(function() {
1171+
compute.request = function(reqOpts, callback) {
1172+
callback(error, apiResponse);
1173+
};
1174+
});
1175+
1176+
it('should execute callback with error & API response', function(done) {
1177+
compute.getSubnetworks({}, function(err, subnetworks, nextQuery, resp) {
1178+
assert.strictEqual(err, error);
1179+
assert.strictEqual(subnetworks, null);
1180+
assert.strictEqual(nextQuery, null);
1181+
assert.strictEqual(resp, apiResponse);
1182+
1183+
done();
1184+
});
1185+
});
1186+
});
1187+
1188+
describe('success', function() {
1189+
var REGION_NAME = 'region-1';
1190+
var FULL_REGION_NAME = 'regions/' + REGION_NAME;
1191+
1192+
var subnetwork = { name: 'subnetwork-1' };
1193+
var apiResponse = {
1194+
items: {}
1195+
};
1196+
1197+
apiResponse.items[FULL_REGION_NAME] = {
1198+
subnetworks: [subnetwork]
1199+
};
1200+
1201+
beforeEach(function() {
1202+
compute.request = function(reqOpts, callback) {
1203+
callback(null, apiResponse);
1204+
};
1205+
});
1206+
1207+
it('should create Subnetwork objects from the response', function(done) {
1208+
var region = {};
1209+
1210+
compute.region = function(name) {
1211+
assert.strictEqual(name, REGION_NAME);
1212+
return region;
1213+
};
1214+
1215+
region.subnetwork = function(name) {
1216+
assert.strictEqual(name, subnetwork.name);
1217+
setImmediate(done);
1218+
return subnetwork;
1219+
};
1220+
1221+
compute.getSubnetworks({}, assert.ifError);
1222+
});
1223+
1224+
it('should build a nextQuery if necessary', function(done) {
1225+
var apiResponseWithNextPageToken = extend({}, apiResponse, {
1226+
nextPageToken: 'next-page-token'
1227+
});
1228+
1229+
var query = { a: 'b', c: 'd' };
1230+
var originalQuery = extend({}, query);
1231+
1232+
compute.request = function(reqOpts, callback) {
1233+
callback(null, apiResponseWithNextPageToken);
1234+
};
1235+
1236+
compute.getSubnetworks(query, function(err, subnetworks, nextQuery) {
1237+
assert.ifError(err);
1238+
1239+
assert.deepEqual(query, originalQuery);
1240+
1241+
assert.deepEqual(nextQuery, extend({}, query, {
1242+
pageToken: apiResponseWithNextPageToken.nextPageToken
1243+
}));
1244+
1245+
done();
1246+
});
1247+
});
1248+
});
1249+
});
1250+
11431251
describe('getFirewalls', function() {
11441252
it('should accept only a callback', function(done) {
11451253
compute.request = function(reqOpts) {

test/compute/network.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,26 @@ describe('Network', function() {
146146
});
147147
});
148148

149+
describe('createSubnetwork', function() {
150+
it('should make the correct call to Region', function(done) {
151+
var name = 'subnetwork-name';
152+
var config = { a: 'b', c: 'd', region: 'region1'};
153+
var expectedConfig = extend({}, config, {
154+
network: network.formattedName,
155+
name: name
156+
});
157+
delete expectedConfig.region;
158+
159+
network.compute.request = function(config_) {
160+
assert.strictEqual(config_.json.name, name);
161+
assert.deepEqual(config_.json, expectedConfig);
162+
done();
163+
};
164+
165+
network.createSubnetwork(name, config, done);
166+
});
167+
});
168+
149169
describe('delete', function() {
150170
it('should call ServiceObject.delete', function(done) {
151171
FakeServiceObject.prototype.delete = function() {
@@ -235,6 +255,52 @@ describe('Network', function() {
235255
});
236256
});
237257

258+
describe('getSubnetworks', function() {
259+
it('should make the correct call to Compute', function(done) {
260+
var options = { a: 'b', c: 'd' };
261+
var expectedOptions = extend({}, options, {
262+
filter: 'network eq .*' + network.formattedName
263+
});
264+
265+
network.compute.getSubnetworks = function(options, callback) {
266+
assert.deepEqual(options, expectedOptions);
267+
callback();
268+
};
269+
270+
network.getSubnetworks(options, done);
271+
});
272+
273+
it('should not require options', function(done) {
274+
network.compute.getSubnetworks = function(options, callback) {
275+
callback();
276+
};
277+
278+
network.getSubnetworks(done);
279+
});
280+
281+
it('should not require any arguments', function(done) {
282+
network.compute.getSubnetworks = function(options, callback) {
283+
assert.deepEqual(options, {
284+
filter: 'network eq .*' + network.formattedName
285+
});
286+
assert.strictEqual(typeof callback, 'undefined');
287+
done();
288+
};
289+
290+
network.getSubnetworks();
291+
});
292+
293+
it('should return the result of calling Compute', function() {
294+
var resultOfgetSubnetworks = {};
295+
296+
network.compute.getSubnetworks = function() {
297+
return resultOfgetSubnetworks;
298+
};
299+
300+
assert.strictEqual(network.getSubnetworks(), resultOfgetSubnetworks);
301+
});
302+
});
303+
238304
describe('getFirewalls', function() {
239305
it('should make the correct call to Compute', function(done) {
240306
var options = { a: 'b', c: 'd' };

0 commit comments

Comments
 (0)