github-actions[bot] commented on code in PR #40916:
URL: https://github.com/apache/beam/pull/40916#discussion_r4469520797


##########
sdks/python/apache_beam/yaml/yaml_enrichment_test.py:
##########
@@ -47,30 +56,108 @@ def test_enrichment_with_bigquery(self):
         Row(label="item1", rank=0),
         Row(label="item2", rank=1),
     ]
-
-    handler = 'BigQuery'
     config = {
         "project": "apache-beam-testing",
         "table_name": "project.database.table",
         "row_restriction_template": "label='item1' or label='item2'",
-        "fields": ["label"]
+        "fields": ["label"],
     }
 
-    with beam.Pipeline() as p:
-      with mock.patch('apache_beam.yaml.yaml_enrichment.enrichment_transform',
-                      FakeEnrichmentTransform(enrichment_handler=handler,
-                                              handler_config=config)):
-        # Force a reload to respect our mock.
-        yaml_provider.standard_providers.cache_clear()
-        input_pcoll = p | 'CreateInput' >> beam.Create(input_data)
-        result = input_pcoll | YamlTransform(
-            f'''
-                    type: Enrichment
-                    config:
-                        enrichment_handler: {handler}
-                        handler_config: {config}
-                    ''')
-        assert_that(result, equal_to(input_data))
+    captured = {}
+
+    def make_handler(**kwargs):
+      captured['kwargs'] = kwargs
+      return _FakeHandler(**kwargs)
+
+    with (mock.patch.object(yaml_enrichment, 'Enrichment', _FakeEnrichment),
+          mock.patch.object(yaml_enrichment,
+                            'BigQueryEnrichmentHandler',
+                            side_effect=make_handler)):
+      with beam.Pipeline(options=_enrichment_options()) as p:
+        result = (
+            p
+            | 'CreateInput' >> beam.Create(input_data)
+            | YamlTransform(
+                f'''
+                type: Enrichment
+                config:
+                  enrichment_handler: BigQuery
+                  handler_config: {config}
+                '''))
+        assert_that(
+            result,
+            equal_to([
+                Row(label="item1", rank=0, enriched_timeout=30),
+                Row(label="item2", rank=1, enriched_timeout=30),
+            ]))
+    self.assertEqual(captured['kwargs'], config)
+
+  def test_enrichment_other_handlers_and_timeout(self):
+    for handler_name, attr_name in [
+        ('BigTable', 'BigTableEnrichmentHandler'),
+        ('VertexAIFeatureStore', 'VertexAIFeatureStoreEnrichmentHandler'),
+        ('FeastFeatureStore', 'FeastFeatureStoreEnrichmentHandler'),
+    ]:
+      with self.subTest(handler=handler_name):
+        with (mock.patch.object(yaml_enrichment, 'Enrichment', 
_FakeEnrichment),
+              mock.patch.object(yaml_enrichment, attr_name, _FakeHandler) as

Review Comment:
   **🟡 Medium**: When passing a replacement object (`_FakeHandler`) as the 
third positional argument (`new`) to `mock.patch.object`, `mock_cls` is bound 
to `_FakeHandler` itself rather than a `MagicMock`. Furthermore, asserting 
`self.assertIs(yaml_enrichment.__dict__[attr_name], mock_cls)` on line 114 
while still inside the `with mock.patch.object(...)` block only checks that 
`mock.patch.object` patched the module attribute—it would pass even if 
`enrichment_transform` instantiated a completely different handler.
   
   If you use `side_effect=_FakeHandler` (or `wraps=_FakeHandler`), `mock_cls` 
will be a `MagicMock` and you can assert 
`mock_cls.assert_called_once_with(row_key='id')` on line 114 to verify that the 
expected handler class was actually invoked with `handler_config`.
   
   ```suggestion
                 mock.patch.object(yaml_enrichment, attr_name, 
side_effect=_FakeHandler) as
   ```



##########
sdks/python/apache_beam/yaml/yaml_enrichment_test.py:
##########
@@ -47,30 +56,108 @@ def test_enrichment_with_bigquery(self):
         Row(label="item1", rank=0),
         Row(label="item2", rank=1),
     ]
-
-    handler = 'BigQuery'
     config = {
         "project": "apache-beam-testing",
         "table_name": "project.database.table",
         "row_restriction_template": "label='item1' or label='item2'",
-        "fields": ["label"]
+        "fields": ["label"],
     }
 
-    with beam.Pipeline() as p:
-      with mock.patch('apache_beam.yaml.yaml_enrichment.enrichment_transform',
-                      FakeEnrichmentTransform(enrichment_handler=handler,
-                                              handler_config=config)):
-        # Force a reload to respect our mock.
-        yaml_provider.standard_providers.cache_clear()
-        input_pcoll = p | 'CreateInput' >> beam.Create(input_data)
-        result = input_pcoll | YamlTransform(
-            f'''
-                    type: Enrichment
-                    config:
-                        enrichment_handler: {handler}
-                        handler_config: {config}
-                    ''')
-        assert_that(result, equal_to(input_data))
+    captured = {}
+
+    def make_handler(**kwargs):
+      captured['kwargs'] = kwargs
+      return _FakeHandler(**kwargs)
+
+    with (mock.patch.object(yaml_enrichment, 'Enrichment', _FakeEnrichment),
+          mock.patch.object(yaml_enrichment,
+                            'BigQueryEnrichmentHandler',
+                            side_effect=make_handler)):
+      with beam.Pipeline(options=_enrichment_options()) as p:
+        result = (
+            p
+            | 'CreateInput' >> beam.Create(input_data)
+            | YamlTransform(
+                f'''
+                type: Enrichment
+                config:
+                  enrichment_handler: BigQuery
+                  handler_config: {config}
+                '''))
+        assert_that(
+            result,
+            equal_to([
+                Row(label="item1", rank=0, enriched_timeout=30),
+                Row(label="item2", rank=1, enriched_timeout=30),
+            ]))
+    self.assertEqual(captured['kwargs'], config)
+
+  def test_enrichment_other_handlers_and_timeout(self):
+    for handler_name, attr_name in [
+        ('BigTable', 'BigTableEnrichmentHandler'),
+        ('VertexAIFeatureStore', 'VertexAIFeatureStoreEnrichmentHandler'),
+        ('FeastFeatureStore', 'FeastFeatureStoreEnrichmentHandler'),
+    ]:
+      with self.subTest(handler=handler_name):
+        with (mock.patch.object(yaml_enrichment, 'Enrichment', 
_FakeEnrichment),
+              mock.patch.object(yaml_enrichment, attr_name, _FakeHandler) as
+              mock_cls):
+          with beam.Pipeline(options=_enrichment_options()) as p:
+            result = (
+                p
+                | beam.Create([Row(id='k1')])
+                | yaml_enrichment.enrichment_transform(
+                    enrichment_handler=handler_name,
+                    handler_config={'row_key': 'id'},
+                    timeout=45))
+            assert_that(result, equal_to([Row(id='k1', enriched_timeout=45)]))
+          self.assertIs(yaml_enrichment.__dict__[attr_name], mock_cls)

Review Comment:
   **🟡 Medium**: Paired with using `side_effect=_FakeHandler` in 
`mock.patch.object` above, replace this attribute identity check with an 
assertion that the handler class was called with the expected configuration.
   
   ```suggestion
             mock_cls.assert_called_once_with(row_key='id')
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to