コード例 #1
0
 def test_create_design_document_with_docid_encoded_url(self):
     """
     Test creating a design document providing an id that has an encoded url
     """
     ddoc = DesignDocument(self.db, "_design/http://example.com")
     self.assertFalse(ddoc.exists())
     self.assertIsNone(ddoc.get("_rev"))
     ddoc.create()
     self.assertTrue(ddoc.exists())
     self.assertTrue(ddoc.get("_rev").startswith("1-"))
コード例 #2
0
 def test_deleting_index(self):
     """
     Test that deleting an index works as expected.
     """
     ddoc = DesignDocument(self.db, '_design/ddoc001')
     self.assertFalse(ddoc.exists())
     index = Index(self.db, 'ddoc001', 'index001', fields=['name', 'age'])
     index.create()
     self.assertTrue(ddoc.exists())
     index.delete()
     self.assertFalse(ddoc.exists())
コード例 #3
0
 def test_deleting_index(self):
     """
     Test that deleting an index works as expected.
     """
     ddoc = DesignDocument(self.db, '_design/ddoc001')
     self.assertFalse(ddoc.exists())
     index = Index(self.db, 'ddoc001', 'index001', fields=['name', 'age'])
     index.create()
     self.assertTrue(ddoc.exists())
     index.delete()
     self.assertFalse(ddoc.exists())
コード例 #4
0
 def test_delete_design_document_success_with_encoded_url(self):
     """
     Test that we can remove a design document from the remote
     database successfully when the document id requires an encoded url.
     """
     ddoc = DesignDocument(self.db, "_design/http://example.com")
     ddoc.create()
     self.assertTrue(ddoc.exists())
     ddoc.delete()
     self.assertFalse(ddoc.exists())
     self.assertEqual(ddoc, {"_id": "_design/http://example.com"})
コード例 #5
0
ファイル: __init__.py プロジェクト: gonrin/gatco_couchdb
    def get(self, key, default=None, remote=False):
        """
        Overrides dictionary __getitem__ behavior to provide a document
        instance for the specified key from the current database.
        If the document instance does not exist locally, then a remote request
        is made and the document is subsequently added to the local cache and
        returned to the caller.
        If the document instance already exists locally then it is returned and
        a remote request is not performed.
        A KeyError will result if the document does not exist locally or in the
        remote database.
        :param str key: Document id used to retrieve the document from the
            database.
        :returns: A Document or DesignDocument object depending on the
            specified document id (key)
        """
        if remote is False:
            if key in list(self.keys()):
                return super(CouchDatabase, self).__getitem__(key)

        if key.startswith('_design/'):
            doc = DesignDocument(self, key)
        else:
            doc = Document(self, key)

        if doc.exists():
            doc.fetch()
            super(CouchDatabase, self).__setitem__(key, doc)
            return doc

        return default
コード例 #6
0
 def __contains__(self, key):
     if not self.remote:
         if key in list(self.keys(remote=False)):
             return True
     if key.startswith("_design/"):
         doc = DesignDocument(self, key)
     else:
         doc = Document(self, key)
     return doc.exists()
コード例 #7
0
 def test_deleting_non_existing_index(self):
     """
     Tests how deleting a non-existing index is handled.
     """
     ddoc = DesignDocument(self.db, '_design/ddoc001')
     index = Index(self.db, 'ddoc001', 'index001', fields=['name', 'age'])
     self.assertFalse(ddoc.exists())
     with self.assertRaises(requests.HTTPError) as cm:
         index.delete()
     err = cm.exception
     self.assertEqual(err.response.status_code, 404)
コード例 #8
0
 def test_deleting_non_existing_index(self):
     """
     Tests how deleting a non-existing index is handled.
     """
     ddoc = DesignDocument(self.db, '_design/ddoc001')
     index = Index(self.db, 'ddoc001', 'index001', fields=['name', 'age'])
     self.assertFalse(ddoc.exists())
     with self.assertRaises(requests.HTTPError) as cm:
         index.delete()
     err = cm.exception
     self.assertEqual(err.response.status_code, 404)
