Skip to content

Commit

Permalink
Better coverage for utils, cover __main__
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Kudinov committed Dec 4, 2017
1 parent 93bb625 commit ffb4f6b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/unit/services/utils/test_utils.py
Expand Up @@ -29,3 +29,28 @@ def test_adds_csr_extension_to_filename_before_reading(self):
Utils.get_csr_as_text('test')
call_arg, _ = mocked_open.call_args
self.assertTrue(call_arg[0].endswith('test.csr'))

@mock.patch('ncssl_api_client.services.utils.utils.Utils.create_directory', return_value=False)
def test_update_path_exists_on_user_cancel(self, utils_mock):
with self.assertRaises(SystemExit):
Utils.update_path('test')

@mock.patch('ncssl_api_client.services.utils.utils.Utils.create_directory', return_value=True)
def test_update_path_returns_path_on_create_directory_success(self, utils_mock):
self.assertIsNone(Utils.update_path('test'))

@mock.patch('ncssl_api_client.services.utils.utils.os.makedirs', return_value=True)
def test_create_directory_returns_true_on_success(self, makedirs_mock):
self.assertTrue(Utils.create_directory('test'))

@mock.patch('ncssl_api_client.services.utils.utils.os.makedirs')
@mock.patch('ncssl_api_client.services.utils.utils.input')
def test_create_directory_returns_false_on_user_cancel(self, input_mock, makedirs_mock):
makedirs_mock.side_effect = EnvironmentError()
self.assertFalse(Utils.create_directory('test'))

@mock.patch('ncssl_api_client.services.utils.utils.os.makedirs')
@mock.patch('ncssl_api_client.services.utils.utils.input', return_value='Y')
def test_create_directory_returns_true_on_user_overwrite(self, input_mock, makedirs_mock):
makedirs_mock.side_effect = EnvironmentError()
self.assertTrue(Utils.create_directory('test'))
22 changes: 22 additions & 0 deletions tests/unit/test__main__.py
@@ -0,0 +1,22 @@
from ncssl_api_client.__main__ import main_cli_wrapper
from unittest import TestCase
import mock


class MainTest(TestCase):

@mock.patch('ncssl_api_client.__main__.main', return_value=mock.MagicMock())
def test_status_code_zero_on_success(self, result_mock):
result_mock().is_successful.return_value = True
with self.assertRaises(SystemExit) as cm:
main_cli_wrapper()

self.assertEqual(cm.exception.code, 0)

@mock.patch('ncssl_api_client.__main__.main', return_value=mock.MagicMock())
def test_status_code_one_on_failure(self, result_mock):
result_mock().is_successful.return_value = False
with self.assertRaises(SystemExit) as cm:
main_cli_wrapper()

self.assertEqual(cm.exception.code, 1)

0 comments on commit ffb4f6b

Please sign in to comment.