Skip to content

Commit

Permalink
Merge pull request #498 from john-thuo1/test_language_formats
Browse files Browse the repository at this point in the history
clean up language tests
  • Loading branch information
andrewtavis authored Oct 29, 2024
2 parents f6685e8 + dfde819 commit 135f32c
Showing 1 changed file with 56 additions and 133 deletions.
189 changes: 56 additions & 133 deletions tests/cli/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import unittest
from io import StringIO
from pathlib import Path
from unittest.mock import MagicMock, Mock, mock_open, patch
from unittest.mock import MagicMock, mock_open, patch

from scribe_data.cli.convert import (
convert_to_csv_or_tsv,
Expand All @@ -35,34 +35,7 @@


class TestConvert(unittest.TestCase):
# MARK: Helper Functions

def setup_language_map(self, mock_language_map: Mock) -> None:
"""
Set up the mock language map for testing.
Parameters
---------
mock_language_map: Mock
Mock object representing the language map
to be configured.
Returns
-------
None
"""
mock_language_map.get.side_effect = lambda lang: {
"english": {
"language": "english",
"iso": "en",
"qid": "Q1860",
},
"french": {
"language": "french",
"iso": "fr",
"qid": "Q150",
},
}.get(lang.lower())
# MARK: Helper Function

def normalize_line_endings(self, data: str) -> str:
"""
Expand All @@ -83,44 +56,27 @@ def normalize_line_endings(self, data: str) -> str:

# MARK: JSON

# @patch("scribe_data.cli.convert.Path", autospec=True)
# def test_convert_to_json_normalized_language(self, mock_path):
#

# mock_path_obj = MagicMock(spec=Path)
# mock_path.return_value = mock_path_obj

# mock_path_obj.suffix = ".csv"
# mock_path_obj.exists.return_value = True

# convert_to_json(
# language="French",
# data_type="nouns",
# output_type="json",
# input_file="input.csv",
# output_dir="/output_dir",
# overwrite=True,
# )

# @patch("scribe_data.cli.convert.Path", autospec=True)
# def test_convert_to_json_unknown_language(self, mock_path):
# mock_input_file_path = MagicMock(spec=Path)
# mock_input_file_path.exists.return_value = True
# mock_path.side_effect = [mock_input_file_path, MagicMock(spec=Path)]

# with self.assertRaises(ValueError) as context:
# convert_to_json(
# language="FakeLanguage",
# data_type="nouns",
# output_type="json",
# input_file="test.csv",
# output_dir="/output_dir",
# overwrite=True,
# )

# self.assertEqual(
# str(context.exception), "Language 'FakeLanguage' is not recognized."
# )
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_json_empty_language(self, mock_path):
csv_data = "key,value\na,1\nb,2"
mock_file = StringIO(csv_data)

mock_path_obj = MagicMock(spec=Path)
mock_path.return_value = mock_path_obj
mock_path_obj.suffix = ".csv"
mock_path_obj.exists.return_value = True
mock_path_obj.open.return_value.__enter__.return_value = mock_file

with self.assertRaises(ValueError) as context:
convert_to_json(
language="",
data_type="nouns",
output_type="json",
input_file="input.csv",
output_dir="/output_dir",
overwrite=True,
)
self.assertIn("Language '' is not recognized.", str(context.exception))

@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_json_with_input_file(self, mock_path):
Expand All @@ -146,7 +102,7 @@ def test_convert_to_json_with_input_file(self, mock_path):

mock_path_obj.open.assert_called_once_with("r", encoding="utf-8")

@patch("scribe_data.cli.convert.Path")
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_json_supported_file_extension_csv(self, mock_path_class):
mock_path_instance = MagicMock(spec=Path)

Expand All @@ -164,7 +120,7 @@ def test_convert_to_json_supported_file_extension_csv(self, mock_path_class):
overwrite=True,
)

@patch("scribe_data.cli.convert.Path")
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_json_supported_file_extension_tsv(self, mock_path_class):
mock_path_instance = MagicMock(spec=Path)

Expand All @@ -182,7 +138,7 @@ def test_convert_to_json_supported_file_extension_tsv(self, mock_path_class):
overwrite=True,
)

@patch("scribe_data.cli.convert.Path")
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_json_unsupported_file_extension(self, mock_path):
mock_path_obj = MagicMock(spec=Path)
mock_path.return_value = mock_path_obj
Expand Down Expand Up @@ -322,62 +278,29 @@ def test_convert_to_json_with_complex_structure(self, mock_path_class):

# MARK: CSV or TSV

# @patch("scribe_data.cli.convert.Path", autospec=True)
# def test_convert_to_csv_or_json_normalized_language(
# self, mock_path
# ):
#

# mock_path_obj = MagicMock(spec=Path)
# mock_path.return_value = mock_path_obj

# mock_path_obj.suffix = ".json"
# mock_path_obj.exists.return_value = True

# mock_json_data = json.dumps({"key1": "value1", "key2": "value2"})
# mock_open_function = mock_open(read_data=mock_json_data)
# mock_path_obj.open = mock_open_function

# convert_to_csv_or_tsv(
# language="English",
# data_type="nouns",
# output_type="csv",
# input_file="input.json",
# output_dir="/output_dir",
# overwrite=True,
# )

# mock_open_function.assert_called_once_with("r", encoding="utf-8")

# @patch("scribe_data.cli.convert.Path", autospec=True)
# def test_convert_to_csv_or_json_unknown_language(
# self, mock_path
# ):
#

# mock_path_obj = MagicMock(spec=Path)
# mock_path.return_value = mock_path_obj

# mock_path_obj.suffix = ".json"
# mock_path_obj.exists.return_value = True

# mock_json_data = json.dumps({"key1": "value1", "key2": "value2"})
# mock_open_function = mock_open(read_data=mock_json_data)
# mock_path_obj.open = mock_open_function

# with self.assertRaises(ValueError) as context:
# convert_to_csv_or_tsv(
# language="FakeLanguage",
# data_type="nouns",
# output_type="csv",
# input_file="input.json",
# output_dir="/output_dir",
# overwrite=True,
# )

# self.assertEqual(
# str(context.exception), "Language 'FakeLanguage' is not recognized."
# )
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_csv_or_json_empty_language(self, mock_path):
mock_path_obj = MagicMock(spec=Path)
mock_path.return_value = mock_path_obj

mock_path_obj.suffix = ".json"
mock_path_obj.exists.return_value = True

mock_json_data = json.dumps({"key1": "value1", "key2": "value2"})
mock_open_function = mock_open(read_data=mock_json_data)
mock_path_obj.open = mock_open_function

with self.assertRaises(ValueError) as context:
convert_to_csv_or_tsv(
language="",
data_type="nouns",
output_type="csv",
input_file="input.json",
output_dir="/output_dir",
overwrite=True,
)

self.assertEqual(str(context.exception), "Language '' is not recognized.")

@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_csv_or_tsv_standarddict_to_csv(self, mock_path_class):
Expand Down Expand Up @@ -710,8 +633,8 @@ def test_convert_to_csv_or_tsv_liststrings_to_tsv(self, mock_path_class):

# MARK: SQLITE

@patch("scribe_data.cli.convert.Path")
@patch("scribe_data.cli.convert.data_to_sqlite")
@patch("scribe_data.cli.convert.Path", autospec=True)
@patch("scribe_data.cli.convert.data_to_sqlite", autospec=True)
@patch("shutil.copy")
def test_convert_to_sqlite(self, mock_shutil_copy, mock_data_to_sqlite, mock_path):
mock_path.return_value.exists.return_value = True
Expand All @@ -728,8 +651,8 @@ def test_convert_to_sqlite(self, mock_shutil_copy, mock_data_to_sqlite, mock_pat
mock_data_to_sqlite.assert_called_with(["english"], ["nouns"])
mock_shutil_copy.assert_called()

@patch("scribe_data.cli.convert.Path")
@patch("scribe_data.cli.convert.data_to_sqlite")
@patch("scribe_data.cli.convert.Path", autospec=True)
@patch("scribe_data.cli.convert.data_to_sqlite", autospec=True)
def test_convert_to_sqlite_no_output_dir(self, mock_data_to_sqlite, mock_path):
mock_input_file = MagicMock()
mock_input_file.exists.return_value = True
Expand All @@ -751,9 +674,9 @@ def test_convert_to_sqlite_no_output_dir(self, mock_data_to_sqlite, mock_path):

mock_data_to_sqlite.assert_called_with(["english"], ["nouns"])

@patch("scribe_data.cli.convert.Path")
@patch("scribe_data.cli.convert.data_to_sqlite")
@patch("scribe_data.cli.convert.get_language_iso")
@patch("scribe_data.cli.convert.Path", autospec=True)
@patch("scribe_data.cli.convert.data_to_sqlite", autospec=True)
@patch("scribe_data.cli.convert.get_language_iso", autospec=True)
@patch("shutil.copy")
def test_convert_to_sqlite_with_language_iso(
self, mock_copy, mock_get_language_iso, mock_data_to_sqlite, mock_path
Expand Down

0 comments on commit 135f32c

Please sign in to comment.