Add unit test for TypeManager and TypeManagerV2

Change-Id: I48923a340860b153ccea981e9974366b95dadf9c
This commit is contained in:
gengchc2 2018-12-25 00:41:31 -08:00
parent 1977c7e9d3
commit 0e3a3d366b
2 changed files with 63 additions and 0 deletions

View File

@ -79,6 +79,14 @@ class TypeManager(unittest.TestCase):
user_id=common.fake_job_0_user_id,
doc_id=common.fake_job_0_job_id)
def test_raise_get_search_query(self):
my_search_error = ['a', 'b']
self.assertRaises(exceptions.StorageEngineError,
self.type_manager.get_search_query,
user_id=common.fake_job_0_user_id,
doc_id=common.fake_job_0_job_id,
search=my_search_error)
def test_get_raise_StorageEngineError_when_db_raises(self):
self.mock_es.get.side_effect = Exception('regular test failure')
self.assertRaises(exceptions.StorageEngineError, self.type_manager.get,
@ -138,6 +146,15 @@ class TypeManager(unittest.TestCase):
self.type_manager.search, user_id='my_user_id',
doc_id='mydocid')
@patch('freezer_api.storage.elastic.elasticsearch.Elasticsearch')
def test_search_raise_StorageEngineError_when_ConnectionError(self,
mock_es):
self.mock_es.search.side_effect = elasticsearch.ConnectionError(
'regular test failure')
self.assertRaises(exceptions.StorageEngineError,
self.type_manager.search,
user_id='my_user_id', doc_id='mydocid')
def test_insert_ok(self):
self.mock_es.index.return_value = {'created': True, '_version': 15}
test_doc = {'test_key_412': 'test_value_412', '_version': 5}
@ -201,6 +218,17 @@ class TypeManager(unittest.TestCase):
self.type_manager.delete, user_id='my_user_id',
doc_id=doc_id)
def test_delete_raises_StorageEngineError_on_es_delete(
self):
results = {'hits': {'hits': [{'_id': mock.Mock}]}}
self.mock_es.search.return_value = results
doc_id = 'mydocid345'
self.mock_es.delete.side_effect = Exception(
'regular test failure')
self.assertRaises(exceptions.StorageEngineError,
self.type_manager.delete,
user_id='my_user_id', doc_id=doc_id)
def test_delete_return_none_when_nothing_is_deleted(self):
doc_id = 'mydocid345'
ret_data = {"hits": {"hits": []}}

View File

@ -98,6 +98,21 @@ class TypeManagerV2(unittest.TestCase):
user_id=common.fake_job_0_user_id,
doc_id=common.fake_job_0_job_id)
def test_raise_get_search_query(self):
my_search_error = ['a', 'b']
self.assertRaises(exceptions.StorageEngineError,
self.type_manager.get_search_query,
project_id='tecs',
user_id=common.fake_job_0_user_id,
doc_id=common.fake_job_0_job_id,
search=my_search_error)
def test_get_raises_AccessForbidden_when_project_id_not_match(self):
self.mock_es.get.return_value = common.fake_job_0_elasticsearch_found
self.assertRaises(exceptions.AccessForbidden, self.type_manager.get,
project_id='tecs1',
doc_id=common.fake_job_0_job_id)
def test_get_raises_AccessForbidden_when_user_id_not_match(self):
self.mock_es.get.return_value = common.fake_job_0_elasticsearch_found
self.assertRaises(exceptions.AccessForbidden, self.type_manager.get,
@ -159,6 +174,15 @@ class TypeManagerV2(unittest.TestCase):
self.type_manager.search, project_id='tecs',
user_id='my_user_id', doc_id='mydocid')
@patch('freezer_api.storage.elasticv2.elasticsearch.Elasticsearch')
def test_search_raise_StorageEngineError_when_ConnectionError(self,
mock_es):
self.mock_es.search.side_effect = elasticsearch.ConnectionError(
'regular test failure')
self.assertRaises(exceptions.StorageEngineError,
self.type_manager.search, project_id='tecs',
user_id='my_user_id', doc_id='mydocid')
def test_insert_ok(self):
self.mock_es.index.return_value = {'created': True, '_version': 15}
test_doc = {'test_key_412': 'test_value_412', '_version': 5}
@ -222,6 +246,17 @@ class TypeManagerV2(unittest.TestCase):
self.type_manager.delete, project_id='tecs',
user_id='my_user_id', doc_id=doc_id)
def test_delete_raises_StorageEngineError_on_es_delete(
self):
results = {'hits': {'hits': [{'_id': mock.Mock}]}}
self.mock_es.search.return_value = results
doc_id = 'mydocid345'
self.mock_es.delete.side_effect = Exception(
'regular test failure')
self.assertRaises(exceptions.StorageEngineError,
self.type_manager.delete, project_id='tecs',
user_id='my_user_id', doc_id=doc_id)
def test_delete_return_none_when_nothing_is_deleted(self):
doc_id = 'mydocid345'
ret_data = {"hits": {"hits": []}}