Skip to content

Commit

Permalink
bigquery/language/logging/prediction/vision: decouple packages
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Aug 26, 2016
1 parent 137efae commit 24a0252
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 115 deletions.
4 changes: 2 additions & 2 deletions packages/bigquery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@
"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",
"string-format-obj": "^1.0.0"
},
"devDependencies": {
"async": "^1.4.2",
"@google-cloud/storage": "*",
"mocha": "^3.0.1",
"node-uuid": "^1.4.3",
"propprop": "^0.3.0",
Expand Down
5 changes: 2 additions & 3 deletions packages/bigquery/src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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.');
}

Expand Down Expand Up @@ -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.');
}

Expand Down
82 changes: 62 additions & 20 deletions packages/bigquery/test/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -70,7 +68,8 @@ describe('BigQuery/Table', function() {
projectId: 'project-id',
job: function(id) {
return { id: id };
}
},
request: util.noop
}
};

Expand Down Expand Up @@ -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,
Expand All @@ -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() {
Expand Down Expand Up @@ -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 };
};
Expand Down Expand Up @@ -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/);
Expand Down Expand Up @@ -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' };
Expand Down Expand Up @@ -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/);
Expand Down
4 changes: 2 additions & 2 deletions packages/language/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@
"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",
"propprop": "^0.3.1",
"string-format-obj": "^1.0.0"
},
"devDependencies": {
"@google-cloud/storage": "*",
"mocha": "^2.1.0",
"node-uuid": "^1.4.7",
"proxyquire": "^1.7.10",
Expand Down
4 changes: 2 additions & 2 deletions packages/language/src/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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)
Expand Down
33 changes: 26 additions & 7 deletions packages/language/test/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,15 +45,17 @@ describe('Document', function() {

before(function() {
Document = proxyquire('../src/document.js', {
'@google-cloud/storage': {
File: FakeFile
'@google-cloud/common': {
util: fakeUtil
}
});

DocumentCache = extend(true, {}, Document);
});

beforeEach(function() {
isCustomTypeOverride = null;

for (var property in DocumentCache) {
if (DocumentCache.hasOwnProperty(property)) {
Document[property] = DocumentCache[property];
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions packages/logging/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 6 additions & 9 deletions packages/logging/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

/**
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion packages/logging/src/sink.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ function Sink(logging, name) {
*
* @example
* var config = {
* // ...
* destination: {
* // ...
* }
* };
*
* sink.create(config, function(err, sink, apiResponse) {
Expand Down
Loading

0 comments on commit 24a0252

Please sign in to comment.