Skip to content

Commit 1217387

Browse files
jmdobryAce Nassri
authored andcommitted
Adds 6 quickstart examples (GoogleCloudPlatform#218)
* Add some simple "quickstart" samples. * Add quickstart tests (GoogleCloudPlatform#215) * First draft of new tests * Fix failing tests * Fix comments * Add comments. * Update comments. * Add another comment. * Cleanup. * Fix region tags. * Fix comments.
1 parent 35b153f commit 1217387

File tree

18 files changed

+763
-0
lines changed

18 files changed

+763
-0
lines changed

bigquery/quickstart.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START bigquery_quickstart]
17+
// Imports and instantiates the Google Cloud client library
18+
// for Google BigQuery
19+
const bigquery = require('@google-cloud/bigquery')({
20+
projectId: 'YOUR_PROJECT_ID'
21+
});
22+
23+
// Creates a new dataset
24+
bigquery.createDataset('my_new_dataset', (err, dataset) => {
25+
if (!err) {
26+
// The dataset was created successfully
27+
}
28+
});
29+
// [END bigquery_quickstart]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2015-2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
const proxyquire = require(`proxyquire`).noPreserveCache();
17+
const bigquery = proxyquire(`@google-cloud/bigquery`, {})();
18+
19+
const datasetName = `my_new_dataset`;
20+
21+
describe(`bigquery:quickstart`, () => {
22+
let bigqueryMock, BigqueryMock;
23+
24+
after((done) => {
25+
bigquery.dataset(datasetName).delete(() => {
26+
// Ignore any error, the dataset might not have been created
27+
done();
28+
});
29+
});
30+
31+
it(`should create a dataset`, (done) => {
32+
bigqueryMock = {
33+
createDataset: (_datasetName) => {
34+
assert.equal(_datasetName, datasetName);
35+
36+
bigquery.createDataset(datasetName, (err, dataset, apiResponse) => {
37+
assert.ifError(err);
38+
assert.notEqual(dataset, undefined);
39+
assert.notEqual(apiResponse, undefined);
40+
done();
41+
});
42+
}
43+
};
44+
BigqueryMock = sinon.stub().returns(bigqueryMock);
45+
46+
proxyquire(`../quickstart`, {
47+
'@google-cloud/bigquery': BigqueryMock
48+
});
49+
});
50+
});

bigquery/test/quickstart.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
const proxyquire = require(`proxyquire`).noCallThru();
17+
18+
describe(`bigquery:quickstart`, () => {
19+
let bigqueryMock, BigqueryMock;
20+
21+
before(() => {
22+
bigqueryMock = {
23+
createDataset: sinon.stub().yields(null, {}, {})
24+
};
25+
BigqueryMock = sinon.stub().returns(bigqueryMock);
26+
});
27+
28+
it(`should create a dataset`, () => {
29+
proxyquire(`../quickstart`, {
30+
'@google-cloud/bigquery': BigqueryMock
31+
});
32+
33+
assert.equal(BigqueryMock.calledOnce, true);
34+
assert.deepEqual(BigqueryMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]);
35+
assert.equal(bigqueryMock.createDataset.calledOnce, true);
36+
assert.deepEqual(bigqueryMock.createDataset.firstCall.args.slice(0, -1), ['my_new_dataset']);
37+
});
38+
});

datastore/quickstart.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START datastore_quickstart]
17+
// Imports and instantiates the Google Cloud client library
18+
// for Google Cloud Datastore
19+
const datastore = require('@google-cloud/datastore')({
20+
projectId: 'YOUR_PROJECT_ID'
21+
});
22+
23+
const taskKey = datastore.key(['Task', 1234]);
24+
25+
// Retrieves an entity
26+
datastore.get(taskKey, (err, entity) => {
27+
if (!err) {
28+
// The entity was retrieved successfully
29+
}
30+
});
31+
// [END datastore_quickstart]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2015-2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
const proxyquire = require(`proxyquire`).noPreserveCache();
17+
const datastore = proxyquire(`@google-cloud/datastore`, {})();
18+
const kind = `Task`;
19+
const message = `Buy milk`;
20+
const key = datastore.key(kind);
21+
22+
describe(`datastore:quickstart`, () => {
23+
let datastoreMock, DatastoreMock;
24+
25+
before((done) => {
26+
datastore.save({
27+
key: key,
28+
data: {
29+
message: message
30+
}
31+
}, () => {
32+
// Datastore is eventually consistent
33+
setTimeout(done, 5000);
34+
});
35+
});
36+
37+
after((done) => {
38+
datastore.delete(key, () => {
39+
// Ignore any error, the entity might not have been created
40+
done();
41+
});
42+
});
43+
44+
it(`should get a task from Datastore`, (done) => {
45+
datastoreMock = {
46+
key: () => {
47+
return key;
48+
},
49+
50+
get: (_key) => {
51+
assert.equal(_key, key);
52+
53+
datastore.get(_key, (err, entity) => {
54+
assert.ifError(err);
55+
assert.notEqual(entity, undefined);
56+
assert.notEqual(entity.key, undefined);
57+
assert.equal(entity.key.kind, kind);
58+
assert.deepEqual(entity.data, { message: message });
59+
done();
60+
});
61+
}
62+
};
63+
DatastoreMock = sinon.stub().returns(datastoreMock);
64+
65+
proxyquire(`../quickstart`, {
66+
'@google-cloud/datastore': DatastoreMock
67+
});
68+
});
69+
});

