Example #1
0
    def test_create_an_index_using_design_prefix(self):
        """
        Test that a JSON index is created correctly in the remote database when
        the ddoc id is already prefixed by '_design/'
        """
        index = Index(self.db,
                      '_design/ddoc001',
                      'index001',
                      fields=['name', 'age'])
        index.create()
        self.assertEqual(index.design_document_id, '_design/ddoc001')
        self.assertEqual(index.name, 'index001')
        with DesignDocument(self.db, index.design_document_id) as ddoc:
            self.assertIsInstance(ddoc.get_view(index.name), QueryIndexView)

            self.assertEquals(ddoc['_id'], index.design_document_id)
            self.assertTrue(ddoc['_rev'].startswith('1-'))

            self.assertEquals(ddoc['indexes'], {})
            self.assertEquals(ddoc['language'], 'query')
            self.assertEquals(ddoc['lists'], {})
            self.assertEquals(ddoc['shows'], {})

            self.assertListEqual(list(ddoc['views'].keys()), [index.name])

            view = ddoc['views'][index.name]
            self.assertEquals(view['map']['fields']['age'], 'asc')
            self.assertEquals(view['map']['fields']['name'], 'asc')
            self.assertEquals(view['options']['def']['fields'],
                              ['name', 'age'])
            self.assertEquals(view['reduce'], '_count')
Example #2
0
 def test_create_an_index_with_empty_ddoc_index_name(self):
     """
     Test that a JSON index is created in the remote database.
     """
     index = Index(self.db, '', '', fields=['name', 'age'])
     index.create()
     self.assertIsNotNone(index.design_document_id)
     self.assertTrue(index.design_document_id.startswith('_design/'))
     self.assertIsNotNone(index.name)
     with DesignDocument(self.db, index.design_document_id) as ddoc:
         self.assertEqual(ddoc['language'], 'query')
         self.assertListEqual(list(ddoc['views'].keys()), [index.name])
         self.assertIsInstance(ddoc.get_view(index.name), QueryIndexView)
         self.assertTrue(ddoc['_rev'].startswith('1-'))
         self.assertEqual(ddoc,
             {'_id': index.design_document_id,
              '_rev': ddoc['_rev'],
              'indexes': {},
              'language': 'query',
              'views': {index.name: {'map': {'fields': {'name': 'asc',
                                                        'age': 'asc'}},
                                     'reduce': '_count',
                                     'options': {'def': {'fields': ['name',
                                                                    'age']},
                                                }}},
              'lists': {},
              'shows': {}
              }
         )
Example #3
0
 def test_create_an_index_using_design_prefix(self):
     """
     Test that a JSON index is created correctly in the remote database when
     the ddoc id is already prefixed by '_design/'
     """
     index = Index(self.db, '_design/ddoc001', 'index001', fields=['name', 'age'])
     index.create()
     self.assertEqual(index.design_document_id, '_design/ddoc001')
     self.assertEqual(index.name, 'index001')
     with DesignDocument(self.db, index.design_document_id) as ddoc:
         self.assertEqual(ddoc['language'], 'query')
         self.assertListEqual(list(ddoc['views'].keys()), ['index001'])
         self.assertIsInstance(ddoc.get_view('index001'), QueryIndexView)
         self.assertTrue(ddoc['_rev'].startswith('1-'))
         self.assertEqual(ddoc,
             {'_id': '_design/ddoc001',
              '_rev': ddoc['_rev'],
              'indexes': {},
              'language': 'query',
              'views': {'index001': {'map': {'fields': {'name': 'asc',
                                                        'age': 'asc'}},
                                     'reduce': '_count',
                                     'options': {'def': {'fields': ['name',
                                                                    'age']},
                                                }}},
              'lists': {},
              'shows': {}
              }
         )
Example #4
0
 def test_create_an_index_with_empty_ddoc_index_name(self):
     """
     Test that a JSON index is created in the remote database.
     """
     index = Index(self.db, '', '', fields=['name', 'age'])
     index.create()
     self.assertIsNotNone(index.design_document_id)
     self.assertTrue(index.design_document_id.startswith('_design/'))
     self.assertIsNotNone(index.name)
     with DesignDocument(self.db, index.design_document_id) as ddoc:
         self.assertEqual(ddoc['language'], 'query')
         self.assertListEqual(list(ddoc['views'].keys()), [index.name])
         self.assertIsInstance(ddoc.get_view(index.name), QueryIndexView)
         self.assertTrue(ddoc['_rev'].startswith('1-'))
         self.assertEqual(ddoc,
             {'_id': index.design_document_id,
              '_rev': ddoc['_rev'],
              'indexes': {},
              'language': 'query',
              'views': {index.name: {'map': {'fields': {'name': 'asc',
                                                        'age': 'asc'}},
                                     'reduce': '_count',
                                     'options': {'def': {'fields': ['name',
                                                                    'age']},
                                                }}},
              'lists': {},
              'shows': {}
              }
         )
