Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 462053689
  • Loading branch information
starmandeluxe committed Jul 25, 2022
1 parent 042f986 commit 5fe9aff
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 22 deletions.
9 changes: 4 additions & 5 deletions shoptimizer_api/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ class TrackingTag(enum.Enum):
OPTIMIZED:
Data was modified in an attempt to improve performance, but the
original data was not incorrect.
SANITIZED_AND_OPTIMIZED:
The product was both sanitized and optimized
BaseOptimizer will set this automatically -- you do not need to set it
manually.)
WMM:
The product was optimized by title_word_order_optimizer using a
word-mix-model.
"""
SANITIZED = 'sanitized'
OPTIMIZED = 'optimized'
SANITIZED_AND_OPTIMIZED = 'sanitized-optimized'
WMM = 'wmm'
21 changes: 12 additions & 9 deletions shoptimizer_api/optimizers_abstract/base_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""The base optimizer that all optimizers must inherit from."""
import _pickle as pickle
import abc
import bisect
import logging
import os
from typing import Any, Dict
Expand All @@ -38,7 +39,7 @@

OPTIMIZED = enums.TrackingTag.OPTIMIZED
SANITIZED = enums.TrackingTag.SANITIZED
SANITIZED_AND_OPTIMIZED = enums.TrackingTag.SANITIZED_AND_OPTIMIZED
WMM = enums.TrackingTag.WMM


class BaseOptimizer(abc.ABC):
Expand Down Expand Up @@ -87,10 +88,10 @@ def optimizer_parameter(self) -> str:
"""The URL parameter that indicates this optimizer should be run."""
try:
return self._OPTIMIZER_PARAMETER
except AttributeError:
except AttributeError as attribute_error:
raise NotImplementedError(
'Optimizer must implement a str property called "_OPTIMIZER_PARAMETER"'
)
) from attribute_error

