Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'Index.document' factory #1179

Merged
merged 2 commits into from
Oct 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gcloud/search/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def index(self, name):
"""Construct an index bound to this client.

:type name: string
:param name: Name of the zone.
:param name: Name of the index.

:rtype: :class:`gcloud.search.index.Index`
:returns: a new ``Index`` instance
Expand Down
15 changes: 15 additions & 0 deletions gcloud/search/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,18 @@ def list_documents(self, max_results=None, page_token=None,
zones = [Document.from_api_repr(resource, self)
for resource in resp['documents']]
return zones, resp.get('nextPageToken')

def document(self, name, rank=None):
"""Construct a document bound to this index.

:type name: string
:param name: Name of the document.

:type rank: integer
:param rank: Rank of the document (defaults to a server-assigned
value based on timestamp).

:rtype: :class:`gcloud.search.document.Document`
:returns: a new ``Document`` instance
"""
return Document(name, index=self, rank=rank)
27 changes: 27 additions & 0 deletions gcloud/search/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,33 @@ def test_list_documents_explicit(self):
'pageToken': TOKEN,
'view': 'FULL'})

def test_document_defaults(self):
from gcloud.search.document import Document
DOCUMENT_ID = 'document-id'
client = _Client(self.PROJECT)
index = self._makeOne(self.INDEX_ID, client)

document = index.document(DOCUMENT_ID)

self.assertTrue(isinstance(document, Document))
self.assertEqual(document.name, DOCUMENT_ID)
self.assertEqual(document.rank, None)
self.assertTrue(document.index is index)

def test_document_explicit(self):
from gcloud.search.document import Document
DOCUMENT_ID = 'document-id'
RANK = 1234
client = _Client(self.PROJECT)
index = self._makeOne(self.INDEX_ID, client)

document = index.document(DOCUMENT_ID, rank=RANK)

self.assertTrue(isinstance(document, Document))
self.assertEqual(document.name, DOCUMENT_ID)
self.assertEqual(document.rank, RANK)
self.assertTrue(document.index is index)


class _Client(object):

Expand Down