Ensure all backend methods respect deleted domains

Change-Id: Iabaf990309e7f9950e61a763176d3d4ada90a8c4
This commit is contained in:
Kiall Mac Innes 2013-06-29 14:59:18 +01:00
parent 08ee80c547
commit f2513a09b8
2 changed files with 20 additions and 3 deletions

View File

@ -270,6 +270,7 @@ class SQLAlchemyStorage(base.Storage):
def get_tenants(self, context):
query = self.session.query(models.Domain.tenant_id,
func.count(models.Domain.id))
query = self._apply_deleted_criteria(context, models.Domain, query)
query = query.group_by(models.Domain.tenant_id)
return [{'id': t[0], 'domain_count': t[1]} for t in query.all()]
@ -277,6 +278,7 @@ class SQLAlchemyStorage(base.Storage):
def get_tenant(self, context, tenant_id):
query = self.session.query(models.Domain.name)
query = query.filter(models.Domain.tenant_id == tenant_id)
query = self._apply_deleted_criteria(context, models.Domain, query)
result = query.all()
@ -289,7 +291,10 @@ class SQLAlchemyStorage(base.Storage):
def count_tenants(self, context):
# tenants are the owner of domains, count the number of unique tenants
# select count(distinct tenant_id) from domains
return self.session.query(distinct(models.Domain.tenant_id)).count()
query = self.session.query(distinct(models.Domain.tenant_id))
query = self._apply_deleted_criteria(context, models.Domain, query)
return query.count()
# Domain Methods
def _find_domains(self, context, criterion, one=False):

View File

@ -464,14 +464,18 @@ class StorageTestCase(TestCase):
def test_get_tenants(self):
# create 3 domains in 2 tenants
self.create_domain(fixture=0, values={'tenant_id': 'One'})
self.create_domain(fixture=1, values={'tenant_id': 'One'})
_, domain = self.create_domain(fixture=1, values={'tenant_id': 'One'})
self.create_domain(fixture=2, values={'tenant_id': 'Two'})
# Delete one of the domains.
self.storage.delete_domain(self.admin_context, domain['id'])
# Ensure we get accurate results
result = self.storage.get_tenants(self.admin_context)
expected = [{
'id': 'One',
'domain_count': 2
'domain_count': 1
}, {
'id': 'Two',
'domain_count': 1
@ -483,6 +487,10 @@ class StorageTestCase(TestCase):
# create 2 domains in a tenant
_, domain_1 = self.create_domain(fixture=0, values={'tenant_id': 1})
_, domain_2 = self.create_domain(fixture=1, values={'tenant_id': 1})
_, domain_3 = self.create_domain(fixture=2, values={'tenant_id': 1})
# Delete one of the domains.
self.storage.delete_domain(self.admin_context, domain_3['id'])
result = self.storage.get_tenant(self.admin_context, 1)
@ -499,6 +507,10 @@ class StorageTestCase(TestCase):
# create 2 domains with 2 tenants
self.create_domain(fixture=0, values={'tenant_id': 1})
self.create_domain(fixture=1, values={'tenant_id': 2})
_, domain = self.create_domain(fixture=2, values={'tenant_id': 2})
# Delete one of the domains.
self.storage.delete_domain(self.admin_context, domain['id'])
tenants = self.storage.count_tenants(self.admin_context)
self.assertEqual(tenants, 2)