From 9b5902db95eb23125a706123e0c0be29bef98b47 Mon Sep 17 00:00:00 2001 From: Luna Date: Tue, 4 Dec 2018 22:54:39 -0300 Subject: [PATCH] tests: add test_embeds - embeds.schemas: add EMBED_FIELD and EMBED_OBJECT.fields to use it --- litecord/embed/sanitizer.py | 2 - litecord/embed/schemas.py | 15 ++++++- tests/test_embeds.py | 83 +++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 tests/test_embeds.py diff --git a/litecord/embed/sanitizer.py b/litecord/embed/sanitizer.py index be3e1be..3051cd3 100644 --- a/litecord/embed/sanitizer.py +++ b/litecord/embed/sanitizer.py @@ -6,8 +6,6 @@ litecord.embed.sanitizer from typing import Dict, Any from logbook import Logger -from litecord.embed.schemas import EmbedURL - log = Logger(__name__) Embed = Dict[str, Any] diff --git a/litecord/embed/schemas.py b/litecord/embed/schemas.py index 2caf82d..39b5881 100644 --- a/litecord/embed/schemas.py +++ b/litecord/embed/schemas.py @@ -56,6 +56,18 @@ EMBED_AUTHOR = { } } +EMBED_FIELD = { + 'name': { + 'type': 'string', 'minlength': 1, 'maxlength': 128, 'required': True + }, + 'value': { + 'type': 'string', 'minlength': 1, 'maxlength': 128, 'required': True + }, + 'inline': { + 'type': 'boolean', 'required': False, 'default': True, + }, +} + EMBED_OBJECT = { 'title': { 'type': 'string', 'minlength': 1, 'maxlength': 128, 'required': False}, @@ -100,9 +112,10 @@ EMBED_OBJECT = { 'schema': EMBED_AUTHOR, 'required': False, }, + 'fields': { 'type': 'list', - 'schema': EMBED_AUTHOR, + 'schema': {'type': 'dict', 'schema': EMBED_FIELD}, 'required': False, }, } diff --git a/tests/test_embeds.py b/tests/test_embeds.py new file mode 100644 index 0000000..0374b7c --- /dev/null +++ b/tests/test_embeds.py @@ -0,0 +1,83 @@ +from litecord.schemas import validate +from litecord.embed.schemas import EMBED_OBJECT + +def validate_embed(embed): + return validate(embed, EMBED_OBJECT) + +def valid(embed: dict): + try: + validate_embed(embed) + return True + except: + return False + +def invalid(embed): + try: + validate_embed(embed) + return False + except: + return True + + +def test_empty_embed(): + valid({}) + + +def test_basic_embed(): + assert valid({ + 'title': 'test', + 'description': 'acab', + 'url': 'https://www.w3.org', + 'color': 123 + }) + + +def test_footer_embed(): + assert invalid({ + 'footer': {} + }) + + assert valid({ + 'title': 'test', + 'footer': { + 'text': 'abcdef' + } + }) + +def test_image(): + assert invalid({ + 'image': {} + }) + + assert valid({ + 'image': { + 'url': 'https://www.w3.org' + } + }) + +def test_author(): + assert invalid({ + 'author': { + 'name': '' + } + }) + + assert valid({ + 'author': { + 'name': 'abcdef' + } + }) + +def test_fields(): + assert valid({ + 'fields': [ + {'name': 'a', 'value': 'b'}, + {'name': 'c', 'value': 'd', 'inline': False}, + ] + }) + + valid({ + 'fields': [ + {'name': 'a'}, + ] + })