





import pytest

from datasets import Dataset, DatasetDict, Features, NamedSplit, Value
from datasets.io.text import TextDatasetReader

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases



def 		A_				(     _lowerCAmelCase							,   _lowerCAmelCase     )						-> int:
 assert isinstance(_lowerCAmelCase							,   _lowerCAmelCase     )
 assert dataset.num_rows == 4
 assert dataset.num_columns == 1
 assert dataset.column_names == ["text"]
 for feature, expected_dtype in expected_features.items():
  assert dataset.features[feature].dtype == expected_dtype



@pytest.mark.parametrize("keep_in_memory"							,   [False, True]     )
def 		A_				(     _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase     )						-> Dict:
 UpperCamelCase	:					Optional[int]       	=   tmp_path / "cache"
 UpperCamelCase	:					Any       	=   {"text": "string"}
 with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
  UpperCamelCase	:					Any       	=   TextDatasetReader(_lowerCAmelCase							,   cache_dir=_lowerCAmelCase							,   keep_in_memory=_lowerCAmelCase     ).read()
 _check_text_dataset(_lowerCAmelCase							,   _lowerCAmelCase     )



@pytest.mark.parametrize(
    "features"							,   [
        None,
        {"text": "string"},
        {"text": "int32"},
        {"text": "float32"},
    ]							,   )
def 		A_				(     _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase     )						-> str:
 UpperCamelCase	:					int       	=   tmp_path / "cache"
 UpperCamelCase	:					int       	=   {"text": "string"}
 UpperCamelCase	:					List[str]       	=   features.copy() if features else default_expected_features
 UpperCamelCase	:					Optional[Any]       	=   (
     Features({feature: Value(_lowerCAmelCase     ) for feature, dtype in features.items()}     ) if features is not None else None
 )
 UpperCamelCase	:					Union[str, Any]       	=   TextDatasetReader(_lowerCAmelCase							,   features=_lowerCAmelCase							,   cache_dir=_lowerCAmelCase     ).read()
 _check_text_dataset(_lowerCAmelCase							,   _lowerCAmelCase     )



@pytest.mark.parametrize("split"							,   [None, NamedSplit("train"     ), "train", "test"]     )
def 		A_				(     _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase     )						-> Union[str, Any]:
 UpperCamelCase	:					Union[str, Any]       	=   tmp_path / "cache"
 UpperCamelCase	:					Optional[Any]       	=   {"text": "string"}
 UpperCamelCase	:					List[str]       	=   TextDatasetReader(_lowerCAmelCase							,   cache_dir=_lowerCAmelCase							,   split=_lowerCAmelCase     ).read()
 _check_text_dataset(_lowerCAmelCase							,   _lowerCAmelCase     )
 assert dataset.split == split if split else "train"



@pytest.mark.parametrize("path_type"							,   [str, list]     )
def 		A_				(     _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase     )						-> List[Any]:
 if issubclass(_lowerCAmelCase							,   _lowerCAmelCase     ):
  UpperCamelCase	:					str       	=   text_path
 elif issubclass(_lowerCAmelCase							,   _lowerCAmelCase     ):
  UpperCamelCase	:					Tuple       	=   [text_path]
 UpperCamelCase	:					Union[str, Any]       	=   tmp_path / "cache"
 UpperCamelCase	:					Any       	=   {"text": "string"}
 UpperCamelCase	:					Optional[Any]       	=   TextDatasetReader(_lowerCAmelCase							,   cache_dir=_lowerCAmelCase     ).read()
 _check_text_dataset(_lowerCAmelCase							,   _lowerCAmelCase     )



def 		A_				(     _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase=("train",)     )						-> str:
 assert isinstance(_lowerCAmelCase							,   _lowerCAmelCase     )
 for split in splits:
  UpperCamelCase	:					Optional[int]       	=   dataset_dict[split]
  assert dataset.num_rows == 4
  assert dataset.num_columns == 1
  assert dataset.column_names == ["text"]
  for feature, expected_dtype in expected_features.items():
   assert dataset.features[feature].dtype == expected_dtype



@pytest.mark.parametrize("keep_in_memory"							,   [False, True]     )
def 		A_				(     _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase     )						-> Union[str, Any]:
 UpperCamelCase	:					int       	=   tmp_path / "cache"
 UpperCamelCase	:					int       	=   {"text": "string"}
 with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
  UpperCamelCase	:					Union[str, Any]       	=   TextDatasetReader({"train": text_path}							,   cache_dir=_lowerCAmelCase							,   keep_in_memory=_lowerCAmelCase     ).read()
 _check_text_datasetdict(_lowerCAmelCase							,   _lowerCAmelCase     )



@pytest.mark.parametrize(
    "features"							,   [
        None,
        {"text": "string"},
        {"text": "int32"},
        {"text": "float32"},
    ]							,   )
def 		A_				(     _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase     )						-> Optional[Any]:
 UpperCamelCase	:					Tuple       	=   tmp_path / "cache"
 # CSV file loses col_1 string dtype information: default now is "int64" instead of "string"
 UpperCamelCase	:					Optional[Any]       	=   {"text": "string"}
 UpperCamelCase	:					Dict       	=   features.copy() if features else default_expected_features
 UpperCamelCase	:					Tuple       	=   (
     Features({feature: Value(_lowerCAmelCase     ) for feature, dtype in features.items()}     ) if features is not None else None
 )
 UpperCamelCase	:					Optional[int]       	=   TextDatasetReader({"train": text_path}							,   features=_lowerCAmelCase							,   cache_dir=_lowerCAmelCase     ).read()
 _check_text_datasetdict(_lowerCAmelCase							,   _lowerCAmelCase     )



@pytest.mark.parametrize("split"							,   [None, NamedSplit("train"     ), "train", "test"]     )
def 		A_				(     _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase     )						-> Any:
 if split:
  UpperCamelCase	:					int       	=   {split: text_path}
 else:
  UpperCamelCase	:					Optional[int]       	=   "train"
  UpperCamelCase	:					str       	=   {"train": text_path, "test": text_path}
 UpperCamelCase	:					int       	=   tmp_path / "cache"
 UpperCamelCase	:					int       	=   {"text": "string"}
 UpperCamelCase	:					str       	=   TextDatasetReader(_lowerCAmelCase							,   cache_dir=_lowerCAmelCase     ).read()
 _check_text_datasetdict(_lowerCAmelCase							,   _lowerCAmelCase							,   splits=list(path.keys()     )     )
 assert all(dataset[split].split == split for split in path.keys()     )







import pytest
import requests

from datasets.utils.file_utils import http_head

from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline



@pytest.mark.integration
def 		A_				(     )						-> List[Any]:
 with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT     ):
  with pytest.raises(_lowerCAmelCase     ):
   requests.request("GET"							,   "https://huggingface.co"     )
  with pytest.raises(requests.exceptions.ConnectTimeout     ):
   requests.request("GET"							,   "https://huggingface.co"							,   timeout=1.0     )



@pytest.mark.integration
def 		A_				(     )						-> Tuple:
 with offline(OfflineSimulationMode.CONNECTION_FAILS     ):
  with pytest.raises(requests.exceptions.ConnectionError     ):
   requests.request("GET"							,   "https://huggingface.co"     )



def 		A_				(     )						-> Optional[int]:
 with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1     ):
  with pytest.raises(_lowerCAmelCase     ):
   http_head("https://huggingface.co"     )
