"""Test for log module.""" from copy import copy from datetime import datetime from unittest.mock import Mock from unittest.mock import patch from flows.cable_ingestion import log from flows.cable_ingestion import status @patch('flows.cable_ingestion.log.datetime') @patch('flows.cable_ingestion.log.datastore') def test_create(mock_datastore, mock_datetime, database_context): """Test create function.""" mock_now = datetime(1900, 1, 1, 1, 1, 1) mock_datastore.context = database_context mock_datetime.now.return_value = mock_now log_params = { 'correlation_id': 'cid123', 'date_end': '2012-12-21', 'date_start': '2000-01-01'} expected = copy(log_params) expected['etl_start'] = mock_now expected['etl_status'] = status.STARTED log.create(**log_params) call_list = mock_datastore.execute.call_args_list called_params = call_list[0][0][1] assert called_params == expected @patch('flows.cable_ingestion.log.datastore') def test_add_run_id(mock_datastore, database_context): """Test add_run_id function.""" mock_datastore.context = database_context log.add_run_id(correlation_id='cid123', workflow_run_id='run456') call_list = mock_datastore.execute.call_args_list expected = {'correlation_id': 'cid123', 'workflow_run_id': 'run456'} assert call_list[0][0][1] == expected @patch('flows.cable_ingestion.log.datastore') def test_update_status(mock_datastore, database_context): """Test update_status function.""" mock_datastore.context = database_context log.update_status(correlation_id='cid123', status='test_status') log.update_status(correlation_id='cid123.1.2.3.4', status='test_status') call_list = mock_datastore.execute.call_args_list expected = { 'correlation_id': 'cid123', 'status': 'test_status', 'etl_end': None} assert call_list[0][0][1] == expected assert call_list[1][0][1] == expected @patch('flows.cable_ingestion.log.datastore') @patch('flows.cable_ingestion.log.queries') def test_get_etl_log(queries, datastore): """Test get_etl_log function.""" correlation_id = '1234-5678-4321.1.2.3.4' test_query = 'get the log row please' queries.SELECT_ETL_LOG = test_query params = {'correlation_id': '1234-5678-4321'} row = ['this', 'is', 'a', 'fake', 'db', 'row'] cursor = Mock() cursor.fetchone.return_value = row datastore.query.return_value = cursor result = log.get_etl_log(correlation_id) assert datastore.execute.called_once_with(test_query, params) assert result == row