Skip to content

Commit 41d189b

Browse files
jmdobryAce Nassri
authored andcommitted
Adds 6 quickstart examples (#218)
* Add some simple "quickstart" samples. * Add quickstart tests (#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 5c88961 commit 41d189b

3 files changed

Lines changed: 125 additions & 0 deletions

File tree

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 pubsub_quickstart]
17+
// Imports and instantiates the Google Cloud client library
18+
// for Google Cloud Pub/Sub
19+
const pubsub = require('@google-cloud/pubsub')({
20+
projectId: 'YOUR_PROJECT_ID'
21+
});
22+
23+
// Creates a new topic
24+
pubsub.createTopic('my-new-topic', (err, topic) => {
25+
if (!err) {
26+
// The topic was created successfully
27+
}
28+
});
29+
// [END pubsub_quickstart]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 pubsub = proxyquire(`@google-cloud/pubsub`, {})();
18+
const uuid = require(`node-uuid`);
19+
20+
const topicName = `nodejs-docs-samples-test-${uuid.v4()}`;
21+
const projectId = process.env.GCLOUD_PROJECT;
22+
const fullTopicName = `projects/${projectId}/topics/${topicName}`;
23+
24+
describe(`pubsub:quickstart`, () => {
25+
let pubsubMock, PubSubMock;
26+
27+
after((done) => {
28+
pubsub.topic(topicName).delete(() => {
29+
// Ignore any error, the topic might not have been created
30+
done();
31+
});
32+
});
33+
34+
it(`should create a topic`, (done) => {
35+
const expectedTopicName = `my-new-topic`;
36+
37+
pubsubMock = {
38+
createTopic: (_topicName) => {
39+
assert.equal(_topicName, expectedTopicName);
40+
41+
pubsub.createTopic(topicName, (err, topic, apiResponse) => {
42+
assert.ifError(err);
43+
assert.notEqual(topic, undefined);
44+
assert.equal(topic.name, fullTopicName);
45+
assert.notEqual(apiResponse, undefined);
46+
done();
47+
});
48+
}
49+
};
50+
PubSubMock = sinon.stub().returns(pubsubMock);
51+
52+
proxyquire(`../quickstart`, {
53+
'@google-cloud/pubsub': PubSubMock
54+
});
55+
});
56+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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(`pubsub:quickstart`, () => {
19+
let pubsubMock, PubSubMock;
20+
21+
const expectedTopicName = `my-new-topic`;
22+
23+
before(() => {
24+
pubsubMock = {
25+
createTopic: sinon.stub().yields(null, {}, {})
26+
};
27+
PubSubMock = sinon.stub().returns(pubsubMock);
28+
});
29+
30+
it(`should create a topic`, () => {
31+
proxyquire(`../quickstart`, {
32+
'@google-cloud/pubsub': PubSubMock
33+
});
34+
35+
assert.equal(PubSubMock.calledOnce, true);
36+
assert.deepEqual(PubSubMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]);
37+
assert.equal(pubsubMock.createTopic.calledOnce, true);
38+
assert.deepEqual(pubsubMock.createTopic.firstCall.args.slice(0, -1), [expectedTopicName]);
39+
});
40+
});

0 commit comments

Comments
 (0)