"""Test for tool_log model.""" from datetime import datetime from unittest.mock import patch from sqlalchemy.exc import SQLAlchemyError from prs.constants import http_response_codes from prs.models import tool_log from tests.testutils import db @db.test_schema def test_initialize_tool_log(): """Test initialize_tool_log with id.""" create_tool_log_response = { 'tool_name': 'Label Details', 'society_id': 41, 'time_start': datetime(2017, 7, 25, 14, 8, 17), 'tool_switches': '[]', 'input_text': '7689', 'status': -1, 'comment': '12345', 'mysql_proc_id': 200, 'progress': 'Initializing', 'file_code': 'B7A81', 'user_ip': '::1' } response = tool_log.initialize_tool_log(create_tool_log_response) assert response.status == http_response_codes.SUCCESS_STATUS assert response.message['tool_log_id'] @db.test_schema def test_update_tool_log_success(update_tool_log_response_success): """Test update_tool_log if data was updated successfully.""" tool_log.initialize_tool_log(update_tool_log_response_success) response = tool_log.update_tool_log(update_tool_log_response_success) assert response.status == http_response_codes.SUCCESS_STATUS assert response.message @db.test_schema def test_update_tool_log_failure( update_tool_log_response_success, update_tool_log_response_failure): """Test update_tool_log if there was a failure updating the data.""" tool_log.initialize_tool_log(update_tool_log_response_success) response = tool_log.update_tool_log(update_tool_log_response_failure) # Failure response also sends http status as 200 assert response.status == http_response_codes.SUCCESS_STATUS assert not response.message @patch('prs.connectors.mysql.ppb_db_session', side_effect=SQLAlchemyError()) def test_update_tool_log_sql_failure(update_tool_log_response_success): """Test update_tool_log with SQLAlchemyError failure updating the data.""" tool_log.initialize_tool_log(update_tool_log_response_success) response = tool_log.update_tool_log(update_tool_log_response_success) assert response.status == http_response_codes.INTERNAL_SERVER_ERROR