Skip to content

Commit

Permalink
Merge pull request #447 from OmarAI2003/simplify-cli-tests-output-check
Browse files Browse the repository at this point in the history
undefined
  • Loading branch information
andrewtavis authored Oct 23, 2024
2 parents 5ec2860 + bbf11fb commit 8620582
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/scribe_data/cli/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ def list_data_types(language: str = None) -> None:
else:
data_types = set()
for lang in languages:
language_dir = (
LANGUAGE_DATA_EXTRACTION_DIR
/ format_sublanguage_name(lang, language_metadata).capitalize()
language_dir = LANGUAGE_DATA_EXTRACTION_DIR / format_sublanguage_name(
lang, language_metadata
)
if language_dir.is_dir():
data_types.update(f.name for f in language_dir.iterdir() if f.is_dir())
Expand Down
84 changes: 69 additions & 15 deletions tests/cli/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,46 @@
from unittest.mock import call, patch

from scribe_data.cli.list import (
get_language_iso,
get_language_qid,
list_all,
list_all_languages,
list_data_types,
# list_languages,
# list_languages_for_data_type,
list_languages,
list_languages_for_data_type,
list_languages_with_metadata_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()
# mock_print.assert_has_calls(expected_calls)
@patch("builtins.print")
def test_list_languages(self, mock_print):
list_languages()

# Verify the headers
mock_print.assert_any_call("Language ISO QID ")
mock_print.assert_any_call("==========================")

# Dynamically get the first language from the metadata.
languages = list_all_languages()
first_language = languages[0]
first_iso = get_language_iso(first_language)
first_qid = get_language_qid(first_language)

# Verify the first language entry.
# Calculate column widths as in the actual function.
language_col_width = max(len(lang) for lang in languages) + 2
iso_col_width = max(len(get_language_iso(lang)) for lang in languages) + 2
qid_col_width = max(len(get_language_qid(lang)) for lang in languages) + 2

# Verify the first language entry with dynamic spacing.
mock_print.assert_any_call(
f"{first_language.capitalize():<{language_col_width}} {first_iso:<{iso_col_width}} {first_qid:<{qid_col_width}}"
)
# Total print calls: N (languages) + 5 (initial line, header, one separator, final line).
self.assertEqual(mock_print.call_count, len(languages) + 4)

@patch("builtins.print")
def test_list_data_types_all_languages(self, mock_print):
Expand Down Expand Up @@ -127,15 +153,43 @@ 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("--------------------------"),
# ]
# mock_print.assert_has_calls(expected_calls)
@patch("builtins.print")
def test_list_languages_for_data_type_valid(self, mock_print):
# Call the function with a specific data type.
list_languages_for_data_type("nouns")

# Dynamically create the header based on column widths.
all_languages = list_languages_with_metadata_for_data_type()

# Calculate column widths as in the actual function.
language_col_width = max(len(lang["name"]) for lang in all_languages) + 2
iso_col_width = max(len(lang["iso"]) for lang in all_languages) + 2
qid_col_width = max(len(lang["qid"]) for lang in all_languages) + 2

# Dynamically generate the expected header string.
expected_header = f"{'Language':<{language_col_width}} {'ISO':<{iso_col_width}} {'QID':<{qid_col_width}}"

# Verify the headers dynamically
mock_print.assert_any_call(expected_header)
mock_print.assert_any_call(
"=" * (language_col_width + iso_col_width + qid_col_width)
)

# Verify the first language entry if there are any languages.

first_language = all_languages[0]["name"].capitalize()
first_iso = all_languages[0]["iso"]
first_qid = all_languages[0]["qid"]

# Verify the first language entry with dynamic spacing.
mock_print.assert_any_call(
f"{first_language:<{language_col_width}} {first_iso:<{iso_col_width}} {first_qid:<{qid_col_width}}"
)

# Check the total number of calls.
# Total calls = N (languages) + 5 (initial line, header, one separator, final line)
expected_calls = len(all_languages) + 4
self.assertEqual(mock_print.call_count, expected_calls)

@patch("scribe_data.cli.list.list_languages")
def test_list_languages_command(self, mock_list_languages):
Expand Down

0 comments on commit 8620582

Please sign in to comment.