






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


__a      :       Union[str, Any]				  =   "hf-internal-testing/tiny-random-bert"
__a      :       Tuple				  =   os.path.join(TRANSFORMERS_CACHE, "models--hf-internal-testing--tiny-random-bert")
__a      :       int				  =   "9b8c223d42b2188cb49d29af482996f9d0f3e5a6"


class       __lowercase      (	unittest.TestCase  ):







     '''simple docstring'''




     def 		lowerCAmelCase_     (				self       :			Optional[int]				):


         """simple docstring"""



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

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

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




     def 		lowerCAmelCase_     (				self       :			Any				):


         """simple docstring"""



         with self.assertRaisesRegex(UpperCamelCase_ ,	"""is not a valid model identifier"""				):
             __A         =	cached_file("""tiny-random-bert""" ,	UpperCamelCase_				)

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

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




     def 		lowerCAmelCase_     (				self       :			Tuple				):


         """simple docstring"""



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

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

         __A         =	cached_file(UpperCamelCase_ ,	"""conf""" ,	_raise_exceptions_for_missing_entries=UpperCamelCase_				)
         self.assertIsNone(UpperCamelCase_				)

         __A         =	cached_file(UpperCamelCase_ ,	"""conf""" ,	local_files_only=UpperCamelCase_ ,	_raise_exceptions_for_missing_entries=UpperCamelCase_				)
         self.assertIsNone(UpperCamelCase_				)

         __A         =	mock.Mock()
         __A         =	500
         __A         =	{}
         __A         =	HTTPError
         __A         =	{}

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




     def 		lowerCAmelCase_     (				self       :			Any				):


         """simple docstring"""



         self.assertTrue(has_file("""hf-internal-testing/tiny-bert-pt-only""" ,	UpperCamelCase_				)				)
         self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only""" ,	UpperCamelCase_				)				)
         self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only""" ,	UpperCamelCase_				)				)




     def 		lowerCAmelCase_     (				self       :			Union[str, Any]				):


         """simple docstring"""



         self.assertIsNone(get_file_from_repo("""bert-base-cased""" ,	"""ahah.txt"""				)				)

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

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

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





     def 		lowerCAmelCase_     (				self       :			Union[str, Any]				):


         """simple docstring"""



         with tempfile.TemporaryDirectory() as tmp_dir:
             __A         =	Path(UpperCamelCase_				) / """a.txt"""
             filename.touch()
             self.assertEqual(get_file_from_repo(UpperCamelCase_ ,	"""a.txt"""				) ,	str(UpperCamelCase_				)				)

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












import os

import pytest

from datasets import (
    get_dataset_config_info,
    get_dataset_config_names,
    get_dataset_infos,
    get_dataset_split_names,
    inspect_dataset,
    inspect_metric,
)


__a      :       List[str]				  =   pytest.mark.integration



@pytest.mark.parametrize("""path"""			,				["""paws""", """csv"""]    )
def 			_SCREAMING_SNAKE_CASE						( __lowercase   :			List[Any]			,				__lowercase   :			Optional[int]    )						->      Tuple:





    """simple docstring"""

    inspect_dataset(__lowercase			,				__lowercase    )
    __A         =	path + """.py"""
    assert script_name in os.listdir(__lowercase    )
    assert "__pycache__" not in os.listdir(__lowercase    )



@pytest.mark.filterwarnings("""ignore:inspect_metric is deprecated:FutureWarning"""    )
@pytest.mark.filterwarnings("""ignore:metric_module_factory is deprecated:FutureWarning"""    )
@pytest.mark.parametrize("""path"""			,				["""accuracy"""]    )
def 			_SCREAMING_SNAKE_CASE						( __lowercase   :			Optional[int]			,				__lowercase   :			Tuple    )						->      Any:





    """simple docstring"""

    inspect_metric(__lowercase			,				__lowercase    )
    __A         =	path + """.py"""
    assert script_name in os.listdir(__lowercase    )
    assert "__pycache__" not in os.listdir(__lowercase    )



