diff --git a/packages/bigquery/package.json b/packages/bigquery/package.json index f52795c9c14..c68248d47e0 100644 --- a/packages/bigquery/package.json +++ b/packages/bigquery/package.json @@ -53,8 +53,7 @@ "arrify": "^1.0.0", "duplexify": "^3.2.0", "extend": "^3.0.0", - "@google-cloud/common": "^0.1.0", - "@google-cloud/storage": "^0.1.0", + "@google-cloud/common": "^0.3.0", "is": "^3.0.1", "modelo": "^4.2.0", "stream-events": "^1.0.1", @@ -62,6 +61,7 @@ }, "devDependencies": { "async": "^1.4.2", + "@google-cloud/storage": "*", "mocha": "^3.0.1", "node-uuid": "^1.4.3", "propprop": "^0.3.0", diff --git a/packages/bigquery/src/table.js b/packages/bigquery/src/table.js index 5aa75928fc4..a608652ce24 100644 --- a/packages/bigquery/src/table.js +++ b/packages/bigquery/src/table.js @@ -28,7 +28,6 @@ var format = require('string-format-obj'); var fs = require('fs'); var is = require('is'); var path = require('path'); -var Storage = require('@google-cloud/storage'); var streamEvents = require('stream-events'); var util = require('util'); @@ -504,7 +503,7 @@ Table.prototype.export = function(destination, options, callback) { extend(true, options, { destinationUris: arrify(destination).map(function(dest) { - if (!(dest instanceof Storage.File)) { + if (!common.util.isCustomType(dest, 'storage/file')) { throw new Error('Destination must be a File object.'); } @@ -787,7 +786,7 @@ Table.prototype.import = function(source, metadata, callback) { extend(true, body.configuration.load, metadata, { sourceUris: arrify(source).map(function(src) { - if (!(src instanceof Storage.File)) { + if (!common.util.isCustomType(src, 'storage/file')) { throw new Error('Source must be a File object.'); } diff --git a/packages/bigquery/test/table.js b/packages/bigquery/test/table.js index 3cc64960f75..20de94c774c 100644 --- a/packages/bigquery/test/table.js +++ b/packages/bigquery/test/table.js @@ -24,19 +24,17 @@ var prop = require('propprop'); var proxyquire = require('proxyquire'); var stream = require('stream'); -var File = require('@google-cloud/storage').File; var ServiceObject = require('@google-cloud/common').ServiceObject; var util = require('@google-cloud/common').util; -function FakeFile(a, b) { - this.request = util.noop; - File.call(this, a, b); -} - var makeWritableStreamOverride; +var isCustomTypeOverride; var fakeUtil = extend({}, util, { + isCustomType: function() { + return (isCustomTypeOverride || util.isCustomType).apply(null, arguments); + }, makeWritableStream: function() { - var args = [].slice.call(arguments); + var args = arguments; (makeWritableStreamOverride || util.makeWritableStream).apply(null, args); } }); @@ -70,7 +68,8 @@ describe('BigQuery/Table', function() { projectId: 'project-id', job: function(id) { return { id: id }; - } + }, + request: util.noop } }; @@ -98,9 +97,6 @@ describe('BigQuery/Table', function() { before(function() { Table = proxyquire('../src/table.js', { - '@google-cloud/storage': { - File: FakeFile - }, '@google-cloud/common': { ServiceObject: FakeServiceObject, streamRouter: fakeStreamRouter, @@ -125,9 +121,11 @@ describe('BigQuery/Table', function() { }); beforeEach(function() { + isCustomTypeOverride = null; makeWritableStreamOverride = null; tableOverrides = {}; table = new Table(DATASET, TABLE_ID); + table.bigQuery.request = util.noop; }); describe('instantiation', function() { @@ -480,12 +478,18 @@ describe('BigQuery/Table', function() { }); describe('export', function() { - var FILE = new FakeFile({ - name: 'bucket-name', - makeReq_: util.noop, - }, 'file.json'); + var FILE = { + name: 'file-name.json', + bucket: { + name: 'bucket-name' + } + }; beforeEach(function() { + isCustomTypeOverride = function() { + return true; + }; + table.bigQuery.job = function(id) { return { id: id }; }; @@ -555,10 +559,25 @@ describe('BigQuery/Table', function() { done(); }; - table.export(FILE, done); + table.export(FILE, assert.ifError); + }); + + it('should check if a destination is a File', function(done) { + isCustomTypeOverride = function(dest, type) { + assert.strictEqual(dest, FILE); + assert.strictEqual(type, 'storage/file'); + setImmediate(done); + return true; + }; + + table.export(FILE, assert.ifError); }); it('should throw if a destination is not a File', function() { + isCustomTypeOverride = function() { + return false; + }; + assert.throws(function() { table.export({}, util.noop); }, /Destination must be a File object/); @@ -794,10 +813,18 @@ describe('BigQuery/Table', function() { describe('import', function() { var FILEPATH = require.resolve('./testdata/testfile.json'); - var FILE = new FakeFile({ - name: 'bucket-name', - makeReq_: util.noop - }, 'file.json'); + var FILE = { + name: 'file-name.json', + bucket: { + name: 'bucket-name' + } + }; + + beforeEach(function() { + isCustomTypeOverride = function() { + return true; + }; + }); it('should accept just a File and a callback', function(done) { var mockJob = { id: 'foo' }; @@ -873,7 +900,22 @@ describe('BigQuery/Table', function() { table.import(FILEPATH, { sourceFormat: 'CSV' }, done); }); + it('should check if a destination is a File', function(done) { + isCustomTypeOverride = function(dest, type) { + assert.strictEqual(dest, FILE); + assert.strictEqual(type, 'storage/file'); + setImmediate(done); + return true; + }; + + table.export(FILE, assert.ifError); + }); + it('should throw if a File object is not provided', function() { + isCustomTypeOverride = function() { + return false; + }; + assert.throws(function() { table.import({}); }, /Source must be a File object/); diff --git a/packages/language/package.json b/packages/language/package.json index aa78ba4beb7..70fb358b95f 100644 --- a/packages/language/package.json +++ b/packages/language/package.json @@ -52,11 +52,10 @@ "language" ], "dependencies": { - "@google-cloud/common": "^0.1.0", - "@google-cloud/storage": "^0.1.0", "arguejs": "^0.2.3", "arrify": "^1.0.1", "extend": "^3.0.0", + "@google-cloud/common": "^0.3.0", "google-gax": "^0.6.0", "google-proto-files": "^0.4.0", "is": "^3.0.1", @@ -64,6 +63,7 @@ "string-format-obj": "^1.0.0" }, "devDependencies": { + "@google-cloud/storage": "*", "mocha": "^2.1.0", "node-uuid": "^1.4.7", "proxyquire": "^1.7.10", diff --git a/packages/language/src/document.js b/packages/language/src/document.js index 762e7625b44..ca3e685636c 100644 --- a/packages/language/src/document.js +++ b/packages/language/src/document.js @@ -21,8 +21,8 @@ 'use strict'; var arrify = require('arrify'); +var common = require('@google-cloud/common'); var extend = require('extend'); -var File = require('@google-cloud/storage').File; var format = require('string-format-obj'); var is = require('is'); var prop = require('propprop'); @@ -76,7 +76,7 @@ function Document(language, config) { this.reqOpts.document.type = 'PLAIN_TEXT'; } - if (content instanceof File) { + if (common.util.isCustomType(content, 'storage/file')) { this.reqOpts.document.gcsContentUri = format('gs://{bucket}/{file}', { bucket: encodeURIComponent(content.bucket.id), file: encodeURIComponent(content.id) diff --git a/packages/language/test/document.js b/packages/language/test/document.js index 2801930c4f8..c6d69cc16ce 100644 --- a/packages/language/test/document.js +++ b/packages/language/test/document.js @@ -22,7 +22,16 @@ var prop = require('propprop'); var proxyquire = require('proxyquire'); var util = require('@google-cloud/common').util; -function FakeFile() {} +var isCustomTypeOverride; +var fakeUtil = extend(true, {}, util, { + isCustomType: function() { + if (isCustomTypeOverride) { + return isCustomTypeOverride.apply(null, arguments); + } + + return false; + } +}); describe('Document', function() { var DocumentCache; @@ -36,8 +45,8 @@ describe('Document', function() { before(function() { Document = proxyquire('../src/document.js', { - '@google-cloud/storage': { - File: FakeFile + '@google-cloud/common': { + util: fakeUtil } }); @@ -45,6 +54,8 @@ describe('Document', function() { }); beforeEach(function() { + isCustomTypeOverride = null; + for (var property in DocumentCache) { if (DocumentCache.hasOwnProperty(property)) { Document[property] = DocumentCache[property]; @@ -123,11 +134,19 @@ describe('Document', function() { }); it('should set the GCS content URI from a File', function() { - var file = new FakeFile(); + var file = { + // Leave spaces in to check that it is URI-encoded: + id: 'file name', + bucket: { + id: 'bucket name' + } + }; - // Leave spaces in to check that it is URI-encoded: - file.bucket = { id: 'bucket id' }; - file.id = 'file name'; + isCustomTypeOverride = function(content, type) { + assert.strictEqual(content, file); + assert.strictEqual(type, 'storage/file'); + return true; + }; var document = new Document(LANGUAGE, { content: file diff --git a/packages/logging/package.json b/packages/logging/package.json index 61bb205e7d1..941836311af 100644 --- a/packages/logging/package.json +++ b/packages/logging/package.json @@ -52,16 +52,16 @@ "dependencies": { "arrify": "^1.0.0", "extend": "^3.0.0", - "@google-cloud/bigquery": "^0.1.0", - "@google-cloud/common": "^0.1.0", - "@google-cloud/pubsub": "^0.1.0", - "@google-cloud/storage": "^0.1.0", "google-proto-files": "^0.2.1", "is": "^3.0.1", "string-format-obj": "^1.0.0" }, "devDependencies": { "async": "^1.4.2", + "@google-cloud/bigquery": "*", + "@google-cloud/common": "*", + "@google-cloud/pubsub": "*", + "@google-cloud/storage": "*", "methmeth": "^1.0.0", "mocha": "^3.0.1", "node-uuid": "^1.4.3", diff --git a/packages/logging/src/index.js b/packages/logging/src/index.js index bc83a4e8460..cd5843ca1c3 100644 --- a/packages/logging/src/index.js +++ b/packages/logging/src/index.js @@ -21,14 +21,11 @@ 'use strict'; var arrify = require('arrify'); -var BigQuery = require('@google-cloud/bigquery'); var common = require('@google-cloud/common'); var extend = require('extend'); var format = require('string-format-obj'); var googleProtoFiles = require('google-proto-files'); var is = require('is'); -var PubSub = require('@google-cloud/pubsub'); -var Storage = require('@google-cloud/storage'); var util = require('util'); /** @@ -152,18 +149,18 @@ Logging.prototype.createSink = function(name, config, callback) { throw new Error('A sink configuration object must be provided.'); } - if (config.destination instanceof Storage.Bucket) { - this.setAclForBucket_(name, config, callback); + if (common.util.isCustomType(config.destination, 'bigquery/dataset')) { + this.setAclForDataset_(name, config, callback); return; } - if (config.destination instanceof BigQuery.Dataset) { - this.setAclForDataset_(name, config, callback); + if (common.util.isCustomType(config.destination, 'pubsub/topic')) { + this.setAclForTopic_(name, config, callback); return; } - if (config.destination instanceof PubSub.Topic) { - this.setAclForTopic_(name, config, callback); + if (common.util.isCustomType(config.destination, 'storage/bucket')) { + this.setAclForBucket_(name, config, callback); return; } diff --git a/packages/logging/src/sink.js b/packages/logging/src/sink.js index ef7c866183a..0f547cbe9ce 100644 --- a/packages/logging/src/sink.js +++ b/packages/logging/src/sink.js @@ -57,7 +57,9 @@ function Sink(logging, name) { * * @example * var config = { - * // ... + * destination: { + * // ... + * } * }; * * sink.create(config, function(err, sink, apiResponse) { diff --git a/packages/logging/test/index.js b/packages/logging/test/index.js index c161064c693..215a88c3ab0 100644 --- a/packages/logging/test/index.js +++ b/packages/logging/test/index.js @@ -41,21 +41,16 @@ var fakeStreamRouter = { } }; +var isCustomTypeOverride; var fakeUtil = extend({}, util, { - makeAuthenticatedRequestFactory: util.noop -}); - -function FakeBucket() { - this.calledWith_ = arguments; -} - -function FakeDataset() { - this.calledWith_ = arguments; -} + isCustomType: function() { + if (isCustomTypeOverride) { + return isCustomTypeOverride.apply(null, arguments); + } -function FakeTopic() { - this.calledWith_ = arguments; -} + return false; + } +}); function FakeEntry() { this.calledWith_ = arguments; @@ -90,15 +85,6 @@ describe('Logging', function() { streamRouter: fakeStreamRouter, util: fakeUtil }, - '@google-cloud/bigquery': { - Dataset: FakeDataset - }, - '@google-cloud/pubsub': { - Topic: FakeTopic - }, - '@google-cloud/storage': { - Bucket: FakeBucket - }, './log.js': FakeLog, './entry.js': FakeEntry, './sink.js': FakeSink @@ -106,11 +92,12 @@ describe('Logging', function() { }); beforeEach(function() { + isCustomTypeOverride = null; + logging = new Logging({ projectId: PROJECT_ID }); logging.projectId = PROJECT_ID; - logging.request = util.noop; }); @@ -179,14 +166,19 @@ describe('Logging', function() { }, /A sink configuration object must be provided\./); }); - it('should set acls for a Bucket destination', function(done) { - var bucket = new FakeBucket(); + it('should set acls for a Dataset destination', function(done) { + var dataset = {}; var CONFIG = { - destination: bucket + destination: dataset }; - logging.setAclForBucket_ = function(name, config, callback) { + isCustomTypeOverride = function(destination, type) { + assert.strictEqual(destination, dataset); + return type === 'bigquery/dataset'; + }; + + logging.setAclForDataset_ = function(name, config, callback) { assert.strictEqual(name, SINK_NAME); assert.strictEqual(config, CONFIG); callback(); // done() @@ -195,14 +187,19 @@ describe('Logging', function() { logging.createSink(SINK_NAME, CONFIG, done); }); - it('should set acls for a Dataset destination', function(done) { - var dataset = new FakeDataset(); + it('should set acls for a Topic destination', function(done) { + var topic = {}; var CONFIG = { - destination: dataset + destination: topic }; - logging.setAclForDataset_ = function(name, config, callback) { + isCustomTypeOverride = function(destination, type) { + assert.strictEqual(destination, topic); + return type === 'pubsub/topic'; + }; + + logging.setAclForTopic_ = function(name, config, callback) { assert.strictEqual(name, SINK_NAME); assert.strictEqual(config, CONFIG); callback(); // done() @@ -211,14 +208,19 @@ describe('Logging', function() { logging.createSink(SINK_NAME, CONFIG, done); }); - it('should set acls for a Topic destination', function(done) { - var topic = new FakeTopic(); + it('should set acls for a Bucket destination', function(done) { + var bucket = {}; var CONFIG = { - destination: topic + destination: bucket }; - logging.setAclForTopic_ = function(name, config, callback) { + isCustomTypeOverride = function(destination, type) { + assert.strictEqual(destination, bucket); + return type === 'storage/bucket'; + }; + + logging.setAclForBucket_ = function(name, config, callback) { assert.strictEqual(name, SINK_NAME); assert.strictEqual(config, CONFIG); callback(); // done() @@ -227,6 +229,7 @@ describe('Logging', function() { logging.createSink(SINK_NAME, CONFIG, done); }); + describe('API request', function() { it('should make the correct API request', function(done) { var config = { @@ -574,11 +577,12 @@ describe('Logging', function() { var bucket; beforeEach(function() { - bucket = new FakeBucket(); - bucket.name = 'bucket-name'; - bucket.acl = { - owners: { - addGroup: util.noop + bucket = { + name: 'bucket-name', + acl: { + owners: { + addGroup: util.noop + } } }; @@ -653,10 +657,11 @@ describe('Logging', function() { var dataset; beforeEach(function() { - dataset = new FakeDataset(); - dataset.id = 'dataset-id'; - dataset.parent = { - projectId: PROJECT_ID + dataset = { + id: 'dataset-id', + parent: { + projectId: PROJECT_ID + } }; CONFIG = { @@ -773,11 +778,12 @@ describe('Logging', function() { var topic; beforeEach(function() { - topic = new FakeTopic(); - topic.name = 'topic-name'; - topic.iam = { - getPolicy: util.noop, - setPolicy: util.noop + topic = { + name: 'topic-name', + iam: { + getPolicy: util.noop, + setPolicy: util.noop + } }; CONFIG = { diff --git a/packages/prediction/package.json b/packages/prediction/package.json index c3837bbf132..a40420c16ed 100644 --- a/packages/prediction/package.json +++ b/packages/prediction/package.json @@ -63,7 +63,7 @@ "devDependencies": { "async": "^1.4.2", "concat-stream": "^1.5.0", - "@google-cloud/storage": "^0.1.0", + "@google-cloud/storage": "*", "mocha": "^3.0.1", "node-uuid": "^1.4.3", "proxyquire": "^1.7.10" diff --git a/packages/vision/package.json b/packages/vision/package.json index a1204ba4c0b..7c12b38662b 100644 --- a/packages/vision/package.json +++ b/packages/vision/package.json @@ -53,8 +53,7 @@ "arrify": "^1.0.0", "async": "^1.4.2", "extend": "^3.0.0", - "@google-cloud/common": "^0.1.0", - "@google-cloud/storage": "^0.1.0", + "@google-cloud/common": "^0.3.0", "is": "^3.0.1", "propprop": "^0.3.0", "request": "^2.70.0", diff --git a/packages/vision/src/index.js b/packages/vision/src/index.js index 9c706e4c148..50f3dc0df28 100644 --- a/packages/vision/src/index.js +++ b/packages/vision/src/index.js @@ -30,7 +30,6 @@ var is = require('is'); var prop = require('propprop'); var request = require('request'); var rgbHex = require('rgb-hex'); -var Storage = require('@google-cloud/storage'); var util = require('util'); var PKG = require('../package.json'); @@ -1275,7 +1274,8 @@ Vision.findImages_ = function(images, callback) { }); return; } - if (image instanceof Storage.File) { + + if (common.util.isCustomType(image, 'storage/file')) { callback(null, { source: { gcsImageUri: format('gs://{bucketName}/{fileName}', { diff --git a/packages/vision/test/index.js b/packages/vision/test/index.js index 777b8510282..5862407e5d6 100644 --- a/packages/vision/test/index.js +++ b/packages/vision/test/index.js @@ -40,10 +40,6 @@ function FakeService() { nodeutil.inherits(FakeService, Service); -function FakeFile() { - this.calledWith_ = arguments; -} - var requestOverride = null; var fakeRequest = function() { return (requestOverride || util.noop).apply(this, arguments); @@ -59,9 +55,6 @@ describe('Vision', function() { before(function() { Vision = proxyquire('../', { request: fakeRequest, - '@google-cloud/storage': { - File: FakeFile - }, '@google-cloud/common': { Service: FakeService, util: fakeUtil @@ -860,11 +853,21 @@ describe('Vision', function() { describe('findImages_', function() { it('should convert a File object', function(done) { - var file = new FakeFile(); - file.bucket = { - name: 'bucket-name' + var file = { + name: 'file-name', + bucket: { + name: 'bucket-name' + } + }; + + var isCustomType = util.isCustomType; + + fakeUtil.isCustomType = function(unknown, type) { + fakeUtil.isCustomType = isCustomType; + assert.strictEqual(unknown, file); + assert.strictEqual(type, 'storage/file'); + return true; }; - file.name = 'file-name'; Vision.findImages_(file, function(err, images) { assert.ifError(err);