"""Term license model tests.""" from datetime import date from decimal import Decimal from unittest.mock import MagicMock from unittest.mock import Mock from unittest.mock import patch from api.models import term_license def apply_split_to_raw(term_licenses, split): """Return raw data with split applied.""" for term in term_licenses: term['value'] = Decimal(term['value']) * split for pay_date, payment in term['payments_schedule'].items(): term['payments_schedule'][pay_date] = (payment * split) return term_licenses @patch('api.models.term_license.distribution_fee') @patch('api.models.term_license.cms') def test_fetch_term_licenses( cms, distribution_fee, term_licenses_converted, term_licenses_converted_sorted_without_projected): """Test fetch and process term license convenience method.""" client = Mock() cms.get_client.return_value = client client.get_term_licenses.return_value = term_licenses_converted split = Decimal(0.725) distribution_fee.get_split_value.return_value = split results = term_license.fetch_term_licenses('123') expected_raw = apply_split_to_raw( term_licenses_converted_sorted_without_projected, split) expected = { 'actuals': {}, 'projections': { date(2017, 1, 1): (Decimal('2000') * split), date(2016, 11, 16): (Decimal('1000') * split)}, 'raw': expected_raw} assert results == expected @patch('api.models.term_license.distribution_fee') @patch('api.models.term_license.cms') def test_fetch_term_licenses_with_invalid_status( cms, distribution_fee, term_licenses_converted_sorted_without_projected, term_licenses_diff_status): """Test fetch_term_licenses when one of the item has diff status.""" client = Mock() client.get_term_licenses.return_value = term_licenses_diff_status cms.get_client.return_value = client split = Decimal(0.5) distribution_fee.get_split_value.return_value = split results = term_license.fetch_term_licenses('123') expected_raw = apply_split_to_raw( term_licenses_converted_sorted_without_projected, split) expected = { 'actuals': {}, 'projections': { date(2017, 1, 1): (Decimal('2000') * split), date(2016, 11, 16): (Decimal('1000') * split)}, 'raw': expected_raw} assert results == expected @patch('api.models.term_license.distribution_fee') @patch('api.models.term_license.cms') def test_fetch_term_licenses_with_closed_status( cms, distribution_fee, term_licenses_closed): """Test fetch_term_licenses when one of the item has diff status.""" client = Mock() client.get_term_licenses.return_value = term_licenses_closed cms.get_client.return_value = client split = Decimal(0.5) distribution_fee.get_split_value.return_value = split closed_buckets = { 'actuals': {}, 'projections': {date(2017, 1, 1): Decimal('100.0')}, 'raw': term_licenses_closed} results = term_license.fetch_term_licenses('123') assert results == closed_buckets @patch('api.models.term_license.distribution_fee') @patch('api.models.term_license.cms') def test_fetch_term_licenses_diff_splits( cms, distribution_fee, term_licenses_converted): """Test fetch_term_licenses with diff split values.""" client = Mock() client.get_term_licenses.return_value = term_licenses_converted cms.get_client.return_value = client split1 = Decimal(0.7) split2 = Decimal(0.4) distribution_fee.get_split_value.side_effect = [ split1, split2] results = term_license.fetch_term_licenses('123') expected = { 'actuals': {}, 'projections': { date(2017, 1, 1): (Decimal('2000') * split2), date(2016, 11, 16): (Decimal('1000') * split1)} } assert results['actuals'] == expected['actuals'] assert results['projections'] == expected['projections'] @patch('api.models.term_license.art_relations') def test_get_store_id_by_name(art_relations): """Test store name lookup function.""" store_name = 'Vooks' store_id = 123 mock_context = MagicMock() mock_cursor = MagicMock() mock_context.__enter__.return_value = (mock_cursor, None) art_relations.context.return_value = mock_context mock_cursor.fetchone.return_value = (store_id,) result = term_license.get_store_id_by_name(store_name) assert result == (store_id,) @patch('api.models.term_license.art_relations') def test_get_store_name_by_id(art_relations): """Test store ID lookup function.""" store_id = 123 store_name = 'Vooks' mock_context = MagicMock() mock_cursor = MagicMock() mock_context.__enter__.return_value = (mock_cursor, None) art_relations.context.return_value = mock_context mock_cursor.fetchone.return_value = (store_name,) result = term_license.get_store_id_by_name(store_id) assert result == (store_name,)