From 454f368f94fe0d93e3bbe9bbf9ed244110d06b5a Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 26 Apr 2019 17:00:01 -0300 Subject: [PATCH] tests.test_embeds: add tests for path_exists - embed.sanitizer: better error handling when fetching the key --- litecord/embed/sanitizer.py | 5 +++-- tests/test_embeds.py | 9 ++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/litecord/embed/sanitizer.py b/litecord/embed/sanitizer.py index bc9f761..d545a79 100644 --- a/litecord/embed/sanitizer.py +++ b/litecord/embed/sanitizer.py @@ -71,8 +71,9 @@ def path_exists(embed: Embed, components_in: Union[List[str], str]): # (via recursion) try: return path_exists(embed[current], components[1:]) - except KeyError: - # if the current component doesn't exist, return False + except (KeyError, TypeError, ValueError): + # if the current component doesn't exist or we can't do a + # key fetch, return False return False diff --git a/tests/test_embeds.py b/tests/test_embeds.py index 05f597c..cbf161c 100644 --- a/tests/test_embeds.py +++ b/tests/test_embeds.py @@ -19,6 +19,7 @@ along with this program. If not, see . from litecord.schemas import validate from litecord.embed.schemas import EMBED_OBJECT +from litecord.embed.sanitizer import path_exists def validate_embed(embed): return validate(embed, EMBED_OBJECT) @@ -95,8 +96,14 @@ def test_fields(): ] }) - valid({ + assert invalid({ 'fields': [ {'name': 'a'}, ] }) + + +def test_path_exists(): + """Test the path_exists() function for embed sanitization.""" + assert path_exists({'a': {'b': 2}}, 'a.b') + assert not path_exists({'a': 'b'}, 'a.b')