tests.test_embeds: add tests for path_exists

- embed.sanitizer: better error handling when fetching the key
This commit is contained in:
Luna 2019-04-26 17:00:01 -03:00
parent 1061eda096
commit 454f368f94
2 changed files with 11 additions and 3 deletions

View File

@ -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

View File

@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
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')