mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-08-26 12:46:08 +00:00
tests: Minor styling fixes and updates
- Add module level comments. - Add comments to reflect docstring styling. - Since most tests can no longer be run directly, remove __main__ invocation uniformly from all the tests. - Remove unnecessary test settings and let them take default values. - Add license header to test settings module. - Fix a minor issue actions test tear down. - Improve key/value store tests.
This commit is contained in:
parent
c16bff7fec
commit
cf1bb65a0c
@ -25,8 +25,7 @@ from plinth import cfg
|
|||||||
|
|
||||||
|
|
||||||
def common(request):
|
def common(request):
|
||||||
"""
|
"""Add additional context values to RequestContext for use in templates.
|
||||||
Add additional context values to RequestContext for use in templates.
|
|
||||||
|
|
||||||
Any resources referenced in the return value are expected to have been
|
Any resources referenced in the return value are expected to have been
|
||||||
initialized or configured externally beforehand.
|
initialized or configured externally beforehand.
|
||||||
|
|||||||
@ -73,7 +73,7 @@ class CoverageCommand(setuptools.Command):
|
|||||||
# Erase any existing HTML report files
|
# Erase any existing HTML report files
|
||||||
try:
|
try:
|
||||||
shutil.rmtree(COVERAGE_REPORT_DIR, True)
|
shutil.rmtree(COVERAGE_REPORT_DIR, True)
|
||||||
except:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Run the coverage analysis
|
# Run the coverage analysis
|
||||||
|
|||||||
@ -1,7 +1,28 @@
|
|||||||
# Django settings for test modules.
|
#
|
||||||
|
# This file is part of Plinth.
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
|
||||||
|
"""
|
||||||
|
Django settings for test modules.
|
||||||
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
TEST_DATA_DIR = os.path.dirname(os.path.abspath(__file__))
|
TEST_DATA_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
'default': {
|
'default': {
|
||||||
'ENGINE': 'django.db.backends.sqlite3',
|
'ENGINE': 'django.db.backends.sqlite3',
|
||||||
@ -9,8 +30,6 @@ DATABASES = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DEFAULT_INDEX_TABLESPACE = ''
|
|
||||||
|
|
||||||
INSTALLED_APPS = (
|
INSTALLED_APPS = (
|
||||||
'plinth',
|
'plinth',
|
||||||
)
|
)
|
||||||
|
|||||||
@ -16,7 +16,9 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
"""Module for Django pre-test configuration and setup."""
|
"""
|
||||||
|
Module for Django pre-test configuration and setup.
|
||||||
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
@ -28,7 +30,7 @@ from django.test.utils import get_runner
|
|||||||
|
|
||||||
def run_tests(pattern=None, return_to_caller=False):
|
def run_tests(pattern=None, return_to_caller=False):
|
||||||
"""Set up the Django test environment and run the specified tests."""
|
"""Set up the Django test environment and run the specified tests."""
|
||||||
os.environ['DJANGO_SETTINGS_MODULE'] =\
|
os.environ['DJANGO_SETTINGS_MODULE'] = \
|
||||||
'plinth.tests.data.django_test_settings'
|
'plinth.tests.data.django_test_settings'
|
||||||
django.setup()
|
django.setup()
|
||||||
TestRunner = get_runner(settings)
|
TestRunner = get_runner(settings)
|
||||||
@ -36,6 +38,7 @@ def run_tests(pattern=None, return_to_caller=False):
|
|||||||
|
|
||||||
if pattern is None:
|
if pattern is None:
|
||||||
pattern = 'plinth.tests'
|
pattern = 'plinth.tests'
|
||||||
|
|
||||||
failures = test_runner.run_tests([pattern])
|
failures = test_runner.run_tests([pattern])
|
||||||
if failures > 0 or not return_to_caller:
|
if failures > 0 or not return_to_caller:
|
||||||
sys.exit(bool(failures))
|
sys.exit(bool(failures))
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
# -*- mode: python; mode: auto-fill; fill-column: 80 -*-
|
|
||||||
#
|
#
|
||||||
# This file is part of Plinth.
|
# This file is part of Plinth.
|
||||||
#
|
#
|
||||||
@ -17,6 +15,10 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
"""
|
||||||
|
Test module for actions utilities that modify configuration.
|
||||||
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import unittest
|
import unittest
|
||||||
@ -45,8 +47,8 @@ class TestPrivileged(unittest.TestCase):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def tearDownClass(cls):
|
def tearDownClass(cls):
|
||||||
os.remove('actions/echo')
|
os.remove(os.path.join(cfg.actions_dir, 'echo'))
|
||||||
os.remove('actions/id')
|
os.remove(os.path.join(cfg.actions_dir, 'id'))
|
||||||
|
|
||||||
def notest_run_as_root(self):
|
def notest_run_as_root(self):
|
||||||
"""1. Privileged actions run as root. """
|
"""1. Privileged actions run as root. """
|
||||||
@ -141,8 +143,3 @@ class TestPrivileged(unittest.TestCase):
|
|||||||
output = run('echo', tuple(options.split()))
|
output = run('echo', tuple(options.split()))
|
||||||
output = output.rstrip('\n')
|
output = output.rstrip('\n')
|
||||||
self.assertEqual(options, output)
|
self.assertEqual(options, output)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
#
|
#
|
||||||
# This file is part of Plinth.
|
# This file is part of Plinth.
|
||||||
#
|
#
|
||||||
@ -16,6 +15,10 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
"""
|
||||||
|
Test module for configuration module.
|
||||||
|
"""
|
||||||
|
|
||||||
import configparser
|
import configparser
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
@ -244,7 +247,3 @@ class CfgTestCase(unittest.TestCase):
|
|||||||
if os.path.isfile(TEST_CONFIG_FILE):
|
if os.path.isfile(TEST_CONFIG_FILE):
|
||||||
os.remove(TEST_CONFIG_FILE)
|
os.remove(TEST_CONFIG_FILE)
|
||||||
shutil.move(SAVED_CONFIG_FILE, TEST_CONFIG_FILE)
|
shutil.move(SAVED_CONFIG_FILE, TEST_CONFIG_FILE)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
unittest.main()
|
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
#
|
#
|
||||||
# This file is part of Plinth.
|
# This file is part of Plinth.
|
||||||
#
|
#
|
||||||
@ -16,6 +15,10 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
"""
|
||||||
|
Test module for Plinth's custom context processors.
|
||||||
|
"""
|
||||||
|
|
||||||
from django.http import HttpRequest
|
from django.http import HttpRequest
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
#
|
#
|
||||||
# This file is part of Plinth.
|
# This file is part of Plinth.
|
||||||
#
|
#
|
||||||
@ -16,15 +15,19 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
"""
|
||||||
|
Test module for key/value store.
|
||||||
|
"""
|
||||||
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from plinth import kvstore
|
from plinth import kvstore
|
||||||
|
|
||||||
|
|
||||||
class KvstoreTestCase(TestCase):
|
class KVStoreTestCase(TestCase):
|
||||||
"""Verify the behavior of the kvstore module."""
|
"""Verify the behavior of the kvstore module."""
|
||||||
|
|
||||||
def test_get(self):
|
def test_get_set(self):
|
||||||
"""Verify that a set value can be retrieved."""
|
"""Verify that a set value can be retrieved."""
|
||||||
key = 'name'
|
key = 'name'
|
||||||
expected_value = 'Guido'
|
expected_value = 'Guido'
|
||||||
@ -32,6 +35,15 @@ class KvstoreTestCase(TestCase):
|
|||||||
actual_value = kvstore.get(key)
|
actual_value = kvstore.get(key)
|
||||||
self.assertEqual(expected_value, actual_value)
|
self.assertEqual(expected_value, actual_value)
|
||||||
|
|
||||||
|
def test_get_set_complex_structures(self):
|
||||||
|
"""Verify that complex structures can be stored and retrieved."""
|
||||||
|
key = 'compex_structure'
|
||||||
|
expected_value = {'k1': 1, 'k2': [2, 3], 'k3': 4.5, 'k4': 'Hello',
|
||||||
|
'k5': {'a': 'b'}}
|
||||||
|
kvstore.set(key, expected_value)
|
||||||
|
actual_value = kvstore.get(key)
|
||||||
|
self.assertEqual(expected_value, actual_value)
|
||||||
|
|
||||||
def test_get_default(self):
|
def test_get_default(self):
|
||||||
"""Verify that either a set value or its default can be retrieved."""
|
"""Verify that either a set value or its default can be retrieved."""
|
||||||
expected = 'default'
|
expected = 'default'
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
#
|
#
|
||||||
# This file is part of Plinth.
|
# This file is part of Plinth.
|
||||||
#
|
#
|
||||||
@ -16,6 +15,10 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
"""
|
||||||
|
Test module for menus.
|
||||||
|
"""
|
||||||
|
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
from django.http import HttpRequest
|
from django.http import HttpRequest
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
@ -38,9 +41,11 @@ def build_menu(size=5):
|
|||||||
'Icon' + str(index),
|
'Icon' + str(index),
|
||||||
URL_TEMPLATE.format(index, index, index),
|
URL_TEMPLATE.format(index, index, index),
|
||||||
random.randint(0, 1000)])
|
random.randint(0, 1000)])
|
||||||
|
|
||||||
menu = Menu()
|
menu = Menu()
|
||||||
for data in item_data:
|
for data in item_data:
|
||||||
menu.add_item(data[0], data[1], data[2], data[3])
|
menu.add_item(data[0], data[1], data[2], data[3])
|
||||||
|
|
||||||
return menu
|
return menu
|
||||||
|
|
||||||
|
|
||||||
@ -133,7 +138,9 @@ class MenuTestCase(TestCase):
|
|||||||
actual_item = menu.add_urlname(expected_label, expected_icon,
|
actual_item = menu.add_urlname(expected_label, expected_icon,
|
||||||
expected_url, expected_order)
|
expected_url, expected_order)
|
||||||
|
|
||||||
|
self.assertEqual(1, len(menu.items))
|
||||||
self.assertIsNotNone(actual_item)
|
self.assertIsNotNone(actual_item)
|
||||||
|
self.assertEqual(actual_item, menu.items[0])
|
||||||
self.assertEqual(expected_label, actual_item.label)
|
self.assertEqual(expected_label, actual_item.label)
|
||||||
self.assertEqual(expected_icon, actual_item.icon)
|
self.assertEqual(expected_icon, actual_item.icon)
|
||||||
self.assertEqual(reversed_url, actual_item.url)
|
self.assertEqual(reversed_url, actual_item.url)
|
||||||
@ -150,7 +157,9 @@ class MenuTestCase(TestCase):
|
|||||||
actual_item = menu.add_item(expected_label, expected_icon,
|
actual_item = menu.add_item(expected_label, expected_icon,
|
||||||
expected_url, expected_order)
|
expected_url, expected_order)
|
||||||
|
|
||||||
|
self.assertEqual(1, len(menu.items))
|
||||||
self.assertIsNotNone(actual_item)
|
self.assertIsNotNone(actual_item)
|
||||||
|
self.assertEqual(actual_item, menu.items[0])
|
||||||
self.assertEqual(expected_label, actual_item.label)
|
self.assertEqual(expected_label, actual_item.label)
|
||||||
self.assertEqual(expected_icon, actual_item.icon)
|
self.assertEqual(expected_icon, actual_item.icon)
|
||||||
self.assertEqual(expected_url, actual_item.url)
|
self.assertEqual(expected_url, actual_item.url)
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
#
|
#
|
||||||
# This file is part of Plinth.
|
# This file is part of Plinth.
|
||||||
#
|
#
|
||||||
@ -16,6 +15,10 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
"""
|
||||||
|
Test module for network configuration utilities.
|
||||||
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
@ -116,7 +119,3 @@ class TestNetwork(unittest.TestCase):
|
|||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
secrets['802-11-wireless-security']['psk'],
|
secrets['802-11-wireless-security']['psk'],
|
||||||
'secretpassword')
|
'secretpassword')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
#
|
#
|
||||||
# This file is part of Plinth.
|
# This file is part of Plinth.
|
||||||
#
|
#
|
||||||
@ -16,6 +15,10 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
"""
|
||||||
|
Test modules for Pagekite functions.
|
||||||
|
"""
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -15,6 +15,10 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
"""
|
||||||
|
Test module for custom Django template tags.
|
||||||
|
"""
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from plinth.templatetags.plinth_extras import mark_active_menuitem
|
from plinth.templatetags.plinth_extras import mark_active_menuitem
|
||||||
@ -30,7 +34,8 @@ class TestShowSubSubMenu(unittest.TestCase):
|
|||||||
else:
|
else:
|
||||||
self.assertFalse(item['active'])
|
self.assertFalse(item['active'])
|
||||||
|
|
||||||
def _verify_active_menuitems(self, menu):
|
@staticmethod
|
||||||
|
def _verify_active_menuitems(menu):
|
||||||
"""Verify that one and only one menuitem is marked as active"""
|
"""Verify that one and only one menuitem is marked as active"""
|
||||||
return sum([item['active'] for item in menu]) == 1
|
return sum([item['active'] for item in menu]) == 1
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user