Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 482409723
  • Loading branch information
rihito0907 authored and starmandeluxe committed Oct 20, 2022
1 parent 00d10ab commit 9665899
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
18 changes: 16 additions & 2 deletions shoptimizer_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions shoptimizer_api/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9665899

Please sign in to comment.