Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_create_a_search_index_invalid_argument(self):
"""
Test that a TEXT index is not created when an invalid argument is given.
"""
index = TextIndex(self.db, 'ddoc001', 'index001', foo='bar')
with self.assertRaises(CloudantArgumentError) as cm:
index.create()
err = cm.exception
self.assertEqual(str(err), 'Invalid argument: foo')
def test_create_a_search_index_invalid_fields_value(self):
"""
Test that a TEXT index is not created when an invalid fields value is
given.
"""
index = TextIndex(self.db, 'ddoc001', 'index001', fields=5)
with self.assertRaises(CloudantArgumentError) as cm:
index.create()
err = cm.exception
self.assertEqual(
str(err),
'Argument fields is not an instance of expected type: '
'<{} \'list\'>'.format('type' if PY2 else 'class')
)
def test_constructor_with_args(self):
"""
Test instantiating a TextIndex by passing in arguments. As a side effect
this test also tests the design_document_id, name, type, and definition
property methods.
"""
index = TextIndex(self.db, 'ddoc-id', 'index-name', foo={'bar': 'baz'})
self.assertIsInstance(index, TextIndex)
self.assertEqual(index.design_document_id, 'ddoc-id')
self.assertEqual(index.name, 'index-name')
self.assertEqual(index.type, 'text')
self.assertEqual(index.definition, {'foo': {'bar': 'baz'}})
def test_constructor_with_only_a_db(self):
"""
Test instantiating an TextIndex with a database only. As a side effect
this test also tests the design_document_id, name, type, and definition
property methods.
"""
index = TextIndex(self.db)
self.assertIsInstance(index, TextIndex)
self.assertIsNone(index.design_document_id)
self.assertIsNone(index.name)
self.assertEqual(index.type, 'text')
self.assertEqual(index.definition, {})
def test_create_a_search_index_invalid_selector_value(self):
"""
Test that a TEXT index is not created when an invalid selector
value is given.
"""
index = TextIndex(self.db, 'ddoc001', 'index001', selector=5)
with self.assertRaises(CloudantArgumentError) as cm:
index.create()
err = cm.exception
self.assertEqual(
str(err),
'Argument selector is not an instance of expected type: '
'<{} \'dict\'>'.format('type' if PY2 else 'class')
)
def test_constructor_with_args(self):
"""
Test instantiating a TextIndex by passing in arguments. As a side effect
this test also tests the design_document_id, name, type, and definition
property methods.
"""
index = TextIndex(self.db, 'ddoc-id', 'index-name', foo={'bar': 'baz'})
self.assertIsInstance(index, TextIndex)
self.assertEqual(index.design_document_id, 'ddoc-id')
self.assertEqual(index.name, 'index-name')
self.assertEqual(index.type, 'text')
self.assertEqual(index.definition, {'foo': {'bar': 'baz'}})
def test_create_a_search_index_invalid_default_field_value(self):
"""
Test that a TEXT index is not created when an invalid default_field
value is given.
"""
index = TextIndex(self.db, 'ddoc001', 'index001', default_field=5)
with self.assertRaises(CloudantArgumentError) as cm:
index.create()
err = cm.exception
self.assertEqual(
str(err),
'Argument default_field is not an instance of expected type: '
'<{} \'dict\'>'.format('type' if PY2 else 'class')
)
def test_create_a_search_index_with_kwargs(self):
"""
Test that a TEXT index is created in the remote database.
"""
index = TextIndex(
self.db,
'ddoc001',
'index001',
fields=[{'name': 'name', 'type':'string'},
{'name': 'age', 'type':'number'}],
selector={},
default_field={'enabled': True, 'analyzer': 'german'})
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.assertEquals(ddoc['_id'], index.design_document_id)
self.assertTrue(ddoc['_rev'].startswith('1-'))
self.assertEquals(ddoc['language'], 'query')
self.assertEquals(ddoc['lists'], {})
def test_create_a_search_index_no_kwargs(self):
"""
Test that a TEXT index is created in the remote database.
"""
index = TextIndex(self.db, 'ddoc001', 'index001')
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.assertEquals(ddoc['_id'], index.design_document_id)
self.assertTrue(ddoc['_rev'].startswith('1-'))
self.assertEquals(ddoc['language'], 'query')
self.assertEquals(ddoc['lists'], {})
self.assertEquals(ddoc['shows'], {})
self.assertEquals(ddoc['views'], {})
index = ddoc['indexes']['index001']
self.assertEquals(index['analyzer']['default'], 'keyword')
self.assertEquals(index['analyzer']['fields']['$default'], 'standard')
self.assertEquals(index['analyzer']['name'], 'perfield')