"""Lambda helpers unit tests.""" from decimal import Decimal from unittest.mock import MagicMock, patch import pandas from snapshot_contract import helpers from snapshot_contract.config import S3_BUCKET_SALES from snapshot_contract.constants import CONTRACT_RATES from snapshot_contract.constants import CONTRACT_TERM_SCHEMA def test_apply_contract_schema(mock_flat_contract_data): """Test applying schema to pandas dataframe of contract data.""" test_df = pandas.DataFrame(mock_flat_contract_data) typed_test_df = test_df.astype(CONTRACT_TERM_SCHEMA) res_df = helpers.apply_contract_schema(test_df) assert res_df.dtypes.to_string() == typed_test_df.dtypes.to_string() rate_values = [] for rate in CONTRACT_RATES: rate_values += list(res_df[rate].values) for val in rate_values: assert isinstance(val, Decimal) def test_build_snapshot_path(): """Test creating s3 path from accounting run detail.""" accounting_run = { 'accounting_period_id': 12, 'accounting_period_name': 'Period Name', 'accounting_run_id': 123, 'run_controller_name': 'RC Name' } acct_period_prefix = '12-period-name' acct_run_prefix = '123-rc-name' expected_path = \ f's3://{S3_BUCKET_SALES}/{acct_period_prefix}/{acct_run_prefix}/snapshots' res = helpers.build_snapshot_path(accounting_run) assert res == expected_path @patch('snapshot_contract.helpers.Country') def test_map_country_codes(mock_country): """Test country codes map to orchard currency_ids.""" country_codes = ['USA'] country_id = 1 mock_country_response = MagicMock() mock_country_response.orchard_id = country_id mock_country.return_value = mock_country_response res = helpers.map_country_codes(country_codes) assert set(res.keys()) == set(country_codes) assert res.get(country_codes[0]) == country_id mock_country.assert_called_once_with(country_codes[0]) def test_isrc_helper(mock_isrcs): """Test isrc helper parses isrc_df for isrc_id.""" isrc_df = pandas.DataFrame(mock_isrcs) isrc_helper = helpers.IsrcHelper(isrc_df) assert isrc_helper.find_isrc_id(mock_isrcs[0]['isrc']) == mock_isrcs[0]['isrcId'] assert isrc_helper.find_isrc_id(mock_isrcs[1]['isrc']) == mock_isrcs[1]['isrcId']