From ec57c84fb23974a7e07cf1c8e4101543a636b4ac Mon Sep 17 00:00:00 2001 From: Jay Dobies Date: Mon, 17 Feb 2014 14:59:50 -0500 Subject: [PATCH] Eager load role data with count information Change-Id: I2a6433f8d3ec2d0d74e22ac2db81801e69aca898 --- tuskar/db/sqlalchemy/api.py | 1 + tuskar/db/sqlalchemy/models.py | 4 +++ tuskar/tests/db/test_api.py | 45 ++++++++++++++++++++++++++++------ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/tuskar/db/sqlalchemy/api.py b/tuskar/db/sqlalchemy/api.py index cabd3eb8..ce2e1215 100644 --- a/tuskar/db/sqlalchemy/api.py +++ b/tuskar/db/sqlalchemy/api.py @@ -208,6 +208,7 @@ class Connection(api.Connection): query = session.query(models.Overcloud).\ options(subqueryload(models.Overcloud.attributes)).\ options(subqueryload(models.Overcloud.counts)).\ + options(subqueryload('counts.overcloud_role')).\ filter_by(id=overcloud_id) result = query.one() diff --git a/tuskar/db/sqlalchemy/models.py b/tuskar/db/sqlalchemy/models.py index b4f46147..bdf44c4d 100644 --- a/tuskar/db/sqlalchemy/models.py +++ b/tuskar/db/sqlalchemy/models.py @@ -128,6 +128,10 @@ class OvercloudRoleCount(Base): # Number of nodes of this configuration that should be deployed num_nodes = Column(Integer, nullable=False) + # Reference to the full role (this is not the foreign key relationship, + # that's overcloud_role_id above, this is to eager load the role data). + overcloud_role = relationship(OvercloudRole.__name__) + def __eq__(self, other): return (self.overcloud_role_id == other.overcloud_role_id and self.overcloud_id == other.overcloud_id) diff --git a/tuskar/tests/db/test_api.py b/tuskar/tests/db/test_api.py index 77422ea7..7a9a5a14 100644 --- a/tuskar/tests/db/test_api.py +++ b/tuskar/tests/db/test_api.py @@ -158,6 +158,14 @@ class OvercloudTests(db_base.DbTestCase): self.connection = dbapi.Connection() + self.role_1 = models.OvercloudRole( + name='name-1', + description='desc-1', + image_name='image-1', + flavor_id='tuvwxyz', + ) + self.saved_role = self.connection.create_overcloud_role(self.role_1) + self.attributes_1 = models.OvercloudAttribute( key='key-1', value='value-1', @@ -169,7 +177,7 @@ class OvercloudTests(db_base.DbTestCase): ) self.count_1 = models.OvercloudRoleCount( - overcloud_role_id='cat-1', + overcloud_role_id=self.saved_role.id, num_nodes=4, ) @@ -188,6 +196,7 @@ class OvercloudTests(db_base.DbTestCase): def test_create_overcloud(self): # Test + self.connection.create_overcloud_role(self.role_1) saved = self.connection.create_overcloud(self.overcloud_1) # Verify @@ -209,6 +218,16 @@ class OvercloudTests(db_base.DbTestCase): for index, count in enumerate(self.overcloud_1.counts): self.assertEqual(saved.counts[index].overcloud_role_id, count.overcloud_role_id) + self.assertTrue(saved.counts[index] is not None) + self.assertTrue(isinstance(saved.counts[index].overcloud_role, + models.OvercloudRole)) + self.assertEqual(saved.counts[index].overcloud_role.name, + self.role_1.name) + self.assertEqual(saved.counts[index].overcloud_role.description, + self.role_1.description) + self.assertEqual(saved.counts[index].overcloud_role.image_name, + self.role_1.image_name) + self.assertEqual(saved.counts[index].num_nodes, count.num_nodes) @@ -304,15 +323,27 @@ class OvercloudTests(db_base.DbTestCase): self.assertEqual(found.attributes[2].value, 'value-4') def test_update_overcloud_counts(self): + # Setup + # Roles + role_2 = self.connection.create_overcloud_role(models.OvercloudRole( + name='name-2', + )) + role_3 = self.connection.create_overcloud_role(models.OvercloudRole( + name='name-3', + )) + role_4 = self.connection.create_overcloud_role(models.OvercloudRole( + name='name-4', + )) + # Add extra counts for enough data self.overcloud_1.counts.append(models.OvercloudRoleCount( - overcloud_role_id='cat-2', + overcloud_role_id=role_2.id, num_nodes=2, )) self.overcloud_1.counts.append(models.OvercloudRoleCount( - overcloud_role_id='cat-3', + overcloud_role_id=role_3.id, num_nodes=3, )) saved = self.connection.create_overcloud(self.overcloud_1) @@ -329,7 +360,7 @@ class OvercloudTests(db_base.DbTestCase): # - Add a fourth saved.counts.append(models.OvercloudRoleCount( - overcloud_role_id='cat-4', + overcloud_role_id=role_4.id, num_nodes=4, )) @@ -339,11 +370,11 @@ class OvercloudTests(db_base.DbTestCase): found = self.connection.get_overcloud_by_id(saved.id) self.assertEqual(3, len(found.counts)) - self.assertEqual(found.counts[0].overcloud_role_id, 'cat-1') + self.assertEqual(found.counts[0].overcloud_role_id, self.saved_role.id) self.assertEqual(found.counts[0].num_nodes, 4) - self.assertEqual(found.counts[1].overcloud_role_id, 'cat-2') + self.assertEqual(found.counts[1].overcloud_role_id, role_2.id) self.assertEqual(found.counts[1].num_nodes, 100) - self.assertEqual(found.counts[2].overcloud_role_id, 'cat-4') + self.assertEqual(found.counts[2].overcloud_role_id, role_4.id) self.assertEqual(found.counts[2].num_nodes, 4) def test_update_nonexistent(self):