"""Manual Adjustment persister model tests.""" import copy import io from _pytest.monkeypatch import MonkeyPatch import boto3 from moto import mock_aws import pytest from sqlalchemy import text from werkzeug.datastructures import FileStorage from manualadjustment import config from manualadjustment import constants from manualadjustment.models import manual_adj_persister as persister from manualadjustment.models.manual_adjustment import ManualAdjustment from manualadjustment.presentation import adjustment_dict from tests.fixtures import flask_global from utils import db_ops from utils.tables import manual_adjustment as ma_data from utils.tables.currencies import data as currency_data 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() @pytest.fixture def expected_for_insert(): """Insert params fixture.""" return { 'amount': 100.00, 'category_id': 2, 'comment': 'this is a test', 'adjust_for_period_id': 99, 'apply_to_period_id': 99, 'parent_id': 5, 'parent_type': 'vendor', 'created_by': 156} @pytest.fixture def attachment_text(): """Text fixture.""" return 'i heart manual adjustments' @pytest.fixture def attachment(attachment_text): """Attachment fixture.""" return FileStorage(io.StringIO(attachment_text)) @pytest.fixture def update_params(): """Update params fixture.""" return { 'parent_id': 12345, 'created_by': 123, 'amount': 1234.56, 'comment': 'Adjusted adjustment', 'category_id': 54, 'adjust_for_period_id': 183, 'apply_to_period_id': 183, 'currencies_id': 2, 'amount_in_original_currency': 2345.67} def test_get_manual_adjustment(): """Test manual adjustment get.""" db_ops.create_tables() db_ops.seed_art_relations() res1, page_count = persister.get_manual_adj_from_db( parent_id=16031, parent_type='vendor', apply_to_period_id=156, category_id=0, page_offset=constants.PAGE_OFFSET_DEFAULT, page_limit=constants.PAGE_LIMIT_DEFAULT) res2, page_count = persister.get_manual_adj_from_db( parent_id=8914, parent_type='vendor', apply_to_period_id=None, category_id=None, page_offset=constants.PAGE_OFFSET_DEFAULT, page_limit=constants.PAGE_LIMIT_DEFAULT) results = [ adjustment_dict(res1[0]), adjustment_dict(res2[0])] test_data = ma_data.data for result, expected in zip(results, test_data): compare_results(result, expected) @mock_aws def test_insert(expected_for_insert): """Test insert.""" sqs_conn = boto3.client('sqs', region_name=config.AWS_REGION) sqs_conn.create_queue(QueueName=config.queue_name) db_ops.create_tables() params = copy.copy(expected_for_insert) params['attachment_location'] = None params['amount_in_original_currency'] = 123.45 params['currencies_id'] = currency_data[0]['id'] adjustment = persister.insert(**params) for param_name in params: model_value = getattr(adjustment, param_name) assert model_value == params[param_name] newest = db_ops.ar_session.query( ManualAdjustment).order_by(text('id DESC')).first() assert newest.id == adjustment.id @mock_aws def test_update_modifies_table_row(update_params): """Test update.""" sqs_conn = boto3.client('sqs', region_name=config.AWS_REGION) sqs_conn.create_queue(QueueName=config.queue_name) db_ops.create_tables() db_ops.seed_art_relations() session = db_ops.ar_session first_one = session.query(ManualAdjustment).first() persister.update(first_one.id, update_params) session.refresh(first_one) for param_name in update_params: assert getattr(first_one, param_name) == update_params[param_name] def test_update_handles_nonexistent_id(update_params): """Test update with bad ids.""" db_ops.create_tables() manual_adj = persister.update(1234567, update_params) assert manual_adj is None def compare_results(first_result, second_result): """Compare results.""" skip_keys = { 'id', '_sa_instance_state', 'date_added', 'adjust_for_period', 'apply_to_period', 'release_manual_adjustments'} for key, value in first_result.items(): if key in skip_keys: continue assert second_result[key] == value