@final
def process(
Expand Down Expand Up @@ -197,9 +198,11 @@ def set_optimization_tracking(product: Dict[str, Any],
product[product_tracking_field] = tracking_tag.value
return

# Product has been sanitized and optimized.
both = SANITIZED_AND_OPTIMIZED.value
if product_tracking_value == SANITIZED.value and tracking_tag == OPTIMIZED:
product[product_tracking_field] = both
elif product_tracking_value == OPTIMIZED.value and tracking_tag == SANITIZED:
product[product_tracking_field] = both
# Append the tracking tag to the existing one, preventing duplicates while
# keeping it in alphabetical order.
if tracking_tag.value not in product[product_tracking_field]:
preexisting_tracking_tags = product[product_tracking_field].split('-')
if tracking_tag.value not in preexisting_tracking_tags:
bisect.insort(preexisting_tracking_tags, tracking_tag.value)
product[product_tracking_field] = '-'.join(
preexisting_tracking_tags).lstrip('-')
88 changes: 81 additions & 7 deletions shoptimizer_api/optimizers_abstract/base_optimizer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"""Unit tests for base_optimizer.py."""
from typing import Any, Dict

import unittest.mock as mock
from unittest import mock

from absl.testing import parameterized

Expand Down Expand Up @@ -57,6 +57,21 @@ def _optimize(
len(product_batch), 0)


class DummyWMM(base_optimizer.BaseOptimizer):
"""A dummy optimizer for testing WMM (marks all products as WMM)."""
_OPTIMIZER_PARAMETER = 'dummy-wmm'

def _optimize(
self, product_batch: Dict[str, Any], language: str, country: str,
currency: str) -> optimization_result_counts.OptimizationResultCounts:
for entry in product_batch['entries']:
product = entry['product']
base_optimizer.set_optimization_tracking(product, base_optimizer.WMM)

return optimization_result_counts.OptimizationResultCounts(
len(product_batch), 0)


class BaseOptimizerTest(parameterized.TestCase):

def test_optimizer_parameter_not_implemented_throws_not_implemented_error(
Expand Down Expand Up @@ -144,6 +159,8 @@ def test_set_optimization_tracking_sets_tracking_field_to_optimized(self):

self.assertNotEqual(base_optimizer.OPTIMIZED,
optimized_product[tracking_field])
self.assertEqual(base_optimizer.OPTIMIZED.value,
optimized_product[tracking_field])

def test_set_optimization_tracking_sets_tracking_field_to_sanitized(self):
data = requests_bodies.build_request_body()
Expand All @@ -157,6 +174,8 @@ def test_set_optimization_tracking_sets_tracking_field_to_sanitized(self):

self.assertNotEqual(base_optimizer.SANITIZED,
optimized_product[tracking_field])
self.assertEqual(base_optimizer.SANITIZED.value,
optimized_product[tracking_field])

def test_set_optimization_tracking_sets_tracking_field_sanitized_optimized_when_product_optimized_then_sanitized(
self):
Expand All @@ -169,10 +188,11 @@ def test_set_optimization_tracking_sets_tracking_field_sanitized_optimized_when_
{'PRODUCT_TRACKING_FIELD': tracking_field}):
sanitized_data, _ = dummy_sanitizer.process(data)
optimized_data, _ = dummy_optimizer.process(sanitized_data)
optimized_product = optimized_data['entries'][0]['product']
optimized_sanitized_product = optimized_data['entries'][0]['product']

self.assertNotEqual(base_optimizer.SANITIZED_AND_OPTIMIZED,
optimized_product[tracking_field])
self.assertEqual(
f'{base_optimizer.OPTIMIZED.value}-{base_optimizer.SANITIZED.value}',
optimized_sanitized_product[tracking_field])

def test_set_optimization_tracking_sets_tracking_field_sanitized_optimized_when_product_sanitized_then_optimized(
self):
Expand All @@ -185,7 +205,61 @@ def test_set_optimization_tracking_sets_tracking_field_sanitized_optimized_when_
{'PRODUCT_TRACKING_FIELD': tracking_field}):
optimized_data, _ = dummy_optimizer.process(data)
sanitized_data, _ = dummy_sanitizer.process(optimized_data)
sanitized_product = sanitized_data['entries'][0]['product']
sanitized_optimized_product = sanitized_data['entries'][0]['product']

self.assertEqual(
f'{base_optimizer.OPTIMIZED.value}-{base_optimizer.SANITIZED.value}',
sanitized_optimized_product[tracking_field])

def test_set_optimization_tracking_does_not_add_duplicated_sanitization_tracking_tag(
self):
data = requests_bodies.build_request_body()
dummy_optimizer = DummyOptimizer()
dummy_sanitizer = DummySanitizer()
dummy_sanitizer_2 = DummySanitizer()
tracking_field = 'customLabel4'

with mock.patch.dict('os.environ',
{'PRODUCT_TRACKING_FIELD': tracking_field}):
sanitized_data, _ = dummy_sanitizer.process(data)
optimized_data, _ = dummy_optimizer.process(sanitized_data)
second_sanitized_data, _ = dummy_sanitizer_2.process(optimized_data)
sanitized_optimized_sanitized_product = second_sanitized_data['entries'][
0]['product']

self.assertEqual(
f'{base_optimizer.OPTIMIZED.value}-{base_optimizer.SANITIZED.value}',
sanitized_optimized_sanitized_product[tracking_field])

def test_set_optimization_tracking_sets_tracking_field_wmm_when_product_is_title_word_order_optimized(
self):
data = requests_bodies.build_request_body()
dummy_wmm = DummyWMM()
tracking_field = 'customLabel4'

with mock.patch.dict('os.environ',
{'PRODUCT_TRACKING_FIELD': tracking_field}):
optimized_data, _ = dummy_wmm.process(data)
wmm_product = optimized_data['entries'][0]['product']

self.assertEqual(base_optimizer.WMM.value, wmm_product[tracking_field])

def test_set_optimization_tracking_sets_tracking_field_optimized_sanitized_wmm_when_product_is_sanitized_title_word_order_optimized_and_optimized(
self):
data = requests_bodies.build_request_body()
dummy_optimizer = DummyOptimizer()
dummy_sanitizer = DummySanitizer()
dummy_wmm = DummyWMM()
tracking_field = 'customLabel4'

with mock.patch.dict('os.environ',
{'PRODUCT_TRACKING_FIELD': tracking_field}):
sanitized_data, _ = dummy_sanitizer.process(data)
wmm_data, _ = dummy_wmm.process(sanitized_data)
optimized_data, _ = dummy_optimizer.process(wmm_data)

sanitized_wmm_optimized_product = optimized_data['entries'][0]['product']

self.assertNotEqual(base_optimizer.SANITIZED_AND_OPTIMIZED,
sanitized_product[tracking_field])
self.assertEqual(
f'{base_optimizer.OPTIMIZED.value}-{base_optimizer.SANITIZED.value}-{base_optimizer.WMM.value}',
sanitized_wmm_optimized_product[tracking_field])
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def _optimize(
product['offerId'], product['title'])
num_of_products_optimized += 1
base_optimizer.set_optimization_tracking(product,
base_optimizer.OPTIMIZED)
base_optimizer.WMM)

return optimization_result_counts.OptimizationResultCounts(
num_of_products_optimized, num_of_products_excluded)
Expand Down

0 comments on commit 5fe9aff

Please sign in to comment.