From d0581243aafda4cfef2ef8be1a76df0c0b3665ca Mon Sep 17 00:00:00 2001 From: James Valleroy Date: Sat, 18 Aug 2018 23:30:00 -0400 Subject: [PATCH] backups: Fix iteration over loaded modules Add tests for affected functions. Signed-off-by: James Valleroy Reviewed-by: Joseph Nuthalapati --- plinth/modules/backups/backups.py | 4 +- plinth/modules/backups/tests/__init__.py | 0 plinth/modules/backups/tests/test_backups.py | 46 ++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 plinth/modules/backups/tests/__init__.py create mode 100644 plinth/modules/backups/tests/test_backups.py diff --git a/plinth/modules/backups/backups.py b/plinth/modules/backups/backups.py index bd7812135..b14fdc4d6 100644 --- a/plinth/modules/backups/backups.py +++ b/plinth/modules/backups/backups.py @@ -180,7 +180,7 @@ def restore_apps(restore_handler, app_names=None, create_subvolume=True): def _list_of_all_apps_for_backup(): """Return a list of all applications that can be backed up.""" apps = [] - for module_name, module in module_loader.loaded_modules.values(): + for module_name, module in module_loader.loaded_modules.items(): # Not installed if module.setup_helper.get_state() == 'needs-setup': continue @@ -197,7 +197,7 @@ def _list_of_all_apps_for_backup(): def _get_apps_in_order(app_names): """Return a list of app modules in order of dependency.""" apps = [] - for module_name, module in module_loader.loaded_modules.values(): + for module_name, module in module_loader.loaded_modules.items(): if module_name in app_names: apps.append((module_name, module)) diff --git a/plinth/modules/backups/tests/__init__.py b/plinth/modules/backups/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/plinth/modules/backups/tests/test_backups.py b/plinth/modules/backups/tests/test_backups.py new file mode 100644 index 000000000..396e6c4ca --- /dev/null +++ b/plinth/modules/backups/tests/test_backups.py @@ -0,0 +1,46 @@ +# +# This file is part of FreedomBox. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +""" +Tests for backups module. +""" + +import unittest + +from plinth.module_loader import load_modules +from ..backups import _list_of_all_apps_for_backup, _get_apps_in_order + + +class TestBackups(unittest.TestCase): + """Test cases for backups module.""" + + def test__list_of_all_apps_for_backups(self): + """Test that apps supporting backup are included in returned list.""" + load_modules() + apps = _list_of_all_apps_for_backup() + assert isinstance(apps, list) + # apps may be empty, if no apps supporting backup are installed. + + def test__get_apps_in_order(self): + """Test that apps are listed in correct dependency order.""" + load_modules() + app_names = ['config', 'names'] + apps = _get_apps_in_order(app_names) + ordered_app_names = [x[0] for x in apps] + + names_index = ordered_app_names.index('names') + config_index = ordered_app_names.index('config') + assert names_index < config_index