Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test index.js file #795

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ util.missingProjectIdError = missingProjectIdError;
* latter is preferred.
*
* @param {object} globalConfig - The global configuration object.
* @param {object} overrides - The instantiation-time configuration object.
* @param {object=} overrides - The instantiation-time configuration object.
* @return {object}
*
* @example
Expand All @@ -83,11 +83,15 @@ util.missingProjectIdError = missingProjectIdError;
function extendGlobalConfig(globalConfig, overrides) {
var options = extend({}, globalConfig);
var hasGlobalConnection = options.credentials || options.keyFilename;

overrides = overrides || {};
var isOverridingConnection = overrides.credentials || overrides.keyFilename;

if (hasGlobalConnection && isOverridingConnection) {
delete options.credentials;
delete options.keyFilename;
}

return extend(true, {}, options, overrides);
}

Expand Down
24 changes: 18 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

'use strict';

var extend = require('extend');

/**
* @type {module:bigquery}
* @private
Expand Down Expand Up @@ -122,29 +124,39 @@ var util = require('./common/util.js');
* //-
*/
function gcloud(config) {
return {
var api = {
bigquery: function(options) {
options = options || {};
return new BigQuery(util.extendGlobalConfig(config, options));
},

datastore: new Datastore(config),

dns: function(options) {
options = options || {};
return new DNS(util.extendGlobalConfig(config, options));
},

pubsub: function(options) {
options = options || {};
return new PubSub(util.extendGlobalConfig(config, options));
},

search: function(options) {
options = options || {};
return new Search(util.extendGlobalConfig(config, options));
},

storage: function(options) {
options = options || {};
return new Storage(util.extendGlobalConfig(config, options));
}
};

// Extend each API to pick up static members, e.g. Datastore.int & Storage.acl
extend(api.bigquery, BigQuery);
extend(api.datastore, Datastore);
extend(api.dns, DNS);
extend(api.pubsub, PubSub);
extend(api.search, Search);
extend(api.storage, Storage);

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


return api;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"scripts": {
"docs": "./scripts/docs.sh",
"lint": "jshint lib/ system-test/ test/ && jscs lib/ system-test/ test/",
"test": "npm run docs && mocha test/*/*.js test/docs.js",
"test": "npm run docs && mocha test/*/*.js test/index.js test/docs.js",
"system-test": "mocha system-test/* --timeout 45000",
"cover": "istanbul cover -x 'system-test/*' _mocha -- --timeout 45000 test/*/*.js test/docs.js system-test/*",
"coveralls": "istanbul cover -x 'system-test/*' _mocha --report lcovonly -- --timeout 45000 test/*/*.js test/docs.js system-test/* -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
Expand Down
227 changes: 227 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/*!
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var assert = require('assert');
var extend = require('extend');
var mockery = require('mockery');
var util = require('../lib/common/util.js');

var extendGlobalConfigOverride = null;
var fakeUtil = extend({}, util, {
extendGlobalConfig: function() {
var method = extendGlobalConfigOverride || util.extendGlobalConfig;
return method.apply(null, arguments);
}
});

function FakeBigQuery() {
this.calledWith_ = [].slice.call(arguments);
}
FakeBigQuery.static = true;

function FakeDatastore() {
this.calledWith_ = [].slice.call(arguments);
}
FakeDatastore.static = true;

function FakeDNS() {
this.calledWith_ = [].slice.call(arguments);
}
FakeDNS.static = true;

function FakePubSub() {
this.calledWith_ = [].slice.call(arguments);
}
FakePubSub.static = true;

function FakeSearch() {
this.calledWith_ = [].slice.call(arguments);
}
FakeSearch.static = true;

function FakeStorage() {
this.calledWith_ = [].slice.call(arguments);
}
FakeStorage.static = true;

describe('gcloud', function() {
var gcloud;

before(function() {
mockery.registerMock('./bigquery', FakeBigQuery);
mockery.registerMock('./datastore', FakeDatastore);
mockery.registerMock('./dns', FakeDNS);
mockery.registerMock('./pubsub', FakePubSub);
mockery.registerMock('./search', FakeSearch);
mockery.registerMock('./storage', FakeStorage);
mockery.registerMock('./common/util.js', fakeUtil);
mockery.enable({
useCleanCache: true,
warnOnUnregistered: false
});
gcloud = require('../lib/index.js');
});

beforeEach(function() {
extendGlobalConfigOverride = null;
});

after(function() {
mockery.deregisterAll();
mockery.disable();
});

it('should export a function', function() {
assert.strictEqual(typeof gcloud, 'function');
});

it('should export static bigquery', function() {
assert.strictEqual(gcloud.bigquery, FakeBigQuery);
});

it('should export static datastore', function() {
assert.strictEqual(gcloud.datastore, FakeDatastore);
});

it('should export static dns', function() {
assert.strictEqual(gcloud.dns, FakeDNS);
});

it('should export static pubsub', function() {
assert.strictEqual(gcloud.pubsub, FakePubSub);
});

it('should export static search', function() {
assert.strictEqual(gcloud.search, FakeSearch);
});

it('should export static storage', function() {
assert.strictEqual(gcloud.storage, FakeStorage);
});

describe('localized auth', function() {
var localGcloud;
var config = { a: 'b', c: 'd' };
var options = { e: 'f', g: 'h' };
var extendedOptions = { a: 'b', c: 'd' };

beforeEach(function() {
localGcloud = gcloud(config);
});

describe('bigquery', function() {
it('should create new BigQuery with extended options', function() {
extendGlobalConfigOverride = function(config_, options_) {
assert.strictEqual(config_, config);
assert.strictEqual(options_, options);
return extendedOptions;
};

var bigquery = localGcloud.bigquery(options);

assert.strictEqual(bigquery.calledWith_[0], extendedOptions);
});

it('should extend the original API', function() {
assert.strictEqual(localGcloud.bigquery.static, FakeBigQuery.static);
});
});

describe('datastore', function() {
it('should create new Datastore from original config', function() {
assert.strictEqual(localGcloud.datastore.calledWith_[0], config);
});

it('should extend the original API', function() {
assert.strictEqual(localGcloud.datastore.static, FakeDatastore.static);
});
});

describe('dns', function() {
it('should create new BigQuery with extended options', function() {
extendGlobalConfigOverride = function(config_, options_) {
assert.strictEqual(config_, config);
assert.strictEqual(options_, options);
return extendedOptions;
};

var dns = localGcloud.dns(options);

assert.strictEqual(dns.calledWith_[0], extendedOptions);
});

it('should extend the original API', function() {
assert.strictEqual(localGcloud.dns.static, FakeDNS.static);
});
});

describe('pubsub', function() {
it('should create new BigQuery with extended options', function() {
extendGlobalConfigOverride = function(config_, options_) {
assert.strictEqual(config_, config);
assert.strictEqual(options_, options);
return extendedOptions;
};

var pubsub = localGcloud.pubsub(options);

assert.strictEqual(pubsub.calledWith_[0], extendedOptions);
});

it('should extend the original API', function() {
assert.strictEqual(localGcloud.pubsub.static, FakePubSub.static);
});
});

describe('search', function() {
it('should create new BigQuery with extended options', function() {
extendGlobalConfigOverride = function(config_, options_) {
assert.strictEqual(config_, config);
assert.strictEqual(options_, options);
return extendedOptions;
};

var search = localGcloud.search(options);

assert.strictEqual(search.calledWith_[0], extendedOptions);
});

it('should extend the original API', function() {
assert.strictEqual(localGcloud.search.static, FakeSearch.static);
});
});

describe('storage', function() {
it('should create new BigQuery with extended options', function() {
extendGlobalConfigOverride = function(config_, options_) {
assert.strictEqual(config_, config);
assert.strictEqual(options_, options);
return extendedOptions;
};

var storage = localGcloud.storage(options);

assert.strictEqual(storage.calledWith_[0], extendedOptions);
});

it('should extend the original API', function() {
assert.strictEqual(localGcloud.storage.static, FakeStorage.static);
});
});
});
});