"""Manual Adjustment logic tests.""" from unittest.mock import MagicMock from _pytest.monkeypatch import MonkeyPatch import boto3 from moto import mock_aws import pytest import validictory from manualadjustment import config from manualadjustment.logic import manual_adjustment as man_adj_logic from manualadjustment.models import manual_adj_persister as persister from manualadjustment.models.manual_adjustment import ManualAdjustment from tests.fixtures import flask_global from utils import db_ops from utils.tables.currencies import data as currency_data from utils.tables.manual_adjustment_category import data as category_data from utils.tables.orchadmin_users import data as user_data from utils.tables.period import data as period_data from utils.tables.vendor import data as vendor_data vendor_id = vendor_data[0].get('vendor_id') period_id = period_data[0].get('period_id') category_id = category_data[0].get('category_id') parent_id = vendor_data[0].get('vendor_id') user_id = user_data[0].get('id') currency_id = currency_data[0].get('id') @pytest.fixture def adj_params(): """Fixture for adjustment parameters.""" return { 'parent_type': 'vendor', 'parent_id': vendor_id, 'created_by': user_id, 'amount': 1234.56, 'comment': 'Awesome adjustment', 'category_id': category_id, 'adjust_for_period_id': period_id, 'apply_to_period_id': period_id, 'currencies_id': currency_id, 'amount_in_original_currency': 2345.67} monkeypatch = MonkeyPatch() def setup_function(function): """Set up the function. Invoked for every test function. Args: function (callable): the function that will run the test. """ flask_global.mock(monkeypatch) def teardown_function(function): """Teardown the function. Invoked everytime a test function has completed. Args: function (callable): the function that has run. """ monkeypatch.undo() @mock_aws def test_create_with_no_attachment(monkeypatch, adj_params): """Test creating an MA without an attachment.""" conn = boto3.client('sqs', region_name=config.AWS_REGION) conn.create_queue(QueueName=config.queue_name) db_ops.create_tables() db_ops.seed_art_relations() result = man_adj_logic.create(attachment=None, request_data=adj_params) assert result.data.attachment_location is None def test_update_success_result(monkeypatch, adj_params): """Test a successful update.""" adjustment_id = 123 monkeypatch.setattr(validictory, 'validate', MagicMock(return_value=True)) check_ids = MagicMock(return_value=(True, None)) monkeypatch.setattr(man_adj_logic, '_check_existence_of_ids', check_ids) mock_model = ManualAdjustment(**adj_params) mock_model.id = adjustment_id mock_update = MagicMock(return_value=mock_model) monkeypatch.setattr(persister, 'update', mock_update) result = man_adj_logic.update(adjustment_id, adj_params) assert result.status == 200 assert result.data.id == adjustment_id for param_name in adj_params: assert getattr(result.data, param_name) == adj_params[param_name] def test_update_with_nonexistent_id(monkeypatch, adj_params): """Test an update with a bad id.""" bad_id = 1234567890 db_ops.create_tables() validate = MagicMock(return_value=True) monkeypatch.setattr(validictory, 'validate', validate) check_ids = MagicMock(return_value=(True, None)) monkeypatch.setattr(man_adj_logic, '_check_existence_of_ids', check_ids) result = man_adj_logic.update(bad_id, adj_params) assert result.status == 404 assert result.data is None assert result.error == 'Manual adjustment not found' assert result.error_detail == bad_id