@pytest.mark.parametrize(
    """path, config_name, expected_splits"""			,				[
        ("""squad""", """plain_text""", ["""train""", """validation"""]),
        ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]),
        ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]),
    ]			,				)
def 			_SCREAMING_SNAKE_CASE						( __lowercase   :			Dict			,				__lowercase   :			Optional[int]			,				__lowercase   :			List[Any]    )						->      Optional[Any]:





    """simple docstring"""

    __A         =	get_dataset_config_info(__lowercase			,				config_name=__lowercase    )
    assert info.config_name == config_name
    assert list(info.splits.keys()    ) == expected_splits



@pytest.mark.parametrize(
    """path, config_name, expected_exception"""			,				[
        ("""paws""", None, ValueError),
    ]			,				)
def 			_SCREAMING_SNAKE_CASE						( __lowercase   :			List[Any]			,				__lowercase   :			int			,				__lowercase   :			List[str]    )						->      int:





    """simple docstring"""

    with pytest.raises(__lowercase    ):
        get_dataset_config_info(__lowercase			,				config_name=__lowercase    )



@pytest.mark.parametrize(
    """path, expected"""			,				[
        ("""squad""", """plain_text"""),
        ("""acronym_identification""", """default"""),
        ("""lhoestq/squad""", """plain_text"""),
        ("""lhoestq/test""", """default"""),
        ("""lhoestq/demo1""", """lhoestq--demo1"""),
        ("""dalle-mini/wit""", """dalle-mini--wit"""),
    ]			,				)
def 			_SCREAMING_SNAKE_CASE						( __lowercase   :			Tuple			,				__lowercase   :			Optional[Any]    )						->      Any:





    """simple docstring"""

    __A         =	get_dataset_config_names(__lowercase    )
    assert expected in config_names



@pytest.mark.parametrize(
    """path, expected_configs, expected_splits_in_first_config"""			,				[
        ("""squad""", ["""plain_text"""], ["""train""", """validation"""]),
        ("""dalle-mini/wit""", ["""dalle-mini--wit"""], ["""train"""]),
        ("""paws""", ["""labeled_final""", """labeled_swap""", """unlabeled_final"""], ["""train""", """test""", """validation"""]),
    ]			,				)
def 			_SCREAMING_SNAKE_CASE						( __lowercase   :			int			,				__lowercase   :			Tuple			,				__lowercase   :			List[Any]    )						->      List[str]:





    """simple docstring"""

    __A         =	get_dataset_infos(__lowercase    )
    assert list(infos.keys()    ) == expected_configs
    __A         =	expected_configs[0]
    assert expected_config in infos
    __A         =	infos[expected_config]
    assert info.config_name == expected_config
    assert list(info.splits.keys()    ) == expected_splits_in_first_config



@pytest.mark.parametrize(
    """path, expected_config, expected_splits"""			,				[
        ("""squad""", """plain_text""", ["""train""", """validation"""]),
        ("""dalle-mini/wit""", """dalle-mini--wit""", ["""train"""]),
        ("""paws""", """labeled_final""", ["""train""", """test""", """validation"""]),
    ]			,				)
def 			_SCREAMING_SNAKE_CASE						( __lowercase   :			Optional[int]			,				__lowercase   :			str			,				__lowercase   :			Union[str, Any]    )						->      Dict:





    """simple docstring"""

    __A         =	get_dataset_infos(__lowercase    )
    assert expected_config in infos
    __A         =	infos[expected_config]
    assert info.config_name == expected_config
    assert list(info.splits.keys()    ) == expected_splits






@pytest.mark.parametrize(
    """path, config_name, expected_exception"""			,				[
        ("""paws""", None, ValueError),
    ]			,				)
def 			_SCREAMING_SNAKE_CASE						( __lowercase   :			List[str]			,				__lowercase   :			Optional[int]			,				__lowercase   :			Tuple    )						->      Union[str, Any]:





    """simple docstring"""

    with pytest.raises(__lowercase    ):
        get_dataset_split_names(__lowercase			,				config_name=__lowercase    )




