Skip to content

Commit

Permalink
refactor(test): add tests for utils module (resolves #50)
Browse files Browse the repository at this point in the history
* Unit tests for `utils` module
* Edit some error messages
* Add type annotations for `mypy` checks
  • Loading branch information
m-charlton committed Sep 21, 2023
1 parent 8002b57 commit 5ec8357
Show file tree
Hide file tree
Showing 2 changed files with 306 additions and 25 deletions.
58 changes: 35 additions & 23 deletions src/scribe_data/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
get_ios_data_path,
get_android_data_path,
get_desktop_data_path,
check_command_line_args
check_command_line_args,
check_and_return_command_line_args
"""

import ast
from typing import Any


def get_scribe_languages():
def get_scribe_languages() -> list[str]:
"""
Returns the list of currently implemented Scribe languages.
"""
Expand All @@ -39,7 +41,7 @@ def get_scribe_languages():
]


def get_language_qid(language):
def get_language_qid(language: str) -> str:
"""
Returns the QID of the given language.
Expand Down Expand Up @@ -67,13 +69,13 @@ def get_language_qid(language):

if language not in language_qid_dict:
raise ValueError(
f"{language.upper()} is not currently not a supported language for QID conversion."
f"{language.upper()} is currently not a supported language for QID conversion."
)

return language_qid_dict[language]


def get_language_iso(language):
def get_language_iso(language: str) -> str:
"""
Returns the ISO code of the given language.
Expand Down Expand Up @@ -101,13 +103,13 @@ def get_language_iso(language):

if language not in language_iso_dict:
raise ValueError(
f"{language.capitalize()} is not currently not a supported language for ISO conversion."
f"{language.capitalize()} is currently not a supported language for ISO conversion."
)

return language_iso_dict[language]


def get_language_from_iso(iso):
def get_language_from_iso(iso: str) -> str:
"""
Returns the language name for the given ISO.
Expand All @@ -134,14 +136,12 @@ def get_language_from_iso(iso):
}

if iso not in iso_language_dict:
raise ValueError(
f"{iso.upper()} is not currently not a supported ISO for language conversion."
)
raise ValueError(f"{iso.upper()} is currently not a supported ISO language.")

return iso_language_dict[iso]


def get_language_words_to_remove(language):
def get_language_words_to_remove(language: str) -> list[str]:
"""
Returns the words that should not be included as autosuggestions for the given language.
Expand All @@ -155,7 +155,7 @@ def get_language_words_to_remove(language):
The words that should not be included as autosuggestions for the given language as values of a dictionary.
"""
language = language.lower()
language_iso_dict = {
words_to_remove: dict[str, list[str]] = {
"english": [
"of",
"the",
Expand All @@ -181,10 +181,15 @@ def get_language_words_to_remove(language):
"swedish": ["of", "the", "The", "and", "Checklist", "Catalogue"],
}

return language_iso_dict[language]
if language not in words_to_remove:
raise ValueError(
f"{language.capitalize()} is currently not a supported language."
)

return words_to_remove[language]

def get_language_words_to_ignore(language):

def get_language_words_to_ignore(language: str) -> list[str]:
"""
Returns the words that should not be included as autosuggestions for the given language.
Expand All @@ -198,7 +203,7 @@ def get_language_words_to_ignore(language):
The words that should not be included as autosuggestions for the given language as values of a dictionary.
"""
language = language.lower()
language_iso_dict = {
words_to_ignore: dict[str, list[str]] = {
"french": [
"XXe",
],
Expand All @@ -210,31 +215,36 @@ def get_language_words_to_ignore(language):
"swedish": ["databasdump"],
}

return language_iso_dict[language]
if language not in words_to_ignore:
raise ValueError(
f"{language.capitalize()} is currently not a supported language."
)

return words_to_ignore[language]


def get_path_from_format_file():
def get_path_from_format_file() -> str:
"""
Returns the directory path from a data formatting file to scribe-org.
"""
return "../../../../../.."


def get_path_from_load_dir():
def get_path_from_load_dir() -> str:
"""
Returns the directory path from the load directory to scribe-org.
"""
return "../../../.."


def get_path_from_et_dir():
def get_path_from_et_dir() -> str:
"""
Returns the directory path from the extract_transform directory to scribe-org.
"""
return "../../../.."


def get_ios_data_path(language: str):
def get_ios_data_path(language: str) -> str:
"""
Returns the path to the data json of the iOS app given a language.
Expand All @@ -250,7 +260,7 @@ def get_ios_data_path(language: str):
return f"/Scribe-iOS/Keyboards/LanguageKeyboards/{language}"


def get_android_data_path(language: str):
def get_android_data_path(language: str) -> str:
"""
Returns the path to the data json of the Android app given a language.
Expand All @@ -266,7 +276,7 @@ def get_android_data_path(language: str):
return f"/Scribe-Android/app/src/main/LanguageKeyboards/{language}"


def get_desktop_data_path(language: str):
def get_desktop_data_path(language: str) -> str:
"""
Returns the path to the data json of the desktop app given a language.
Expand All @@ -282,7 +292,9 @@ def get_desktop_data_path(language: str):
return f"/Scribe-Desktop/scribe/language_guis/{language}"


def check_command_line_args(file_name, passed_values, values_to_check):
def check_command_line_args(
file_name: str, passed_values: Any, values_to_check: list[str]
) -> list[str]:
"""
Checks command line arguments passed to Scribe-Data files.
Expand Down
Loading

0 comments on commit 5ec8357

Please sign in to comment.