mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-04-22 10:01:45 +00:00
Add to context processor and menu test cases
- Minor style updates to other tests
This commit is contained in:
parent
f184c23c31
commit
e69b500247
@ -1,5 +1,4 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
# -*- mode: python; mode: auto-fill; fill-column: 80 -*-
|
|
||||||
#
|
#
|
||||||
# This file is part of Plinth.
|
# This file is part of Plinth.
|
||||||
#
|
#
|
||||||
@ -15,6 +14,7 @@
|
|||||||
#
|
#
|
||||||
# You should have received a copy of the GNU Affero General Public License
|
# 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/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
|
||||||
import configparser
|
import configparser
|
||||||
import os
|
import os
|
||||||
@ -45,35 +45,33 @@ class CfgTestCase(unittest.TestCase):
|
|||||||
cls.config_file = cfg.DEFAULT_CONFIG_FILE
|
cls.config_file = cfg.DEFAULT_CONFIG_FILE
|
||||||
cls.directory = cfg.DEFAULT_ROOT
|
cls.directory = cfg.DEFAULT_ROOT
|
||||||
else:
|
else:
|
||||||
cls.directory = os.path.realpath(".")
|
cls.directory = os.path.realpath('.')
|
||||||
cls.config_file = os.path.join(cls.directory,
|
cls.config_file = os.path.join(cls.directory,
|
||||||
CONFIG_FILENAME)
|
CONFIG_FILENAME)
|
||||||
if not(os.path.isfile(cls.config_file)):
|
if not(os.path.isfile(cls.config_file)):
|
||||||
raise FileNotFoundError('File {} could not be found.',
|
raise FileNotFoundError('File {} could not be found.',
|
||||||
format(CONFIG_FILENAME))
|
format(CONFIG_FILENAME))
|
||||||
|
|
||||||
#Tests
|
# Tests
|
||||||
|
|
||||||
def test_read_main_menu(self):
|
def test_read_main_menu(self):
|
||||||
"""Verify that the cfg.main_menu container is initially empty."""
|
"""Verify that the cfg.main_menu container is initially empty."""
|
||||||
# menu should be empty before...
|
# Menu should be empty before...
|
||||||
self.assertTrue(len(cfg.main_menu.items) == 0)
|
self.assertEqual(len(cfg.main_menu.items), 0)
|
||||||
cfg.read()
|
cfg.read()
|
||||||
# ...and after reading the config file
|
# ...and after reading the config file
|
||||||
self.assertTrue(len(cfg.main_menu.items) == 0)
|
self.assertEqual(len(cfg.main_menu.items), 0)
|
||||||
|
|
||||||
def test_read_official_config_file(self):
|
def test_read_official_config_file(self):
|
||||||
"""Verify that the plinth.config file can be read correctly."""
|
"""Verify that the plinth.config file can be read correctly."""
|
||||||
|
# Read the plinth.config file directly
|
||||||
|
parser = self.read_config_file(self.config_file)
|
||||||
|
|
||||||
# read the plinth.config file directly
|
# Read the plinth.config file via the cfg module
|
||||||
parser = self.read_config_file(CfgTestCase.config_file)
|
|
||||||
|
|
||||||
# read the plinth.config file via the cfg module
|
|
||||||
cfg.read()
|
cfg.read()
|
||||||
|
|
||||||
# compare the two sets of configuration values
|
# Compare the two sets of configuration values.
|
||||||
# Note that the count of items within each section includes the number
|
# Note that the count of items within each section includes the number
|
||||||
# of default items (1, for 'root')
|
# of default items (1, for 'root').
|
||||||
self.assertEqual(3, len(parser.items('Name')))
|
self.assertEqual(3, len(parser.items('Name')))
|
||||||
self.assertEqual(parser.get('Name', 'product_name'), cfg.product_name)
|
self.assertEqual(parser.get('Name', 'product_name'), cfg.product_name)
|
||||||
self.assertEqual(parser.get('Name', 'box_name'), cfg.box_name)
|
self.assertEqual(parser.get('Name', 'box_name'), cfg.box_name)
|
||||||
@ -127,7 +125,7 @@ class CfgTestCase(unittest.TestCase):
|
|||||||
def read_config_file(self, file):
|
def read_config_file(self, file):
|
||||||
"""Read the configuration file independently from cfg.py."""
|
"""Read the configuration file independently from cfg.py."""
|
||||||
parser = configparser.ConfigParser(
|
parser = configparser.ConfigParser(
|
||||||
defaults={'root': CfgTestCase.directory})
|
defaults={'root': self.directory})
|
||||||
parser.read(file)
|
parser.read(file)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
@ -141,8 +139,8 @@ class CfgTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
def rename_official_config_file(self):
|
def rename_official_config_file(self):
|
||||||
"""Rename the official config file so that it can't be read."""
|
"""Rename the official config file so that it can't be read."""
|
||||||
shutil.move(CfgTestCase.config_file,
|
shutil.move(self.config_file,
|
||||||
os.path.join(CfgTestCase.directory, SAVED_CONFIG_FILE))
|
os.path.join(self.directory, SAVED_CONFIG_FILE))
|
||||||
|
|
||||||
def replace_official_config_file(self, test_file):
|
def replace_official_config_file(self, test_file):
|
||||||
"""Replace plinth.config with the specified test config file."""
|
"""Replace plinth.config with the specified test config file."""
|
||||||
@ -150,14 +148,14 @@ class CfgTestCase(unittest.TestCase):
|
|||||||
test_data_directory = os.path.join(os.path.dirname(
|
test_data_directory = os.path.join(os.path.dirname(
|
||||||
os.path.realpath(__file__)), 'data')
|
os.path.realpath(__file__)), 'data')
|
||||||
shutil.copy2(os.path.join(test_data_directory, test_file),
|
shutil.copy2(os.path.join(test_data_directory, test_file),
|
||||||
CfgTestCase.config_file)
|
self.config_file)
|
||||||
|
|
||||||
def restore_official_config_file(self):
|
def restore_official_config_file(self):
|
||||||
"""Restore the official plinth.config file."""
|
"""Restore the official plinth.config file."""
|
||||||
if os.path.isfile(CfgTestCase.config_file):
|
if os.path.isfile(self.config_file):
|
||||||
os.remove(CfgTestCase.config_file)
|
os.remove(self.config_file)
|
||||||
shutil.move(os.path.join(CfgTestCase.directory, SAVED_CONFIG_FILE),
|
shutil.move(os.path.join(self.directory, SAVED_CONFIG_FILE),
|
||||||
CfgTestCase.config_file)
|
self.config_file)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
# -*- mode: python; mode: auto-fill; fill-column: 80 -*-
|
|
||||||
#
|
#
|
||||||
# This file is part of Plinth.
|
# This file is part of Plinth.
|
||||||
#
|
#
|
||||||
@ -15,6 +14,7 @@
|
|||||||
#
|
#
|
||||||
# You should have received a copy of the GNU Affero General Public License
|
# 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/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
|
||||||
from django.http import HttpRequest
|
from django.http import HttpRequest
|
||||||
import unittest
|
import unittest
|
||||||
@ -44,6 +44,21 @@ class ContextProcessorsTestCase(unittest.TestCase):
|
|||||||
self.assertIsNotNone(urls)
|
self.assertIsNotNone(urls)
|
||||||
self.assertEqual(['/', '/aaa/', '/aaa/bbb/', '/aaa/bbb/ccc/'], urls)
|
self.assertEqual(['/', '/aaa/', '/aaa/bbb/', '/aaa/bbb/ccc/'], urls)
|
||||||
|
|
||||||
|
def test_common_border_conditions(self):
|
||||||
|
"""Verify that the 'common' functions works for border conditions."""
|
||||||
|
request = HttpRequest()
|
||||||
|
request.path = ''
|
||||||
|
response = cp.common(request)
|
||||||
|
self.assertEqual([], response['active_menu_urls'])
|
||||||
|
|
||||||
|
request.path = '/'
|
||||||
|
response = cp.common(request)
|
||||||
|
self.assertEqual(['/'], response['active_menu_urls'])
|
||||||
|
|
||||||
|
request.path = '/aaa/bbb'
|
||||||
|
response = cp.common(request)
|
||||||
|
self.assertEqual(['/', '/aaa/'], response['active_menu_urls'])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
# -*- mode: python; mode: auto-fill; fill-column: 80 -*-
|
|
||||||
#
|
#
|
||||||
# This file is part of Plinth.
|
# This file is part of Plinth.
|
||||||
#
|
#
|
||||||
@ -15,6 +14,7 @@
|
|||||||
#
|
#
|
||||||
# You should have received a copy of the GNU Affero General Public License
|
# 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/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
|
||||||
from django.http import HttpRequest
|
from django.http import HttpRequest
|
||||||
import random
|
import random
|
||||||
@ -80,8 +80,9 @@ class MenuTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
# Verify that the order of every item is equal to or greater
|
# Verify that the order of every item is equal to or greater
|
||||||
# than the order of the item preceding it
|
# than the order of the item preceding it
|
||||||
for i in range(1, 5):
|
for index in range(1, 5):
|
||||||
self.assertTrue(menu.items[i].order >= menu.items[i-1].order)
|
self.assertGreaterEqual(menu.items[index].order,
|
||||||
|
menu.items[index - 1].order)
|
||||||
|
|
||||||
@unittest.skip('requires configuring Django beforehand')
|
@unittest.skip('requires configuring Django beforehand')
|
||||||
def test_add_urlname(self):
|
def test_add_urlname(self):
|
||||||
@ -108,27 +109,37 @@ class MenuTestCase(unittest.TestCase):
|
|||||||
"""Verify that an active menu item can be correctly retrieved."""
|
"""Verify that an active menu item can be correctly retrieved."""
|
||||||
menu = self.build_menu()
|
menu = self.build_menu()
|
||||||
|
|
||||||
for i in range(1, 8):
|
for index in range(1, 8):
|
||||||
request = HttpRequest()
|
request = HttpRequest()
|
||||||
request.path = URL_TEMPLATE.format(i, i, i)
|
request.path = URL_TEMPLATE.format(index, index, index)
|
||||||
item = menu.active_item(request)
|
item = menu.active_item(request)
|
||||||
if i <= 5:
|
if index <= 5:
|
||||||
self.assertEqual('Item' + str(i), item.label)
|
self.assertEqual('Item' + str(index), item.label)
|
||||||
self.assertEqual(request.path, item.url)
|
self.assertEqual(request.path, item.url)
|
||||||
else:
|
else:
|
||||||
self.assertIsNone(item)
|
self.assertIsNone(item)
|
||||||
|
|
||||||
|
def test_active_item_when_inside_subpath(self):
|
||||||
|
"""Verify that the current URL could be a sub-path of menu item."""
|
||||||
|
menu = self.build_menu()
|
||||||
|
expected_url = URL_TEMPLATE.format(1, 1, 1)
|
||||||
|
request = HttpRequest()
|
||||||
|
request.path = expected_url + 'd/e/f/'
|
||||||
|
item = menu.active_item(request)
|
||||||
|
self.assertEqual('Item1', item.label)
|
||||||
|
self.assertEqual(expected_url, item.url)
|
||||||
|
|
||||||
# Helper methods
|
# Helper methods
|
||||||
|
|
||||||
def build_menu(self, size=5):
|
def build_menu(self, size=5):
|
||||||
"""Build a menu with the specified number of items."""
|
"""Build a menu with the specified number of items."""
|
||||||
random.seed()
|
random.seed()
|
||||||
item_data = []
|
item_data = []
|
||||||
for i in range(1, size+1):
|
for index in range(1, size + 1):
|
||||||
item_data.append(['Item' + str(i),
|
item_data.append(['Item' + str(index),
|
||||||
'Icon' + str(i),
|
'Icon' + str(index),
|
||||||
URL_TEMPLATE.format(i, i, i),
|
URL_TEMPLATE.format(index, index, index),
|
||||||
random.randint(0, 100)])
|
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])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user