from unittest.mock import MagicMock from unittest.mock import patch import pytest from ows_accounting.models import accounting_statement_export from ows_accounting.utils import dynamodb @pytest.fixture def mock_records(): return [{'generation_total_time': '438.7435712814331', 'transaction_types': 'all', 'user_id_type': '18805L', 'lines_scanned': '2009222', 'download_avro_file_end': '2016-06-01 17:35:58', 'generation_start': '2016-06-01 17:35:28', 'user_params': '206__all__en_US__txt', 'status': 'GENERATED', 'account_id': '18805', 'download_avro_file_start': '2016-06-01 17:35:28', 'generation_end': '2016-06-01 17:42:46', 'number_format': 'en_US', 'number_of_partitions': '3', 'file_type': 'txt', 'payment_interval': 'month', 'account_type': 'label', 'requested_datetime': '2016-07-01 17:42:46', 'period_ids': '206'}, {'generation_total_time': '438.7435712814331', 'transaction_types': 'all', 'user_id_type': '18805L', 'lines_scanned': '2009222', 'download_avro_file_end': '2016-06-01 17:35:58', 'generation_start': '2016-06-01 17:35:28', 'user_params': '206__all__en_US__txt', 'status': 'GENERATED', 'account_id': '18805', 'download_avro_file_start': '2016-06-01 17:35:28', 'generation_end': '2016-06-01 17:42:46', 'number_format': 'en_US', 'number_of_partitions': '3', 'file_type': 'txt', 'payment_interval': 'month', 'account_type': 'label', 'requested_datetime': '2016-07-02 10:42:46', 'period_ids': '206'}, {'generation_total_time': '258.09422302246094', 'transaction_types': 'all', 'user_id_type': '18805L', 'lines_scanned': '2009222', 'download_avro_file_end': '2016-05-26 17:45:57', 'generation_start': '2016-05-26 17:45:45', 'user_params': '206__all__en_US__xls', 'status': 'GENERATED', 'account_id': '18805', 'download_avro_file_start': '2016-05-26 17:45:45', 'generation_end': '2016-05-26 17:50:03', 'number_format': 'en_US', 'number_of_partitions': '3', 'file_type': 'xls', 'payment_interval': 'month', 'account_type': 'label', 'requested_datetime': '2016-07-05 19:42:46', 'period_ids': '206'}, {'generation_total_time': '438.7435712814331', 'transaction_types': 'all', 'user_id_type': '18805L', 'lines_scanned': '2009222', 'download_avro_file_end': '2016-06-01 17:35:58', 'generation_start': '2016-06-01 17:35:28', 'user_params': '206__all__en_US__txt', 'status': 'GENERATED', 'account_id': '18805', 'download_avro_file_start': '2016-06-01 17:35:28', 'generation_end': '2016-06-01 17:42:46', 'number_format': 'en_US', 'number_of_partitions': '3', 'file_type': 'txt', 'payment_interval': 'month', 'account_type': 'label', 'requested_datetime': '2016-07-01 13:42:46', 'period_ids': '206'}, {'generation_total_time': '438.7435712814331', 'transaction_types': 'all', 'user_id_type': '18805L', 'lines_scanned': '2009222', 'download_avro_file_end': '2016-06-01 17:35:58', 'generation_start': '2016-06-01 17:35:28', 'user_params': '206__all__en_US__txt', 'status': 'GENERATED', 'account_id': '18805', 'download_avro_file_start': '2016-06-01 17:35:28', 'generation_end': '2016-06-01 17:42:46', 'number_format': 'en_US', 'number_of_partitions': '3', 'file_type': 'txt', 'payment_interval': 'month', 'account_type': 'label', 'period_ids': '206'}] def test_get_records_quarter_label(monkeypatch, mock_records): """Test get_records method """ monkeypatch.setattr( dynamodb, 'get_table', MagicMock(return_value='table')) monkeypatch.setattr( dynamodb, 'query_items', MagicMock(return_value=mock_records)) model_obj = accounting_statement_export.AccountingStatementExport() model_obj.get_records(['202', '203', '204'], 8869, 'L', {}, 0, 10) dynamodb.query_items.assert_any_call( 'table', 'user_id_type', '8869L', period_ids='202,203,204') def test_get_records(monkeypatch, mock_records): """Test get_records method """ mock_table = MagicMock() monkeypatch.setattr( dynamodb, 'get_table', MagicMock(return_value=mock_table)) monkeypatch.setattr( dynamodb, 'query_items', MagicMock(return_value=mock_records)) model_obj = accounting_statement_export.AccountingStatementExport() records = model_obj.get_records(['206'], 18805, 'L', {}, 0, 10) assert records == { 'items': [{'generation_total_time': '258.09422302246094', 'transaction_types': 'all', 'user_id_type': '18805L', 'lines_scanned': '2009222', 'download_avro_file_end': '2016-05-26 17:45:57', 'generation_start': '2016-05-26 17:45:45', 'user_params': '206__all__en_US__xls', 'status': 'GENERATED', 'account_id': '18805', 'download_avro_file_start': '2016-05-26 17:45:45', 'generation_end': '2016-05-26 17:50:03', 'number_format': 'en_US', 'number_of_partitions': '3', 'file_type': 'xls', 'payment_interval': 'month', 'account_type': 'label', 'requested_datetime': '2016-07-05 19:42:46', 'period_ids': '206'}, {'generation_total_time': '438.7435712814331', 'transaction_types': 'all', 'user_id_type': '18805L', 'lines_scanned': '2009222', 'download_avro_file_end': '2016-06-01 17:35:58', 'generation_start': '2016-06-01 17:35:28', 'user_params': '206__all__en_US__txt', 'status': 'GENERATED', 'account_id': '18805', 'download_avro_file_start': '2016-06-01 17:35:28', 'generation_end': '2016-06-01 17:42:46', 'number_format': 'en_US', 'number_of_partitions': '3', 'file_type': 'txt', 'payment_interval': 'month', 'account_type': 'label', 'requested_datetime': '2016-07-02 10:42:46', 'period_ids': '206'}, {'generation_total_time': '438.7435712814331', 'transaction_types': 'all', 'user_id_type': '18805L', 'lines_scanned': '2009222', 'download_avro_file_end': '2016-06-01 17:35:58', 'generation_start': '2016-06-01 17:35:28', 'user_params': '206__all__en_US__txt', 'status': 'GENERATED', 'account_id': '18805', 'download_avro_file_start': '2016-06-01 17:35:28', 'generation_end': '2016-06-01 17:42:46', 'number_format': 'en_US', 'number_of_partitions': '3', 'file_type': 'txt', 'payment_interval': 'month', 'account_type': 'label', 'requested_datetime': '2016-07-01 17:42:46', 'period_ids': '206'}, {'generation_total_time': '438.7435712814331', 'transaction_types': 'all', 'user_id_type': '18805L', 'lines_scanned': '2009222', 'download_avro_file_end': '2016-06-01 17:35:58', 'generation_start': '2016-06-01 17:35:28', 'user_params': '206__all__en_US__txt', 'status': 'GENERATED', 'account_id': '18805', 'download_avro_file_start': '2016-06-01 17:35:28', 'generation_end': '2016-06-01 17:42:46', 'number_format': 'en_US', 'number_of_partitions': '3', 'file_type': 'txt', 'payment_interval': 'month', 'account_type': 'label', 'requested_datetime': '2016-07-01 13:42:46', 'period_ids': '206'}, {'generation_total_time': '438.7435712814331', 'transaction_types': 'all', 'user_id_type': '18805L', 'lines_scanned': '2009222', 'download_avro_file_end': '2016-06-01 17:35:58', 'generation_start': '2016-06-01 17:35:28', 'user_params': '206__all__en_US__txt', 'status': 'GENERATED', 'account_id': '18805', 'download_avro_file_start': '2016-06-01 17:35:28', 'generation_end': '2016-06-01 17:42:46', 'number_format': 'en_US', 'number_of_partitions': '3', 'file_type': 'txt', 'payment_interval': 'month', 'account_type': 'label', 'period_ids': '206'}], 'pagination': { 'type': 'standard', 'offset': 0, 'limit': 10, 'total_records': 5 } } records = model_obj.get_records(['206'], 18805, 'L', {}, 1, 1) assert records == { 'items': [ {'generation_total_time': '438.7435712814331', 'transaction_types': 'all', 'user_id_type': '18805L', 'lines_scanned': '2009222', 'download_avro_file_end': '2016-06-01 17:35:58', 'generation_start': '2016-06-01 17:35:28', 'user_params': '206__all__en_US__txt', 'status': 'GENERATED', 'account_id': '18805', 'download_avro_file_start': '2016-06-01 17:35:28', 'generation_end': '2016-06-01 17:42:46', 'number_format': 'en_US', 'number_of_partitions': '3', 'file_type': 'txt', 'payment_interval': 'month', 'account_type': 'label', 'requested_datetime': '2016-07-02 10:42:46', 'period_ids': '206'}], 'pagination': { 'type': 'standard', 'offset': 1, 'limit': 1, 'total_records': 5 } } records = model_obj.get_records(['206'], 18805, 'L', {}, 1, 2) assert records == { 'items': [ {'generation_total_time': '438.7435712814331', 'transaction_types': 'all', 'user_id_type': '18805L', 'lines_scanned': '2009222', 'download_avro_file_end': '2016-06-01 17:35:58', 'generation_start': '2016-06-01 17:35:28', 'user_params': '206__all__en_US__txt', 'status': 'GENERATED', 'account_id': '18805', 'download_avro_file_start': '2016-06-01 17:35:28', 'generation_end': '2016-06-01 17:42:46', 'number_format': 'en_US', 'number_of_partitions': '3', 'file_type': 'txt', 'payment_interval': 'month', 'account_type': 'label', 'requested_datetime': '2016-07-01 17:42:46', 'period_ids': '206'}, {'generation_total_time': '438.7435712814331', 'transaction_types': 'all', 'user_id_type': '18805L', 'lines_scanned': '2009222', 'download_avro_file_end': '2016-06-01 17:35:58', 'generation_start': '2016-06-01 17:35:28', 'user_params': '206__all__en_US__txt', 'status': 'GENERATED', 'account_id': '18805', 'download_avro_file_start': '2016-06-01 17:35:28', 'generation_end': '2016-06-01 17:42:46', 'number_format': 'en_US', 'number_of_partitions': '3', 'file_type': 'txt', 'payment_interval': 'month', 'account_type': 'label', 'requested_datetime': '2016-07-01 13:42:46', 'period_ids': '206'}], 'pagination': { 'type': 'standard', 'offset': 1, 'limit': 2, 'total_records': 5 } } def test_add_pagination(monkeypatch, mock_records): """Test _add_pagination. """ monkeypatch.setattr( dynamodb, 'get_table', MagicMock(return_value=MagicMock())) mock_model = accounting_statement_export.AccountingStatementExport() resp = mock_model._add_pagination(mock_records, 2, 1) assert resp == { 'items': [{'generation_total_time': '258.09422302246094', 'transaction_types': 'all', 'user_id_type': '18805L', 'lines_scanned': '2009222', 'download_avro_file_end': '2016-05-26 17:45:57', 'generation_start': '2016-05-26 17:45:45', 'user_params': '206__all__en_US__xls', 'status': 'GENERATED', 'account_id': '18805', 'download_avro_file_start': '2016-05-26 17:45:45', 'generation_end': '2016-05-26 17:50:03', 'number_format': 'en_US', 'number_of_partitions': '3', 'file_type': 'xls', 'payment_interval': 'month', 'account_type': 'label', 'requested_datetime': '2016-07-05 19:42:46', 'period_ids': '206'}], 'pagination': { 'type': 'standard', 'offset': 2, 'limit': 1, 'total_records': 5 } } @patch('ows_accounting.models.accounting_statement_export.dynamodb') def test_get_item(mock_dynamodb_util): """Test _get_report_status. """ mock_table = MagicMock() mock_dynamodb_util.query_items.return_value = [] mock_dynamodb_util.get_table.return_value = mock_table model = accounting_statement_export.AccountingStatementExport() resp = model.get_item('18805L', '205', 'DA,DT', 'txt', 'en_US') assert resp.errors.get('message') == 'DOESNOTEXIST' assert resp.status == 400 mock_dynamodb_util.query_items.return_value = [{'status': 'PENDING'}] resp = model.get_item('20372L', '206', 'all', 'xls', 'es_ES') assert resp.message.get('status') == 'PENDING' assert resp.status == 200 mock_dynamodb_util.query_items.assert_any_call( mock_table, 'user_id_type', '18805L', period_ids='205', transaction_types='DA,DT', file_type='txt', number_format='en_US') mock_dynamodb_util.query_items.assert_any_call( mock_table, 'user_id_type', '20372L', period_ids='206', transaction_types='all', file_type='xls', number_format='es_ES') @patch('ows_accounting.models.accounting_statement_export.dynamodb') def test_put_item(mock_dynamodb_util): """Test put item. """ mock_table = MagicMock() mock_dynamodb_util.get_table.return_value = mock_table model = accounting_statement_export.AccountingStatementExport() mock_table.put_item.return_value = 'test_resp' resp = model.put_item('18805L', '205__DA,DT__es_ES__txt', status='PENDING') item = dict( user_id_type='18805L', user_params='205__DA,DT__es_ES__txt', status='PENDING') mock_table.put_item.assert_any_call(Item=item) assert resp.message == 'test_resp' assert resp.status == 200 @patch('ows_accounting.models.accounting_statement_export.dynamodb') def test_filter_out_records(mock_dynamodb_util): """Test _filter_out_records """ mock_records = [ { 'generation_start': '2016-05-25 13:56:00', 'requested_by': 'The Artist Forever Known As Prince', 'period_ids': '202,203,204', 'transaction_types': 'AEA,AEV,AL,AS,AV,CE', 'file_type': 'txt', 'number_format': 'us', 's3_url': 's3://public.theorchard.com/reports/123', 'status': 'GENERATED'}, { 'generation_start': '2016-05-25 13:56:00', 'requested_by': 'The Artist Forever Known As Prince', 'period_ids': '202,203,204', 'transaction_types': 'AEA,AEV,AL,AS,AV,CE', 'file_type': 'AVRO', 'number_format': 'us', 's3_url': 's3://public.theorchard.com/reports/123', 'status': 'GENERATED'}] expected = [ { 'generation_start': '2016-05-25 13:56:00', 'requested_by': 'The Artist Forever Known As Prince', 'period_ids': '202,203,204', 'transaction_types': 'AEA,AEV,AL,AS,AV,CE', 'file_type': 'txt', 'number_format': 'us', 's3_url': 's3://public.theorchard.com/reports/123', 'status': 'GENERATED'}] model = accounting_statement_export.AccountingStatementExport() actual = model._filter_out_records(mock_records, {'file_type': 'AVRO'}) assert actual == expected actual = model._filter_out_records( mock_records, {'period_ids': '202,203,204', 'number_format': 'us'}) assert actual == [] @patch('ows_accounting.models.accounting_statement_export.dynamodb') def test_get_avro_item(mock_dynamodb_util): """Test _get_report_status. """ mock_table = MagicMock() mock_dynamodb_util.query_items.return_value = [] mock_dynamodb_util.get_table.return_value = mock_table model = accounting_statement_export.AccountingStatementExport() resp = model.get_avro_item('18805L', '205') assert resp.errors.get('message') == 'DOESNOTEXIST' assert resp.status == 400 mock_dynamodb_util.query_items.return_value = [{'status': 'GENERATED'}] resp = model.get_avro_item('20372L', '206') assert resp.message.get('status') == 'GENERATED' assert resp.status == 200 mock_dynamodb_util.query_items.assert_any_call( mock_table, 'user_id_type', '18805L', period_ids='205', file_type='AVRO') mock_dynamodb_util.query_items.assert_any_call( mock_table, 'user_id_type', '20372L', period_ids='206', file_type='AVRO')