From 96658991f378fa3b90e1c2e05b38e9e485797306 Mon Sep 17 00:00:00 2001 From: Rihito Hashido Date: Thu, 20 Oct 2022 16:43:16 +0900 Subject: [PATCH] Internal change PiperOrigin-RevId: 482409723 --- shoptimizer_api/main.py | 18 ++++++++++++++++-- shoptimizer_api/main_test.py | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/shoptimizer_api/main.py b/shoptimizer_api/main.py index 174b7dc..3cff646 100644 --- a/shoptimizer_api/main.py +++ b/shoptimizer_api/main.py @@ -131,8 +131,8 @@ def optimize() -> Tuple[str, http.HTTPStatus]: 'condition_optimizer_config_override': flask.request.headers.get('condition_optimizer_config_override', ''), 'free_shipping_optimizer_config_override': - flask.request.headers.get( - 'free_shipping_optimizer_config_override', ''), + flask.request.headers.get('free_shipping_optimizer_config_override', + ''), 'gender_optimizer_config_override': flask.request.headers.get('gender_optimizer_config_override', ''), 'promo_text_removal_optimizer_config_override': @@ -163,6 +163,8 @@ def optimize() -> Tuple[str, http.HTTPStatus]: product_batch = flask.request.json + _sanitize_batch(product_batch) + optimized_product_batch, builtin_optimizer_results = _run_optimizers( product_batch, lang_url_parameter, country_url_parameter, currency_url_parameter, _builtin_optimizer_cache) @@ -240,6 +242,18 @@ def _check_request_valid(lang_url_parameter: str) -> (bool, str): return True, '' +def _sanitize_batch(product_batch: Dict[str, Any]) -> None: + """Cleans up the product batch. + + Args: + product_batch: A batch of product data. + """ + for entry in product_batch['entries']: + product = entry['product'] + for attribute in ('title', 'description'): + product[attribute] = str(product.get(attribute, '')) + + def _run_optimizers( product_batch: Dict[str, Any], language: str, diff --git a/shoptimizer_api/main_test.py b/shoptimizer_api/main_test.py index 31b1b58..2c48de6 100644 --- a/shoptimizer_api/main_test.py +++ b/shoptimizer_api/main_test.py @@ -75,6 +75,27 @@ def test_entries_with_no_list_of_products_returns_error(self): response_dict['error-msg']) self.assertEqual(http.HTTPStatus.BAD_REQUEST, response.status_code) + @parameterized.named_parameters([{ + 'testcase_name': 'title_is_numeric', + 'attribute': 'title' + }, { + 'testcase_name': 'description_is_numeric', + 'attribute': 'description' + }]) + def test_numeric_attribute_is_converted_to_string(self, attribute): + request_body = requests_bodies.build_request_body( + properties_to_be_updated={attribute: 1.234}) + + response = self.test_client.post( + f'{main._V1_BASE_URL}/batch/optimize', json=request_body) + + response_data = response.data.decode('utf-8') + response_dict = json.loads(response_data) + + self.assertIsInstance( + response_dict['optimized-data']['entries'][0]['product'][attribute], + str) + def test_unmapped_route_returns_404(self): response = self.test_client.get(f'{main._V1_BASE_URL}/unmapped-route') self.assertEqual(http.HTTPStatus.NOT_FOUND, response.status_code)