Skip to content

Commit 96b5925

Browse files
committed
Add implicit connection/project support to Topic ctor.
1 parent b48e262 commit 96b5925

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

gcloud/pubsub/test_topic.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ def _makeOne(self, *args, **kw):
2525
return self._getTargetClass()(*args, **kw)
2626

2727
def test_ctor_wo_inferred_project_or_connection(self):
28+
from gcloud._testing import _monkey_defaults as _monkey_base_defaults
29+
from gcloud.pubsub._testing import _monkey_defaults
2830
TOPIC_NAME = 'topic_name'
29-
topic = self._makeOne(TOPIC_NAME)
31+
PROJECT = 'PROJECT'
32+
conn = _Connection()
33+
with _monkey_base_defaults(project=PROJECT):
34+
with _monkey_defaults(connection=conn):
35+
topic = self._makeOne(TOPIC_NAME)
3036
self.assertEqual(topic.name, TOPIC_NAME)
31-
self.assertEqual(topic.project, None)
32-
self.assertEqual(topic.connection, None)
37+
self.assertEqual(topic.project, PROJECT)
38+
self.assertTrue(topic.connection is conn)
3339

3440
def test_ctor_w_explicit_project_and_connection(self):
3541
TOPIC_NAME = 'topic_name'

gcloud/pubsub/topic.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
import base64
1818

19+
from gcloud._helpers import get_default_project
1920
from gcloud.exceptions import NotFound
21+
from gcloud.pubsub._implicit_environ import get_default_connection
2022

2123

2224
class Topic(object):
@@ -40,6 +42,10 @@ class Topic(object):
4042
environment.
4143
"""
4244
def __init__(self, name, project=None, connection=None):
45+
if project is None:
46+
project = get_default_project()
47+
if connection is None:
48+
connection = get_default_connection()
4349
self.name = name
4450
self.project = project
4551
self.connection = connection

0 commit comments

Comments
 (0)