-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #930 from dhermes/pubsub-subscription-factory
Implementing `topic.subscription` factory.
- Loading branch information
Showing
8 changed files
with
154 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2015 Google Inc. All rights reserved. | ||
# | ||
# 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. | ||
|
||
"""Helper functions for shared behavior.""" | ||
|
||
|
||
def topic_name_from_path(path, project): | ||
"""Validate a topic URI path and get the topic name. | ||
:type path: string | ||
:param path: URI path for a topic API request. | ||
:type project: string | ||
:param project: The project associated with the request. It is | ||
included for validation purposes. | ||
:rtype: string | ||
:returns: Topic name parsed from ``path``. | ||
:raises: :class:`ValueError` if the ``path`` is ill-formed or if | ||
the project from the ``path`` does not agree with the | ||
``project`` passed in. | ||
""" | ||
# PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME) | ||
path_parts = path.split('/') | ||
if (len(path_parts) != 4 or path_parts[0] != 'projects' or | ||
path_parts[2] != 'topics'): | ||
raise ValueError('Expected path to be of the form ' | ||
'projects/{project}/topics/{topic_name}') | ||
if (len(path_parts) != 4 or path_parts[0] != 'projects' or | ||
path_parts[2] != 'topics' or path_parts[1] != project): | ||
raise ValueError('Project from client should agree with ' | ||
'project from resource.') | ||
|
||
return path_parts[3] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright 2015 Google Inc. All rights reserved. | ||
# | ||
# 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. | ||
|
||
import unittest2 | ||
|
||
|
||
class Test_topic_name_from_path(unittest2.TestCase): | ||
|
||
def _callFUT(self, path, project): | ||
from gcloud.pubsub._helpers import topic_name_from_path | ||
return topic_name_from_path(path, project) | ||
|
||
def test_invalid_path_length(self): | ||
PATH = 'projects/foo' | ||
PROJECT = None | ||
self.assertRaises(ValueError, self._callFUT, PATH, PROJECT) | ||
|
||
def test_invalid_path_format(self): | ||
TOPIC_NAME = 'TOPIC_NAME' | ||
PROJECT = 'PROJECT' | ||
PATH = 'foo/%s/bar/%s' % (PROJECT, TOPIC_NAME) | ||
self.assertRaises(ValueError, self._callFUT, PATH, PROJECT) | ||
|
||
def test_invalid_project(self): | ||
TOPIC_NAME = 'TOPIC_NAME' | ||
PROJECT1 = 'PROJECT1' | ||
PROJECT2 = 'PROJECT2' | ||
PATH = 'projects/%s/topics/%s' % (PROJECT1, TOPIC_NAME) | ||
self.assertRaises(ValueError, self._callFUT, PATH, PROJECT2) | ||
|
||
def test_valid_data(self): | ||
TOPIC_NAME = 'TOPIC_NAME' | ||
PROJECT = 'PROJECT' | ||
PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME) | ||
topic_name = self._callFUT(PATH, PROJECT) | ||
self.assertEqual(topic_name, TOPIC_NAME) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters