"""Tests for Revenue model.""" import datetime import json from unittest import mock import pytest from ows_accounting.models import passthrough @pytest.mark.parametrize( 'cache_prefix, account_id, period_id, expected_key', [ ('test', 1234, '4242', 'test:1234:4242'), ('eq_bonus', 4321, '12344', 'eq_bonus:4321:12344')]) def test_get_key(cache_prefix, account_id, period_id, expected_key): """Test key generation for saving revenue data.""" key = passthrough._get_key(cache_prefix, account_id, period_id) assert key == expected_key @mock.patch('ows_accounting.models.passthrough.time') def test_get_created_time(time): """Test get_created_tiem utility function.""" expected_time = '20180709181448' time.strftime.return_value = '20180709181448' assert passthrough._get_created_time() == expected_time @pytest.mark.parametrize('account_id', [1234, 4321]) @mock.patch('ows_accounting.models.passthrough.json') @mock.patch('ows_accounting.models.passthrough._get_key') @mock.patch('ows_accounting.models.passthrough.redis') def test_get_from_cache(redis, _get_key, json, account_id): """Test get_from_cache function.""" data = {'test': 'value'} period_id = '4242' key = 'test key' cache_prefix = 'test_prefix' json.loads.return_value = data data_mock = mock.MagicMock() redis.client.get.return_value = data_mock _get_key.return_value = key result = passthrough.get_from_cache(cache_prefix, account_id, period_id) assert result == data _get_key.assert_called_once_with(cache_prefix, account_id, period_id) redis.client.get.assert_called_once_with(key) data_mock.decode.assert_called_once_with('utf8') @mock.patch('ows_accounting.models.passthrough._get_key') @mock.patch('ows_accounting.models.passthrough.redis') def test_get_from_cache_no_data(redis, _get_key): """Test get_from_cache function.""" redis.client.get.return_value = None assert passthrough.get_from_cache('prefix', '1234', '4242') == {} @mock.patch('ows_accounting.models.passthrough.g') @mock.patch('ows_accounting.models.passthrough._get_key') @mock.patch('ows_accounting.models.passthrough.redis') def test_save_to_cache(redis, _get_key, g, app_context): """Test save_to_cache funciton.""" data = {'test': 'value'} key = mock.MagicMock() cache_prefix = 'test_prefix' _get_key.return_value = key account_id = '54321' period_id = '4242' passthrough.save_to_cache(cache_prefix, account_id, period_id, data) redis.client.set.assert_called_once_with( key, json.dumps(data)) _get_key.assert_called_once_with(cache_prefix, account_id, period_id) assert g.log.error.call_count == 0 @mock.patch('ows_accounting.models.passthrough.g') @mock.patch('ows_accounting.models.passthrough._get_key') @mock.patch('ows_accounting.models.passthrough.redis') def test_save_to_cache_logs_error(redis, _get_key, g, app_context): """Ensure that redis errors are logged.""" redis.client.set.return_value = None period_id = '4141' passthrough.save_to_cache('prefix', '123', period_id, {'test': 'value'}) assert g.log.error.call_count == 1 # @mock.patch('ows_accounting.models.passthrough.save_to_cache') # @mock.patch('ows_accounting.models.passthrough.snowflake') # @mock.patch('ows_accounting.models.passthrough.get_from_cache') # def test_get_account_eq_bonus_returns_cached( # get_from_cache, snowflake, save_to_cache): # """Test get_account_eq_bonus utility function.""" # expected_value = 'cached' # get_from_cache.return_value = expected_value # account_id = '123' # period_id = '42' # result = passthrough.get_account_eq_bonus(account_id, period_id) # get_from_cache.assert_called_once_with( # 'eq_bonus', account_id, period_id) # assert result == expected_value # assert save_to_cache.call_count == 0 # assert snowflake.db_session.call_count == 0 @mock.patch( 'ows_accounting.models.passthrough.get_currency_symbol_and_exchange_rate') @mock.patch('ows_accounting.models.passthrough.passthrough') @mock.patch('ows_accounting.models.passthrough.save_to_cache') @mock.patch('ows_accounting.models.passthrough.snowflake') @mock.patch('ows_accounting.models.passthrough.get_from_cache') def test_get_account_eq_bonus( get_from_cache, snowflake, save_to_cache, sql, get_currency_symbol_and_exchange_rate): """Test get_account_eq_bonus utility function.""" get_from_cache.return_value = None period_id = '4242' sql.SQL_GET_EQ_AGGREGATED = 'sql' sql.SQL_get_currency_symbol_and_exchange_rate = 'currency sql' account_id = '123' currency_symbol = 'USD' exchange_rate = 1 get_currency_symbol_and_exchange_rate.return_value = { 'currency_html_symbol': currency_symbol, 'exchange_rate': exchange_rate } eq_bonus_fetch_result = (123.225, 122.225) expected_result = { 'eq_bonus': 123.22, 'usd_eq_bonus': 122.22, 'currency_html_symbol': currency_symbol} mock_session = mock.MagicMock() mock_session.__enter__.return_value = mock_session mock_execute = mock.MagicMock() mock_execute.fetchone.return_value = eq_bonus_fetch_result mock_session.execute.return_value = mock_execute snowflake.db_session.return_value = mock_session result = passthrough.get_account_eq_bonus(account_id, period_id) assert result == expected_result # get_from_cache.assert_called_once_with( # 'eq_bonus', account_id, period_id) mock_session.execute.assert_has_calls([ mock.call('sql', {'vendor_id': account_id, 'period_id': period_id}), ], any_order=True) get_currency_symbol_and_exchange_rate.assert_called_once_with( account_id, period_id) # save_to_cache.assert_called_once_with( # 'eq_bonus', account_id, period_id, expected_result) # @mock.patch('ows_accounting.models.passthrough.save_to_cache') # @mock.patch('ows_accounting.models.passthrough.mysql') # @mock.patch('ows_accounting.models.passthrough.get_from_cache') # def test_get_account_eq_payment_details_returns_cached( # get_from_cache, mysql, save_to_cache): # """Test get_account_eq_bonus utility function.""" # expected_value = 'cached' # get_from_cache.return_value = expected_value # account_id = '123' # period_id = '42' # result = passthrough.get_account_eq_payment_details( # account_id, period_id) # get_from_cache.assert_called_once_with( # 'eq_payment', account_id, period_id) # assert result == expected_value # assert save_to_cache.call_count == 0 # assert mysql.db_session.call_count == 0 @mock.patch( 'ows_accounting.models.passthrough.get_currency_symbol_and_exchange_rate') @mock.patch('ows_accounting.models.passthrough.passthrough') @mock.patch('ows_accounting.models.passthrough.save_to_cache') @mock.patch('ows_accounting.models.passthrough.mysql') @mock.patch('ows_accounting.models.passthrough.get_from_cache') def test_get_account_eq_payment_details( get_from_cache, mysql, save_to_cache, sql, get_currency_symbol_and_exchange_rate): """Test get_account_eq_bonus utility function.""" get_from_cache.return_value = None period_id = '4242' sql.SQL_GET_EQ_CHECKS_PAID = 'sql' account_id = '123' # rounded ROUND_HALF_EVEN, so 123.226 would be rounded to 123.23 # but in this case 123.225 will be rounded to 123.22 # also note that check_no is built using 'Spotify Equity Payment' # prefix and the actual database check_no field. fetch_result = [( 123.225, 'SP PAYOUT', datetime.datetime(2018, 1, 15), 'Check comments')] expected_result = [ { 'amount': 246.45, 'check_no': 'SP PAYOUT', 'currency_html_symbol': 'EUR', 'date': '01/15/2018', 'comments': 'Check comments', }] mock_session = mock.MagicMock() mock_session.__enter__.return_value = mock_session mock_execute = mock.MagicMock() mock_execute.fetchall.return_value = fetch_result mock_session.execute.return_value = mock_execute mysql.db_session.return_value = mock_session currency_symbol = 'EUR' exchange_rate = 2 get_currency_symbol_and_exchange_rate.return_value = { 'currency_html_symbol': currency_symbol, 'exchange_rate': exchange_rate } result = passthrough.get_account_eq_payment_details( account_id, period_id) assert result == expected_result # get_from_cache.assert_called_once_with( # 'eq_payment', account_id, period_id) mock_session.execute.assert_called_once_with( 'sql', {'vendor_id': account_id, 'period_id': period_id}) # save_to_cache.assert_called_once_with( # 'eq_payment', account_id, period_id, expected_result) get_currency_symbol_and_exchange_rate.assert_called_once_with( account_id, period_id) @mock.patch('ows_accounting.models.passthrough.passthrough') @mock.patch('ows_accounting.models.passthrough.snowflake') def test_get_currency_symbol_and_exchange_rate(snowflake, sql): """Test get_currency_symbol_and_exchange_rate utility function.""" account_id = '4242' period_id = '321' currency_sql = 'currency and exchange rate' currency_symbol_sql = 'currency symbol' sql.SQL_GET_CURRENCY_ID_AND_EXCHANGE_RATE = currency_sql sql.SQL_GET_CURRENCY_SYMBOL = currency_symbol_sql expected_currency_id = 42 expected_currency_symbol = 'test' expected_exchange_rate = 42.0 fetch_results = [ (expected_currency_id, expected_exchange_rate), (expected_currency_symbol,) ] mock_session = mock.MagicMock() mock_session.__enter__.return_value = mock_session mock_execute = mock.MagicMock() mock_execute.fetchone.side_effect = fetch_results mock_session.execute.return_value = mock_execute snowflake.db_session.return_value = mock_session expected_result = { 'currency_html_symbol': expected_currency_symbol, 'exchange_rate': expected_exchange_rate} result = passthrough.get_currency_symbol_and_exchange_rate( account_id, period_id) assert result == expected_result expected_calls = [ mock.call( currency_sql, {'vendor_id': account_id, 'period_id': period_id}), mock.call().fetchone(), mock.call( currency_symbol_sql, {'currency_id': expected_currency_id}), mock.call().fetchone(), ] mock_session.execute.assert_has_calls(expected_calls)