



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__		(				_SCREAMING_SNAKE_CASE   :	Union[str, Any]     ,				_SCREAMING_SNAKE_CASE   :	Optional[Any]				)->Optional[Any]:
      assert isinstance(_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE				)
      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__		(				_SCREAMING_SNAKE_CASE   :	List[Any]     ,				_SCREAMING_SNAKE_CASE   :	Optional[Any]     ,				_SCREAMING_SNAKE_CASE   :	Dict     ,				_SCREAMING_SNAKE_CASE   :	List[Any]				)->Any:
      _lowerCAmelCase 			=  tmp_path / '''cache'''
      _lowerCAmelCase 			=  {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
      with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
            _lowerCAmelCase 			=  SqlDatasetReader(
                '''dataset'''     ,				'''sqlite:///''' + sqlite_path     ,				cache_dir=_SCREAMING_SNAKE_CASE     ,				keep_in_memory=_SCREAMING_SNAKE_CASE				).read()
      _check_sql_dataset(_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE				)
@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__		(				_SCREAMING_SNAKE_CASE   :	Optional[Any]     ,				_SCREAMING_SNAKE_CASE   :	List[str]     ,				_SCREAMING_SNAKE_CASE   :	Optional[int]     ,				_SCREAMING_SNAKE_CASE   :	str				)->List[str]:
      _lowerCAmelCase 			=  tmp_path / '''cache'''
      _lowerCAmelCase 			=  {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
      _lowerCAmelCase 			=  features.copy() if features else default_expected_features
      _lowerCAmelCase 			=  (
          Features({feature: Value(_SCREAMING_SNAKE_CASE				) for feature, dtype in features.items()}				) if features is not None else None
      )
      _lowerCAmelCase 			=  SqlDatasetReader('''dataset'''     ,				'''sqlite:///''' + sqlite_path     ,				features=_SCREAMING_SNAKE_CASE     ,				cache_dir=_SCREAMING_SNAKE_CASE				).read()
      _check_sql_dataset(_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE				)
def 							UpperCAmelCase__		(				_SCREAMING_SNAKE_CASE   :	List[Any]				)->int:
      with contextlib.closing(sqlitea.connect(_SCREAMING_SNAKE_CASE				)				) as con:
            _lowerCAmelCase 			=  con.cursor()
            cur.execute('''SELECT * FROM dataset'''				)
            for row in cur:
                  yield row
@require_sqlalchemy
def 							UpperCAmelCase__		(				_SCREAMING_SNAKE_CASE   :	Any     ,				_SCREAMING_SNAKE_CASE   :	Dict     ,				_SCREAMING_SNAKE_CASE   :	Dict				)->Dict:
      _lowerCAmelCase 			=  tmp_path / '''cache'''
      _lowerCAmelCase 			=  os.path.join(_SCREAMING_SNAKE_CASE     ,				'''tmp.sql'''				)
      _lowerCAmelCase 			=  SqlDatasetReader('''dataset'''     ,				'''sqlite:///''' + sqlite_path     ,				cache_dir=_SCREAMING_SNAKE_CASE				).read()
      SqlDatasetWriter(_SCREAMING_SNAKE_CASE     ,				'''dataset'''     ,				'''sqlite:///''' + output_sqlite_path     ,				num_proc=1				).write()

      _lowerCAmelCase 			=  iter_sql_file(_SCREAMING_SNAKE_CASE				)
      _lowerCAmelCase 			=  iter_sql_file(_SCREAMING_SNAKE_CASE				)

      for rowa, rowa in zip(_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE				):
            assert rowa == rowa
@require_sqlalchemy
def 							UpperCAmelCase__		(				_SCREAMING_SNAKE_CASE   :	int     ,				_SCREAMING_SNAKE_CASE   :	Dict     ,				_SCREAMING_SNAKE_CASE   :	Optional[int]				)->str:
      _lowerCAmelCase 			=  tmp_path / '''cache'''
      _lowerCAmelCase 			=  os.path.join(_SCREAMING_SNAKE_CASE     ,				'''tmp.sql'''				)
      _lowerCAmelCase 			=  SqlDatasetReader('''dataset'''     ,				'''sqlite:///''' + sqlite_path     ,				cache_dir=_SCREAMING_SNAKE_CASE				).read()
      SqlDatasetWriter(_SCREAMING_SNAKE_CASE     ,				'''dataset'''     ,				'''sqlite:///''' + output_sqlite_path     ,				num_proc=2				).write()

      _lowerCAmelCase 			=  iter_sql_file(_SCREAMING_SNAKE_CASE				)
      _lowerCAmelCase 			=  iter_sql_file(_SCREAMING_SNAKE_CASE				)

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


@require_sqlalchemy
def 							UpperCAmelCase__		(				_SCREAMING_SNAKE_CASE   :	Optional[Any]     ,				_SCREAMING_SNAKE_CASE   :	str     ,				_SCREAMING_SNAKE_CASE   :	Union[str, Any]				)->Optional[Any]:
      _lowerCAmelCase 			=  tmp_path / '''cache'''
      _lowerCAmelCase 			=  os.path.join(_SCREAMING_SNAKE_CASE     ,				'''tmp.sql'''				)
      _lowerCAmelCase 			=  SqlDatasetReader('''dataset'''     ,				'''sqlite:///''' + sqlite_path     ,				cache_dir=_SCREAMING_SNAKE_CASE				).read()
      with pytest.raises(_SCREAMING_SNAKE_CASE				):
            SqlDatasetWriter(_SCREAMING_SNAKE_CASE     ,				'''dataset'''     ,				'''sqlite:///''' + output_sqlite_path     ,				num_proc=0				).write()




from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch





@require_torch
@require_tf
class 		UpperCAmelCase						(						snake_case_				):





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  SMALL_MODEL_IDENTIFIER
         _lowerCAmelCase 			=  '''pt'''
         _lowerCAmelCase 			=  '''tf'''





   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase			):
         _lowerCAmelCase 			=  AutoModel.from_pretrained(self.test_model			)
         model_pt.save_pretrained(_lowerCAmelCase			)





   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase			):
         _lowerCAmelCase 			=  TFAutoModel.from_pretrained(self.test_model       ,     from_pt=_lowerCAmelCase			)
         model_tf.save_pretrained(_lowerCAmelCase			)





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  '''mock_framework'''

         # Framework provided - return whatever the user provides
         _lowerCAmelCase 			=  FeaturesManager.determine_framework(self.test_model       ,     _lowerCAmelCase			)
         self.assertEqual(_lowerCAmelCase       ,     _lowerCAmelCase			)

         # Local checkpoint and framework provided - return provided framework
         # PyTorch checkpoint
         with TemporaryDirectory() as local_pt_ckpt:
               self._setup_pt_ckpt(_lowerCAmelCase			)
               _lowerCAmelCase 			=  FeaturesManager.determine_framework(_lowerCAmelCase       ,     _lowerCAmelCase			)
               self.assertEqual(_lowerCAmelCase       ,     _lowerCAmelCase			)

         # TensorFlow checkpoint
         with TemporaryDirectory() as local_tf_ckpt:
               self._setup_tf_ckpt(_lowerCAmelCase			)
               _lowerCAmelCase 			=  FeaturesManager.determine_framework(_lowerCAmelCase       ,     _lowerCAmelCase			)
               self.assertEqual(_lowerCAmelCase       ,     _lowerCAmelCase			)





   def 	__lowerCAmelCase							(	self			):
         # PyTorch checkpoint
         with TemporaryDirectory() as local_pt_ckpt:
               self._setup_pt_ckpt(_lowerCAmelCase			)
               _lowerCAmelCase 			=  FeaturesManager.determine_framework(_lowerCAmelCase			)
               self.assertEqual(_lowerCAmelCase       ,     self.framework_pt			)

         # TensorFlow checkpoint
         with TemporaryDirectory() as local_tf_ckpt:
               self._setup_tf_ckpt(_lowerCAmelCase			)
               _lowerCAmelCase 			=  FeaturesManager.determine_framework(_lowerCAmelCase			)
               self.assertEqual(_lowerCAmelCase       ,     self.framework_tf			)

         # Invalid local checkpoint
         with TemporaryDirectory() as local_invalid_ckpt:
               with self.assertRaises(_lowerCAmelCase			):
                     _lowerCAmelCase 			=  FeaturesManager.determine_framework(_lowerCAmelCase			)





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  MagicMock(return_value=_lowerCAmelCase			)
         with patch('''transformers.onnx.features.is_tf_available'''       ,     _lowerCAmelCase			):
               _lowerCAmelCase 			=  FeaturesManager.determine_framework(self.test_model			)
               self.assertEqual(_lowerCAmelCase       ,     self.framework_pt			)

         # PyTorch not in environment -> use TensorFlow
         _lowerCAmelCase 			=  MagicMock(return_value=_lowerCAmelCase			)
         with patch('''transformers.onnx.features.is_torch_available'''       ,     _lowerCAmelCase			):
               _lowerCAmelCase 			=  FeaturesManager.determine_framework(self.test_model			)
               self.assertEqual(_lowerCAmelCase       ,     self.framework_tf			)

         # Both in environment -> use PyTorch
         _lowerCAmelCase 			=  MagicMock(return_value=_lowerCAmelCase			)
         _lowerCAmelCase 			=  MagicMock(return_value=_lowerCAmelCase			)
         with patch('''transformers.onnx.features.is_tf_available'''       ,     _lowerCAmelCase			), patch(
             '''transformers.onnx.features.is_torch_available'''       ,     _lowerCAmelCase			):
               _lowerCAmelCase 			=  FeaturesManager.determine_framework(self.test_model			)
               self.assertEqual(_lowerCAmelCase       ,     self.framework_pt			)

         # Both not in environment -> raise error
         _lowerCAmelCase 			=  MagicMock(return_value=_lowerCAmelCase			)
         _lowerCAmelCase 			=  MagicMock(return_value=_lowerCAmelCase			)
         with patch('''transformers.onnx.features.is_tf_available'''       ,     _lowerCAmelCase			), patch(
             '''transformers.onnx.features.is_torch_available'''       ,     _lowerCAmelCase			):
               with self.assertRaises(_lowerCAmelCase			):
                     _lowerCAmelCase 			=  FeaturesManager.determine_framework(self.test_model			)