"""Unit tests for Theatrical log.py module.""" from copy import copy from datetime import datetime import json from unittest import mock from unittest.mock import patch import pytest from flows.flow import DatabaseParam from flows.theatrical import log from flows.theatrical import status @pytest.fixture(params=[ {'correlation_id': 'cid123', 'date_end': '2012-12-21', 'date_start': '2000-01-01', 'with_archive': False, 'upcs': DatabaseParam('upcs', data=['upcfoo', 'upcbar'])}, {'correlation_id': 'cid123', 'date_end': '2012-12-21', 'date_start': '2000-01-01', 'with_archive': True, 'upcs': DatabaseParam('upcs', data=['upcfoo', 'upcbar'])}, {'correlation_id': 'cid123', 'date_end': '2012-12-21', 'date_start': '2000-01-01', 'upcs': DatabaseParam('upcs', data=[]), 'with_archive': True}]) def log_params(request): """Set fixture for create function.""" return request.param @patch('flows.theatrical.log.datetime') @patch('flows.theatrical.log.datastore') def test_create(mock_datastore, mock_datetime, database_context, log_params): """Test create function.""" mock_now = datetime(1900, 1, 1, 1, 1, 1) mock_datastore.context = database_context mock_datetime.now.return_value = mock_now expected = copy(log_params) expected['etl_start'] = mock_now expected['etl_status'] = status.STARTED expected['upcs'] = json.dumps(expected['upcs'].data) log.create(**log_params) mock_datastore.execute.assert_called_with(mock.ANY, expected) @patch('flows.theatrical.log.datastore') def test_add_run_id(mock_datastore, database_context): """Test add_run_id function.""" mock_datastore.context = database_context run_params = {'correlation_id': 'cid123', 'workflow_run_id': 'run456'} log.add_run_id(**run_params) mock_datastore.execute.assert_called_with(mock.ANY, run_params) @patch('flows.theatrical.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') expected = { 'correlation_id': 'cid123', 'status': 'test_status'} mock_datastore.execute.assert_has_calls([ mock.call(mock.ANY, expected), mock.call(mock.ANY, expected)])