i2p: Convert router configuration tests to pytest style

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Sunil Mohan Adapa 2019-04-29 16:36:00 -07:00
parent 97ed7fe144
commit 56d511368d
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2

View File

@ -14,8 +14,11 @@
# 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/>.
#
"""
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