code: def test_collect_optimization(tmp_dir, dvc, mocker):
      (stage,) = tmp_dir.dvc_gen("foo", "foo text")
  
      # Forget cached stages and graph and error out on collection
      dvc._reset()
      mocker.patch(
          "dvc.repo.Repo.index",
          property(raiser(Exception("Should not collect"))),
      )
  
      # Should read stage directly instead of collecting the whole graph
      dvc.stage.collect(stage.path)
      dvc.stage.collect_granular(stage.path)
  
imports: import os
  from operator import itemgetter
  import pytest
  from funcy import raiser
  from dvc.dvcfile import PROJECT_FILE, FileIsGitIgnored
  from dvc.exceptions import NoOutputOrStageError
  from dvc.repo import Repo
  from dvc.stage.exceptions import StageFileDoesNotExistError, StageNameUnspecified, StageNotFound
  from dvc.utils import relpath
  from dvc.utils.fs import remove
  from dvc.utils.strictyaml import YAMLValidationError
label_desc: Use image node_id 22 for calling a global function with example usage: test_collect_optimization(tmp_dir, dvc, mocker) without return types