-
Notifications
You must be signed in to change notification settings - Fork 69
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
Add test for CLI tool #170
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c8f6627
add tests for cli
mhmohona 5597428
fix pytest
mhmohona 7a48d27
Merge branch 'main' into unittest
andrewtavis 62f4062
Minor adjustments to code formatting and import order
andrewtavis 3b23302
Merge branch 'main' into unittest
andrewtavis f2b6f4a
Merge branch 'scribe-org:main' into unittest
mhmohona d3b527f
add more tests
mhmohona a3f86de
remove unnecessary lines
mhmohona File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
""" | ||
Tests for the CLI convert functionality. | ||
|
||
.. raw:: html | ||
<!-- | ||
* Copyright (C) 2024 Scribe | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
--> | ||
""" | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
""" | ||
Tests for the CLI get functionality. | ||
|
||
.. raw:: html | ||
<!-- | ||
* Copyright (C) 2024 Scribe | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
--> | ||
""" | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
""" | ||
Tests for the list file functions. | ||
|
||
.. raw:: html | ||
<!-- | ||
* Copyright (C) 2024 Scribe | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
--> | ||
""" | ||
|
||
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe one day could be a language 😅 Wikidata does have data for it 😇 But fine for now :) |
||
|
||
@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() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm - just noting something down here that came to mind just now..
Concerning the discussion we had on providing package options without PyICU and consequently the emoji functionality - omitting
emoji-keywords
as an available data type in the command output will probably be a good idea. With that, testing that the appropriate options are getting outputted given whether with/without PyICU might be good too....but! That's for when we get there 😉