Skip to content

Commit

Permalink
Update tests to remove language map
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtavis committed Oct 24, 2024
1 parent c49c169 commit fbb5e5a
Showing 1 changed file with 26 additions and 108 deletions.
134 changes: 26 additions & 108 deletions tests/cli/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,11 @@ def setup_language_map(self, mock_language_map: Mock) -> None:
"language": "english",
"iso": "en",
"qid": "Q1860",
"remove-words": ["of", "the", "The", "and"],
"ignore-words": [],
},
"french": {
"language": "french",
"iso": "fr",
"qid": "Q150",
"remove-words": ["of", "the", "The", "and"],
"ignore-words": ["XXe"],
},
}.get(lang.lower())

Expand All @@ -87,10 +83,9 @@ def normalize_line_endings(self, data: str) -> str:

# MARK: JSON

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

# mock_path_obj = MagicMock(spec=Path)
# mock_path.return_value = mock_path_obj
Expand All @@ -107,19 +102,15 @@ def normalize_line_endings(self, data: str) -> str:
# overwrite=True,
# )

# mock_language_map.get.assert_called_with("french")

# @patch("scribe_data.cli.convert.language_map", autospec=True)
# @patch("scribe_data.cli.convert.Path", autospec=True)
# def test_convert_to_json_unknown_language(self, mock_path, mock_language_map):
# mock_language_map.get.return_value = None
# 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="UnsupportedLanguage",
# language="FakeLanguage",
# data_type="nouns",
# output_type="json",
# input_file="test.csv",
Expand All @@ -128,17 +119,14 @@ def normalize_line_endings(self, data: str) -> str:
# )

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

@patch("scribe_data.cli.convert.language_map", autospec=True)
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_json_with_input_file(self, mock_path, mock_language_map):
def test_convert_to_json_with_input_file(self, mock_path):
csv_data = "key,value\na,1\nb,2"
mock_file = StringIO(csv_data)

self.setup_language_map(mock_language_map)

mock_path_obj = MagicMock(spec=Path)
mock_path.return_value = mock_path_obj
mock_path_obj.suffix = ".csv"
Expand All @@ -158,13 +146,8 @@ def test_convert_to_json_with_input_file(self, mock_path, mock_language_map):

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

@patch("scribe_data.cli.convert.language_map", autospec=True)
@patch("scribe_data.cli.convert.Path")
def test_convert_to_json_supported_file_extension_csv(
self, mock_path_class, mock_language_map
):
self.setup_language_map(mock_language_map)

def test_convert_to_json_supported_file_extension_csv(self, mock_path_class):
mock_path_instance = MagicMock(spec=Path)

mock_path_class.return_value = mock_path_instance
Expand All @@ -181,12 +164,8 @@ def test_convert_to_json_supported_file_extension_csv(
overwrite=True,
)

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

mock_path_class.return_value = mock_path_instance
Expand All @@ -203,12 +182,8 @@ def test_convert_to_json_supported_file_extension_tsv(
overwrite=True,
)

@patch("scribe_data.cli.convert.language_map", autospec=True)
@patch("scribe_data.cli.convert.Path")
def test_convert_to_json_unsupported_file_extension(
self, mock_path, mock_language_map
):
self.setup_language_map(mock_language_map)
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 All @@ -231,15 +206,12 @@ def test_convert_to_json_unsupported_file_extension(
"Unsupported file extension '.txt' for test.txt. Please provide a '.csv' or '.tsv' file.",
)

@patch("scribe_data.cli.convert.language_map", autospec=True)
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_json_standard_csv(self, mock_path_class, mock_language_map):
def test_convert_to_json_standard_csv(self, mock_path_class):
csv_data = "key,value\na,1\nb,2"
expected_json = {"a": "1", "b": "2"}
mock_file_obj = StringIO(csv_data)

self.setup_language_map(mock_language_map)

mock_input_file_path = MagicMock(spec=Path)
mock_input_file_path.suffix = ".csv"
mock_input_file_path.exists.return_value = True
Expand Down Expand Up @@ -273,11 +245,8 @@ def test_convert_to_json_standard_csv(self, mock_path_class, mock_language_map):

self.assertEqual(json.loads(written_data), expected_json)

