"""Test for Label Details model.""" import re from unittest.mock import MagicMock from oto import response from oto import status as http_status import pytest from prs.connectors.mysql import ar_db_session from prs.models import label_details from prs.utils import shared_utils from tests.testutils import db def _convert_query_params_to_sqlite(query, query_params): """Utility function to make a query it SQLite compatible.""" query = re.sub( 'FIND_IN_SET\(:country_id, territory_carve_out\)', '0', query) query = re.sub('FIND_IN_SET\(.*\)', '1', query) query = re.sub('NOW\(.*\)', "date('2017-10-19')", query) for param_name, param_value in query_params.items(): if isinstance(param_value, list): if param_value: if(type(param_value[0]) == tuple): param_value = [val[0] for val in param_value] param_value = '('+','.join(map(str, param_value))+')' else: param_value = '('+','.join(map(str, param_value))+')' query = re.sub(':'+param_name, str(param_value), query) return query @db.test_schema @pytest.mark.parametrize( 'description, country_id', [ ('Country id is 0', 0), ('Country id is 1', 1), ('Country id greater than 1', 3)]) def test_get_label_details_success(description, country_id, monkeypatch): """Test get_label_details with no errors.""" db.insert_to_table_raw( ar_db_session(), db.INSERT_INTO_TABLE_VW_LABEL_DETAIL) db.insert_to_table_raw( ar_db_session(), db.INSERT_INTO_TABLE_VW_ALL_VENDOR_CONTRACTS) db.insert_to_table_raw( ar_db_session(), db.INSERT_INTO_TABLE_VW_ACTIVE_VENDOR_CONTRACT) db.insert_to_table_raw( ar_db_session(), db.INSERT_INTO_TABLE_CURRENCIES) label_filter_args = { 'country_id': country_id, 'ignore_rome': 0, 'include_local_labels': 0, 'check_blacklist': 1, 'eligible_vendor_list': [547, 548]} label_query, label_data_args = label_details._generate_label_details_query( label_filter_args) label_query = _convert_query_params_to_sqlite(label_query, label_data_args) monkeypatch.setattr( shared_utils, 'log_info_to_loggly', MagicMock(return_value=response.Response())) monkeypatch.setattr( label_details, '_generate_label_details_query', MagicMock(return_value=[label_query, label_data_args])) label_response = label_details.get_label_details(label_filter_args) assert label_response.status == http_status.OK assert label_response.message