Skip to content

Commit

Permalink
Refactor list_languages and list_languages_for_data_type functions: D…
Browse files Browse the repository at this point in the history
…ynamic spacing and improved test coverage

- Implement dynamic spacing for language, ISO, and QID columns in both functions.
- Update unit tests to verify header formatting and first language entry dynamically for each function.
- Enhance total print call count validation to ensure accuracy.
  • Loading branch information
OmarAI2003 committed Oct 20, 2024
1 parent 9d5c37c commit c44f4ae
Showing 1 changed file with 72 additions and 15 deletions.
87 changes: 72 additions & 15 deletions tests/cli/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,46 @@
from scribe_data.cli.list import (
list_all,
list_data_types,
# list_languages,
# list_languages_for_data_type,
list_languages,
list_languages_for_data_type,
list_wrapper,
list_all_languages,
get_language_iso,
get_language_qid,
list_languages_with_metadata_for_data_type,
)
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):
# Call the function
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}}"
)
self.assertEqual(
mock_print.call_count, len(languages) + 5
) # Total print calls: N (languages) + 5 (initial line, header, two separators, final line).

@patch("builtins.print")
def test_list_data_types_all_languages(self, mock_print):
Expand Down Expand Up @@ -129,15 +157,44 @@ 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
expected_calls = (
len(all_languages) + 5
) # Total calls = N (languages) + 5 (initial line, header, two separators, final line)
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 c44f4ae

Please sign in to comment.