@patch("scribe_data.cli.convert.language_map", autospec=True)
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_json_with_multiple_keys(
self, mock_path_class, mock_language_map
):
def test_convert_to_json_with_multiple_keys(self, mock_path_class):
csv_data = "key,value1,value2\na,1,x\nb,2,y\nc,3,z"
expected_json = {
"a": {"value1": "1", "value2": "x"},
Expand All @@ -286,8 +255,6 @@ def test_convert_to_json_with_multiple_keys(
}
mock_file_obj = StringIO(csv_data)

self.setup_language_map(mock_language_map)

mock_input_file_path = MagicMock(spec=Path)
mock_input_file_path.suffix = ".csv"
mock_input_file_path.exists.return_value = True
Expand Down Expand Up @@ -316,20 +283,15 @@ def test_convert_to_json_with_multiple_keys(
)
self.assertEqual(json.loads(written_data), expected_json)

@patch("scribe_data.cli.convert.language_map", autospec=True)
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_json_with_complex_structure(
self, mock_path_class, mock_language_map
):
def test_convert_to_json_with_complex_structure(self, mock_path_class):
csv_data = "key,emoji,is_base,rank\na,πŸ˜€,true,1\nb,πŸ˜…,false,2"
expected_json = {
"a": [{"emoji": "πŸ˜€", "is_base": True, "rank": 1}],
"b": [{"emoji": "πŸ˜…", "is_base": False, "rank": 2}],
}
mock_file_obj = StringIO(csv_data)

self.setup_language_map(mock_language_map)

mock_input_file_path = MagicMock(spec=Path)
mock_input_file_path.suffix = ".csv"
mock_input_file_path.exists.return_value = True
Expand Down Expand Up @@ -360,12 +322,11 @@ def test_convert_to_json_with_complex_structure(

# MARK: CSV or TSV

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

# mock_path_obj = MagicMock(spec=Path)
# mock_path.return_value = mock_path_obj
Expand All @@ -386,16 +347,13 @@ def test_convert_to_json_with_complex_structure(
# overwrite=True,
# )

# mock_language_map.get.assert_called_with("english")

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

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

# mock_path_obj = MagicMock(spec=Path)
# mock_path.return_value = mock_path_obj
Expand All @@ -409,7 +367,7 @@ def test_convert_to_json_with_complex_structure(

# with self.assertRaises(ValueError) as context:
# convert_to_csv_or_tsv(
# language="UnsupportedLanguage",
# language="FakeLanguage",
# data_type="nouns",
# output_type="csv",
# input_file="input.json",
Expand All @@ -418,21 +376,16 @@ def test_convert_to_json_with_complex_structure(
# )

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

@patch("scribe_data.cli.convert.language_map", autospec=True)
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_csv_or_tsv_standarddict_to_csv(
self, mock_path_class, mock_language_map
):
def test_convert_to_csv_or_tsv_standarddict_to_csv(self, mock_path_class):
json_data = '{"a": "1", "b": "2"}'
expected_csv_output = "preposition,value\n" "a,1\n" "b,2\n"

mock_file_obj = StringIO(json_data)

self.setup_language_map(mock_language_map)

mock_input_file_path = MagicMock(spec=Path)
mock_input_file_path.suffix = ".json"
mock_input_file_path.exists.return_value = True
Expand Down Expand Up @@ -467,19 +420,14 @@ def test_convert_to_csv_or_tsv_standarddict_to_csv(

self.assertEqual(written_data, expected_csv_output)

@patch("scribe_data.cli.convert.language_map", autospec=True)
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_csv_or_tsv_standarddict_to_tsv(
self, mock_path_class, mock_language_map
):
def test_convert_to_csv_or_tsv_standarddict_to_tsv(self, mock_path_class):
json_data = '{"a": "1", "b": "2"}'

expected_tsv_output = "preposition\tvalue\n" "a\t1\n" "b\t2\n"

mock_file_obj = StringIO(json_data)

self.setup_language_map(mock_language_map)

mock_input_file_path = MagicMock(spec=Path)
mock_input_file_path.suffix = ".json"
mock_input_file_path.exists.return_value = True
Expand Down Expand Up @@ -513,19 +461,14 @@ def test_convert_to_csv_or_tsv_standarddict_to_tsv(

self.assertEqual(written_data, expected_tsv_output)

@patch("scribe_data.cli.convert.language_map", autospec=True)
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_csv_or_tsv_nesteddict_to_csv(
self, mock_path_class, mock_language_map
):
def test_convert_to_csv_or_tsv_nesteddict_to_csv(self, mock_path_class):
json_data = (
'{"a": {"value1": "1", "value2": "x"}, "b": {"value1": "2", "value2": "y"}}'
)
expected_csv_output = "noun,value1,value2\n" "a,1,x\n" "b,2,y\n"
mock_file_obj = StringIO(json_data)

self.setup_language_map(mock_language_map)

mock_input_file_path = MagicMock(spec=Path)
mock_input_file_path.suffix = ".json"
mock_input_file_path.exists.return_value = True
Expand Down Expand Up @@ -558,20 +501,15 @@ def test_convert_to_csv_or_tsv_nesteddict_to_csv(
expected_csv_output = self.normalize_line_endings(expected_csv_output)
self.assertEqual(written_data, expected_csv_output)

@patch("scribe_data.cli.convert.language_map", autospec=True)
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_csv_or_tsv_nesteddict_to_tsv(
self, mock_path_class, mock_language_map
):
def test_convert_to_csv_or_tsv_nesteddict_to_tsv(self, mock_path_class):
json_data = (
'{"a": {"value1": "1", "value2": "x"}, "b": {"value1": "2", "value2": "y"}}'
)
expected_tsv_output = "noun\tvalue1\tvalue2\n" "a\t1\tx\n" "b\t2\ty\n"

mock_file_obj = StringIO(json_data)

self.setup_language_map(mock_language_map)

mock_input_file_path = MagicMock(spec=Path)
mock_input_file_path.suffix = ".json"
mock_input_file_path.exists.return_value = True
Expand Down Expand Up @@ -605,19 +543,14 @@ def test_convert_to_csv_or_tsv_nesteddict_to_tsv(

self.assertEqual(written_data, expected_tsv_output)

@patch("scribe_data.cli.convert.language_map", autospec=True)
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_csv_or_tsv_listofdicts_to_csv(
self, mock_path_class, mock_language_map
):
def test_convert_to_csv_or_tsv_listofdicts_to_csv(self, mock_path_class):
json_data = '{"a": [{"emoji": "πŸ˜€", "is_base": true, "rank": 1}, {"emoji": "πŸ˜…", "is_base": false, "rank": 2}]}'
expected_csv_output = (
"word,emoji,is_base,rank\n" "a,πŸ˜€,True,1\n" "a,πŸ˜…,False,2\n"
)
mock_file_obj = StringIO(json_data)

self.setup_language_map(mock_language_map)

mock_input_file_path = MagicMock(spec=Path)
mock_input_file_path.suffix = ".json"
mock_input_file_path.exists.return_value = True
Expand Down Expand Up @@ -650,19 +583,14 @@ def test_convert_to_csv_or_tsv_listofdicts_to_csv(
expected_csv_output = self.normalize_line_endings(expected_csv_output)
self.assertEqual(written_data, expected_csv_output)

@patch("scribe_data.cli.convert.language_map", autospec=True)
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_csv_or_tsv_listofdicts_to_tsv(
self, mock_path_class, mock_language_map
):
def test_convert_to_csv_or_tsv_listofdicts_to_tsv(self, mock_path_class):
json_data = '{"a": [{"emoji": "πŸ˜€", "is_base": true, "rank": 1}, {"emoji": "πŸ˜…", "is_base": false, "rank": 2}]}'
expected_tsv_output = (
"word\temoji\tis_base\trank\n" "a\tπŸ˜€\tTrue\t1\n" "a\tπŸ˜…\tFalse\t2\n"
)
mock_file_obj = StringIO(json_data)

self.setup_language_map(mock_language_map)

# Mock input file path.
mock_input_file_path = MagicMock(spec=Path)
mock_input_file_path.suffix = ".json"
Expand Down Expand Up @@ -697,20 +625,15 @@ def test_convert_to_csv_or_tsv_listofdicts_to_tsv(
expected_tsv_output = self.normalize_line_endings(expected_tsv_output)
self.assertEqual(written_data, expected_tsv_output)

@patch("scribe_data.cli.convert.language_map", autospec=True)
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_csv_or_tsv_liststrings_to_csv(
self, mock_path_class, mock_language_map
):
def test_convert_to_csv_or_tsv_liststrings_to_csv(self, mock_path_class):
json_data = '{"a": ["x", "y", "z"]}'
expected_csv_output = (
"autosuggestion,autosuggestion_1,autosuggestion_2,autosuggestion_3\n"
"a,x,y,z\n"
)
mock_file_obj = StringIO(json_data)

self.setup_language_map(mock_language_map)

mock_input_file_path = MagicMock(spec=Path)
mock_input_file_path.suffix = ".json"
mock_input_file_path.exists.return_value = True
Expand Down Expand Up @@ -744,20 +667,15 @@ def test_convert_to_csv_or_tsv_liststrings_to_csv(
expected_csv_output = self.normalize_line_endings(expected_csv_output)
self.assertEqual(written_data, expected_csv_output)

@patch("scribe_data.cli.convert.language_map", autospec=True)
@patch("scribe_data.cli.convert.Path", autospec=True)
def test_convert_to_csv_or_tsv_liststrings_to_tsv(
self, mock_path_class, mock_language_map
):
def test_convert_to_csv_or_tsv_liststrings_to_tsv(self, mock_path_class):
json_data = '{"a": ["x", "y", "z"]}'
expected_tsv_output = (
"autosuggestion\tautosuggestion_1\tautosuggestion_2\tautosuggestion_3\n"
"a\tx\ty\tz\n"
)
mock_file_obj = StringIO(json_data)

self.setup_language_map(mock_language_map)

mock_input_file_path = MagicMock(spec=Path)
mock_input_file_path.suffix = ".json"
mock_input_file_path.exists.return_value = True
Expand Down

0 comments on commit fbb5e5a

Please sign in to comment.