From ce9eacb751534a83c1765ab8f2b7cb19b4f52862 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Mon, 29 Apr 2019 14:14:24 -0700 Subject: [PATCH] i2p: Convert unit tests to pytest style Signed-off-by: Sunil Mohan Adapa --- plinth/modules/i2p/tests/test_helpers.py | 85 +++++++++++++----------- 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/plinth/modules/i2p/tests/test_helpers.py b/plinth/modules/i2p/tests/test_helpers.py index dc817c396..c9181161a 100644 --- a/plinth/modules/i2p/tests/test_helpers.py +++ b/plinth/modules/i2p/tests/test_helpers.py @@ -13,10 +13,14 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # -import unittest +""" +Unit tests for helpers of I2P application. +""" from pathlib import Path +import pytest + from plinth.modules.i2p.helpers import TunnelEditor DATA_DIR = Path(__file__).parent / 'data' @@ -24,41 +28,48 @@ TUNNEL_CONF_PATH = DATA_DIR / 'i2ptunnel.config' TUNNEL_HTTP_NAME = 'I2P HTTP Proxy' -class TunnelEditorTests(unittest.TestCase): - - def setUp(self): - self.editor = TunnelEditor(str(TUNNEL_CONF_PATH)) - - def test_reading_conf(self): - self.editor.read_conf() - self.assertGreater(len(self.editor.lines), 1) - - def test_setting_idx(self): - self.editor.read_conf() - self.assertIsNone(self.editor.idx) - self.editor.set_tunnel_idx(TUNNEL_HTTP_NAME) - self.assertEqual(self.editor.idx, 0) - - def test_setting_tunnel_props(self): - self.editor.read_conf() - self.editor.set_tunnel_idx('I2P HTTP Proxy') - interface = '0.0.0.0' - self.editor.set_tunnel_prop('interface', interface) - self.assertEqual(self.editor['interface'], interface) - - def test_getting_inexistent_props(self): - self.editor.read_conf() - self.editor.idx = 0 - self.assertRaises(KeyError, self.editor.__getitem__, 'blabla') - - def test_setting_new_props(self): - self.editor.read_conf() - self.editor.idx = 0 - value = 'lol' - prop = 'blablabla' - self.editor[prop] = value - self.assertEqual(self.editor[prop], value) +@pytest.fixture +def editor(): + """Setup editor for each test.""" + return TunnelEditor(str(TUNNEL_CONF_PATH)) -if __name__ == '__main__': - unittest.main() +def test_reading_conf(editor): + """Test reading configuration file.""" + editor.read_conf() + assert len(editor.lines) > 1 + + +def test_setting_idx(editor): + """Test setting index for editing a tunnel.""" + editor.read_conf() + assert editor.idx is None + editor.set_tunnel_idx(TUNNEL_HTTP_NAME) + assert editor.idx == 0 + + +def test_setting_tunnel_props(editor): + """Test setting a tunnel property.""" + editor.read_conf() + editor.set_tunnel_idx('I2P HTTP Proxy') + interface = '0.0.0.0' + editor.set_tunnel_prop('interface', interface) + assert editor['interface'] == interface + + +def test_getting_nonexistent_props(editor): + """Test getting nonexistent property.""" + editor.read_conf() + editor.idx = 0 + with pytest.raises(KeyError): + _ = editor['blabla'] + + +def test_setting_new_props(editor): + """Test setting new properties.""" + editor.read_conf() + editor.idx = 0 + value = 'lol' + prop = 'blablabla' + editor[prop] = value + assert editor[prop] == value