Example #5
0
    def test_create_an_index_with_empty_ddoc_index_name(self):
        """
        Test that a JSON index is created in the remote database.
        """
        index = Index(self.db, '', '', fields=['name', 'age'])
        index.create()
        self.assertIsNotNone(index.design_document_id)
        self.assertTrue(index.design_document_id.startswith('_design/'))
        self.assertIsNotNone(index.name)
        with DesignDocument(self.db, index.design_document_id) as ddoc:
            self.assertIsInstance(ddoc.get_view(index.name), QueryIndexView)

            self.assertEquals(ddoc['_id'], index.design_document_id)
            self.assertTrue(ddoc['_rev'].startswith('1-'))

            self.assertEquals(ddoc['indexes'], {})
            self.assertEquals(ddoc['language'], 'query')
            self.assertEquals(ddoc['lists'], {})
            self.assertEquals(ddoc['shows'], {})

            self.assertListEqual(list(ddoc['views'].keys()), [index.name])

            view = ddoc['views'][index.name]
            self.assertEquals(view['map']['fields']['age'], 'asc')
            self.assertEquals(view['map']['fields']['name'], 'asc')
            self.assertEquals(view['options']['def']['fields'],
                              ['name', 'age'])
            self.assertEquals(view['reduce'], '_count')
Example #6
0
 def test_create_uses_custom_encoder(self):
     """
     Test that the create method uses the custom encoder
     """
     self.set_up_client(auto_connect=True, encoder="AEncoder")
     database = self.client[self.test_dbname]
     index = Index(database, '_design/ddoc001', 'index001', fields=['name', 'age'])
     with self.assertRaises(TypeError):
         index.create()
Example #7
0
 def test_create_uses_custom_encoder(self):
     """
     Test that the create method uses the custom encoder
     """
     self.set_up_client(auto_connect=True, encoder="AEncoder")
     database = self.client[self.test_dbname]
     index = Index(database, '_design/ddoc001', 'index001', fields=['name', 'age'])
     with self.assertRaises(TypeError):
         index.create()
Example #8
0
 def test_index_via_query(self):
     """
     Test that a created index will produce expected query results.
     """
     index = Index(self.db, 'ddoc001', 'index001', fields=['age'])
     index.create()
     self.populate_db_with_documents(100)
     query = Query(self.db)
     resp = query(fields=['name', 'age'], selector={'age': {'$eq': 6}})
     self.assertEqual(resp['docs'], [{'name': 'julia', 'age': 6}])
Example #9
0
 def test_create_fails_due_to_index_name_validation(self):
     """
     Ensure that if the index name is not a string the create call fails.
     """
     index = Index(self.db, 'ddoc001', ['index001'], fields=['name', 'age'])
     with self.assertRaises(CloudantArgumentError) as cm:
         index.create()
     err = cm.exception
     self.assertEqual(str(err),
                      'The index name: [\'index001\'] is not a string.')
Example #10
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)
Example #11
0
 def test_index_usage_via_query(self):
     """
     Test that a query will warn if the indexes that exist do not satisfy the
     query selector.
     """
     index = Index(self.db, 'ddoc001', 'index001', fields=['name'])
     index.create()
     self.populate_db_with_documents(100)
     result = self.db.get_query_result(fields=['name', 'age'],
             selector={'age': {'$eq': 6}}, raw_result=True)
     self.assertTrue(str(result['warning']).startswith("no matching index found"))
Example #12
0
 def test_create_fails_due_to_def_validation(self):
     """
     Ensure that if the index definition contains anything other than
     "fields" the create call fails.
     """
     index = Index(self.db, fields=['name', 'age'], selector={})
     with self.assertRaises(CloudantArgumentError) as cm:
         index.create()
     err = cm.exception
     self.assertTrue(str(err).endswith(
         'A JSON index requires that only a \'fields\' argument is provided.'))
Example #13
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)
Example #14
0
 def test_create_fails_due_to_def_validation(self):
     """
     Ensure that if the index definition contains anything other than
     "fields" the create call fails.
     """
     index = Index(self.db, fields=['name', 'age'], selector={})
     with self.assertRaises(CloudantArgumentError) as cm:
         index.create()
     err = cm.exception
     self.assertTrue(str(err).endswith(
         'A JSON index requires that only a \'fields\' argument is provided.'))
