mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-27 10:44:33 +00:00
i2p: Convert unit tests to pytest style
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
parent
a73f002ed6
commit
ce9eacb751
@ -13,10 +13,14 @@
|
||||
# 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/>.
|
||||
#
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user