Track API usage.
(snapshots: SnapshotFixture)
| 418 | |
| 419 | @pytest.fixture(autouse=True) |
| 420 | def track_api_usage(snapshots: SnapshotFixture): |
| 421 | """Track API usage.""" |
| 422 | yield |
| 423 | if (request := REQUEST_CONTEXT.get()) is None: |
| 424 | return |
| 425 | response_mocker = ResponseMocker() |
| 426 | calls = {} |
| 427 | |
| 428 | for call in response_mocker.calls: |
| 429 | if (_test_caller := call.pop("_test_caller", None)) is None: |
| 430 | continue |
| 431 | if _test_caller not in calls: |
| 432 | if call.get("_uses_setup_integration"): |
| 433 | calls[_test_caller] = { |
| 434 | c: -1 |
| 435 | for c in [ |
| 436 | "https://data-v2.hacs.xyz/appdaemon/data.json", |
| 437 | "https://data-v2.hacs.xyz/critical/data.json", |
| 438 | "https://data-v2.hacs.xyz/integration/data.json", |
| 439 | "https://data-v2.hacs.xyz/plugin/data.json", |
| 440 | "https://data-v2.hacs.xyz/python_script/data.json", |
| 441 | "https://data-v2.hacs.xyz/removed/data.json", |
| 442 | "https://data-v2.hacs.xyz/template/data.json", |
| 443 | "https://data-v2.hacs.xyz/theme/data.json", |
| 444 | ] |
| 445 | } |
| 446 | else: |
| 447 | calls[_test_caller] = {} |
| 448 | if (url := call.get("url")) not in calls[_test_caller]: |
| 449 | calls[_test_caller][url] = 0 |
| 450 | calls[_test_caller][url] += 1 |
| 451 | |
| 452 | filtered_calls = { |
| 453 | k: v |
| 454 | for k, v in {t: {k: v for k, v in c.items() if v != 0} for t, c in calls.items()}.items() |
| 455 | if v |
| 456 | } |
| 457 | |
| 458 | snapshotfile = f"api-usage/{request.node.location[0].replace(".py", "")}{ |
| 459 | slugify(f"::{request.node.name}")}.json" |
| 460 | |
| 461 | if not filtered_calls: |
| 462 | assert not os.path.exists(f"tests/snapshots/{snapshotfile}") |
| 463 | return |
| 464 | |
| 465 | response_mocker.calls = [] |
| 466 | |
| 467 | snapshots.assert_match( |
| 468 | safe_json_dumps(filtered_calls), |
| 469 | snapshotfile |
| 470 | ) |
nothing calls this directly
no test coverage detected
searching dependent graphs…