"""Base flow module tests.""" import json from unittest.mock import Mock from unittest.mock import patch from flows.flow import DatabaseParam from flows.flow import FlowBase @patch('flows.flow.datastore') def test_query(datastore): """Test the data lookup logic.""" key = 'test_key' cid = '1234-5678.1.2.3' cid_base = '1234-5678' data = ['123', '234', '345'] data_json = json.dumps(data) query_result = Mock() query_result.fetchone.return_value = [data_json] datastore.query.return_value = query_result param = DatabaseParam(key) result = param.get_data({'correlation_id': cid}) assert result == data query_params = datastore.query.call_args_list[0][0][1] assert cid_base in query_params assert key in query_params @patch('flows.flow.datastore') def test_query_skip(datastore): """Test the data lookup is skipped if data is set.""" key = 'test_key' cid = '1234-5678.1.2.3' data = ['123', '234', '345'] param = DatabaseParam(key, data=data) result = param.get_data({'correlation_id': cid}) assert result == data assert not datastore.query.called @patch('flows.flow.datastore') def test_missing_data(datastore): """Test the data lookup logic with no data stored in the databse.""" key = 'test_key' cid = '1234-5678.1.2.3' datastore.query.side_effect = Exception param = DatabaseParam(key) result = param.get_data({'correlation_id': cid}) assert result is None assert datastore.query.called def test_requirements(): """Test garcon requirements hook.""" key = 'test_key' param = DatabaseParam(key) requirements = list(param.requirements) assert key in requirements assert 'correlation_id' in requirements def test_json_conversion(): """Test raw data to JSON conversion.""" key = 'test_key' data = ['123', '234', '345'] data_json = json.dumps(data) param = DatabaseParam(key, data=data) assert param.data_json == data_json @patch('flows.flow.datastore') def test_put_data_json(datastore): """Test JSON conversion when uploading data into the database.""" key = 'test_key' cid = '1234-5678.1.2.3' cid_base = '1234-5678' data = ['123', '234', '345'] data_json = json.dumps(data) param = DatabaseParam(key, data=data) param.put_data(cid) query_params = datastore.execute.call_args_list[0][0][1] assert cid_base in query_params assert data_json in query_params assert data not in query_params assert key in query_params @patch('flows.flow.activity') def test_flowbase_init(mock_activity): """Test __init__ function of FlowBase class.""" new_flow = FlowBase('test', 'base_flow_test', '1.0') mock_activity.create.assert_called_with( 'test', 'base_flow_test', version='1.0', on_exception=new_flow.on_exception)