Fix db api with hash rating

Fix the logic of specify either service_uuid, field_uuid
or group_uuid when list_thresholds and list_mappings.
And add the corresponding test cases.

Change-Id: I4fe27a07e369728396d440b6b2f3462ee74d5f4d
Closes-bug: #1615941
This commit is contained in:
zhangguoqing 2016-08-25 03:29:45 +00:00
parent 59ce21c3c1
commit bf39479822
2 changed files with 31 additions and 2 deletions

View File

@ -181,7 +181,7 @@ class HashMap(api.HashMap):
q = q.join(
models.HashMapMapping.field)
q = q.filter(models.HashMapField.field_id == field_uuid)
elif not service_uuid and not field_uuid:
elif not service_uuid and not field_uuid and not group_uuid:
raise api.ClientHashMapError(
'You must specify either service_uuid,'
' field_uuid or group_uuid.')
@ -216,7 +216,7 @@ class HashMap(api.HashMap):
q = q.join(
models.HashMapThreshold.field)
q = q.filter(models.HashMapField.field_id == field_uuid)
elif not service_uuid and not field_uuid:
elif not service_uuid and not field_uuid and not group_uuid:
raise api.ClientHashMapError(
'You must specify either service_uuid,'
' field_uuid or group_uuid.')

View File

@ -151,6 +151,22 @@ class HashMapRatingTest(tests.TestCase):
new_mapping_db = self._db_api.get_mapping(mapping_db.mapping_id)
self.assertIsNone(new_mapping_db.group_id)
def test_list_mappings_from_only_group(self):
service_db = self._db_api.create_service('compute')
group_db = self._db_api.create_group('test_group')
mapping_tiny = self._db_api.create_mapping(
cost='1.337',
map_type='flat',
service_id=service_db.service_id,
group_id=group_db.group_id)
self._db_api.create_mapping(
cost='42',
map_type='flat',
service_id=service_db.service_id)
mappings = self._db_api.list_mappings(group_uuid=group_db.group_id)
self.assertEqual([mapping_tiny.mapping_id],
mappings)
def test_list_mappings_from_group(self):
service_db = self._db_api.create_service('compute')
field_db = self._db_api.create_field(service_db.service_id,
@ -493,6 +509,19 @@ class HashMapRatingTest(tests.TestCase):
self.assertEqual(decimal.Decimal('0.1337'), threshold.cost)
self.assertEqual(field_db.id, threshold.field_id)
def test_list_thresholds_from_only_group(self):
service_db = self._db_api.create_service('compute')
group_db = self._db_api.create_group('test_group')
threshold_db = self._db_api.create_threshold(
level=10,
cost='1.337',
map_type='flat',
service_id=service_db.service_id,
group_id=group_db.group_id)
thresholds = self._db_api.list_thresholds(
group_uuid=group_db.group_id)
self.assertEqual([threshold_db.threshold_id], thresholds)
def test_list_thresholds_from_services(self):
service_db = self._db_api.create_service('compute')
threshold_db = self._db_api.create_threshold(