"""Tests for log module.""" from copy import copy from datetime import datetime import json from unittest.mock import patch from flows.digital import log from flows.digital import status from flows.flow import DatabaseParam @patch('flows.digital.log.datetime') @patch('flows.digital.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 upcs_raw = ('123', '234', '345') upcs = DatabaseParam('upcs', data=upcs_raw) log_params = { 'correlation_id': 'cid123', 'date_end': '2012-12-21', 'date_start': '2000-01-01', 'upcs': upcs} expected = copy(log_params) expected['etl_start'] = mock_now expected['etl_status'] = status.STARTED expected['upcs'] = json.dumps(upcs_raw) 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.digital.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.digital.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', new_status='test_status') log.update_status(correlation_id='cid123.1.2.3.4', new_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