From 4ff8ca8f64a8fcc655cc97146a2f7c18811b31e3 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Fri, 14 Jun 2019 08:50:44 -0700 Subject: [PATCH] Add a test for the _joinedload_all helper This just uses mock to make sure _joinedload_all() is doing what we expect. Change-Id: Ifc352eb3accdda309ea941ca9f8389817090056a --- nova/tests/unit/db/test_db_api.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/nova/tests/unit/db/test_db_api.py b/nova/tests/unit/db/test_db_api.py index d747f05f3d74..0e39db19a4ef 100644 --- a/nova/tests/unit/db/test_db_api.py +++ b/nova/tests/unit/db/test_db_api.py @@ -191,6 +191,36 @@ class DbTestCase(test.TestCase): return meta, sys_meta +class HelperTestCase(test.TestCase): + @mock.patch.object(sqlalchemy_api, 'joinedload') + def test_joinedload_helper(self, mock_jl): + query = sqlalchemy_api._joinedload_all('foo.bar.baz') + + # We call sqlalchemy.orm.joinedload() on the first element + mock_jl.assert_called_once_with('foo') + + # Then first.joinedload(second) + column2 = mock_jl.return_value + column2.joinedload.assert_called_once_with('bar') + + # Then second.joinedload(third) + column3 = column2.joinedload.return_value + column3.joinedload.assert_called_once_with('baz') + + self.assertEqual(column3.joinedload.return_value, query) + + @mock.patch.object(sqlalchemy_api, 'joinedload') + def test_joinedload_helper_single(self, mock_jl): + query = sqlalchemy_api._joinedload_all('foo') + + # We call sqlalchemy.orm.joinedload() on the first element + mock_jl.assert_called_once_with('foo') + + # We should have gotten back just the result of the joinedload() + # call if there were no other elements + self.assertEqual(mock_jl.return_value, query) + + class DecoratorTestCase(test.TestCase): def _test_decorator_wraps_helper(self, decorator): def test_func():