From 923c8305ae2ba2f6757cdafe0d10fdf7cd49a889 Mon Sep 17 00:00:00 2001 From: Justin Riley Date: Tue, 2 Aug 2016 23:19:04 -0400 Subject: [PATCH] add tests for utils module --- onedns/tests/test_shell.py | 33 +++++++++++++++++++++++++++++++++ onedns/tests/test_utils.py | 24 ++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 onedns/tests/test_shell.py create mode 100644 onedns/tests/test_utils.py diff --git a/onedns/tests/test_shell.py b/onedns/tests/test_shell.py new file mode 100644 index 0000000..a8dddd3 --- /dev/null +++ b/onedns/tests/test_shell.py @@ -0,0 +1,33 @@ +import sys + +import mock + +from testfixtures import LogCapture + +IPY = mock.MagicMock() +IPY.embed = mock.MagicMock() + +IPY_MODULES = { + 'IPython': IPY, +} + + +@mock.patch.dict('sys.modules', IPY_MODULES) +def test_with_ipython(): + from onedns import utils + ns = dict(test=True) + utils.shell(local_ns=ns) + IPY.embed.assert_called_once_with(user_ns=ns) + + +@mock.patch.object(sys, 'path', []) +def test_without_ipython(): + from onedns import utils + with LogCapture() as log: + utils.shell() + log.check( + ('onedns', 'ERROR', + 'Unable to load IPython:\n\nNo module named IPython\n'), + ('onedns', 'ERROR', + 'Please check that IPython is installed and working.'), + ) diff --git a/onedns/tests/test_utils.py b/onedns/tests/test_utils.py new file mode 100644 index 0000000..d0950b8 --- /dev/null +++ b/onedns/tests/test_utils.py @@ -0,0 +1,24 @@ +import os + +import mock + +from onedns import utils + + +ONE_XMLRPC = 'https://controller:2633/RPC2' + + +def test_get_kwargs_from_dict(): + d = dict(ONE_XMLRPC=ONE_XMLRPC) + kwargs = utils.get_kwargs_from_dict(d, prefix='ONE_') + assert kwargs['XMLRPC'] == ONE_XMLRPC + kwargs = utils.get_kwargs_from_dict(d, prefix='ONE_', lower=True) + assert kwargs['xmlrpc'] == ONE_XMLRPC + + +@mock.patch.dict(os.environ, {'ONE_XMLRPC': ONE_XMLRPC}) +def test_get_kwargs_from_env(): + kwargs = utils.get_kwargs_from_env(prefix='ONE_') + assert kwargs['XMLRPC'] == ONE_XMLRPC + kwargs = utils.get_kwargs_from_env(prefix='ONE_', lower=True) + assert kwargs['xmlrpc'] == ONE_XMLRPC