Skip to content

Commit

Permalink
Merge pull request #1179 from tseaver/search-index_document_factory
Browse files Browse the repository at this point in the history
Add 'Index.document' factory
  • Loading branch information
tseaver committed Oct 14, 2015
2 parents 7196d60 + 72ad42d commit 60799e8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
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

0 comments on commit 60799e8

Please sign in to comment.