"""Test manual_adjustments model.""" from datetime import date from decimal import Decimal from unittest.mock import Mock from unittest.mock import patch import pytest from api.models import manual_adjustments @pytest.fixture def manual_adjustment_rows(): """Example ows-manual-adjustment api response. Returns: dict: mocked api response. """ value = {'manual_adjustments': [ { 'amount': '-120', 'apply_to_period': {'month': 1, 'year': 2017}, 'adjust_for_period': {'month': 2, 'year': 2017}, 'release_manual_adjustments': [ { 'amount': -50, 'release_id': 12345678, 'category': {'category': 'This'}}, { 'amount': -50, 'release_id': 12345678, 'category': {'category': 'That'}}, ], 'amount_in_original_currency': '-100'}, { 'amount': '-200', 'apply_to_period': {'month': 2, 'year': 2017}, 'adjust_for_period': {'month': 2, 'year': 2017}, 'release_manual_adjustments': [ { 'amount': -50, 'release_id': 12345678, 'category': {'category': 'This'}}, { 'amount': -50, 'release_id': 12345678, 'category': {'category': 'That'}}, { 'amount': -50, 'release_id': 12345678, 'category': {'category': 'Third'}}, { 'amount': -50, 'release_id': 12345678, 'category': {'category': 'Fourth'}}, ], 'amount_in_original_currency': '-200'}, { 'amount': '-350', 'apply_to_period': {'month': 3, 'year': 2017}, 'adjust_for_period': {'month': 4, 'year': 2017}, 'release_manual_adjustments': [ { 'amount': -220, 'release_id': 12345678, 'category': {'category': 'This'}}, { 'amount': -80, 'release_id': 12345678, 'category': {'category': 'That'}}, ], 'amount_in_original_currency': '-300'}, { 'amount': '-350', 'apply_to_period': {'month': 3, 'year': 2017}, 'adjust_for_period': {'month': 2, 'year': 2017}, 'release_manual_adjustments': [ { 'amount': -220, 'release_id': 87657654, 'category': {'category': 'This'}}, { 'amount': -80, 'release_id': 87657654, 'category': {'category': 'That'}}, ], 'amount_in_original_currency': '-300'}, { 'amount': '-400', 'apply_to_period': {'month': 4, 'year': 2017}, 'adjust_for_period': {'month': 4, 'year': 2017}, 'release_manual_adjustments': [ { 'amount': -150, 'release_id': 12345678, 'category': {'category': 'This'}}, { 'amount': -50, 'release_id': 12345678, 'category': {'category': 'That'}}, { 'amount': -50, 'release_id': 12345678, 'category': {'category': 'Foo'}}, { 'amount': -150, 'release_id': 12345678, 'category': {'category': 'Bar'}} ], 'amount_in_original_currency': '-400'}, { 'amount': '-100', 'category': { 'category': 'Another Vendor Level' }, 'apply_to_period': {'month': 1, 'year': 2017}, 'adjust_for_period': {'month': 1, 'year': 2017}, 'release_manual_adjustments': [ ], 'amount_in_original_currency': '-100'}, { 'amount': '-100', 'category': { 'category': 'Non-Recoupable Bonus' }, 'apply_to_period': {'month': 2, 'year': 2017}, 'adjust_for_period': {'month': 1, 'year': 2017}, 'release_manual_adjustments': [ ], 'amount_in_original_currency': '-150'} ], 'pagination': { 'count': 7, 'limit': 50 }} return value @pytest.fixture def manual_adjustment_rows_empty(): """Example ows-manual-adjustment api response. Returns: dict: mocked api response. """ value = { 'manual_adjustments': [], 'pagination': { 'count': 0, 'limit': 50}} return value @pytest.fixture def manual_adjustment_rma(): """Release level data from manual_adjustment_rows fixture. Returns: dict: category: {date: Decimal} of release manual adjustments. """ value = { 'release': { 'foo': { date(2017, 4, 1): Decimal(-50.000000), }, 'bar': { date(2017, 4, 1): Decimal(-150.000000), }, 'this': { date(2017, 2, 1): Decimal(-110.000000), date(2017, 4, 1): round(Decimal(-406.666667), 6), }, 'that': { date(2017, 2, 1): Decimal(-110.000000), date(2017, 4, 1): round(Decimal(-143.333333), 6), }, 'third': { date(2017, 2, 1): Decimal(-50.000000), }, 'fourth': { date(2017, 2, 1): Decimal(-50.000000), }, 'another_vendor_level': { date(2017, 1, 1): Decimal(-100.000000), }, 'non_recoupable_bonus': { date(2017, 1, 1): Decimal(-100.000000), } }, 'labels': { 'advances': 'Advances', 'this': 'This', 'that': 'That', 'third': 'Third', 'fourth': 'Fourth', 'foo': 'Foo', 'bar': 'Bar', 'non_recoupable_bonus': 'Non-Recoupable Bonus', 'another_vendor_level': 'Another Vendor Level' } } return value @patch('api.models.manual_adjustments.manual_adjustment.get') def test_fetch_expenses(ma_get, manual_adjustment_rows, manual_adjustment_rows_empty, manual_adjustment_rma): """Test fetch_expense function.""" vendor_id = 54321 release_id = 12345678 response = Mock() response.json.return_value = manual_adjustment_rows response2 = Mock() response2.json.return_value = manual_adjustment_rows_empty ma_get.side_effect = [response, response2] expected = manual_adjustment_rma results = manual_adjustments.fetch_manual_adjustments( vendor_id, release_id) assert results == expected