From c8f6627fe7b0dd8d6cebc4d6abaf5946596cddb2 Mon Sep 17 00:00:00 2001 From: Mahfuza Humayra Mohona Date: Wed, 10 Jul 2024 19:55:39 +0600 Subject: [PATCH 1/5] add tests for cli --- src/scribe_data/cli/test_scribe_data_cli.py | 221 ++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 src/scribe_data/cli/test_scribe_data_cli.py diff --git a/src/scribe_data/cli/test_scribe_data_cli.py b/src/scribe_data/cli/test_scribe_data_cli.py new file mode 100644 index 000000000..51aab9380 --- /dev/null +++ b/src/scribe_data/cli/test_scribe_data_cli.py @@ -0,0 +1,221 @@ +import unittest +from unittest.mock import patch, MagicMock +from io import StringIO + +from cli_utils import correct_data_type, print_formatted_data, get_language_data + +from main import main +from total import get_total_lexemes, get_qid_by_input +from list import ( + list_wrapper, + list_languages, + list_data_types, + list_all, + list_languages_for_data_type, +) + + +class TestScribeDataCLI(unittest.TestCase): + def setUp(self): + self.maxDiff = None + + # Test CLI Utils + + def test_correct_data_type(self): + """Test the correct_data_type function for various inputs""" + print("Running test_correct_data_type") + self.assertEqual(correct_data_type("noun"), "nouns") + self.assertEqual(correct_data_type("verbs"), "verbs") + self.assertIsNone(correct_data_type("nonexistent")) + + @patch("builtins.print") + def test_print_formatted_data(self, mock_print): + """Test the print_formatted_data function""" + print("Running test_print_formatted_data") + data = {"key1": "value1", "key2": "value2"} + print(f"Data: {data}") + print_formatted_data(data, "test_type") + mock_print.assert_called() + + # Test List Command + + @patch("sys.stdout", new_callable=StringIO) + def test_list_languages(self, mock_stdout): + """Test the list_languages function""" + print("Running test_list_languages") + list_languages() + output = mock_stdout.getvalue() + print(f"Output: {output}") + self.assertIn("Language", output) + self.assertIn("ISO", output) + self.assertIn("QID", output) + self.assertIn("English", output) + + @patch("sys.stdout", new_callable=StringIO) + def test_list_data_types(self, mock_stdout): + """Test the list_data_types function""" + print("Running test_list_data_types") + list_data_types() + output = mock_stdout.getvalue() + print(f"Output: {output}") + self.assertIn("Available data types: All languages", output) + self.assertIn("nouns", output) + self.assertIn("verbs", output) + + @patch("sys.stdout", new_callable=StringIO) + def test_list_data_types_for_language(self, mock_stdout): + """Test the list_data_types function for a specific language""" + print("Running test_list_data_types_for_language") + list_data_types("English") + output = mock_stdout.getvalue() + print(f"Output: {output}") + self.assertIn("Available data types: English", output) + + @patch("sys.stdout", new_callable=StringIO) + def test_list_all(self, mock_stdout): + """Test the list_all function""" + print("Running test_list_all") + list_all() + output = mock_stdout.getvalue() + print(f"Output: {output}") + self.assertIn("Language", output) + self.assertIn("Available data types: All languages", output) + + @patch("sys.stdout", new_callable=StringIO) + def test_list_languages_for_data_type(self, mock_stdout): + """Test the list_languages_for_data_type function""" + print("Running test_list_languages_for_data_type") + list_languages_for_data_type("nouns") + output = mock_stdout.getvalue() + print(f"Output: {output}") + self.assertIn("Available languages: nouns", output) + self.assertIn("English", output) + + @patch("list.list_languages") + @patch("list.list_data_types") + @patch("list.list_all") + @patch("list.list_languages_for_data_type") + def test_list_wrapper( + self, + mock_list_languages_for_data_type, + mock_list_all, + mock_list_data_types, + mock_list_languages, + ): + """Test the list_wrapper function for various inputs""" + print("Running test_list_wrapper") + list_wrapper() + mock_list_all.assert_called_once() + + list_wrapper(language=True) + mock_list_languages.assert_called_once() + + list_wrapper(data_type=True) + mock_list_data_types.assert_called_once() + + list_wrapper(language=True, data_type="nouns") + mock_list_languages_for_data_type.assert_called_once_with("nouns") + + # Test Total Command + + def test_get_qid_by_input(self): + """Test the get_qid_by_input function for various inputs""" + print("Running test_get_qid_by_input") + self.assertEqual(get_qid_by_input("English"), "Q1860") + self.assertEqual(get_qid_by_input("french"), "Q150") + self.assertIsNone(get_qid_by_input("NonexistentLanguage")) + + @patch("total.SPARQLWrapper") + def test_get_total_lexemes(self, mock_sparql_wrapper): + """Test the get_total_lexemes function""" + print("Running test_get_total_lexemes") + mock_sparql_wrapper.return_value.query.return_value.convert.return_value = { + "results": {"bindings": [{"total": {"value": "100000"}}]} + } + + with patch("sys.stdout", new_callable=StringIO) as mock_stdout: + get_total_lexemes("English", "nouns") + output = mock_stdout.getvalue() + print(f"Output: {output}") + self.assertIn("Language: English", output) + self.assertIn("Data type: Nouns", output) + self.assertIn("Total number of lexemes: 100000", output) + + # Edge Cases and Error Handling + + def test_list_data_types_nonexistent_language(self): + """Test that list_data_types raises a ValueError for a nonexistent language""" + print("Running test_list_data_types_nonexistent_language") + with self.assertRaises(ValueError): + list_data_types("NonexistentLanguage") + + def test_list_data_types_no_data_available(self): + """Test that list_data_types raises a ValueError when no data is available""" + print("Running test_list_data_types_no_data_available") + with patch("pathlib.Path.iterdir", return_value=[]): + with self.assertRaises(ValueError): + list_data_types("English") + + @patch("total.SPARQLWrapper") + def test_get_total_lexemes_no_results(self, mock_sparql_wrapper): + """Test the get_total_lexemes function when no results are returned""" + print("Running test_get_total_lexemes_no_results") + mock_sparql_wrapper.return_value.query.return_value.convert.return_value = { + "results": {"bindings": []} + } + + with patch("sys.stdout", new_callable=StringIO) as mock_stdout: + get_total_lexemes("English", "nouns") + output = mock_stdout.getvalue() + print(f"Output: {output}") + self.assertIn("Total number of lexemes: 0", output) + + def test_get_language_data(self): + """Test the get_language_data function for various inputs""" + print("Running test_get_language_data") + self.assertEqual(get_language_data("english", "language"), "English") + self.assertEqual(get_language_data("en", "iso"), "en") + self.assertEqual(get_language_data("Q1860", "qid"), "Q1860") + self.assertIsNone(get_language_data("nonexistent", "language")) + self.assertIsNone(get_language_data("english", "nonexistent_attribute")) + + @patch("argparse.ArgumentParser.parse_args") + def test_main_with_invalid_command(self, mock_parse_args): + """Test the main function with an invalid command""" + print("Running test_main_with_invalid_command") + mock_parse_args.return_value = MagicMock(command="invalid_command") + + # Capture stdout to check printed messages + with patch("sys.stdout", new_callable=StringIO) as mock_stdout: + with self.assertRaises(SystemExit) as cm: + main() + + # Check if the program exited with status code 1 + self.assertEqual(cm.exception.code, 1) + + # Check if the usage message is printed + self.assertIn("usage:", mock_stdout.getvalue()) + + # test_scribe_data_cli.py + + @patch("total.SPARQLWrapper") + def test_get_total_lexemes_sparql_error(self, mock_sparql_wrapper): + print("Running test_get_total_lexemes_sparql_error") + mock_sparql_wrapper.return_value.query.side_effect = Exception( + "SPARQL query failed" + ) + + with self.assertRaises(Exception): + get_total_lexemes("English", "nouns") + + @patch("argparse.ArgumentParser.parse_args") + def test_main_invalid_command(self, mock_parse_args): + """Test main function with invalid command""" + print("Running test_main_invalid_command") + mock_parse_args.return_value = MagicMock(command="invalid") + with self.assertRaises(SystemExit): + main() + + +if __name__ == "__main__": + unittest.main() From 5597428136cbba2eac24eccccabe78ecde35eefe Mon Sep 17 00:00:00 2001 From: Mahfuza Humayra Mohona Date: Wed, 10 Jul 2024 20:23:25 +0600 Subject: [PATCH 2/5] fix pytest --- src/scribe_data/cli/test_scribe_data_cli.py | 112 +------------------- 1 file changed, 4 insertions(+), 108 deletions(-) diff --git a/src/scribe_data/cli/test_scribe_data_cli.py b/src/scribe_data/cli/test_scribe_data_cli.py index 51aab9380..4605f1000 100644 --- a/src/scribe_data/cli/test_scribe_data_cli.py +++ b/src/scribe_data/cli/test_scribe_data_cli.py @@ -1,13 +1,10 @@ import unittest -from unittest.mock import patch, MagicMock +from unittest.mock import patch from io import StringIO -from cli_utils import correct_data_type, print_formatted_data, get_language_data - -from main import main -from total import get_total_lexemes, get_qid_by_input -from list import ( - list_wrapper, +from scribe_data.cli.cli_utils import correct_data_type, print_formatted_data +from scribe_data.cli.total import get_qid_by_input +from scribe_data.cli.list import ( list_languages, list_data_types, list_all, @@ -91,31 +88,6 @@ def test_list_languages_for_data_type(self, mock_stdout): self.assertIn("Available languages: nouns", output) self.assertIn("English", output) - @patch("list.list_languages") - @patch("list.list_data_types") - @patch("list.list_all") - @patch("list.list_languages_for_data_type") - def test_list_wrapper( - self, - mock_list_languages_for_data_type, - mock_list_all, - mock_list_data_types, - mock_list_languages, - ): - """Test the list_wrapper function for various inputs""" - print("Running test_list_wrapper") - list_wrapper() - mock_list_all.assert_called_once() - - list_wrapper(language=True) - mock_list_languages.assert_called_once() - - list_wrapper(data_type=True) - mock_list_data_types.assert_called_once() - - list_wrapper(language=True, data_type="nouns") - mock_list_languages_for_data_type.assert_called_once_with("nouns") - # Test Total Command def test_get_qid_by_input(self): @@ -125,22 +97,6 @@ def test_get_qid_by_input(self): self.assertEqual(get_qid_by_input("french"), "Q150") self.assertIsNone(get_qid_by_input("NonexistentLanguage")) - @patch("total.SPARQLWrapper") - def test_get_total_lexemes(self, mock_sparql_wrapper): - """Test the get_total_lexemes function""" - print("Running test_get_total_lexemes") - mock_sparql_wrapper.return_value.query.return_value.convert.return_value = { - "results": {"bindings": [{"total": {"value": "100000"}}]} - } - - with patch("sys.stdout", new_callable=StringIO) as mock_stdout: - get_total_lexemes("English", "nouns") - output = mock_stdout.getvalue() - print(f"Output: {output}") - self.assertIn("Language: English", output) - self.assertIn("Data type: Nouns", output) - self.assertIn("Total number of lexemes: 100000", output) - # Edge Cases and Error Handling def test_list_data_types_nonexistent_language(self): @@ -156,66 +112,6 @@ def test_list_data_types_no_data_available(self): with self.assertRaises(ValueError): list_data_types("English") - @patch("total.SPARQLWrapper") - def test_get_total_lexemes_no_results(self, mock_sparql_wrapper): - """Test the get_total_lexemes function when no results are returned""" - print("Running test_get_total_lexemes_no_results") - mock_sparql_wrapper.return_value.query.return_value.convert.return_value = { - "results": {"bindings": []} - } - - with patch("sys.stdout", new_callable=StringIO) as mock_stdout: - get_total_lexemes("English", "nouns") - output = mock_stdout.getvalue() - print(f"Output: {output}") - self.assertIn("Total number of lexemes: 0", output) - - def test_get_language_data(self): - """Test the get_language_data function for various inputs""" - print("Running test_get_language_data") - self.assertEqual(get_language_data("english", "language"), "English") - self.assertEqual(get_language_data("en", "iso"), "en") - self.assertEqual(get_language_data("Q1860", "qid"), "Q1860") - self.assertIsNone(get_language_data("nonexistent", "language")) - self.assertIsNone(get_language_data("english", "nonexistent_attribute")) - - @patch("argparse.ArgumentParser.parse_args") - def test_main_with_invalid_command(self, mock_parse_args): - """Test the main function with an invalid command""" - print("Running test_main_with_invalid_command") - mock_parse_args.return_value = MagicMock(command="invalid_command") - - # Capture stdout to check printed messages - with patch("sys.stdout", new_callable=StringIO) as mock_stdout: - with self.assertRaises(SystemExit) as cm: - main() - - # Check if the program exited with status code 1 - self.assertEqual(cm.exception.code, 1) - - # Check if the usage message is printed - self.assertIn("usage:", mock_stdout.getvalue()) - - # test_scribe_data_cli.py - - @patch("total.SPARQLWrapper") - def test_get_total_lexemes_sparql_error(self, mock_sparql_wrapper): - print("Running test_get_total_lexemes_sparql_error") - mock_sparql_wrapper.return_value.query.side_effect = Exception( - "SPARQL query failed" - ) - - with self.assertRaises(Exception): - get_total_lexemes("English", "nouns") - - @patch("argparse.ArgumentParser.parse_args") - def test_main_invalid_command(self, mock_parse_args): - """Test main function with invalid command""" - print("Running test_main_invalid_command") - mock_parse_args.return_value = MagicMock(command="invalid") - with self.assertRaises(SystemExit): - main() - if __name__ == "__main__": unittest.main() From 62f4062b1ed0ef2f553d82832bb8cd6fd293f684 Mon Sep 17 00:00:00 2001 From: Andrew Tavis McAllister Date: Sun, 11 Aug 2024 22:02:56 +0200 Subject: [PATCH 3/5] Minor adjustments to code formatting and import order --- src/scribe_data/cli/test_scribe_data_cli.py | 50 ++++++++++++++------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/src/scribe_data/cli/test_scribe_data_cli.py b/src/scribe_data/cli/test_scribe_data_cli.py index 4605f1000..8b036fcd8 100644 --- a/src/scribe_data/cli/test_scribe_data_cli.py +++ b/src/scribe_data/cli/test_scribe_data_cli.py @@ -1,25 +1,25 @@ import unittest -from unittest.mock import patch from io import StringIO +from unittest.mock import patch from scribe_data.cli.cli_utils import correct_data_type, print_formatted_data -from scribe_data.cli.total import get_qid_by_input from scribe_data.cli.list import ( - list_languages, - list_data_types, list_all, + list_data_types, + list_languages, list_languages_for_data_type, ) +from scribe_data.cli.total import get_qid_by_input class TestScribeDataCLI(unittest.TestCase): def setUp(self): self.maxDiff = None - # Test CLI Utils - def test_correct_data_type(self): - """Test the correct_data_type function for various inputs""" + """ + Test the correct_data_type function for various inputs. + """ print("Running test_correct_data_type") self.assertEqual(correct_data_type("noun"), "nouns") self.assertEqual(correct_data_type("verbs"), "verbs") @@ -27,7 +27,9 @@ def test_correct_data_type(self): @patch("builtins.print") def test_print_formatted_data(self, mock_print): - """Test the print_formatted_data function""" + """ + Test the print_formatted_data function. + """ print("Running test_print_formatted_data") data = {"key1": "value1", "key2": "value2"} print(f"Data: {data}") @@ -38,7 +40,9 @@ def test_print_formatted_data(self, mock_print): @patch("sys.stdout", new_callable=StringIO) def test_list_languages(self, mock_stdout): - """Test the list_languages function""" + """ + Test the list_languages function. + """ print("Running test_list_languages") list_languages() output = mock_stdout.getvalue() @@ -50,7 +54,9 @@ def test_list_languages(self, mock_stdout): @patch("sys.stdout", new_callable=StringIO) def test_list_data_types(self, mock_stdout): - """Test the list_data_types function""" + """ + Test the list_data_types function. + """ print("Running test_list_data_types") list_data_types() output = mock_stdout.getvalue() @@ -61,7 +67,9 @@ def test_list_data_types(self, mock_stdout): @patch("sys.stdout", new_callable=StringIO) def test_list_data_types_for_language(self, mock_stdout): - """Test the list_data_types function for a specific language""" + """ + Test the list_data_types function for a specific language. + """ print("Running test_list_data_types_for_language") list_data_types("English") output = mock_stdout.getvalue() @@ -70,7 +78,9 @@ def test_list_data_types_for_language(self, mock_stdout): @patch("sys.stdout", new_callable=StringIO) def test_list_all(self, mock_stdout): - """Test the list_all function""" + """ + Test the list_all function. + """ print("Running test_list_all") list_all() output = mock_stdout.getvalue() @@ -80,7 +90,9 @@ def test_list_all(self, mock_stdout): @patch("sys.stdout", new_callable=StringIO) def test_list_languages_for_data_type(self, mock_stdout): - """Test the list_languages_for_data_type function""" + """ + Test the list_languages_for_data_type function. + """ print("Running test_list_languages_for_data_type") list_languages_for_data_type("nouns") output = mock_stdout.getvalue() @@ -91,7 +103,9 @@ def test_list_languages_for_data_type(self, mock_stdout): # Test Total Command def test_get_qid_by_input(self): - """Test the get_qid_by_input function for various inputs""" + """ + Test the get_qid_by_input function for various inputs. + """ print("Running test_get_qid_by_input") self.assertEqual(get_qid_by_input("English"), "Q1860") self.assertEqual(get_qid_by_input("french"), "Q150") @@ -100,13 +114,17 @@ def test_get_qid_by_input(self): # Edge Cases and Error Handling def test_list_data_types_nonexistent_language(self): - """Test that list_data_types raises a ValueError for a nonexistent language""" + """ + Test that list_data_types raises a ValueError for a nonexistent language. + """ print("Running test_list_data_types_nonexistent_language") with self.assertRaises(ValueError): list_data_types("NonexistentLanguage") def test_list_data_types_no_data_available(self): - """Test that list_data_types raises a ValueError when no data is available""" + """ + Test that list_data_types raises a ValueError when no data is available. + """ print("Running test_list_data_types_no_data_available") with patch("pathlib.Path.iterdir", return_value=[]): with self.assertRaises(ValueError): From d3b527fde982f40207545f03762711c2385835ac Mon Sep 17 00:00:00 2001 From: Mahfuza Humayra Mohona Date: Mon, 26 Aug 2024 08:09:44 +0600 Subject: [PATCH 4/5] add more tests --- src/scribe_data/cli/test_scribe_data_cli.py | 135 --------------- tests/cli/test_convert.py | 73 +++++++++ tests/cli/test_get.py | 45 +++++ tests/cli/test_list.py | 172 ++++++++++++++++++++ tests/cli/test_total.py | 133 +++++++++++++++ 5 files changed, 423 insertions(+), 135 deletions(-) delete mode 100644 src/scribe_data/cli/test_scribe_data_cli.py create mode 100644 tests/cli/test_convert.py create mode 100644 tests/cli/test_get.py create mode 100644 tests/cli/test_list.py create mode 100644 tests/cli/test_total.py diff --git a/src/scribe_data/cli/test_scribe_data_cli.py b/src/scribe_data/cli/test_scribe_data_cli.py deleted file mode 100644 index 8b036fcd8..000000000 --- a/src/scribe_data/cli/test_scribe_data_cli.py +++ /dev/null @@ -1,135 +0,0 @@ -import unittest -from io import StringIO -from unittest.mock import patch - -from scribe_data.cli.cli_utils import correct_data_type, print_formatted_data -from scribe_data.cli.list import ( - list_all, - list_data_types, - list_languages, - list_languages_for_data_type, -) -from scribe_data.cli.total import get_qid_by_input - - -class TestScribeDataCLI(unittest.TestCase): - def setUp(self): - self.maxDiff = None - - def test_correct_data_type(self): - """ - Test the correct_data_type function for various inputs. - """ - print("Running test_correct_data_type") - self.assertEqual(correct_data_type("noun"), "nouns") - self.assertEqual(correct_data_type("verbs"), "verbs") - self.assertIsNone(correct_data_type("nonexistent")) - - @patch("builtins.print") - def test_print_formatted_data(self, mock_print): - """ - Test the print_formatted_data function. - """ - print("Running test_print_formatted_data") - data = {"key1": "value1", "key2": "value2"} - print(f"Data: {data}") - print_formatted_data(data, "test_type") - mock_print.assert_called() - - # Test List Command - - @patch("sys.stdout", new_callable=StringIO) - def test_list_languages(self, mock_stdout): - """ - Test the list_languages function. - """ - print("Running test_list_languages") - list_languages() - output = mock_stdout.getvalue() - print(f"Output: {output}") - self.assertIn("Language", output) - self.assertIn("ISO", output) - self.assertIn("QID", output) - self.assertIn("English", output) - - @patch("sys.stdout", new_callable=StringIO) - def test_list_data_types(self, mock_stdout): - """ - Test the list_data_types function. - """ - print("Running test_list_data_types") - list_data_types() - output = mock_stdout.getvalue() - print(f"Output: {output}") - self.assertIn("Available data types: All languages", output) - self.assertIn("nouns", output) - self.assertIn("verbs", output) - - @patch("sys.stdout", new_callable=StringIO) - def test_list_data_types_for_language(self, mock_stdout): - """ - Test the list_data_types function for a specific language. - """ - print("Running test_list_data_types_for_language") - list_data_types("English") - output = mock_stdout.getvalue() - print(f"Output: {output}") - self.assertIn("Available data types: English", output) - - @patch("sys.stdout", new_callable=StringIO) - def test_list_all(self, mock_stdout): - """ - Test the list_all function. - """ - print("Running test_list_all") - list_all() - output = mock_stdout.getvalue() - print(f"Output: {output}") - self.assertIn("Language", output) - self.assertIn("Available data types: All languages", output) - - @patch("sys.stdout", new_callable=StringIO) - def test_list_languages_for_data_type(self, mock_stdout): - """ - Test the list_languages_for_data_type function. - """ - print("Running test_list_languages_for_data_type") - list_languages_for_data_type("nouns") - output = mock_stdout.getvalue() - print(f"Output: {output}") - self.assertIn("Available languages: nouns", output) - self.assertIn("English", output) - - # Test Total Command - - def test_get_qid_by_input(self): - """ - Test the get_qid_by_input function for various inputs. - """ - print("Running test_get_qid_by_input") - self.assertEqual(get_qid_by_input("English"), "Q1860") - self.assertEqual(get_qid_by_input("french"), "Q150") - self.assertIsNone(get_qid_by_input("NonexistentLanguage")) - - # Edge Cases and Error Handling - - def test_list_data_types_nonexistent_language(self): - """ - Test that list_data_types raises a ValueError for a nonexistent language. - """ - print("Running test_list_data_types_nonexistent_language") - with self.assertRaises(ValueError): - list_data_types("NonexistentLanguage") - - def test_list_data_types_no_data_available(self): - """ - Test that list_data_types raises a ValueError when no data is available. - """ - print("Running test_list_data_types_no_data_available") - with patch("pathlib.Path.iterdir", return_value=[]): - with self.assertRaises(ValueError): - list_data_types("English") - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/cli/test_convert.py b/tests/cli/test_convert.py new file mode 100644 index 000000000..eb56b80b7 --- /dev/null +++ b/tests/cli/test_convert.py @@ -0,0 +1,73 @@ +""" +Tests for the CLI convert functionality. + +.. raw:: html + +""" + +import unittest +from unittest.mock import patch +from pathlib import Path +from scribe_data.cli.convert import export_json, convert_to_sqlite + + +class TestConvert(unittest.TestCase): + + @patch('scribe_data.cli.convert.Path') + @patch('scribe_data.cli.convert.data_to_sqlite') + @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 + + convert_to_sqlite('english', 'nouns', '/output', True) + + 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') + def test_convert_to_sqlite_no_output_dir(self, mock_data_to_sqlite, mock_path): + convert_to_sqlite('english', 'nouns', None, True) + + mock_data_to_sqlite.assert_called_with(['english'], ['nouns']) + mock_path.assert_not_called() + + @patch('scribe_data.cli.convert.Path') + @patch('scribe_data.cli.convert.data_to_sqlite') + @patch('scribe_data.cli.convert.get_language_iso') + @patch('shutil.copy') + def test_convert_to_sqlite_with_language_iso(self, mock_copy, mock_get_language_iso, mock_data_to_sqlite, mock_path): + mock_get_language_iso.return_value = "en" + mock_path.return_value.exists.return_value = True + + convert_to_sqlite("English", "data_type", "/output", True) + + mock_data_to_sqlite.assert_called_with(["English"], ["data_type"]) + mock_copy.assert_called() + + @patch('scribe_data.cli.convert.language_map') + def test_export_json_invalid_language(self, mock_language_map): + mock_language_map.get.return_value = None + + with self.assertRaises(ValueError): + export_json("invalid", "data_type", Path("/output"), True) + + + def test_convert_to_sqlite_no_language(self): + with self.assertRaises(ValueError): + convert_to_sqlite(None, "data_type", "/output", True) diff --git a/tests/cli/test_get.py b/tests/cli/test_get.py new file mode 100644 index 000000000..4bd09b5bd --- /dev/null +++ b/tests/cli/test_get.py @@ -0,0 +1,45 @@ +""" +Tests for the CLI get functionality. + +.. raw:: html + +""" + +import unittest +from unittest.mock import patch, call +from scribe_data.cli.get import get_data + +class TestCLIGetCommand(unittest.TestCase): + @patch('scribe_data.cli.get.query_data') + @patch('scribe_data.cli.get.export_json') + @patch('scribe_data.cli.get.convert_to_csv_or_tsv') + @patch('os.system') + def test_get_command(self, mock_system, mock_convert, mock_export_json, mock_query_data): + expected_calls = [ + call(['English'], ['nouns']), + call(['English'], ['nouns']), + call() + ] + + # Execute the test + get_data(language='English', data_type='nouns', output_dir='outputs', output_type='json') + get_data(language='English', data_type='nouns', output_dir='outputs', output_type='csv') + get_data(all=True) + + # Validate the calls + mock_query_data.assert_has_calls(expected_calls, any_order=True) diff --git a/tests/cli/test_list.py b/tests/cli/test_list.py new file mode 100644 index 000000000..8fa2f5c97 --- /dev/null +++ b/tests/cli/test_list.py @@ -0,0 +1,172 @@ +""" +Tests for the list file functions. + +.. raw:: html + +""" + +import unittest +from unittest.mock import patch, call +from scribe_data.cli.list import ( + list_languages, + list_data_types, + list_all, + list_languages_for_data_type, + list_wrapper, +) +from scribe_data.cli.main import main + +class TestListFunctions(unittest.TestCase): + @patch("builtins.print") + def test_list_languages(self, mock_print): + list_languages() + expected_calls = [ + call(), + call('Language ISO QID '), + call('-----------------------'), + call('English en Q1860 '), + call('French fr Q150 '), + call('German de Q188 '), + call('Italian it Q652 '), + call('Portuguese pt Q5146 '), + call('Russian ru Q7737 '), + call('Spanish es Q1321 '), + call('Swedish sv Q9027 '), + call('-----------------------'), + call(), + ] + mock_print.assert_has_calls(expected_calls) + + @patch("builtins.print") + def test_list_data_types_all_languages(self, mock_print): + list_data_types() + expected_calls = [ + call(), + call('Available data types: All languages'), + call('-----------------------------------'), + call('emoji-keywords'), + call('nouns'), + call('prepositions'), + call('translations'), + call('verbs'), + call('-----------------------------------'), + call(), + ] + mock_print.assert_has_calls(expected_calls) + + @patch("builtins.print") + def test_list_data_types_specific_language(self, mock_print): + list_data_types("English") + expected_calls = [ + call(), + call('Available data types: English'), + call('-----------------------------'), + call('emoji-keywords'), + call('nouns'), + call('translations'), + call('verbs'), + call('-----------------------------'), + call(), + ] + mock_print.assert_has_calls(expected_calls) + + def test_list_data_types_invalid_language(self): + with self.assertRaises(ValueError): + list_data_types("InvalidLanguage") + + def test_list_data_types_no_data_types(self): + with self.assertRaises(ValueError): + list_data_types("Klingon") + + @patch("scribe_data.cli.list.list_languages") + @patch("scribe_data.cli.list.list_data_types") + def test_list_all(self, mock_list_data_types, mock_list_languages): + list_all() + mock_list_languages.assert_called_once() + mock_list_data_types.assert_called_once() + + @patch("scribe_data.cli.list.list_all") + def test_list_wrapper_all(self, mock_list_all): + list_wrapper(all_bool=True) + mock_list_all.assert_called_once() + + @patch("scribe_data.cli.list.list_languages") + def test_list_wrapper_languages(self, mock_list_languages): + list_wrapper(language=True) + mock_list_languages.assert_called_once() + + @patch("scribe_data.cli.list.list_data_types") + def test_list_wrapper_data_types(self, mock_list_data_types): + list_wrapper(data_type=True) + mock_list_data_types.assert_called_once() + + @patch("builtins.print") + def test_list_wrapper_language_and_data_type(self, mock_print): + list_wrapper(language=True, data_type=True) + mock_print.assert_called_with("Please specify either a language or a data type.") + + @patch("scribe_data.cli.list.list_languages_for_data_type") + def test_list_wrapper_languages_for_data_type(self, mock_list_languages_for_data_type): + list_wrapper(language=True, data_type="example_data_type") + mock_list_languages_for_data_type.assert_called_with("example_data_type") + + @patch("scribe_data.cli.list.list_data_types") + def test_list_wrapper_data_types_for_language(self, mock_list_data_types): + list_wrapper(language="English", data_type=True) + mock_list_data_types.assert_called_with("English") + + @patch("builtins.print") + def test_list_languages_for_data_type_valid(self, mock_print): + list_languages_for_data_type("nouns") + expected_calls = [ + call(), + call('Available languages: nouns'), + call('--------------------------'), + call('English'), + call('French'), + call('German'), + call('Italian'), + call('Portuguese'), + call('Russian'), + call('Spanish'), + call('Swedish'), + call('--------------------------'), + call(), + ] + mock_print.assert_has_calls(expected_calls) + + @patch('scribe_data.cli.list.list_languages') + def test_list_languages_command(self, mock_list_languages): + test_args = ['main.py', 'list', '--language'] + with patch('sys.argv', test_args): + main() + mock_list_languages.assert_called_once() + + @patch('scribe_data.cli.list.list_data_types') + def test_list_data_types_command(self, mock_list_data_types): + test_args = ['main.py', 'list', '--data-type'] + with patch('sys.argv', test_args): + main() + mock_list_data_types.assert_called_once() + + @patch('scribe_data.cli.list.list_all') + def test_list_all_command(self, mock_list_all): + test_args = ['main.py', 'list', '--all'] + with patch('sys.argv', test_args): + main() + mock_list_all.assert_called_once() diff --git a/tests/cli/test_total.py b/tests/cli/test_total.py new file mode 100644 index 000000000..6f43223ac --- /dev/null +++ b/tests/cli/test_total.py @@ -0,0 +1,133 @@ +""" +Tests for the CLI total functionality. + +.. raw:: html + +""" + +import unittest +from unittest.mock import patch, MagicMock, call +from scribe_data.cli.total import get_total_lexemes + +class TestTotalLexemes(unittest.TestCase): + @patch('scribe_data.cli.total.get_qid_by_input') + @patch('scribe_data.cli.total.sparql.query') + def test_get_total_lexemes_valid(self, mock_query, mock_get_qid): + # Setup mock responses + mock_get_qid.side_effect = lambda x: {'english': 'Q1860', 'nouns': 'Q1084'}.get(x.lower(), None) + mock_results = MagicMock() + mock_results.convert.return_value = { + "results": { + "bindings": [ + {"total": {"value": "42"}} + ] + } + } + mock_query.return_value = mock_results + + # Call the function + with patch('builtins.print') as mock_print: + get_total_lexemes('English', 'nouns') + + # Check outputs + mock_print.assert_called_once_with('Language: English\nData type: nouns\nTotal number of lexemes: 42') + + @patch('scribe_data.cli.total.get_qid_by_input') + @patch('scribe_data.cli.total.sparql.query') + def test_get_total_lexemes_no_results(self, mock_query, mock_get_qid): + # Setup mock responses + mock_get_qid.side_effect = lambda x: {'english': 'Q1860', 'nouns': 'Q1084'}.get(x.lower(), None) + mock_results = MagicMock() + mock_results.convert.return_value = { + "results": { + "bindings": [] + } + } + mock_query.return_value = mock_results + + # Call the function + with patch('builtins.print') as mock_print: + get_total_lexemes('English', 'nouns') + + # Check outputs + mock_print.assert_called_once_with('Total number of lexemes: Not found') + + @patch('scribe_data.cli.total.get_qid_by_input') + @patch('scribe_data.cli.total.sparql.query') + def test_get_total_lexemes_invalid_language(self, mock_query, mock_get_qid): + mock_get_qid.side_effect = lambda x: None + mock_query.return_value = MagicMock() + + with patch('builtins.print') as mock_print: + get_total_lexemes('InvalidLanguage', 'nouns') + + mock_print.assert_called_once_with('Total number of lexemes: Not found') + + @patch('scribe_data.cli.total.get_qid_by_input') + @patch('scribe_data.cli.total.sparql.query') + def test_get_total_lexemes_empty_and_none_inputs(self, mock_query, mock_get_qid): + mock_get_qid.return_value = None + mock_query.return_value = MagicMock() + + # Call the function with empty and None inputs + with patch('builtins.print') as mock_print: + get_total_lexemes('', 'nouns') + get_total_lexemes(None, 'verbs') + + expected_calls = [ + call('Total number of lexemes: Not found'), + call('Total number of lexemes: Not found') + ] + mock_print.assert_has_calls(expected_calls, any_order=True) + + + @patch('scribe_data.cli.total.get_qid_by_input') + @patch('scribe_data.cli.total.sparql.query') + def test_get_total_lexemes_nonexistent_language(self, mock_query, mock_get_qid): + mock_get_qid.return_value = None + mock_query.return_value = MagicMock() + + with patch('builtins.print') as mock_print: + get_total_lexemes('Martian', 'nouns') + + mock_print.assert_called_once_with('Total number of lexemes: Not found') + + @patch('scribe_data.cli.total.get_qid_by_input') + @patch('scribe_data.cli.total.sparql.query') + def test_get_total_lexemes_various_data_types(self, mock_query, mock_get_qid): + mock_get_qid.side_effect = lambda x: {'english': 'Q1860', 'verbs': 'Q24905', 'nouns': 'Q1084'}.get(x.lower(), None) + mock_results = MagicMock() + mock_results.convert.return_value = { + "results": { + "bindings": [ + {"total": {"value": "30"}} + ] + } + } + mock_query.return_value = mock_results + + # Call the function with different data types + with patch('builtins.print') as mock_print: + get_total_lexemes('English', 'verbs') + get_total_lexemes('English', 'nouns') + + expected_calls = [ + call('Language: English\nData type: verbs\nTotal number of lexemes: 30'), + call('Language: English\nData type: nouns\nTotal number of lexemes: 30') + ] + mock_print.assert_has_calls(expected_calls) From a3f86de65beb1b5f0e07bbec7c58788fb07e6e45 Mon Sep 17 00:00:00 2001 From: Mahfuza Humayra Mohona Date: Mon, 26 Aug 2024 10:41:09 +0600 Subject: [PATCH 5/5] remove unnecessary lines --- tests/cli/test_convert.py | 1 - tests/cli/test_total.py | 8 +------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/tests/cli/test_convert.py b/tests/cli/test_convert.py index eb56b80b7..c77a0b3a0 100644 --- a/tests/cli/test_convert.py +++ b/tests/cli/test_convert.py @@ -25,7 +25,6 @@ from pathlib import Path from scribe_data.cli.convert import export_json, convert_to_sqlite - class TestConvert(unittest.TestCase): @patch('scribe_data.cli.convert.Path') diff --git a/tests/cli/test_total.py b/tests/cli/test_total.py index 6f43223ac..ee7cdc649 100644 --- a/tests/cli/test_total.py +++ b/tests/cli/test_total.py @@ -28,7 +28,6 @@ class TestTotalLexemes(unittest.TestCase): @patch('scribe_data.cli.total.get_qid_by_input') @patch('scribe_data.cli.total.sparql.query') def test_get_total_lexemes_valid(self, mock_query, mock_get_qid): - # Setup mock responses mock_get_qid.side_effect = lambda x: {'english': 'Q1860', 'nouns': 'Q1084'}.get(x.lower(), None) mock_results = MagicMock() mock_results.convert.return_value = { @@ -40,17 +39,14 @@ def test_get_total_lexemes_valid(self, mock_query, mock_get_qid): } mock_query.return_value = mock_results - # Call the function with patch('builtins.print') as mock_print: get_total_lexemes('English', 'nouns') - # Check outputs mock_print.assert_called_once_with('Language: English\nData type: nouns\nTotal number of lexemes: 42') @patch('scribe_data.cli.total.get_qid_by_input') @patch('scribe_data.cli.total.sparql.query') def test_get_total_lexemes_no_results(self, mock_query, mock_get_qid): - # Setup mock responses mock_get_qid.side_effect = lambda x: {'english': 'Q1860', 'nouns': 'Q1084'}.get(x.lower(), None) mock_results = MagicMock() mock_results.convert.return_value = { @@ -60,11 +56,9 @@ def test_get_total_lexemes_no_results(self, mock_query, mock_get_qid): } mock_query.return_value = mock_results - # Call the function with patch('builtins.print') as mock_print: get_total_lexemes('English', 'nouns') - # Check outputs mock_print.assert_called_once_with('Total number of lexemes: Not found') @patch('scribe_data.cli.total.get_qid_by_input') @@ -95,7 +89,6 @@ def test_get_total_lexemes_empty_and_none_inputs(self, mock_query, mock_get_qid) ] mock_print.assert_has_calls(expected_calls, any_order=True) - @patch('scribe_data.cli.total.get_qid_by_input') @patch('scribe_data.cli.total.sparql.query') def test_get_total_lexemes_nonexistent_language(self, mock_query, mock_get_qid): @@ -119,6 +112,7 @@ def test_get_total_lexemes_various_data_types(self, mock_query, mock_get_qid): ] } } + mock_query.return_value = mock_results # Call the function with different data types