|
4 | 4 | """ |
5 | 5 |
|
6 | 6 | import unittest |
7 | | -from unittest.mock import Mock, patch, MagicMock |
| 7 | +from unittest.mock import Mock, patch, MagicMock, PropertyMock |
8 | 8 | from dataclasses import dataclass |
9 | 9 | import tempfile |
10 | 10 |
|
@@ -333,6 +333,85 @@ def test_list_deployment_configs_non_string_model(self): |
333 | 333 | self.assertIn("only supported for JumpStart", str(context.exception)) |
334 | 334 |
|
335 | 335 |
|
| 336 | +class TestPreDeployBenchmarkData(unittest.TestCase): |
| 337 | + """Pre-deploy JumpStart benchmark data: list_deployment_configs() and the |
| 338 | + benchmark_metrics property must work before build()/deploy() is called. |
| 339 | + """ |
| 340 | + |
| 341 | + def _jumpstart_mb(self): |
| 342 | + """A JumpStart-config ModelBuilder, constructed the same way as |
| 343 | + TestFromJumpStartConfig (explicit role_arn, no live role/instance |
| 344 | + resolution).""" |
| 345 | + js_config = JumpStartConfig(model_id="test-model", model_version="1.0.0") |
| 346 | + return ModelBuilder.from_jumpstart_config( |
| 347 | + jumpstart_config=js_config, |
| 348 | + role_arn="arn:aws:iam::123456789012:role/SageMakerRole", |
| 349 | + ) |
| 350 | + |
| 351 | + def test_get_deployment_configs_ensures_metadata(self): |
| 352 | + """_get_deployment_configs lazily loads metadata configs itself, so every |
| 353 | + caller benefits. Pre-fix it read _metadata_configs (None pre-deploy) and |
| 354 | + returned [] without ever loading them.""" |
| 355 | + mb = self._jumpstart_mb() |
| 356 | + mb._metadata_configs = None |
| 357 | + |
| 358 | + with patch.object(mb, "_ensure_metadata_configs") as ensure: |
| 359 | + result = mb._get_deployment_configs(None, None) |
| 360 | + |
| 361 | + ensure.assert_called_once() |
| 362 | + self.assertEqual(result, []) |
| 363 | + |
| 364 | + def test_list_deployment_configs_loads_metadata_when_pre_deploy(self): |
| 365 | + """list_deployment_configs() with no instance_type returns configs for a |
| 366 | + pre-deploy JumpStart model instead of [].""" |
| 367 | + mb = self._jumpstart_mb() |
| 368 | + mb.config_name = None |
| 369 | + mb.instance_type = None |
| 370 | + |
| 371 | + with patch.object(mb, "_is_jumpstart_model_id", return_value=True), patch.object( |
| 372 | + mb, "_is_model_customization", return_value=False |
| 373 | + ), patch.object(mb, "_use_jumpstart_equivalent", return_value=False), patch.object( |
| 374 | + mb, "_get_deployment_configs", return_value=[Mock()] |
| 375 | + ), patch.object( |
| 376 | + mb, "deployment_config_response_data", return_value=[{"DeploymentConfigName": "c1"}] |
| 377 | + ): |
| 378 | + result = mb.list_deployment_configs() |
| 379 | + |
| 380 | + self.assertEqual(result, [{"DeploymentConfigName": "c1"}]) |
| 381 | + |
| 382 | + def test_benchmark_metrics_property_returns_dataframe(self): |
| 383 | + """The benchmark_metrics property builds a DataFrame from the config |
| 384 | + benchmark data (it did not exist before, causing AttributeError).""" |
| 385 | + mb = self._jumpstart_mb() |
| 386 | + sample = { |
| 387 | + "Instance Type": ["ml.g5.2xlarge", "ml.g5.12xlarge"], |
| 388 | + "Latency (ms)": [100.0, 80.0], |
| 389 | + } |
| 390 | + |
| 391 | + with patch.object(mb, "_get_deployment_configs_benchmarks_data", return_value=sample): |
| 392 | + df = mb.benchmark_metrics |
| 393 | + |
| 394 | + self.assertEqual(list(df["Instance Type"]), ["ml.g5.2xlarge", "ml.g5.12xlarge"]) |
| 395 | + |
| 396 | + def test_display_benchmark_metrics_no_attribute_error(self): |
| 397 | + """display_benchmark_metrics() reads the benchmark_metrics property and |
| 398 | + no longer raises AttributeError for a JumpStart model. The property is |
| 399 | + patched to a stand-in frame so the assertion is on the wiring, not on |
| 400 | + pandas' optional markdown renderer.""" |
| 401 | + mb = self._jumpstart_mb() |
| 402 | + df = MagicMock() |
| 403 | + df.to_markdown.return_value = "table" |
| 404 | + |
| 405 | + with patch.object(mb, "_is_jumpstart_model_id", return_value=True), patch.object( |
| 406 | + mb, "_use_jumpstart_equivalent", return_value=False |
| 407 | + ), patch.object( |
| 408 | + type(mb), "benchmark_metrics", new_callable=PropertyMock, return_value=df |
| 409 | + ): |
| 410 | + mb.display_benchmark_metrics() |
| 411 | + |
| 412 | + df.to_markdown.assert_called_once() |
| 413 | + |
| 414 | + |
336 | 415 | class TestTransformer(unittest.TestCase): |
337 | 416 | """Test transformer method.""" |
338 | 417 |
|
|
0 commit comments