"""Integration tests for ows-accounting.""" from decimal import Decimal, getcontext, ROUND_HALF_UP from tests.integration import conftest from tests.integration.conftest import label_7482, user_headers from tests.testutils.mysql.mysql_query_helper import MySQLQueryHelper from tests.testutils.period_helper import PeriodHelper def test_physical_reserves(current_accounting_period): """get_physical_reserves endpoint should return physical reserves data for a given period by vendor_id.""" test_user = label_7482() headers = user_headers( test_user['user_type'], test_user['id'], test_user['vend_contact_id']) ows_accounting_client = conftest.ows_accounting_api_client(headers) period_helper = PeriodHelper() params = {"vendor_id": test_user['id'], "period_id": current_accounting_period} results = ows_accounting_client.get_physical_reserves(params) assert results.status_code == 200, \ 'Result of call was {}, expected 200.'.format(results.status_code) json_results = results.json() data_to_show_on_ui = [] for result in json_results: if (int(result['period']) >= current_accounting_period): data_to_show_on_ui.append(result) for reserve in data_to_show_on_ui: getcontext().rounding = ROUND_HALF_UP period_id = period_helper.name_to_period_id( reserve['reserve_taken_date']) apply_to_period = period_helper.name_to_period_id( reserve['reserve_release_date']) db_expectations = MySQLQueryHelper.get_phys_reserves( test_user['id'], period_id) reserve_from_service = round(Decimal(reserve['amount']), 2) reserve_from_db = round(db_expectations['amount'], 2) assert reserve['currency'] == 'USD', \ 'Currency was {}, expected USD.'.format(reserve['currency']) assert db_expectations['currencies_id'] == 1, \ 'Currency was {}, expected 1.'.format( db_expectations['currencies_id']) assert apply_to_period == db_expectations['apply_to_period_id'], \ 'apply_to_period from ows-accounting [{}] didn\'t match ' \ 'value from DB [{}].'.format( apply_to_period, db_expectations['apply_to_period_id']) assert reserve_from_service == reserve_from_db, \ 'reserve value from ows-accounting [{}] didn\'t match ' \ 'value from DB [{}]'.format(reserve_from_service, reserve_from_db)