datastore/test/quickstart.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2015-2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
const proxyquire = require(`proxyquire`).noPreserveCache();
17+
18+
describe(`datastore:quickstart`, () => {
19+
let datastoreMock, DatastoreMock;
20+
21+
before(() => {
22+
datastoreMock = {
23+
get: sinon.stub().yields(null, { key: 1234 }),
24+
key: sinon.stub.returns(`task/1234`)
25+
};
26+
DatastoreMock = sinon.stub().returns(datastoreMock);
27+
});
28+
29+
it(`should get a task from Datastore`, () => {
30+
proxyquire(`../quickstart`, {
31+
'@google-cloud/datastore': DatastoreMock
32+
});
33+
34+
assert.equal(DatastoreMock.calledOnce, true);
35+
assert.deepEqual(DatastoreMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]);
36+
assert.equal(datastoreMock.get.calledOnce, true);
37+
assert.deepEqual(datastoreMock.get.firstCall.args.slice(0, -1), [datastoreMock.key(['Task', 1234])]);
38+
});
39+
});

logging/quickstart.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START logging_quickstart]
17+
// Imports and instantiates the Google Cloud client library
18+
// for Stackdriver Logging
19+
const logging = require('@google-cloud/logging')({
20+
projectId: 'YOUR_PROJECT_ID'
21+
});
22+
23+
// Selects the log to write to
24+
const log = logging.log('my-log');
25+
// Prepares a log entry
26+
const entry = log.entry({ type: 'global' }, 'Hello, world!');
27+
28+
// Writes the log entry
29+
log.write(entry, (err) => {
30+
if (!err) {
31+
// The entry was logged successfully
32+
}
33+
});
34+
// [END logging_quickstart]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2015-2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
const proxyquire = require(`proxyquire`).noPreserveCache();
17+
const logging = proxyquire(`@google-cloud/logging`, {})();
18+
const uuid = require(`node-uuid`);
19+
20+
const logName = `nodejs-docs-samples-test-${uuid.v4()}`;
21+
22+
describe(`logging:quickstart`, () => {
23+
let logMock, loggingMock, LoggingMock;
24+
25+
after((done) => {
26+
logging.log(logName).delete(() => {
27+
// Ignore any error, the topic might not have been created
28+
done();
29+
});
30+
});
31+
32+
it(`should log an entry`, (done) => {
33+
const expectedlogName = `my-log`;
34+
35+
logMock = {
36+
entry: sinon.stub().returns({}),
37+
write: (_entry) => {
38+
assert.deepEqual(_entry, {});
39+
40+
const log = logging.log(logName);
41+
const entry = log.entry({ type: `global` }, `Hello, world!`);
42+
log.write(entry, (err, apiResponse) => {
43+
assert.ifError(err);
44+
assert.notEqual(apiResponse, undefined);
45+
// Logs are eventually consistent
46+
setTimeout(done, 5000);
47+
});
48+
}
49+
};
50+
loggingMock = {
51+
log: (_logName) => {
52+
assert.equal(_logName, expectedlogName);
53+
return logMock;
54+
}
55+
};
56+
LoggingMock = sinon.stub().returns(loggingMock);
57+
58+
proxyquire(`../quickstart`, {
59+
'@google-cloud/logging': LoggingMock
60+
});
61+
});
62+
});

0 commit comments

Comments
 (0)