Example #15
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())
Example #16
0
 def test_index_usage_via_query(self):
     """
     Test that a query will warn if the indexes that exist do not satisfy the
     query selector.
     """
     index = Index(self.db, 'ddoc001', 'index001', fields=['name'])
     index.create()
     self.populate_db_with_documents(100)
     result = self.db.get_query_result(fields=['name', 'age'],
             selector={'age': {'$eq': 6}}, raw_result=True)
     self.assertTrue(str(result['warning']).startswith("no matching index found"))
Example #17
0
 def test_index_as_a_dict_with_none_attributes(self):
     """
     Test the conversion of an Index object that contains attributes set to
     None into a dictionary representation of that object.
     """
     index = Index(self.db)
     self.assertEqual(index.as_a_dict(), {
         'ddoc': None,
         'name': None,
         'type': 'json',
         'def': {}
     })
Example #18
0
 def test_index_as_a_dict_with_none_attributes(self):
     """
     Test the conversion of an Index object that contains attributes set to
     None into a dictionary representation of that object.
     """
     index = Index(self.db)
     self.assertEqual(index.as_a_dict(), {
         'ddoc': None,
         'name': None,
         'type': 'json',
         'def': {}
     })
Example #19
0
 def test_create_fails_due_to_index_name_validation(self):
     """
     Ensure that if the index name is not a string the create call fails.
     """
     index = Index(self.db, 'ddoc001', ['index001'], fields=['name', 'age'])
     with self.assertRaises(CloudantArgumentError) as cm:
         index.create()
     err = cm.exception
     self.assertEqual(
         str(err),
         'The index name: [\'index001\'] is not a string.'
     )
Example #20
0
 def test_index_to_dictionary(self):
     """
     Test the conversion of an Index object into a dictionary representation
     of that object.
     """
     index = Index(self.db, 'ddoc-id', 'index-name', foo={'bar': 'baz'})
     self.assertEqual(index.as_a_dict(), {
         'ddoc': 'ddoc-id',
         'name': 'index-name',
         'type': 'json',
         'def': {'foo': {'bar': 'baz'}}
     })
Example #21
0
 def test_index_to_dictionary(self):
     """
     Test the conversion of an Index object into a dictionary representation
     of that object.
     """
     index = Index(self.db, 'ddoc-id', 'index-name', foo={'bar': 'baz'})
     self.assertEqual(index.as_a_dict(), {
         'ddoc': 'ddoc-id',
         'name': 'index-name',
         'type': 'json',
         'def': {'foo': {'bar': 'baz'}}
     })
Example #22
0
 def test_index_via_query(self):
     """
     Test that a created index will produce expected query results.
     """
     index = Index(self.db, 'ddoc001', 'index001', fields=['age'])
     index.create()
     self.populate_db_with_documents(100)
     query = Query(self.db)
     resp = query(
         fields=['name', 'age'],
         selector={'age': {'$eq': 6}}
     )
     self.assertEqual(resp['docs'], [{'name': 'julia', 'age': 6}])
Example #23
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.')
Example #24
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.'
     )
Example #25
0
 def test_retrieve_index_url(self):
     """
     Test constructing the Index url
     """
     index = Index(self.db)
     self.assertEqual(index.index_url, '/'.join(
         (self.db.database_url, '_index')))
 def test_index_usage_via_query(self):
     """
     Test that a query will fail if the indexes that exist do not satisfy the
     query selector.
     """
     index = Index(self.db, 'ddoc001', 'index001', fields=['name'])
     index.create()
     self.populate_db_with_documents(100)
     query = Query(self.db)
     with self.assertRaises(requests.HTTPError) as cm:
         resp = query(
             fields=['name', 'age'],
             selector={'age': {'$eq': 6}}
         )
     err = cm.exception
     self.assertEqual(err.response.status_code, 400)
Example #27
0
 def test_constructor_with_only_a_db(self):
     """
     Test instantiating an Index with a database only.  As a side effect
     this test also tests the design_document_id, name, type, and definition
     property methods.
     """
     index = Index(self.db)
     self.assertIsInstance(index, Index)
     self.assertIsNone(index.design_document_id)
     self.assertIsNone(index.name)
     self.assertEqual(index.type, 'json')
     self.assertEqual(index.definition, {})
Example #28
0
 def test_constructor_with_args(self):
     """
     Test instantiating an Index by passing in arguments.  As a side effect
     this test also tests the design_document_id, name, type, and definition
     property methods.
     """
     index = Index(self.db, 'ddoc-id', 'index-name', foo={'bar': 'baz'})
     self.assertIsInstance(index, Index)
     self.assertEqual(index.design_document_id, 'ddoc-id')
     self.assertEqual(index.name, 'index-name')
     self.assertEqual(index.type, 'json')
     self.assertEqual(index.definition, {'foo': {'bar': 'baz'}})
Example #29
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())