From fbb5e5ae86af6b6dc46e5fefca18f35ac2aa2ef1 Mon Sep 17 00:00:00 2001 From: Andrew Tavis McAllister Date: Thu, 24 Oct 2024 23:56:27 +0200 Subject: [PATCH] Update tests to remove language map --- tests/cli/test_convert.py | 134 ++++++++------------------------------ 1 file changed, 26 insertions(+), 108 deletions(-) diff --git a/tests/cli/test_convert.py b/tests/cli/test_convert.py index 1109b803..5927f3c4 100644 --- a/tests/cli/test_convert.py +++ b/tests/cli/test_convert.py @@ -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()) @@ -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 @@ -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", @@ -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" @@ -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 @@ -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 @@ -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 @@ -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 @@ -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"}, @@ -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 @@ -316,11 +283,8 @@ 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}], @@ -328,8 +292,6 @@ def test_convert_to_json_with_complex_structure( } 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 @@ -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 @@ -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 @@ -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", @@ -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 @@ -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 @@ -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 @@ -558,11 +501,8 @@ 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"}}' ) @@ -570,8 +510,6 @@ def test_convert_to_csv_or_tsv_nesteddict_to_tsv( 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 @@ -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 @@ -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" @@ -697,11 +625,8 @@ 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" @@ -709,8 +634,6 @@ def test_convert_to_csv_or_tsv_liststrings_to_csv( ) 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 @@ -744,11 +667,8 @@ 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" @@ -756,8 +676,6 @@ def test_convert_to_csv_or_tsv_liststrings_to_tsv( ) 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