Skip to content

Commit

Permalink
Finished Error handling class and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexLuo602 committed Dec 3, 2024
1 parent 961d2f6 commit 65e513f
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 43 deletions.
51 changes: 35 additions & 16 deletions i18nilize/src/internationalize/error_handler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os

"""
Expand All @@ -8,7 +9,7 @@ class ErrorHandler():
Sets up the directory where the languages are located
"""
def __init__(self, translations_dir):
self.translations_dif = translations_dir
self.translations_dir = translations_dir

"""
Verify each language file is valid
Expand All @@ -20,12 +21,12 @@ def verify_languages(self):
"""
errors = {}

all_languages = os.listdir(self.translations_dif)
all_language_files = os.listdir(self.translations_dir)

for language in all_languages:
error = self.handle_error(language, False)
for language_file in all_language_files:
error = self.handle_error(language_file)
if error != "":
errors[language] = error
errors[language_file] = error

return errors

Expand All @@ -35,19 +36,19 @@ def verify_languages(self):
Input: string: language_file (source of error), boolean: error_expected (is the error expected?)\
Output: descriptive string about the error from the language file
"""
def handle_error(self, language, error_expected):
def handle_error(self, language_file, error_expected=False):
result = ""

# Verify if file is invalid
result = self.handle_invalid_file(language)
result = self.handle_invalid_file(language_file)
if result != "":
return result

# Verify if any keys are invalid
result = self.handle_invalid_keys(language)
result = self.handle_invalid_keys(language_file)

if result == "" and error_expected:
raise Exception(f"expected error in {language} but no error was found")
raise Exception(f"expected error in {language_file} but no error was found")

return result

Expand All @@ -57,7 +58,17 @@ def handle_error(self, language, error_expected):
- An empty string if there isn't any errors
- A descriptive message about the error
"""
def handle_invalid_file(self, language):
def handle_invalid_file(self, language_file):
language_location = os.path.join(self.translations_dir, language_file)
try:
with open(language_location, "r") as file:
json.load(file)
except json.JSONDecodeError as e:
return "Invalid Language File, try fixing the json format."
except Exception as e:
print(f"Unexpected Error: {e}")
raise e
# return empty string if no error found
return ""

"""
Expand All @@ -66,10 +77,18 @@ def handle_invalid_file(self, language):
- An empty string if there aren't any errors
- A descriptive message about the invalid key(s)
"""
def handle_invalid_keys(self, language):
for key in language.keys():
if not isinstance(key,str):
return "Key is not a string."
if not key.strip():
return "Key is empty."
def handle_invalid_keys(self, language_file):
language_location = os.path.join(self.translations_dir, language_file)
language = {}
try:
with open(language_location, "r") as file:
language = json.load(file)
for key in language:
if not isinstance(language[key], str):
return "Value is not a string."
if not key.strip():
return "Key is empty."
except Exception as e:
print(f"Unexpected Error: {e}")
raise e
return ""
6 changes: 6 additions & 0 deletions i18nilize/tests/resources/error_handling/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"combined_issues.json": "Value is not a string.",
"empty_keys.json": "Key is empty.",
"invalid_file.json": "Invalid Language File, try fixing the json format.",
"non_string_values.json": "Value is not a string."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Hi": 123,
" ": "merci",
"hello-world": "salut"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"": "bonjour",
" ": "merci"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hello": "bonjour"
"thanks": "merci"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Hi": 123,
"Thanks": "merci"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hello": "bonjour",
"thanks": "merci"
}
81 changes: 54 additions & 27 deletions i18nilize/tests/test_error_handler.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,66 @@
import json
import os
import unittest
from src.internationalize.error_handler import ErrorHandler

class TestErrorHandler(unittest.TestCase):
def setUp(self):
self.test_folder = os.path.join("tests", "resources", "error_handling")
self.translations_folder = os.path.join(self.test_folder, "translations")
self.handler = ErrorHandler(self.translations_folder)

# ================== Test Keys ================================
def test_valid_keys(self):
handler = ErrorHandler({})
language = {
"hello": "bonjour",
"thanks": "merci"
}
self.assertEqual(handler.handle_invalid_keys(language), "")

def test_non_string_keys(self):
handler = ErrorHandler({})
language = {
123: "bonjour",
None: "merci"
}
self.assertEqual(handler.handle_invalid_keys(language), "Key is not a string.")
language = "valid_keys.json"
self.assertEqual(self.handler.handle_invalid_keys(language), "")

def test_non_string_values(self):
language = "non_string_values.json"
self.assertEqual(self.handler.handle_invalid_keys(language), "Value is not a string.")

def test_empty_or_whitespace_keys(self):
handler = ErrorHandler({})
language = {
"": "bonjour",
" ": "merci"
}
self.assertEqual(handler.handle_invalid_keys(language), "Key is empty.")
language = "empty_keys.json"
self.assertEqual(self.handler.handle_invalid_keys(language), "Key is empty.")

def test_combined_issues(self):
handler = ErrorHandler({})
language = {
123: "bonjour",
" ": "merci",
"hello-world": "salut"
}
self.assertEqual(handler.handle_invalid_keys(language), "Key is not a string.")
language = "combined_issues.json"
self.assertEqual(self.handler.handle_invalid_keys(language), "Value is not a string.")

# ================== Test Invalid Files ================================
def test_invalid_file(self):
language = "invalid_file.json"
self.assertEqual(self.handler.handle_invalid_file(language), "Invalid Language File, try fixing the json format.")

def test_invalid_file(self):
language = "invalid_file.json"
self.assertEqual(self.handler.handle_invalid_file(language), "Invalid Language File, try fixing the json format.")

# ================== Test Error Handler ================================
def test_invalid_file(self):
language = "invalid_file.json"
self.assertEqual(self.handler.handle_error(language), "Invalid Language File, try fixing the json format.")

def test_non_string_values(self):
language = "non_string_values.json"
self.assertEqual(self.handler.handle_error(language), "Value is not a string.")

def test_valid_expected(self):
language = "valid_keys.json"
self.assertEqual(self.handler.handle_error(language), "")

def test_valid_unexpected(self):
language = "valid_keys.json"
with self.assertRaises(Exception) as context:
self.handler.handle_error(language, True)

self.assertEqual(str(context.exception), "expected error in valid_keys.json but no error was found")

# ================== Test Folder ================================
def test_invalid_file(self):
with open(os.path.join(self.test_folder, "expected.json"), "r") as file:
expected = json.load(file)

self.assertEqual(self.handler.verify_languages(), expected)

if __name__ == "__main__":
unittest.main()

0 comments on commit 65e513f

Please sign in to comment.