-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finished Error handling class and tests
- Loading branch information
1 parent
961d2f6
commit 65e513f
Showing
8 changed files
with
116 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
} |
5 changes: 5 additions & 0 deletions
5
i18nilize/tests/resources/error_handling/translations/combined_issues.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"Hi": 123, | ||
" ": "merci", | ||
"hello-world": "salut" | ||
} |
4 changes: 4 additions & 0 deletions
4
i18nilize/tests/resources/error_handling/translations/empty_keys.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"": "bonjour", | ||
" ": "merci" | ||
} |
4 changes: 4 additions & 0 deletions
4
i18nilize/tests/resources/error_handling/translations/invalid_file.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"hello": "bonjour" | ||
"thanks": "merci" | ||
} |
4 changes: 4 additions & 0 deletions
4
i18nilize/tests/resources/error_handling/translations/non_string_values.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"Hi": 123, | ||
"Thanks": "merci" | ||
} |
4 changes: 4 additions & 0 deletions
4
i18nilize/tests/resources/error_handling/translations/valid_keys.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"hello": "bonjour", | ||
"thanks": "merci" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |