

'''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,
)


__snake_case			:     Optional[Any]										=      'hf-internal-testing/tiny-random-bert'
__snake_case			:     str										=      os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
__snake_case			:     int										=      '9b8c223d42b2188cb49d29af482996f9d0f3e5a6'



class lowerCamelCase			(  unittest.TestCase						):



			'''simple docstring'''






			def lowercase__						(				self	:						Any			)      ->		Optional[Any]:





									'''simple docstring'''
									A__					:       Dict     =cached_file(lowerCAmelCase_		,      lowerCAmelCase_			)
									# Should have downloaded the file in here
									self.assertTrue(os.path.isdir(lowerCAmelCase_			)			)
									# Cache should contain at least those three subfolders:
									for subfolder in ["blobs", "refs", "snapshots"]:
															self.assertTrue(os.path.isdir(os.path.join(lowerCAmelCase_		,      lowerCAmelCase_			)			)			)
									with open(os.path.join(lowerCAmelCase_		,      """refs"""		,      """main"""			)			) as f:
															A__					:       str     =f.read()
									self.assertEqual(lowerCAmelCase_		,      os.path.join(lowerCAmelCase_		,      """snapshots"""		,      lowerCAmelCase_		,      lowerCAmelCase_			)			)
									self.assertTrue(os.path.isfile(lowerCAmelCase_			)			)

									# File is cached at the same place the second time.
									A__					:       Dict     =cached_file(lowerCAmelCase_		,      lowerCAmelCase_			)
									self.assertEqual(lowerCAmelCase_		,      lowerCAmelCase_			)

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


			def lowercase__						(				self	:						Optional[int]			)      ->		Optional[int]:





									'''simple docstring'''
									with self.assertRaisesRegex(lowerCAmelCase_		,      """is not a valid model identifier"""			):
															A__					:       Union[str, Any]     =cached_file("""tiny-random-bert"""		,      lowerCAmelCase_			)

									with self.assertRaisesRegex(lowerCAmelCase_		,      """is not a valid git identifier"""			):
															A__					:       List[Any]     =cached_file(lowerCAmelCase_		,      lowerCAmelCase_		,      revision="""aaaa"""			)

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


			def lowercase__						(				self	:						str			)      ->		Dict:





									'''simple docstring'''
									with self.assertRaisesRegex(lowerCAmelCase_		,      """does not appear to have a file named"""			):
															A__					:       List[Any]     =cached_file(lowerCAmelCase_		,      """conf"""			)

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

									A__					:       str     =cached_file(lowerCAmelCase_		,      """conf"""		,      _raise_exceptions_for_missing_entries=lowerCAmelCase_			)
									self.assertIsNone(lowerCAmelCase_			)

									A__					:       Any     =cached_file(lowerCAmelCase_		,      """conf"""		,      local_files_only=lowerCAmelCase_		,      _raise_exceptions_for_missing_entries=lowerCAmelCase_			)
									self.assertIsNone(lowerCAmelCase_			)

									A__					:       List[Any]     =mock.Mock()
									A__					:       str     =5_00
									A__					:       str     ={}
									A__					:       Union[str, Any]     =HTTPError
									A__					:       List[Any]     ={}

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


			def lowercase__						(				self	:						Union[str, Any]			)      ->		Optional[int]:





									'''simple docstring'''
									self.assertTrue(has_file("""hf-internal-testing/tiny-bert-pt-only"""		,      lowerCAmelCase_			)			)
									self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only"""		,      lowerCAmelCase_			)			)
									self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only"""		,      lowerCAmelCase_			)			)


			def lowercase__						(				self	:						List[Any]			)      ->		int:





									'''simple docstring'''
									# `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(lowerCAmelCase_		,      """is not a valid model identifier"""			):
															get_file_from_repo("""bert-base-case"""		,      lowerCAmelCase_			)

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

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



			def lowercase__						(				self	:						List[str]			)      ->		List[Any]:





									'''simple docstring'''
									with tempfile.TemporaryDirectory() as tmp_dir:
															A__					:       Optional[Any]     =Path(lowerCAmelCase_			) / """a.txt"""
															filename.touch()
															self.assertEqual(get_file_from_repo(lowerCAmelCase_		,      """a.txt"""			)		,      str(lowerCAmelCase_			)			)

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




'''simple docstring'''



from __future__ import annotations
def 						__lowerCamelCase	(       __snake_case					:		list[int]	)	->							bool:






						"""simple docstring"""





						return len(set(__snake_case	)	) == len(__snake_case	)


if __name__ == "__main__":
		import doctest

		doctest.testmod()

