tests: add test_embeds

- embeds.schemas: add EMBED_FIELD and EMBED_OBJECT.fields to use it
This commit is contained in:
Luna 2018-12-04 22:54:39 -03:00
parent 5de64a93ee
commit 9b5902db95
3 changed files with 97 additions and 3 deletions

View File

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

View File

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

83
tests/test_embeds.py Normal file
View File

@ -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'},
]
})