"""simple docstring"""






import contextlib
import os
import sqlitea

import pytest

from datasets import Dataset, Features, Value
from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy


def     UpperCAmelCase__ (       lowerCAmelCase__  :int      ,	lowerCAmelCase__  :Tuple	)				->					Union[str, Any]:
      '''simple docstring'''


      assert isinstance(lowerCAmelCase__      ,	lowerCAmelCase__	)
      assert dataset.num_rows == 4
      assert dataset.num_columns == 3
      assert dataset.column_names == ["col_1", "col_2", "col_3"]
      for feature, expected_dtype in expected_features.items():
            assert dataset.features[feature].dtype == expected_dtype


@require_sqlalchemy
@pytest.mark.parametrize("""keep_in_memory"""      ,	[False, True]	)
def     UpperCAmelCase__ (       lowerCAmelCase__  :Dict      ,	lowerCAmelCase__  :Optional[Any]      ,	lowerCAmelCase__  :str      ,	lowerCAmelCase__  :List[str]	)				->					int:
      '''simple docstring'''


      lowercase														=			tmp_path / """cache"""
      lowercase														=			{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
      with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
            lowercase														=			SqlDatasetReader(
                """dataset"""      ,	"""sqlite:///""" + sqlite_path      ,	cache_dir=lowerCAmelCase__      ,	keep_in_memory=lowerCAmelCase__	).read()
      _check_sql_dataset(lowerCAmelCase__      ,	lowerCAmelCase__	)


@require_sqlalchemy
@pytest.mark.parametrize(
    """features"""      ,	[
        None,
        {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""},
        {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""},
        {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""},
        {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""},
    ]      ,	)
def     UpperCAmelCase__ (       lowerCAmelCase__  :int      ,	lowerCAmelCase__  :Union[str, Any]      ,	lowerCAmelCase__  :List[Any]      ,	lowerCAmelCase__  :Union[str, Any]	)				->					Optional[int]:
      '''simple docstring'''


      lowercase														=			tmp_path / """cache"""
      lowercase														=			{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
      lowercase														=			features.copy() if features else default_expected_features
      lowercase														=			(
          Features({feature: Value(lowerCAmelCase__	) for feature, dtype in features.items()}	) if features is not None else None
      )
      lowercase														=			SqlDatasetReader("""dataset"""      ,	"""sqlite:///""" + sqlite_path      ,	features=lowerCAmelCase__      ,	cache_dir=lowerCAmelCase__	).read()
      _check_sql_dataset(lowerCAmelCase__      ,	lowerCAmelCase__	)


def     UpperCAmelCase__ (       lowerCAmelCase__  :Any	)				->					Optional[int]:
      '''simple docstring'''


      with contextlib.closing(sqlitea.connect(lowerCAmelCase__	)	) as con:
            lowercase														=			con.cursor()
            cur.execute("""SELECT * FROM dataset"""	)
            for row in cur:
                  yield row


@require_sqlalchemy
def     UpperCAmelCase__ (       lowerCAmelCase__  :Tuple      ,	lowerCAmelCase__  :str      ,	lowerCAmelCase__  :int	)				->					List[Any]:
      '''simple docstring'''


      lowercase														=			tmp_path / """cache"""
      lowercase														=			os.path.join(lowerCAmelCase__      ,	"""tmp.sql"""	)
      lowercase														=			SqlDatasetReader("""dataset"""      ,	"""sqlite:///""" + sqlite_path      ,	cache_dir=lowerCAmelCase__	).read()
      SqlDatasetWriter(lowerCAmelCase__      ,	"""dataset"""      ,	"""sqlite:///""" + output_sqlite_path      ,	num_proc=1	).write()

      lowercase														=			iter_sql_file(lowerCAmelCase__	)
      lowercase														=			iter_sql_file(lowerCAmelCase__	)

      for rowa, rowa in zip(lowerCAmelCase__      ,	lowerCAmelCase__	):
            assert rowa == rowa


@require_sqlalchemy
def     UpperCAmelCase__ (       lowerCAmelCase__  :str      ,	lowerCAmelCase__  :Optional[Any]      ,	lowerCAmelCase__  :Optional[int]	)				->					Optional[int]:
      '''simple docstring'''


      lowercase														=			tmp_path / """cache"""
      lowercase														=			os.path.join(lowerCAmelCase__      ,	"""tmp.sql"""	)
      lowercase														=			SqlDatasetReader("""dataset"""      ,	"""sqlite:///""" + sqlite_path      ,	cache_dir=lowerCAmelCase__	).read()
      SqlDatasetWriter(lowerCAmelCase__      ,	"""dataset"""      ,	"""sqlite:///""" + output_sqlite_path      ,	num_proc=2	).write()

      lowercase														=			iter_sql_file(lowerCAmelCase__	)
      lowercase														=			iter_sql_file(lowerCAmelCase__	)

      for rowa, rowa in zip(lowerCAmelCase__      ,	lowerCAmelCase__	):
            assert rowa == rowa






@require_sqlalchemy
def     UpperCAmelCase__ (       lowerCAmelCase__  :Dict      ,	lowerCAmelCase__  :Dict      ,	lowerCAmelCase__  :Tuple	)				->					Any:
      '''simple docstring'''


      lowercase														=			tmp_path / """cache"""
      lowercase														=			os.path.join(lowerCAmelCase__      ,	"""tmp.sql"""	)
      lowercase														=			SqlDatasetReader("""dataset"""      ,	"""sqlite:///""" + sqlite_path      ,	cache_dir=lowerCAmelCase__	).read()
      with pytest.raises(lowerCAmelCase__	):
            SqlDatasetWriter(lowerCAmelCase__      ,	"""dataset"""      ,	"""sqlite:///""" + output_sqlite_path      ,	num_proc=0	).write()




"""simple docstring"""






import os
from pathlib import Path
from unittest.mock import patch

import pytest
import zstandard as zstd

from datasets.download.download_config import DownloadConfig
from datasets.utils.file_utils import (
    OfflineModeIsEnabled,
    cached_path,
    fsspec_get,
    fsspec_head,
    ftp_get,
    ftp_head,
    get_from_cache,
    http_get,
    http_head,
)


__lowerCAmelCase					: List[Any]        ="""\
    Text data.
    Second line of data."""

__lowerCAmelCase					: Any        ="""file"""


@pytest.fixture(scope="""session"""	)
def     UpperCAmelCase__ (       lowerCAmelCase__  :str	)				->					Union[str, Any]:
      '''simple docstring'''


      lowercase														=			tmp_path_factory.mktemp("""data"""	) / (FILE_PATH + """.zstd""")
      lowercase														=			bytes(lowerCAmelCase__      ,	"""utf-8"""	)
      with zstd.open(lowerCAmelCase__      ,	"""wb"""	) as f:
            f.write(lowerCAmelCase__	)
      return path


@pytest.fixture
def     UpperCAmelCase__ (       lowerCAmelCase__  :Tuple	)				->					Dict:
      '''simple docstring'''


      with open(os.path.join(tmpfs.local_root_dir      ,	lowerCAmelCase__	)      ,	"""w"""	) as f:
            f.write(lowerCAmelCase__	)
      return FILE_PATH


@pytest.mark.parametrize("""compression_format"""      ,	["""gzip""", """xz""", """zstd"""]	)
def     UpperCAmelCase__ (       lowerCAmelCase__  :Any      ,	lowerCAmelCase__  :Any      ,	lowerCAmelCase__  :List[Any]      ,	lowerCAmelCase__  :Optional[Any]      ,	lowerCAmelCase__  :Any      ,	lowerCAmelCase__  :int	)				->					Union[str, Any]:
      '''simple docstring'''


      lowercase														=			{"""gzip""": gz_file, """xz""": xz_file, """zstd""": zstd_path}
      lowercase														=			input_paths[compression_format]
      lowercase														=			tmp_path / """cache"""
      lowercase														=			DownloadConfig(cache_dir=lowerCAmelCase__      ,	extract_compressed_file=lowerCAmelCase__	)
      lowercase														=			cached_path(lowerCAmelCase__      ,	download_config=lowerCAmelCase__	)
      with open(lowerCAmelCase__	) as f:
            lowercase														=			f.read()
      with open(lowerCAmelCase__	) as f:
            lowercase														=			f.read()
      assert extracted_file_content == expected_file_content


@pytest.mark.parametrize("""default_extracted"""      ,	[True, False]	)
@pytest.mark.parametrize("""default_cache_dir"""      ,	[True, False]	)
def     UpperCAmelCase__ (       lowerCAmelCase__  :Dict      ,	lowerCAmelCase__  :Tuple      ,	lowerCAmelCase__  :Tuple      ,	lowerCAmelCase__  :Union[str, Any]      ,	lowerCAmelCase__  :List[str]	)				->					Any:
      '''simple docstring'''


      lowercase														=			"""custom_cache"""
      lowercase														=			"""custom_extracted_dir"""
      lowercase														=			tmp_path / """custom_extracted_path"""
      if default_extracted:
            lowercase														=			("""downloads""" if default_cache_dir else custom_cache_dir, """extracted""")
      else:
            monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_DIR"""      ,	lowerCAmelCase__	)
            monkeypatch.setattr("""datasets.config.EXTRACTED_DATASETS_PATH"""      ,	str(lowerCAmelCase__	)	)
            lowercase														=			custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

      lowercase														=			xz_file
      lowercase														=			(
          DownloadConfig(extract_compressed_file=lowerCAmelCase__	)
          if default_cache_dir
          else DownloadConfig(cache_dir=tmp_path / custom_cache_dir      ,	extract_compressed_file=lowerCAmelCase__	)
      )
      lowercase														=			cached_path(lowerCAmelCase__      ,	download_config=lowerCAmelCase__	)
      assert Path(lowerCAmelCase__	).parent.parts[-2:] == expected


def     UpperCAmelCase__ (       lowerCAmelCase__  :List[str]	)				->					Tuple:
      '''simple docstring'''


      lowercase														=			str(Path(lowerCAmelCase__	).resolve()	)
      assert cached_path(lowerCAmelCase__	) == text_file
      # relative path
      lowercase														=			str(Path(lowerCAmelCase__	).resolve().relative_to(Path(os.getcwd()	)	)	)
      assert cached_path(lowerCAmelCase__	) == text_file


def     UpperCAmelCase__ (       lowerCAmelCase__  :Any	)				->					List[Any]:
      '''simple docstring'''


      lowercase														=			str(tmp_path.resolve() / """__missing_file__.txt"""	)
      with pytest.raises(lowerCAmelCase__	):
            cached_path(lowerCAmelCase__	)
      # relative path
      lowercase														=			"""./__missing_file__.txt"""
      with pytest.raises(lowerCAmelCase__	):
            cached_path(lowerCAmelCase__	)


def     UpperCAmelCase__ (       lowerCAmelCase__  :Any	)				->					Union[str, Any]:
      '''simple docstring'''


      lowercase														=			get_from_cache(f'tmp://{tmpfs_file}'	)
      with open(lowerCAmelCase__	) as f:
            lowercase														=			f.read()
      assert output_file_content == FILE_CONTENT


@patch("""datasets.config.HF_DATASETS_OFFLINE"""      ,	lowerCAmelCase__	)
def     UpperCAmelCase__ (       )				->					str:
      '''simple docstring'''


      with pytest.raises(lowerCAmelCase__	):
            cached_path("""https://huggingface.co"""	)


@patch("""datasets.config.HF_DATASETS_OFFLINE"""      ,	lowerCAmelCase__	)
def     UpperCAmelCase__ (       lowerCAmelCase__  :Tuple	)				->					Any:
      '''simple docstring'''


      lowercase														=			tmp_path_factory.mktemp("""data"""	) / """file.html"""
      with pytest.raises(lowerCAmelCase__	):
            http_get("""https://huggingface.co"""      ,	temp_file=lowerCAmelCase__	)
      with pytest.raises(lowerCAmelCase__	):
            http_head("""https://huggingface.co"""	)


@patch("""datasets.config.HF_DATASETS_OFFLINE"""      ,	lowerCAmelCase__	)
def     UpperCAmelCase__ (       lowerCAmelCase__  :str	)				->					Any:
      '''simple docstring'''


      lowercase														=			tmp_path_factory.mktemp("""data"""	) / """file.html"""
      with pytest.raises(lowerCAmelCase__	):
            ftp_get("""ftp://huggingface.co"""      ,	temp_file=lowerCAmelCase__	)
      with pytest.raises(lowerCAmelCase__	):
            ftp_head("""ftp://huggingface.co"""	)






@patch("""datasets.config.HF_DATASETS_OFFLINE"""      ,	lowerCAmelCase__	)
def     UpperCAmelCase__ (       lowerCAmelCase__  :Any	)				->					Dict:
      '''simple docstring'''


      lowercase														=			tmp_path_factory.mktemp("""data"""	) / """file.html"""
      with pytest.raises(lowerCAmelCase__	):
            fsspec_get("""s3://huggingface.co"""      ,	temp_file=lowerCAmelCase__	)
      with pytest.raises(lowerCAmelCase__	):
            fsspec_head("""s3://huggingface.co"""	)



