cfg: Convert tests to pytest style

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
This commit is contained in:
Sunil Mohan Adapa 2019-05-01 16:10:32 -07:00 committed by Joseph Nuthalapati
parent 05bf200906
commit 29b2d664b0
No known key found for this signature in database
GPG Key ID: 5398F00A2FA43C35

View File

@ -21,7 +21,8 @@ Test module for configuration module.
import configparser import configparser
import logging import logging
import os import os
import unittest
import pytest
from plinth import cfg from plinth import cfg
@ -34,34 +35,35 @@ CONFIG_FILE_WITH_MISSING_SECTIONS = \
logging.disable(logging.CRITICAL) logging.disable(logging.CRITICAL)
pytestmark = pytest.mark.usefixtures('load_cfg')
class TestCfg(unittest.TestCase):
"""Verify that the configuration module behaves as expected."""
@classmethod @pytest.fixture(name='test_config_file')
def setUpClass(cls): def fixture_test_config_file(load_cfg):
"""Locate and copy the official plinth.config file.""" """Test fixture to return the configuration file path"""
cls.test_config_file, cls.test_config_dir = cfg.get_config_paths() return cfg.get_config_paths()[0]
@classmethod
def tearDownClass(cls):
"""Cleanup after all tests are completed."""
cfg.read()
def test_read_default_config_file(self): @pytest.fixture(name='test_config_dir')
def fixture_test_config_dir(load_cfg):
"""Test fixture to return the configuration file directory."""
return cfg.get_config_paths()[1]
def test_read_default_config_file(test_config_dir, test_config_file):
"""Verify that the default config file can be read correctly.""" """Verify that the default config file can be read correctly."""
# Read the plinth.config file directly # Read the plinth.config file directly
parser = configparser.ConfigParser( parser = configparser.ConfigParser(defaults={'root': test_config_dir})
defaults={'root': self.test_config_dir}) parser.read(test_config_file)
parser.read(self.test_config_file)
# Read the plinth.config file via the cfg module # Read the plinth.config file via the cfg module
cfg.read(self.test_config_file, self.test_config_dir) cfg.read(test_config_file, test_config_dir)
# Compare the two results # Compare the two results
self.compare_configurations(parser) compare_configurations(parser)
def test_read_primary_config_file(self):
def test_read_primary_config_file():
"""Verify that the primary config file is used by default.""" """Verify that the primary config file is used by default."""
original_config_path = cfg.DEFAULT_CONFIG_FILE original_config_path = cfg.DEFAULT_CONFIG_FILE
original_root_directory = cfg.DEFAULT_ROOT original_root_directory = cfg.DEFAULT_ROOT
@ -79,68 +81,66 @@ class TestCfg(unittest.TestCase):
cfg.read() cfg.read()
except configparser.NoOptionError: except configparser.NoOptionError:
pass pass
self.assertEqual(cfg.config_file, expected_config_path) assert cfg.config_file == expected_config_path
self.assertEqual(cfg.root, expected_root_directory) assert cfg.root == expected_root_directory
finally: finally:
cfg.DEFAULT_CONFIG_FILE = original_config_path cfg.DEFAULT_CONFIG_FILE = original_config_path
cfg.DEFAULT_ROOT = original_root_directory cfg.DEFAULT_ROOT = original_root_directory
def test_read_fallback_config_file(self):
def test_read_fallback_config_file():
"""Verify that the correct fallback config file is used""" """Verify that the correct fallback config file is used"""
fallback_root = os.path.realpath('.') fallback_root = os.path.realpath('.')
fallback_config_file = os.path.join(fallback_root, 'plinth.config') fallback_config_file = os.path.join(fallback_root, 'plinth.config')
config_path, root_directory = cfg.get_fallback_config_paths() config_path, root_directory = cfg.get_fallback_config_paths()
cfg.read(config_path, root_directory) cfg.read(config_path, root_directory)
self.assertEqual(cfg.config_file, fallback_config_file) assert cfg.config_file == fallback_config_file
self.assertEqual(cfg.root, fallback_root) assert cfg.root == fallback_root
def test_read_missing_config_file(self):
def test_read_missing_config_file():
"""Verify that an exception is raised when there's no config file.""" """Verify that an exception is raised when there's no config file."""
self.assertRaises(FileNotFoundError, cfg.read, 'x-non-existant-file', with pytest.raises(FileNotFoundError):
'x-root-directory') cfg.read('x-non-existant-file', 'x-root-directory')
def test_read_config_file_with_missing_sections(self):
def test_read_config_file_with_missing_sections(test_config_dir):
"""Verify that missing configuration sections can be detected.""" """Verify that missing configuration sections can be detected."""
self.assertRaises(configparser.NoSectionError, cfg.read, with pytest.raises(configparser.NoSectionError):
CONFIG_FILE_WITH_MISSING_SECTIONS, cfg.read(CONFIG_FILE_WITH_MISSING_SECTIONS, test_config_dir)
self.test_config_dir)
def test_read_config_file_with_missing_options(self):
def test_read_config_file_with_missing_options(test_config_dir):
"""Verify that missing configuration options can be detected.""" """Verify that missing configuration options can be detected."""
self.assertRaises(configparser.NoOptionError, cfg.read, with pytest.raises(configparser.NoOptionError):
CONFIG_FILE_WITH_MISSING_OPTIONS, cfg.read(CONFIG_FILE_WITH_MISSING_OPTIONS, test_config_dir)
self.test_config_dir)
def compare_configurations(self, parser):
def compare_configurations(parser):
"""Compare two sets of configuration values.""" """Compare 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(9, len(parser.items('Path'))) assert len(parser.items('Path')) == 9
self.assertEqual(parser.get('Path', 'root'), cfg.root) assert parser.get('Path', 'root') == cfg.root
self.assertEqual(parser.get('Path', 'file_root'), cfg.file_root) assert parser.get('Path', 'file_root') == cfg.file_root
self.assertEqual(parser.get('Path', 'config_dir'), cfg.config_dir) assert parser.get('Path', 'config_dir') == cfg.config_dir
self.assertEqual( assert parser.get('Path', 'custom_static_dir') == cfg.custom_static_dir
parser.get('Path', 'custom_static_dir'), cfg.custom_static_dir) assert parser.get('Path', 'data_dir') == cfg.data_dir
self.assertEqual(parser.get('Path', 'data_dir'), cfg.data_dir) assert parser.get('Path', 'store_file') == cfg.store_file
self.assertEqual(parser.get('Path', 'store_file'), cfg.store_file) assert parser.get('Path', 'actions_dir') == cfg.actions_dir
self.assertEqual(parser.get('Path', 'actions_dir'), cfg.actions_dir) assert parser.get('Path', 'doc_dir') == cfg.doc_dir
self.assertEqual(parser.get('Path', 'doc_dir'), cfg.doc_dir)
self.assertEqual(6, len(parser.items('Network'))) assert len(parser.items('Network')) == 6
self.assertEqual(parser.get('Network', 'host'), cfg.host) assert parser.get('Network', 'host') == cfg.host
self.assertEqual(int(parser.get('Network', 'port')), cfg.port) assert int(parser.get('Network', 'port')) == cfg.port
self.assertEqual( assert parser.get('Network', 'secure_proxy_ssl_header') == \
parser.get('Network', 'secure_proxy_ssl_header'), cfg.secure_proxy_ssl_header
cfg.secure_proxy_ssl_header) assert isinstance(cfg.use_x_forwarded_for, bool)
self.assertIsInstance(cfg.use_x_forwarded_for, bool) assert parser.get('Network', 'use_x_forwarded_for') == \
self.assertEqual( str(cfg.use_x_forwarded_for)
parser.get('Network', 'use_x_forwarded_for'), assert isinstance(cfg.use_x_forwarded_host, bool)
str(cfg.use_x_forwarded_for)) assert parser.get('Network', 'use_x_forwarded_host') == \
self.assertIsInstance(cfg.use_x_forwarded_host, bool) str(cfg.use_x_forwarded_host)
self.assertEqual( assert len(parser.items('Misc')) == 3
parser.get('Network', 'use_x_forwarded_host'), assert parser.get('Misc', 'danube_edition') == str(cfg.danube_edition)
str(cfg.use_x_forwarded_host)) assert parser.get('Misc', 'box_name') == cfg.box_name
self.assertEqual(3, len(parser.items('Misc')))
self.assertEqual(
parser.get('Misc', 'danube_edition'), str(cfg.danube_edition))
self.assertEqual(parser.get('Misc', 'box_name'), cfg.box_name)