





"""simple docstring"""





import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


lowerCAmelCase__     			=			'''hf-internal-testing/tiny-random-bert'''
lowerCAmelCase__     			=			os.path.join(TRANSFORMERS_CACHE, '''models--hf-internal-testing--tiny-random-bert''')
lowerCAmelCase__     			=			'''9b8c223d42b2188cb49d29af482996f9d0f3e5a6'''





class        _lowerCamelCase      (       unittest.TestCase ):


						def     snake_case_   (self		) ->   str:
								UpperCamelCase   =	cached_file(__a   ,							__a		)
								# Should have downloaded the file in here
								self.assertTrue(os.path.isdir(__a		)		)
								# Cache should contain at least those three subfolders:
								for subfolder in ["blobs", "refs", "snapshots"]:
										self.assertTrue(os.path.isdir(os.path.join(__a   ,							__a		)		)		)
								with open(os.path.join(__a   ,							"refs"   ,							"main"		)		) as f:
										UpperCamelCase   =	f.read()
								self.assertEqual(__a   ,							os.path.join(__a   ,							"snapshots"   ,							__a   ,							__a		)		)
								self.assertTrue(os.path.isfile(__a		)		)

								# File is cached at the same place the second time.
								UpperCamelCase   =	cached_file(__a   ,							__a		)
								self.assertEqual(__a   ,							__a		)

								# Using a specific revision to test the full commit hash.
								UpperCamelCase   =	cached_file(__a   ,							__a   ,							revision="9b8c223"		)
								self.assertEqual(__a   ,							os.path.join(__a   ,							"snapshots"   ,							__a   ,							__a		)		)


						def     snake_case_   (self		) ->   Dict:
								with self.assertRaisesRegex(__a   ,							"is not a valid model identifier"		):
										UpperCamelCase   =	cached_file("tiny-random-bert"   ,							__a		)

								with self.assertRaisesRegex(__a   ,							"is not a valid git identifier"		):
										UpperCamelCase   =	cached_file(__a   ,							__a   ,							revision="aaaa"		)

								with self.assertRaisesRegex(__a   ,							"does not appear to have a file named"		):
										UpperCamelCase   =	cached_file(__a   ,							"conf"		)


						def     snake_case_   (self		) ->   List[str]:
								with self.assertRaisesRegex(__a   ,							"does not appear to have a file named"		):
										UpperCamelCase   =	cached_file(__a   ,							"conf"		)

								with open(os.path.join(__a   ,							"refs"   ,							"main"		)		) as f:
										UpperCamelCase   =	f.read()
								self.assertTrue(os.path.isfile(os.path.join(__a   ,							".no_exist"   ,							__a   ,							"conf"		)		)		)

								UpperCamelCase   =	cached_file(__a   ,							"conf"   ,							_raise_exceptions_for_missing_entries=__a		)
								self.assertIsNone(__a		)

								UpperCamelCase   =	cached_file(__a   ,							"conf"   ,							local_files_only=__a   ,							_raise_exceptions_for_missing_entries=__a		)
								self.assertIsNone(__a		)

								UpperCamelCase   =	mock.Mock()
								UpperCamelCase   =	5_00
								UpperCamelCase   =	{}
								UpperCamelCase   =	HTTPError
								UpperCamelCase   =	{}

								# Under the mock environment we get a 500 error when trying to reach the tokenizer.
								with mock.patch("requests.Session.request"   ,							return_value=__a		) as mock_head:
										UpperCamelCase   =	cached_file(__a   ,							"conf"   ,							_raise_exceptions_for_connection_errors=__a		)
										self.assertIsNone(__a		)
										# This check we did call the fake head request
										mock_head.assert_called()


						def     snake_case_   (self		) ->   Optional[int]:
								self.assertTrue(has_file("hf-internal-testing/tiny-bert-pt-only"   ,							__a		)		)
								self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only"   ,							__a		)		)
								self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only"   ,							__a		)		)


						def     snake_case_   (self		) ->   List[str]:
								# `get_file_from_repo` returns None if the file does not exist
								self.assertIsNone(get_file_from_repo("bert-base-cased"   ,							"ahah.txt"		)		)

								# The function raises if the repository does not exist.
								with self.assertRaisesRegex(__a   ,							"is not a valid model identifier"		):
										get_file_from_repo("bert-base-case"   ,							__a		)

								# The function raises if the revision does not exist.
								with self.assertRaisesRegex(__a   ,							"is not a valid git identifier"		):
										get_file_from_repo("bert-base-cased"   ,							__a   ,							revision="ahaha"		)

								UpperCamelCase   =	get_file_from_repo("bert-base-cased"   ,							__a		)
								# The name is the cached name which is not very easy to test, so instead we load the content.
								UpperCamelCase   =	json.loads(open(__a   ,							"r"		).read()		)
								self.assertEqual(config["hidden_size"]   ,							7_68		)




						def     snake_case_   (self		) ->   Tuple:
								with tempfile.TemporaryDirectory() as tmp_dir:
										UpperCamelCase   =	Path(__a		) / "a.txt"
										filename.touch()
										self.assertEqual(get_file_from_repo(__a   ,							"a.txt"		)   ,							str(__a		)		)

										self.assertIsNone(get_file_from_repo(__a   ,							"b.txt"		)		)













"""simple docstring"""





from diffusers.utils.testing_utils import require_onnxruntime





@require_onnxruntime
class        _lowerCamelCase      :
						pass






