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:
Sunil Mohan Adapa 2015-05-24 12:16:51 +05:30
parent c16bff7fec
commit cf1bb65a0c
12 changed files with 82 additions and 34 deletions

View File

@ -25,8 +25,7 @@ from plinth import cfg
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
initialized or configured externally beforehand.

View File

@ -73,7 +73,7 @@ class CoverageCommand(setuptools.Command):
# Erase any existing HTML report files
try:
shutil.rmtree(COVERAGE_REPORT_DIR, True)
except:
except Exception:
pass
# Run the coverage analysis

View File

@ -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
TEST_DATA_DIR = os.path.dirname(os.path.abspath(__file__))
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
@ -9,8 +30,6 @@ DATABASES = {
}
}
DEFAULT_INDEX_TABLESPACE = ''
INSTALLED_APPS = (
'plinth',
)

View File

@ -16,7 +16,9 @@
# 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 sys
@ -28,7 +30,7 @@ from django.test.utils import get_runner
def run_tests(pattern=None, return_to_caller=False):
"""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'
django.setup()
TestRunner = get_runner(settings)
@ -36,6 +38,7 @@ def run_tests(pattern=None, return_to_caller=False):
if pattern is None:
pattern = 'plinth.tests'
failures = test_runner.run_tests([pattern])
if failures > 0 or not return_to_caller:
sys.exit(bool(failures))

View File

@ -1,5 +1,3 @@
#!/usr/bin/python3
# -*- mode: python; mode: auto-fill; fill-column: 80 -*-
#
# This file is part of Plinth.
#
@ -17,6 +15,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Test module for actions utilities that modify configuration.
"""
import os
import shutil
import unittest
@ -45,8 +47,8 @@ class TestPrivileged(unittest.TestCase):
@classmethod
def tearDownClass(cls):
os.remove('actions/echo')
os.remove('actions/id')
os.remove(os.path.join(cfg.actions_dir, 'echo'))
os.remove(os.path.join(cfg.actions_dir, 'id'))
def notest_run_as_root(self):
"""1. Privileged actions run as root. """
@ -141,8 +143,3 @@ class TestPrivileged(unittest.TestCase):
output = run('echo', tuple(options.split()))
output = output.rstrip('\n')
self.assertEqual(options, output)
if __name__ == "__main__":
unittest.main()

View File

@ -1,4 +1,3 @@
#!/usr/bin/python3
#
# This file is part of Plinth.
#
@ -16,6 +15,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Test module for configuration module.
"""
import configparser
import os
import shutil
@ -244,7 +247,3 @@ class CfgTestCase(unittest.TestCase):
if os.path.isfile(TEST_CONFIG_FILE):
os.remove(TEST_CONFIG_FILE)
shutil.move(SAVED_CONFIG_FILE, TEST_CONFIG_FILE)
if __name__ == '__main__':
unittest.main()

View File

@ -1,4 +1,3 @@
#!/usr/bin/python3
#
# This file is part of Plinth.
#
@ -16,6 +15,10 @@
# 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.test import TestCase

View File

@ -1,4 +1,3 @@
#!/usr/bin/python3
#
# This file is part of Plinth.
#
@ -16,15 +15,19 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Test module for key/value store.
"""
from django.test import TestCase
from plinth import kvstore
class KvstoreTestCase(TestCase):
class KVStoreTestCase(TestCase):
"""Verify the behavior of the kvstore module."""
def test_get(self):
def test_get_set(self):
"""Verify that a set value can be retrieved."""
key = 'name'
expected_value = 'Guido'
@ -32,6 +35,15 @@ class KvstoreTestCase(TestCase):
actual_value = kvstore.get(key)
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):
"""Verify that either a set value or its default can be retrieved."""
expected = 'default'

View File

@ -1,4 +1,3 @@
#!/usr/bin/python3
#
# This file is part of Plinth.
#
@ -16,6 +15,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Test module for menus.
"""
from django.core.urlresolvers import reverse
from django.http import HttpRequest
from django.test import TestCase
@ -38,9 +41,11 @@ def build_menu(size=5):
'Icon' + str(index),
URL_TEMPLATE.format(index, index, index),
random.randint(0, 1000)])
menu = Menu()
for data in item_data:
menu.add_item(data[0], data[1], data[2], data[3])
return menu
@ -133,7 +138,9 @@ class MenuTestCase(TestCase):
actual_item = menu.add_urlname(expected_label, expected_icon,
expected_url, expected_order)
self.assertEqual(1, len(menu.items))
self.assertIsNotNone(actual_item)
self.assertEqual(actual_item, menu.items[0])
self.assertEqual(expected_label, actual_item.label)
self.assertEqual(expected_icon, actual_item.icon)
self.assertEqual(reversed_url, actual_item.url)
@ -150,7 +157,9 @@ class MenuTestCase(TestCase):
actual_item = menu.add_item(expected_label, expected_icon,
expected_url, expected_order)
self.assertEqual(1, len(menu.items))
self.assertIsNotNone(actual_item)
self.assertEqual(actual_item, menu.items[0])
self.assertEqual(expected_label, actual_item.label)
self.assertEqual(expected_icon, actual_item.icon)
self.assertEqual(expected_url, actual_item.url)

View File

@ -1,4 +1,3 @@
#!/usr/bin/python3
#
# This file is part of Plinth.
#
@ -16,6 +15,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Test module for network configuration utilities.
"""
import os
import unittest
@ -116,7 +119,3 @@ class TestNetwork(unittest.TestCase):
self.assertEqual(
secrets['802-11-wireless-security']['psk'],
'secretpassword')
if __name__ == "__main__":
unittest.main()

View File

@ -1,4 +1,3 @@
#!/usr/bin/python3
#
# This file is part of Plinth.
#
@ -16,6 +15,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Test modules for Pagekite functions.
"""
import unittest

View File

@ -15,6 +15,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Test module for custom Django template tags.
"""
import unittest
from plinth.templatetags.plinth_extras import mark_active_menuitem
@ -30,7 +34,8 @@ class TestShowSubSubMenu(unittest.TestCase):
else:
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"""
return sum([item['active'] for item in menu]) == 1