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

Adds 6 quickstart examples #218

Merged
merged 8 commits into from
Sep 28, 2016
Merged
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
29 changes: 29 additions & 0 deletions bigquery/quickstart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2016, Google, Inc.
// 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';

// [START bigquery_quickstart]
// Imports and instantiates the Google Cloud client library
// for Google BigQuery
const bigquery = require('@google-cloud/bigquery')({
projectId: 'YOUR_PROJECT_ID'
});

// Creates a new dataset
bigquery.createDataset('my_new_dataset', (err, dataset) => {
if (!err) {
// The dataset was created successfully
}
});
// [END bigquery_quickstart]
50 changes: 50 additions & 0 deletions bigquery/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2015-2016, Google, Inc.
// 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';

const proxyquire = require(`proxyquire`).noPreserveCache();
const bigquery = proxyquire(`@google-cloud/bigquery`, {})();

const datasetName = `my_new_dataset`;

describe(`bigquery:quickstart`, () => {
let bigqueryMock, BigqueryMock;

after((done) => {
bigquery.dataset(datasetName).delete(() => {
// Ignore any error, the dataset might not have been created
done();
});
});

it(`should create a dataset`, (done) => {
bigqueryMock = {
createDataset: (_datasetName) => {
assert.equal(_datasetName, datasetName);

bigquery.createDataset(datasetName, (err, dataset, apiResponse) => {
assert.ifError(err);
assert.notEqual(dataset, undefined);
assert.notEqual(apiResponse, undefined);
done();
});
}
};
BigqueryMock = sinon.stub().returns(bigqueryMock);

proxyquire(`../quickstart`, {
'@google-cloud/bigquery': BigqueryMock
});
});
});
38 changes: 38 additions & 0 deletions bigquery/test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2016, Google, Inc.
// 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';

const proxyquire = require(`proxyquire`).noCallThru();

describe(`bigquery:quickstart`, () => {
let bigqueryMock, BigqueryMock;

before(() => {
bigqueryMock = {
createDataset: sinon.stub().yields(null, {}, {})
};
BigqueryMock = sinon.stub().returns(bigqueryMock);
});

it(`should create a dataset`, () => {
proxyquire(`../quickstart`, {
'@google-cloud/bigquery': BigqueryMock
});

assert.equal(BigqueryMock.calledOnce, true);
assert.deepEqual(BigqueryMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]);
assert.equal(bigqueryMock.createDataset.calledOnce, true);
assert.deepEqual(bigqueryMock.createDataset.firstCall.args.slice(0, -1), ['my_new_dataset']);
});
});
31 changes: 31 additions & 0 deletions datastore/quickstart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2016, Google, Inc.
// 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';

// [START datastore_quickstart]
// Imports and instantiates the Google Cloud client library
// for Google Cloud Datastore
const datastore = require('@google-cloud/datastore')({
projectId: 'YOUR_PROJECT_ID'
});

const taskKey = datastore.key(['Task', 1234]);

// Retrieves an entity
datastore.get(taskKey, (err, entity) => {
if (!err) {
// The entity was retrieved successfully
}
});
// [END datastore_quickstart]
69 changes: 69 additions & 0 deletions datastore/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2015-2016, Google, Inc.
// 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';

const proxyquire = require(`proxyquire`).noPreserveCache();
const datastore = proxyquire(`@google-cloud/datastore`, {})();
const kind = `Task`;
const message = `Buy milk`;
const key = datastore.key(kind);

describe(`datastore:quickstart`, () => {
let datastoreMock, DatastoreMock;

before((done) => {
datastore.save({
key: key,
data: {
message: message
}
}, () => {
// Datastore is eventually consistent
setTimeout(done, 5000);
});
});

after((done) => {
datastore.delete(key, () => {
// Ignore any error, the entity might not have been created
done();
});
});

it(`should get a task from Datastore`, (done) => {
datastoreMock = {
key: () => {
return key;
},

get: (_key) => {
assert.equal(_key, key);

datastore.get(_key, (err, entity) => {
assert.ifError(err);
assert.notEqual(entity, undefined);
assert.notEqual(entity.key, undefined);
assert.equal(entity.key.kind, kind);
assert.deepEqual(entity.data, { message: message });
done();
});
}
};
DatastoreMock = sinon.stub().returns(datastoreMock);

proxyquire(`../quickstart`, {
'@google-cloud/datastore': DatastoreMock
});
});
});
39 changes: 39 additions & 0 deletions datastore/test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2015-2016, Google, Inc.
// 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';

const proxyquire = require(`proxyquire`).noPreserveCache();

describe(`datastore:quickstart`, () => {
let datastoreMock, DatastoreMock;

before(() => {
datastoreMock = {
get: sinon.stub().yields(null, { key: 1234 }),
key: sinon.stub.returns(`task/1234`)
};
DatastoreMock = sinon.stub().returns(datastoreMock);
});

it(`should get a task from Datastore`, () => {
proxyquire(`../quickstart`, {
'@google-cloud/datastore': DatastoreMock
});

assert.equal(DatastoreMock.calledOnce, true);
assert.deepEqual(DatastoreMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]);
assert.equal(datastoreMock.get.calledOnce, true);
assert.deepEqual(datastoreMock.get.firstCall.args.slice(0, -1), [datastoreMock.key(['Task', 1234])]);
});
});
34 changes: 34 additions & 0 deletions logging/quickstart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2016, Google, Inc.
// 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';

// [START logging_quickstart]
// Imports and instantiates the Google Cloud client library
// for Stackdriver Logging
const logging = require('@google-cloud/logging')({
projectId: 'YOUR_PROJECT_ID'
});

// Selects the log to write to
const log = logging.log('my-log');
// Prepares a log entry
const entry = log.entry({ type: 'global' }, 'Hello, world!');

// Writes the log entry
log.write(entry, (err) => {
if (!err) {
// The entry was logged successfully
}
});
// [END logging_quickstart]
62 changes: 62 additions & 0 deletions logging/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2015-2016, Google, Inc.
// 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';

const proxyquire = require(`proxyquire`).noPreserveCache();
const logging = proxyquire(`@google-cloud/logging`, {})();
const uuid = require(`node-uuid`);

const logName = `nodejs-docs-samples-test-${uuid.v4()}`;

describe(`logging:quickstart`, () => {
let logMock, loggingMock, LoggingMock;

after((done) => {
logging.log(logName).delete(() => {
// Ignore any error, the topic might not have been created
done();
});
});

it(`should log an entry`, (done) => {
const expectedlogName = `my-log`;

logMock = {
entry: sinon.stub().returns({}),
write: (_entry) => {
assert.deepEqual(_entry, {});

const log = logging.log(logName);
const entry = log.entry({ type: `global` }, `Hello, world!`);
log.write(entry, (err, apiResponse) => {
assert.ifError(err);
assert.notEqual(apiResponse, undefined);
// Logs are eventually consistent
setTimeout(done, 5000);
});
}
};
loggingMock = {
log: (_logName) => {
assert.equal(_logName, expectedlogName);
return logMock;
}
};
LoggingMock = sinon.stub().returns(loggingMock);

proxyquire(`../quickstart`, {
'@google-cloud/logging': LoggingMock
});
});
});
Loading