From 377a50e213f5c2be3679487a5d7782303c6cc2dc Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Wed, 1 May 2019 16:07:48 -0700 Subject: [PATCH] config: Convert tests to pytest style Signed-off-by: Sunil Mohan Adapa Reviewed-by: Joseph Nuthalapati --- plinth/modules/config/tests/test_config.py | 102 +++++++++++---------- 1 file changed, 56 insertions(+), 46 deletions(-) diff --git a/plinth/modules/config/tests/test_config.py b/plinth/modules/config/tests/test_config.py index 950d10edd..aab9e6f85 100644 --- a/plinth/modules/config/tests/test_config.py +++ b/plinth/modules/config/tests/test_config.py @@ -14,66 +14,76 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # - """ Tests for config module. """ import os -import unittest from plinth import __main__ as plinth_main from ..forms import ConfigurationForm -class TestConfig(unittest.TestCase): - """Test cases for testing the config module.""" - def test_hostname_field(self): - """Test that hostname field accepts only valid hostnames.""" - valid_hostnames = [ - 'a', '0a', 'a0', 'AAA', '00', '0-0', 'example-hostname', 'example', - '012345678901234567890123456789012345678901234567890123456789012'] - invalid_hostnames = [ - '', '-', '-a', 'a-', '.a', 'a.', 'a.a', '?', 'a?a', - '0123456789012345678901234567890123456789012345678901234567890123'] +def test_hostname_field(): + """Test that hostname field accepts only valid hostnames.""" + valid_hostnames = [ + 'a', '0a', 'a0', 'AAA', '00', '0-0', 'example-hostname', 'example', + '012345678901234567890123456789012345678901234567890123456789012' + ] + invalid_hostnames = [ + '', '-', '-a', 'a-', '.a', 'a.', 'a.a', '?', 'a?a', + '0123456789012345678901234567890123456789012345678901234567890123' + ] - for hostname in valid_hostnames: - form = ConfigurationForm({'hostname': hostname, - 'domainname': 'example.com'}) - self.assertTrue(form.is_valid()) + for hostname in valid_hostnames: + form = ConfigurationForm({ + 'hostname': hostname, + 'domainname': 'example.com' + }) + assert form.is_valid() - for hostname in invalid_hostnames: - form = ConfigurationForm({'hostname': hostname, - 'domainname': 'example.com'}) - self.assertFalse(form.is_valid()) + for hostname in invalid_hostnames: + form = ConfigurationForm({ + 'hostname': hostname, + 'domainname': 'example.com' + }) + assert not form.is_valid() - def test_domainname_field(self): - """Test that domainname field accepts only valid domainnames.""" - valid_domainnames = [ - '', 'a', '0a', 'a0', 'AAA', '00', '0-0', 'example-hostname', - 'example', 'example.org', 'a.b.c.d', 'a-0.b-0.c-0', - '012345678901234567890123456789012345678901234567890123456789012', - ((('x' * 63) + '.') * 3) + 'x' * 61] - invalid_domainnames = [ - '-', '-a', 'a-', '.a', 'a.', '?', 'a?a', 'a..a', 'a.-a', '.', - ((('x' * 63) + '.') * 3) + 'x' * 62, 'x' * 64] - for domainname in valid_domainnames: - form = ConfigurationForm({'hostname': 'example', - 'domainname': domainname}) - self.assertTrue(form.is_valid()) +def test_domainname_field(): + """Test that domainname field accepts only valid domainnames.""" + valid_domainnames = [ + '', 'a', '0a', 'a0', 'AAA', '00', '0-0', 'example-hostname', 'example', + 'example.org', 'a.b.c.d', 'a-0.b-0.c-0', + '012345678901234567890123456789012345678901234567890123456789012', + ((('x' * 63) + '.') * 3) + 'x' * 61 + ] + invalid_domainnames = [ + '-', '-a', 'a-', '.a', 'a.', '?', 'a?a', 'a..a', 'a.-a', '.', + ((('x' * 63) + '.') * 3) + 'x' * 62, 'x' * 64 + ] - for domainname in invalid_domainnames: - form = ConfigurationForm({'hostname': 'example', - 'domainname': domainname}) - self.assertFalse(form.is_valid()) + for domainname in valid_domainnames: + form = ConfigurationForm({ + 'hostname': 'example', + 'domainname': domainname + }) + assert form.is_valid() - def test_locale_path(self): - """ - Test that the 'locale' directory is in the same folder as __main__.py. - This is required for detecting translated languages. - """ - plinth_dir = os.path.dirname(plinth_main.__file__) - locale_dir = os.path.join(plinth_dir, 'locale') - self.assertTrue(os.path.isdir(locale_dir)) + for domainname in invalid_domainnames: + form = ConfigurationForm({ + 'hostname': 'example', + 'domainname': domainname + }) + assert not form.is_valid() + + +def test_locale_path(): + """ + Test that the 'locale' directory is in the same folder as __main__.py. + This is required for detecting translated languages. + """ + plinth_dir = os.path.dirname(plinth_main.__file__) + locale_dir = os.path.join(plinth_dir, 'locale') + assert os.path.isdir(locale_dir)