Skip to content

Commit

Permalink
Minor formatting and edits to outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtavis committed Oct 15, 2024
1 parent 424f5c1 commit ad8d2b0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
52 changes: 31 additions & 21 deletions src/scribe_data/cli/cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import difflib
import json
from pathlib import Path
from typing import Union, List
from typing import List, Union

from scribe_data.utils import DEFAULT_JSON_EXPORT_DIR

Expand Down Expand Up @@ -164,15 +164,16 @@ def validate_language_and_data_type(
Parameters
----------
language : str or list
The language(s) to validate.
data_type : str or list
The data type(s) to validate.
language : str or list
The language(s) to validate.
data_type : str or list
The data type(s) to validate.
Raises
------
ValueError
If any of the languages or data types is invalid, with all errors reported together.
ValueError
If any of the languages or data types is invalid, with all errors reported together.
"""

def validate_single_item(item, valid_options, item_type):
Expand All @@ -181,17 +182,17 @@ def validate_single_item(item, valid_options, item_type):
Parameters
----------
item : str
The item to validate.
valid_options : list
A list of valid options against which the item will be validated.
item_type : str
A description of the item type (e.g., "language", "data-type") used in error messages.
item : str
The item to validate.
valid_options : list
A list of valid options against which the item will be validated.
item_type : str
A description of the item type (e.g., "language", "data-type") used in error messages.
Returns
-------
str or None
Returns an error message if the item is invalid, or None if the item is valid.
str or None
Returns an error message if the item is invalid, or None if the item is valid.
"""
if (
isinstance(item, str)
Expand All @@ -201,45 +202,54 @@ def validate_single_item(item, valid_options, item_type):
):
closest_match = difflib.get_close_matches(item, valid_options, n=1)
closest_match_str = (
f" The closest matching {item_type} is {closest_match[0]}"
f" The closest matching {item_type} is {closest_match[0]}."
if closest_match
else ""
)
return f"Invalid {item_type} {item}{closest_match_str}"

return f"Invalid {item_type} {item}.{closest_match_str}"

return None

errors = []

# Handle language validation
# Handle language validation.
if language is None or isinstance(language, bool):
pass

elif isinstance(language, str):
language = [language]

elif not isinstance(language, list):
errors.append("Language must be a string or a list of strings.")

if language is not None and isinstance(language, list):
for lang in language:
error = validate_single_item(lang, language_to_qid.keys(), "language")

if error:
errors.append(error)

# Handle data type validation
# Handle data type validation.
if data_type is None or isinstance(data_type, bool):
pass

elif isinstance(data_type, str):
data_type = [data_type]

elif not isinstance(data_type, list):
errors.append("Data type must be a string or a list of strings.")

if data_type is not None and isinstance(data_type, list):
for dt in data_type:
error = validate_single_item(dt, data_type_metadata.keys(), "data-type")

if error:
errors.append(error)

# Raise ValueError with the combined error message
# Raise ValueError with the combined error message.
if errors:
raise ValueError(" and ".join(errors) + " passed.")
raise ValueError("\n".join(errors))

else:
return True
3 changes: 2 additions & 1 deletion src/scribe_data/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ def main() -> None:
validate_language_and_data_type(
language=args.language, data_type=args.data_type
)

except ValueError as e:
print(e)
print(f"Input validation failed with error: {e}")
return

if args.upgrade:
Expand Down
24 changes: 8 additions & 16 deletions tests/cli/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
validate_language_and_data_type,
)

# MARK: Utils


class TestCLIUtils(unittest.TestCase):
def test_correct_data_type(self):
Expand Down Expand Up @@ -145,6 +147,9 @@ def test_print_formatted_data_unknown_type(self):
mock_print.assert_called_once_with("unknown data type")


# MARK: Validate


class TestValidateLanguageAndDataType(unittest.TestCase):
def setUp(self):
self.qid_mapping = {
Expand Down Expand Up @@ -182,9 +187,7 @@ def test_validate_language_and_data_type_invalid_language(self, mock_get_qid):
language=language_qid, data_type=data_type_qid
)

self.assertEqual(
str(context.exception), "Invalid language InvalidLanguage passed."
)
self.assertEqual(str(context.exception), "Invalid language InvalidLanguage.")

@patch("scribe_data.cli.total.get_qid_by_input")
def test_validate_language_and_data_type_invalid_data_type(self, mock_get_qid):
Expand All @@ -198,9 +201,7 @@ def test_validate_language_and_data_type_invalid_data_type(self, mock_get_qid):
language=language_qid, data_type=data_type_qid
)

self.assertEqual(
str(context.exception), "Invalid data-type InvalidDataType passed."
)
self.assertEqual(str(context.exception), "Invalid data-type InvalidDataType.")

@patch("scribe_data.cli.total.get_qid_by_input")
def test_validate_language_and_data_type_both_invalid(self, mock_get_qid):
Expand All @@ -216,7 +217,7 @@ def test_validate_language_and_data_type_both_invalid(self, mock_get_qid):

self.assertEqual(
str(context.exception),
"Invalid language InvalidLanguage and Invalid data-type InvalidDataType passed.",
"Invalid language InvalidLanguage.\nInvalid data-type InvalidDataType.",
)

def test_validate_language_and_data_type_with_list(self):
Expand All @@ -241,15 +242,6 @@ def test_validate_language_and_data_type_with_qids(self):
"validate_language_and_data_type raised ValueError unexpectedly with valid QIDs!"
)

def test_validate_language_and_data_type_invalid_list(self):
"""Test validation with invalid lists."""
languages = ["English", "Klingon"]
data_types = ["nouns", "alienverbs"]
with self.assertRaises(ValueError) as context:
validate_language_and_data_type(languages, data_types)
self.assertIn("Invalid language Klingon", str(context.exception))
self.assertIn("Invalid data-type alienverbs", str(context.exception))

def test_validate_language_and_data_type_mixed_validity_in_lists(self):
"""Test validation with mixed valid and invalid entries in lists."""
languages = ["English", "InvalidLanguage"]
Expand Down

0 comments on commit ad8d2b0

Please sign in to comment.