コード例 #9
0
 def test_deleting_index_without_index_name(self):
     """
     Tests that deleting an index without an index name provided fails as
     expected.
     """
     ddoc = DesignDocument(self.db, '_design/ddoc001')
     index = Index(self.db, 'ddoc001', fields=['name', 'age'])
     self.assertFalse(ddoc.exists())
     with self.assertRaises(CloudantArgumentError) as cm:
         index.delete()
     err = cm.exception
     self.assertEqual(
         str(err), 'Deleting an index requires an index name be provided.')
コード例 #10
0
 def __getitem__(self, key):
     if not self.remote:
         if key in list(self.keys(remote=False)):
             return super(CouchDatabase, self).__getitem__(key)
     if key.startswith("_design/"):
         doc = DesignDocument(self, key)
     else:
         doc = Document(self, key)
     if doc.exists():
         doc.fetch()
         super(CouchDatabase, self).__setitem__(key, doc)
         return doc
     else:
         raise KeyError(key)
コード例 #11
0
 def test_deleting_index_without_index_name(self):
     """
     Tests that deleting an index without an index name provided fails as
     expected.
     """
     ddoc = DesignDocument(self.db, '_design/ddoc001')
     index = Index(self.db, 'ddoc001', fields=['name', 'age'])
     self.assertFalse(ddoc.exists())
     with self.assertRaises(CloudantArgumentError) as cm:
         index.delete()
     err = cm.exception
     self.assertEqual(
         str(err),
         'Deleting an index requires an index name be provided.'
     )
コード例 #12
0
def create_latest_recommendations_index():

    ddoc_fn = '''
function(doc) {
  emit([doc.user, doc.timestamp], null);
}
'''    
    db = cloudant_client[CL_RECOMMENDDB]
    index_name = 'latest-recommendation-index'

    ddoc = DesignDocument(db, index_name)
    if ddoc.exists():
        ddoc.fetch()
        ddoc.update_view(index_name, ddoc_fn)
        print('updated', index_name)
    else:
        ddoc.add_view(index_name, ddoc_fn)
        print('created', index_name)
    ddoc.save()
コード例 #13
0
def create_authdb_indexes():

    db = cloudant_client[CL_AUTHDB]

    ddoc_fn = '''
function(doc){
  if (doc.email) {
    emit(doc.email);
  }
}
'''    
    view_name = 'authdb-email-index'

    ddoc = DesignDocument(db, view_name)
    if ddoc.exists():
        ddoc.fetch()
        ddoc.update_view(view_name, ddoc_fn)
        print('updated', view_name)
    else:
        ddoc.add_view(view_name, ddoc_fn)
        print('created', view_name)
    ddoc.save()
コード例 #14
0
def create_moviedb_indexes():

    ddoc_fn = '''
function(doc){
  index("default", doc._id);
  if (doc.name){
    index("name", doc.name, {"store": true});
  }
}
'''    
    db = cloudant_client[CL_MOVIEDB]
    index_name = 'movie-search-index'

    ddoc = DesignDocument(db, index_name)
    if ddoc.exists():
        ddoc.fetch()
        ddoc.update_search_index(index_name, ddoc_fn, analyzer=None)
        print('updated', index_name)
    else:
        ddoc.add_search_index(index_name, ddoc_fn, analyzer=None)
        print('created', index_name)
    ddoc.save()
コード例 #15
0
from cloudant.client import CouchDB
from cloudant.view import View
from cloudant.error import CloudantClientException

vote_map = """\
function(doc) {
   if(doc.pollID && doc.votes) {
      emit(doc.pollID, {
         pollID: doc.pollID,
         votes: doc.votes
      });
   }
}
"""

client = CouchDB(None,
                 None,
                 url='http://' + os.environ['DB_NAME'] + ':5984',
                 admin_party=True,
                 connect=True)
try:
    couch = client.create_database(dbname=os.environ['DB_NAME'],
                                   throw_on_exists=True)
except CloudantClientException:
    couch = client[os.environ['DB_NAME']]

ddoc = DesignDocument(couch, '_design/' + os.environ['DB_NAME'])
ddoc.add_view(os.environ['DB_NAME'], vote_map)
if ddoc.exists() is not True: ddoc.save()
vote_view = View(ddoc, os.environ['DB_NAME'])