class_docstr: Download manager test.
  
  During tests, the `tf.io.gfile` API is mocked. Instead, files I/O are tracked
  through `fs` (`tfds.testing.MockFs`).
  
  The downloader/extrator results are mocked with:
  
    dl_results: Dict[str, UrlInfo], mapping `url` -> `downloader.download(url)`
    dl_fnames: Dict[str, str], mapping `url` -> `filename`, if the name
      cannot be extracted from the url (e.g. `https://a.org/download?id=123`)
    extract_results: Dict[str, str], mapping `path` -> `extractor.extract()`
  
  To check whether url/path are actually downloaded vs cached reused:
  
    downloaded_urls: Track calls of `downloader.download`
    extracted_paths: Track calls of `extractor.extract`
class_docstr_tok: ["Download","manager","test",".","During","tests",",","the","`","tf.io.gfile","`","API","is","mocked",".","Instead",",","files","I\/O","are","tracked","through","`","fs","`","(","`","tfds.testing.MockFs","`",")",".","The","downloader\/extrator","results","are","mocked","with",":","dl_results",":","Dict","[","str",",","UrlInfo","]",",","mapping","`","url","`","-",">","`","downloader.download","(","url",")","`","dl_fnames",":","Dict","[","str",",","str","]",",","mapping","`","url","`","-",">","`","filename","`",",","if","the","name","can","not","be","extracted","from","the","url","(","e.g",".","`","https",":","\/\/a.org\/download","?","id=123","`",")","extract_results",":","Dict","[","str",",","str","]",",","mapping","`","path","`","-",">","`","extractor.extract","(",")","`","To","check","whether","url\/path","are","actually","downloaded","vs","cached","reused",":","downloaded_urls",":","Track","calls","of","`","downloader.download","`","extracted_paths",":","Track","calls","of","`","extractor.extract","`"]
docstr: Tests that the URL is downloaded only once.
code: def test_download_cached(self):
      """Tests that the URL is downloaded only once."""
      a = Artifact("x")
      self.dl_results[a.url] = a.url_info
  
      # Download the URL
      dl_manager = self._get_manager(
          register_checksums=False,
      )
      self.assertEqual(dl_manager.download(a.url), a.url_path)
      self.assertCountEqual(self.downloaded_urls, [a.url])
      self.assertContainFiles([a.url_path, _info_path(a.url_path)])
  
      # Reuse downloaded cache
      dl_manager = self._get_manager(
          register_checksums=False,
      )
      self.assertEqual(dl_manager.download(a.url), a.url_path)
      self.assertCountEqual(self.downloaded_urls, [a.url])
      self.assertContainFiles([a.url_path, _info_path(a.url_path)])
  
      # Reuse downloaded cache and register the checksums
      dl_manager = self._get_manager(
          register_checksums=True,  # <<< Register checksums !!!
      )
      self.assertEqual(dl_manager.download(a.url), a.file_path)
      self.assertCountEqual(self.downloaded_urls, [a.url])
      # The files have been renamed `url_path` -> `file_path`
      self.assertContainFiles(
          [
              a.file_path,
              _info_path(a.file_path),
              "/checksums/checksums.tsv",
          ]
      )
  
      # After checksums have been registered, `file_path` is used
      dl_manager = self._get_manager(
          register_checksums=False,
          url_infos={a.url: a.url_info},
      )
      self.assertEqual(dl_manager.download(a.url), a.file_path)
      self.assertCountEqual(self.downloaded_urls, [a.url])
      self.assertContainFiles(
          [
              a.file_path,
              _info_path(a.file_path),
              "/checksums/checksums.tsv",
          ]
      )
  
      # Registering checksums twice still reuse the cached `file_path`
      dl_manager = self._get_manager(
          register_checksums=True,  # <<< Re-register checksums...
          url_infos={
              a.url: a.url_info
          },  # ...but checksums already known
      )
      self.assertEqual(dl_manager.download(a.url), a.file_path)
      self.assertCountEqual(
          self.downloaded_urls, [a.url]
      )  # Still one download
      self.assertContainFiles(
          [
              a.file_path,
              _info_path(a.file_path),
              "/checksums/checksums.tsv",
          ]
      )
  
      # Checksums unknown, so `file_path` unknown, re-downloading
      dl_manager = self._get_manager(
          register_checksums=False,
      )
      self.assertEqual(dl_manager.download(a.url), a.url_path)
      self.assertCountEqual(
          self.downloaded_urls, [a.url, a.url]
      )  # Re-download!!
      self.assertContainFiles(
          [
              a.url_path,
              _info_path(a.url_path),
              # `file_path` still exists from previous download
              a.file_path,
              _info_path(a.file_path),
              "/checksums/checksums.tsv",
          ]
      )
  
imports: import collections
  import hashlib
  import json
  import os
  import pickle
  from unittest import mock
  from absl.testing import parameterized
  from etils import epath
  import promise
  import tensorflow
  from tensorflow_datasets import testing
  from tensorflow_datasets.core.download import checksums
  from tensorflow_datasets.core.download import download_manager
  from tensorflow_datasets.core.download import downloader
  from tensorflow_datasets.core.download import extractor
  from tensorflow_datasets.core.download import resource
  from tensorflow_datasets.core.utils import tree_utils
label_desc: Use image node_id 20 for calling the DownloadManagerTest obj's underlying member method code with example usage: obj.test_download_cached() without return types