diff --git a/lib/pubsub/topic.js b/lib/pubsub/topic.js index 81508fa272ca..df78cbddbdd6 100644 --- a/lib/pubsub/topic.js +++ b/lib/pubsub/topic.js @@ -64,6 +64,10 @@ var ServiceObject = require('../common/service-object.js'); function Topic(pubsub, name) { var baseUrl = '/topics'; + this.name = Topic.formatName_(pubsub.projectId, name); + this.pubsub = pubsub; + this.unformattedName = name.split('/').pop(); + var methods = { /** * Create a topic. @@ -140,7 +144,7 @@ function Topic(pubsub, name) { ServiceObject.call(this, { parent: pubsub, baseUrl: baseUrl, - id: name, + id: this.unformattedName, createMethod: pubsub.createTopic.bind(pubsub), methods: methods }); @@ -173,12 +177,8 @@ function Topic(pubsub, name) { */ this.iam = new IAM(pubsub, { baseUrl: baseUrl, - id: name + id: this.unformattedName }); - - this.name = Topic.formatName_(pubsub.projectId, name); - this.pubsub = pubsub; - this.unformattedName = name; } nodeutil.inherits(Topic, ServiceObject); diff --git a/test/pubsub/topic.js b/test/pubsub/topic.js index a83a5cf81c3f..6cc636e2982a 100644 --- a/test/pubsub/topic.js +++ b/test/pubsub/topic.js @@ -35,12 +35,13 @@ function FakeServiceObject() { nodeutil.inherits(FakeServiceObject, ServiceObject); -describe('Topic', function() { +describe.only('Topic', function() { var Topic; var topic; var PROJECT_ID = 'test-project'; - var TOPIC_NAME = 'test-topic'; + var TOPIC_NAME = 'projects/' + PROJECT_ID + '/topics/test-topic'; + var TOPIC_UNFORMATTED_NAME = TOPIC_NAME.split('/').pop(); var PUBSUB = { projectId: PROJECT_ID, createTopic: util.noop @@ -85,7 +86,7 @@ describe('Topic', function() { assert.strictEqual(calledWith.parent, pubsubInstance); assert.strictEqual(calledWith.baseUrl, '/topics'); - assert.strictEqual(calledWith.id, TOPIC_NAME); + assert.strictEqual(calledWith.id, TOPIC_UNFORMATTED_NAME); assert.deepEqual(calledWith.methods, { create: true, delete: true, @@ -100,7 +101,7 @@ describe('Topic', function() { PUBSUB, { baseUrl: '/topics', - id: TOPIC_NAME + id: TOPIC_UNFORMATTED_NAME } ]); }); @@ -117,6 +118,10 @@ describe('Topic', function() { it('should assign pubsub object to `this`', function() { assert.deepEqual(topic.pubsub, PUBSUB); }); + + it('should localize the unformatted name', function() { + assert.strictEqual(topic.unformattedName, TOPIC_UNFORMATTED_NAME); + }); }); describe('formatMessage_', function() { @@ -142,16 +147,14 @@ describe('Topic', function() { }); describe('formatName_', function() { - var fullName = 'projects/' + PROJECT_ID + '/topics/' + TOPIC_NAME; - it('should format name', function() { - var formattedName = Topic.formatName_(PROJECT_ID, TOPIC_NAME); - assert.equal(formattedName, fullName); + var formattedName = Topic.formatName_(PROJECT_ID, TOPIC_UNFORMATTED_NAME); + assert.equal(formattedName, TOPIC_NAME); }); it('should format name when given a complete name', function() { - var formattedName = Topic.formatName_(PROJECT_ID, fullName); - assert.equal(formattedName, fullName); + var formattedName = Topic.formatName_(PROJECT_ID, TOPIC_NAME); + assert.equal(formattedName, TOPIC_NAME); }); });