"""Tests for deployment script.""" from unittest.mock import MagicMock, Mock, call from alooma import Client import pytest import deploy # noqa TEST_CODE = 'print("Hello World")' MODULE_NAME = 'main' FILES = [MagicMock(stem='main'), MagicMock(stem='config')] @pytest.fixture def successful_response(): """Succesfull response from Alooma.""" return MagicMock(status_code=200) @pytest.fixture def failed_response(): """Failed response from Alooma.""" return MagicMock(status_code=400) @pytest.fixture def api_mock(mocker): """Patch `get_api_client` function with mock client.""" return mocker.patch( 'deploy.get_api_client', return_value=MagicMock(spec=Client)) @pytest.fixture def patch_read_code(mocker): """Patch code-reading functions.""" patch_mock = mocker.patch('deploy.Path', spec=True) patch_mock.return_value.glob.return_value = FILES file_mock = MagicMock() file_mock.__enter__ = Mock( return_value=MagicMock(read=Mock(return_value=TEST_CODE))) mocker.patch('deploy.open', return_value=file_mock) def test_deploy_module(mocker, successful_response, api_mock): """Test successful deploy of a module to Alooma.""" api_mock.set_transform.return_value = successful_response deploy.deploy_module(TEST_CODE, MODULE_NAME, api_mock) api_mock.set_transform.assert_called_with(TEST_CODE, MODULE_NAME) def test_deploy_module_failed(mocker, failed_response, api_mock): """Test failed deploy of a module to Alooma.""" api_mock.set_transform.return_value = failed_response with pytest.raises(Exception, match=deploy.FAILED_EXCEPTION_MESSAGE): deploy.deploy_module(TEST_CODE, MODULE_NAME, api_mock) api_mock.set_transform.assert_called_with(TEST_CODE, MODULE_NAME) def test_deploy_directory(mocker, successful_response, patch_read_code): """Test successful deploy of a directory to Alooma.""" alooma_mock = Mock(spec=Client) alooma_mock.set_transform.return_value = successful_response mocker.spy(deploy, 'deploy_module') deploy.deploy_directory('testdirectory', alooma_mock) exected_calls = [ call(TEST_CODE, 'main', alooma_mock), call(TEST_CODE, 'config', alooma_mock)] deploy.deploy_module.assert_has_calls(exected_calls) assert deploy.deploy_module.call_count == 2 def test_deploy_directory_failed(mocker, failed_response, patch_read_code): """Test failed deploy of a module to Alooma.""" alooma_mock = Mock(spec=Client) alooma_mock.set_transform.return_value = failed_response mocker.spy(deploy, 'deploy_module') with pytest.raises(Exception, match=deploy.FAILED_EXCEPTION_MESSAGE): deploy.deploy_directory('testdirectory', alooma_mock) deploy.deploy_module.assert_called_with(TEST_CODE, 'main', alooma_mock) assert deploy.deploy_module.call_count == 1 def test_get_alooma_client(mocker): """Test get Alooma client successfully.""" client_mock = Mock(spec=Client) mocker.patch('deploy.alooma.Client', return_value=client_mock) client = deploy.get_api_client() assert client == client_mock def test_get_alooma_client_failed(mocker): """Test get Alooma client failed.""" alooma_error = 'Connection to Alooma failed.' mocker.patch('deploy.alooma.Client', side_effect=Exception(alooma_error)) with pytest.raises(Exception, match=alooma_error): deploy.get_api_client()