Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up language tests #498

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 55 additions & 134 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,35 +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:
"""
Normalize line endings in a given string.
Expand All @@ -82,45 +54,27 @@ def normalize_line_endings(self, data: str) -> str:
return data.replace("\r\n", "\n").replace("\r", "\n")

# MARK: JSON
@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)

# @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."
# )
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 +100,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 +118,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 +136,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 +276,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 +631,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 +649,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 +672,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
Loading