code: def test_cached_dataset(cache_dir=None):
      # patch DATALAD_TESTS_CACHE to not use the actual cache with
      # the test testing that very cache.
      cache_dir = Path(cache_dir)
      ds_url = "https://github.com/datalad/testrepo--minimalds"
      name_in_cache = url2filename(ds_url)
      annexed_file = Path("inannex") / "animated.gif"
  
      with patch(CACHE_PATCH_STR, new=cache_dir):
  
          @cached_dataset(url=ds_url)
          def decorated_test1(ds):
              # we get a Dataset instance
              assert_is_instance(ds, Dataset)
              # it's a clone in a temp. location, not within the cache
              assert_not_in(cache_dir, ds.pathobj.parents)
              assert_result_count(
                  ds.siblings(),
                  1,
                  type="sibling",
                  name=DEFAULT_REMOTE,
                  url=(cache_dir / name_in_cache).as_posix(),
              )
              here = ds.config.get("annex.uuid")
              origin = ds.config.get(
                  f"remote.{DEFAULT_REMOTE}.annex-uuid"
              )
              where = ds.repo.whereis(str(annexed_file))
              assert_not_in(here, where)
              assert_not_in(origin, where)
  
              return ds.pathobj, ds.repo.pathobj
  
          @cached_dataset(url=ds_url, paths=str(annexed_file))
          def decorated_test2(ds):
              # we get a Dataset instance
              assert_is_instance(ds, Dataset)
              # it's a clone in a temp. location, not within the cache
              assert_not_in(cache_dir, ds.pathobj.parents)
              assert_result_count(
                  ds.siblings(),
                  1,
                  type="sibling",
                  name=DEFAULT_REMOTE,
                  url=(cache_dir / name_in_cache).as_posix(),
              )
              here = ds.config.get("annex.uuid")
              origin = ds.config.get(
                  f"remote.{DEFAULT_REMOTE}.annex-uuid"
              )
              where = ds.repo.whereis(str(annexed_file))
              assert_in(here, where)
              assert_in(origin, where)
  
              return ds.pathobj, ds.repo.pathobj
  
          @cached_dataset(url=ds_url)
          def decorated_test3(ds):
              # we get a Dataset instance
              assert_is_instance(ds, Dataset)
              # it's a clone in a temp. location, not within the cache
              assert_not_in(cache_dir, ds.pathobj.parents)
              assert_result_count(
                  ds.siblings(),
                  1,
                  type="sibling",
                  name=DEFAULT_REMOTE,
                  url=(cache_dir / name_in_cache).as_posix(),
              )
              # origin is the same cached dataset, that got this content in
              # decorated_test2 before. Should still be there. But "here" we
              # didn't request it
              here = ds.config.get("annex.uuid")
              origin = ds.config.get(
                  f"remote.{DEFAULT_REMOTE}.annex-uuid"
              )
              where = ds.repo.whereis(str(annexed_file))
              assert_not_in(here, where)
              assert_in(origin, where)
  
              return ds.pathobj, ds.repo.pathobj
  
          @cached_dataset(
              url=ds_url,
              version="541cf855d13c2a338ff2803d4488daf0035e568f",
          )
          def decorated_test4(ds):
              # we get a Dataset instance
              assert_is_instance(ds, Dataset)
              # it's a clone in a temp. location, not within the cache
              assert_not_in(cache_dir, ds.pathobj.parents)
              assert_result_count(
                  ds.siblings(),
                  1,
                  type="sibling",
                  name=DEFAULT_REMOTE,
                  url=(cache_dir / name_in_cache).as_posix(),
              )
              # origin is the same cached dataset, that got this content in
              # decorated_test2 before. Should still be there. But "here" we
              # didn't request it
              here = ds.config.get("annex.uuid")
              origin = ds.config.get(
                  f"remote.{DEFAULT_REMOTE}.annex-uuid"
              )
              where = ds.repo.whereis(str(annexed_file))
              assert_not_in(here, where)
              assert_in(origin, where)
  
              assert_equal(
                  ds.repo.get_hexsha(),
                  "541cf855d13c2a338ff2803d4488daf0035e568f",
              )
  
              return ds.pathobj, ds.repo.pathobj
  
          first_dspath, first_repopath = decorated_test1()
          second_dspath, second_repopath = decorated_test2()
          decorated_test3()
          decorated_test4()
  
          # first and second are not the same, only their origin is:
          assert_not_equal(first_dspath, second_dspath)
          assert_not_equal(first_repopath, second_repopath)
  
file_path: datalad/datalad/tests/test_utils_cached_dataset.py
imports: from unittest.mock import patch
  from datalad.distribution.dataset import Dataset
  from datalad.support.annexrepo import AnnexRepo
  from datalad.support.gitrepo import GitRepo
  from datalad.tests.utils_cached_dataset import cached_dataset, cached_url, get_cached_dataset, url2filename
  from datalad.tests.utils_pytest import DEFAULT_REMOTE, assert_equal, assert_false, assert_in, assert_is, assert_is_instance, assert_not_equal, assert_not_in, assert_raises, assert_result_count, assert_true, skip_if_no_network, with_tempfile
  from datalad.utils import Path, ensure_list, opj
label_desc: Use image node_id 2 for calling a global function with example usage: test_cached_dataset(cache_dir) and returns: ds,  ds, ds,  ds, ds,  ds, ds,  ds