From 56d511368d0c12ca7b214648baf65418f89a336e Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Mon, 29 Apr 2019 16:36:00 -0700 Subject: [PATCH] i2p: Convert router configuration tests to pytest style Signed-off-by: Sunil Mohan Adapa --- .../modules/i2p/tests/test_router_editor.py | 123 +++++++++--------- 1 file changed, 62 insertions(+), 61 deletions(-) diff --git a/plinth/modules/i2p/tests/test_router_editor.py b/plinth/modules/i2p/tests/test_router_editor.py index bd689c924..cbf188d3a 100644 --- a/plinth/modules/i2p/tests/test_router_editor.py +++ b/plinth/modules/i2p/tests/test_router_editor.py @@ -14,8 +14,11 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # +""" +Test I2P router configuration editing helper. +""" -import unittest +import pytest from plinth.modules.i2p.helpers import RouterEditor from plinth.modules.i2p.tests import DATA_DIR @@ -23,65 +26,63 @@ from plinth.modules.i2p.tests import DATA_DIR ROUTER_CONF_PATH = str(DATA_DIR / 'router.config') -class RouterEditingTests(unittest.TestCase): - - def setUp(self): - self.editor = RouterEditor(ROUTER_CONF_PATH) - - def test_count_favs(self): - self.editor.read_conf() - favs = self.editor.get_favorites() - self.assertEqual(len(favs.keys()), 17) - - def test_add_normal_favorite(self): - self.editor.read_conf() - name = 'Somewhere' - url = 'http://somewhere-again.i2p' - description = "Just somewhere else" - self.editor.add_favorite( - name, url, description - ) - - favs = self.editor.get_favorites() - self.assertIn(url, favs) - favorite = favs[url] - self.assertEqual(favorite['name'], name) - self.assertEqual(favorite['description'], description) - - self.assertEqual(len(favs), 18) - - def test_add_favorite_with_comma(self): - self.editor.read_conf() - name = 'Name,with,comma' - expected_name = name.replace(',', '.') - url = 'http://url-without-comma.i2p' - description = "Another,comma,to,play,with" - expected_description = description.replace(',', '.') - - self.editor.add_favorite( - name, url, description - ) - - favs = self.editor.get_favorites() - self.assertIn(url, favs) - favorite = favs[url] - self.assertEqual(favorite['name'], expected_name) - self.assertEqual(favorite['description'], expected_description) - - self.assertEqual(len(favs), 18) - - def test_add_fav_to_empty_config(self): - self.editor.conf_filename = '/tmp/inexistent.conf' - self.editor.read_conf() - self.assertEqual(len(self.editor.get_favorites()), 0) - - name = 'Test Favorite' - url = 'http://test-fav.i2p' - self.editor.add_favorite( - name, url - ) - self.assertEqual(len(self.editor.get_favorites()), 1) +@pytest.fixture +def editor(): + """Return editor instance object for each test.""" + return RouterEditor(ROUTER_CONF_PATH) -if __name__ == '__main__': - unittest.main() +def test_count_favorites(editor): + """Test counting favorites.""" + editor.read_conf() + favorites = editor.get_favorites() + assert len(favorites.keys()) == 17 + + +def test_add_normal_favorite(editor): + """Test adding a normal favorite.""" + editor.read_conf() + name = 'Somewhere' + url = 'http://somewhere-again.i2p' + description = "Just somewhere else" + editor.add_favorite(name, url, description) + + favorites = editor.get_favorites() + assert url in favorites + favorite = favorites[url] + assert favorite['name'] == name + assert favorite['description'] == description + + assert len(favorites) == 18 + + +def test_add_favorite_with_comma(editor): + """Test adding a favorite with common in its name.""" + editor.read_conf() + name = 'Name,with,comma' + expected_name = name.replace(',', '.') + url = 'http://url-without-comma.i2p' + description = "Another,comma,to,play,with" + expected_description = description.replace(',', '.') + + editor.add_favorite(name, url, description) + + favorites = editor.get_favorites() + assert url in favorites + favorite = favorites[url] + assert favorite['name'] == expected_name + assert favorite['description'] == expected_description + + assert len(favorites) == 18 + + +def test_add_fav_to_empty_config(editor): + """Test adding favorite to empty configuration.""" + editor.conf_filename = '/tmp/inexistent.conf' + editor.read_conf() + assert not editor.get_favorites() + + name = 'Test Favorite' + url = 'http://test-fav.i2p' + editor.add_favorite(name, url) + assert len(editor.get_favorites()) == 1