Browse Source

Add more tests for colour, embeds, files, buttons and modals

pull/9963/head
Jun-Ah 준아 6 months ago
committed by GitHub
parent
commit
0298f81a5c
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 66
      tests/test_colour.py
  2. 269
      tests/test_embed.py
  3. 56
      tests/test_files.py
  4. 167
      tests/test_ui_buttons.py
  5. 102
      tests/test_ui_modals.py

66
tests/test_colour.py

@ -44,6 +44,7 @@ import pytest
('rgb(20%, 24%, 56%)', 0x333D8F),
('rgb(20%, 23.9%, 56.1%)', 0x333D8F),
('rgb(51, 61, 143)', 0x333D8F),
('0x#333D8F', 0x333D8F),
],
)
def test_from_str(value, expected):
@ -53,6 +54,7 @@ def test_from_str(value, expected):
@pytest.mark.parametrize(
('value'),
[
None,
'not valid',
'0xYEAH',
'#YEAH',
@ -62,8 +64,72 @@ def test_from_str(value, expected):
'rgb(30, -1, 60)',
'invalid(a, b, c)',
'rgb(',
'#1000000',
'#FFFFFFF',
"rgb(101%, 50%, 50%)",
"rgb(50%, -10%, 50%)",
"rgb(50%, 50%, 150%)",
"rgb(256, 100, 100)",
],
)
def test_from_str_failures(value):
with pytest.raises(ValueError):
discord.Colour.from_str(value)
@pytest.mark.parametrize(
('value', 'expected'),
[
(discord.Colour.default(), 0x000000),
(discord.Colour.teal(), 0x1ABC9C),
(discord.Colour.dark_teal(), 0x11806A),
(discord.Colour.brand_green(), 0x57F287),
(discord.Colour.green(), 0x2ECC71),
(discord.Colour.dark_green(), 0x1F8B4C),
(discord.Colour.blue(), 0x3498DB),
(discord.Colour.dark_blue(), 0x206694),
(discord.Colour.purple(), 0x9B59B6),
(discord.Colour.dark_purple(), 0x71368A),
(discord.Colour.magenta(), 0xE91E63),
(discord.Colour.dark_magenta(), 0xAD1457),
(discord.Colour.gold(), 0xF1C40F),
(discord.Colour.dark_gold(), 0xC27C0E),
(discord.Colour.orange(), 0xE67E22),
(discord.Colour.dark_orange(), 0xA84300),
(discord.Colour.brand_red(), 0xED4245),
(discord.Colour.red(), 0xE74C3C),
(discord.Colour.dark_red(), 0x992D22),
(discord.Colour.lighter_grey(), 0x95A5A6),
(discord.Colour.dark_grey(), 0x607D8B),
(discord.Colour.light_grey(), 0x979C9F),
(discord.Colour.darker_grey(), 0x546E7A),
(discord.Colour.og_blurple(), 0x7289DA),
(discord.Colour.blurple(), 0x5865F2),
(discord.Colour.greyple(), 0x99AAB5),
(discord.Colour.dark_theme(), 0x313338),
(discord.Colour.fuchsia(), 0xEB459E),
(discord.Colour.yellow(), 0xFEE75C),
(discord.Colour.dark_embed(), 0x2B2D31),
(discord.Colour.light_embed(), 0xEEEFF1),
(discord.Colour.pink(), 0xEB459F),
],
)
def test_static_colours(value, expected):
assert value.value == expected
@pytest.mark.parametrize(
('value', 'property', 'expected'),
[
(discord.Colour(0x000000), 'r', 0),
(discord.Colour(0xFFFFFF), 'g', 255),
(discord.Colour(0xABCDEF), 'b', 239),
(discord.Colour(0x44243B), 'r', 68),
(discord.Colour(0x333D8F), 'g', 61),
(discord.Colour(0xDBFF00), 'b', 0),
],
)
def test_colour_properties(value, property, expected):
assert getattr(value, property) == expected

269
tests/test_embed.py

@ -0,0 +1,269 @@
"""
The MIT License (MIT)
Copyright (c) 2015-present Rapptz
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
import datetime
import discord
import pytest
@pytest.mark.parametrize(
('title', 'description', 'colour', 'url'),
[
('title', 'description', 0xABCDEF, 'https://example.com'),
('title', 'description', 0xFF1294, None),
('title', 'description', discord.Colour(0x333D8F), 'https://example.com'),
('title', 'description', discord.Colour(0x44243B), None),
],
)
def test_embed_initialization(title, description, colour, url):
embed = discord.Embed(title=title, description=description, colour=colour, url=url)
assert embed.title == title
assert embed.description == description
assert embed.colour == colour or embed.colour == discord.Colour(colour)
assert embed.url == url
@pytest.mark.parametrize(
('text', 'icon_url'),
[
('Hello discord.py', 'https://example.com'),
('text', None),
(None, 'https://example.com'),
(None, None),
],
)
def test_embed_set_footer(text, icon_url):
embed = discord.Embed()
embed.set_footer(text=text, icon_url=icon_url)
assert embed.footer.text == text
assert embed.footer.icon_url == icon_url
def test_embed_remove_footer():
embed = discord.Embed()
embed.set_footer(text='Hello discord.py', icon_url='https://example.com')
embed.remove_footer()
assert embed.footer.text is None
assert embed.footer.icon_url is None
@pytest.mark.parametrize(
('name', 'url', 'icon_url'),
[
('Rapptz', 'http://example.com', 'http://example.com/icon.png'),
('NCPlayz', None, 'http://example.com/icon.png'),
('Jackenmen', 'http://example.com', None),
],
)
def test_embed_set_author(name, url, icon_url):
embed = discord.Embed()
embed.set_author(name=name, url=url, icon_url=icon_url)
assert embed.author.name == name
assert embed.author.url == url
assert embed.author.icon_url == icon_url
def test_embed_remove_author():
embed = discord.Embed()
embed.set_author(name='Rapptz', url='http://example.com', icon_url='http://example.com/icon.png')
embed.remove_author()
assert embed.author.name is None
assert embed.author.url is None
assert embed.author.icon_url is None
@pytest.mark.parametrize(
('thumbnail'),
[
('http://example.com'),
(None),
],
)
def test_embed_set_thumbnail(thumbnail):
embed = discord.Embed()
embed.set_thumbnail(url=thumbnail)
assert embed.thumbnail.url == thumbnail
@pytest.mark.parametrize(
('image'),
[
('http://example.com'),
(None),
],
)
def test_embed_set_image(image):
embed = discord.Embed()
embed.set_image(url=image)
assert embed.image.url == image
@pytest.mark.parametrize(
('name', 'value', 'inline'),
[
('music', 'music value', True),
('sport', 'sport value', False),
],
)
def test_embed_add_field(name, value, inline):
embed = discord.Embed()
embed.add_field(name=name, value=value, inline=inline)
assert len(embed.fields) == 1
assert embed.fields[0].name == name
assert embed.fields[0].value == value
assert embed.fields[0].inline == inline
def test_embed_insert_field():
embed = discord.Embed()
embed.add_field(name='name', value='value', inline=True)
embed.insert_field_at(0, name='name 2', value='value 2', inline=False)
assert embed.fields[0].name == 'name 2'
assert embed.fields[0].value == 'value 2'
assert embed.fields[0].inline is False
def test_embed_set_field_at():
embed = discord.Embed()
embed.add_field(name='name', value='value', inline=True)
embed.set_field_at(0, name='name 2', value='value 2', inline=False)
assert embed.fields[0].name == 'name 2'
assert embed.fields[0].value == 'value 2'
assert embed.fields[0].inline is False
def test_embed_set_field_at_failure():
embed = discord.Embed()
with pytest.raises(IndexError):
embed.set_field_at(0, name='name', value='value', inline=True)
def test_embed_clear_fields():
embed = discord.Embed()
embed.add_field(name="field 1", value="value 1", inline=False)
embed.add_field(name="field 2", value="value 2", inline=False)
embed.add_field(name="field 3", value="value 3", inline=False)
embed.clear_fields()
assert len(embed.fields) == 0
def test_embed_remove_field():
embed = discord.Embed()
embed.add_field(name='name', value='value', inline=True)
embed.remove_field(0)
assert len(embed.fields) == 0
@pytest.mark.parametrize(
('title', 'description', 'url'),
[
('title 1', 'description 1', 'https://example.com'),
('title 2', 'description 2', None),
],
)
def test_embed_copy(title, description, url):
embed = discord.Embed(title=title, description=description, url=url)
embed_copy = embed.copy()
assert embed == embed_copy
assert embed.title == embed_copy.title
assert embed.description == embed_copy.description
assert embed.url == embed_copy.url
@pytest.mark.parametrize(
('title', 'description'),
[
('title 1', 'description 1'),
('title 2', 'description 2'),
],
)
def test_embed_len(title, description):
embed = discord.Embed(title=title, description=description)
assert len(embed) == len(title) + len(description)
@pytest.mark.parametrize(
('title', 'description', 'fields', 'footer', 'author'),
[
(
'title 1',
'description 1',
[('field name 1', 'field value 1'), ('field name 2', 'field value 2')],
'footer 1',
'author 1',
),
('title 2', 'description 2', [('field name 3', 'field value 3')], 'footer 2', 'author 2'),
],
)
def test_embed_len_with_options(title, description, fields, footer, author):
embed = discord.Embed(title=title, description=description)
for name, value in fields:
embed.add_field(name=name, value=value)
embed.set_footer(text=footer)
embed.set_author(name=author)
assert len(embed) == len(title) + len(description) + len("".join([name + value for name, value in fields])) + len(
footer
) + len(author)
def test_embed_to_dict():
timestamp = datetime.datetime.now(datetime.timezone.utc)
embed = discord.Embed(title="Test Title", description="Test Description", timestamp=timestamp)
data = embed.to_dict()
assert data['title'] == "Test Title"
assert data['description'] == "Test Description"
assert data['timestamp'] == timestamp.isoformat()
def test_embed_from_dict():
data = {
'title': 'Test Title',
'description': 'Test Description',
'url': 'http://example.com',
'color': 0x00FF00,
'timestamp': '2024-07-03T12:34:56+00:00',
}
embed = discord.Embed.from_dict(data)
assert embed.title == 'Test Title'
assert embed.description == 'Test Description'
assert embed.url == 'http://example.com'
assert embed.colour is not None and embed.colour.value == 0x00FF00
assert embed.timestamp is not None and embed.timestamp.isoformat() == '2024-07-03T12:34:56+00:00'
@pytest.mark.parametrize(
('value'),
[
-0.5,
'#FFFFFF',
],
)
def test_embed_colour_setter_failure(value):
embed = discord.Embed()
with pytest.raises(TypeError):
embed.colour = value

56
tests/test_files.py

@ -27,6 +27,7 @@ from __future__ import annotations
from io import BytesIO
import discord
import pytest
FILE = BytesIO()
@ -127,3 +128,58 @@ def test_file_not_spoiler_with_overriding_name_double_spoiler():
f.filename = 'SPOILER_SPOILER_.gitignore'
assert f.filename == 'SPOILER_.gitignore'
assert f.spoiler == True
def test_file_reset():
f = discord.File('.gitignore')
f.reset(seek=True)
assert f.fp.tell() == 0
f.reset(seek=False)
assert f.fp.tell() == 0
def test_io_reset():
f = discord.File(FILE)
f.reset(seek=True)
assert f.fp.tell() == 0
f.reset(seek=False)
assert f.fp.tell() == 0
def test_io_failure():
class NonSeekableReadable(BytesIO):
def seekable(self):
return False
def readable(self):
return False
f = NonSeekableReadable()
with pytest.raises(ValueError) as excinfo:
discord.File(f)
assert str(excinfo.value) == f"File buffer {f!r} must be seekable and readable"
def test_io_to_dict():
buffer = BytesIO(b"test content")
file = discord.File(buffer, filename="test.txt", description="test description")
data = file.to_dict(0)
assert data["id"] == 0
assert data["filename"] == "test.txt"
assert data["description"] == "test description"
def test_file_to_dict():
f = discord.File('.gitignore', description="test description")
data = f.to_dict(0)
assert data["id"] == 0
assert data["filename"] == ".gitignore"
assert data["description"] == "test description"

167
tests/test_ui_buttons.py

@ -0,0 +1,167 @@
"""
The MIT License (MIT)
Copyright (c) 2015-present Rapptz
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
import discord
import pytest
def test_button_init():
button = discord.ui.Button(
label="Click me!",
)
assert button.label == "Click me!"
assert button.style == discord.ButtonStyle.secondary
assert button.disabled == False
assert button.url == None
assert button.emoji == None
assert button.sku_id == None
def test_button_with_sku_id():
button = discord.ui.Button(
label="Click me!",
sku_id=1234567890,
)
assert button.label == "Click me!"
assert button.style == discord.ButtonStyle.premium
assert button.sku_id == 1234567890
def test_button_with_url():
button = discord.ui.Button(
label="Click me!",
url="https://example.com",
)
assert button.label == "Click me!"
assert button.style == discord.ButtonStyle.link
assert button.url == "https://example.com"
def test_mix_both_custom_id_and_url():
with pytest.raises(TypeError):
discord.ui.Button(
label="Click me!",
url="https://example.com",
custom_id="test",
)
def test_mix_both_custom_id_and_sku_id():
with pytest.raises(TypeError):
discord.ui.Button(
label="Click me!",
sku_id=1234567890,
custom_id="test",
)
def test_mix_both_url_and_sku_id():
with pytest.raises(TypeError):
discord.ui.Button(
label="Click me!",
url="https://example.com",
sku_id=1234567890,
)
def test_invalid_url():
button = discord.ui.Button(
label="Click me!",
)
with pytest.raises(TypeError):
button.url = 1234567890 # type: ignore
def test_invalid_custom_id():
with pytest.raises(TypeError):
discord.ui.Button(
label="Click me!",
custom_id=1234567890, # type: ignore
)
button = discord.ui.Button(
label="Click me!",
)
with pytest.raises(TypeError):
button.custom_id = 1234567890 # type: ignore
def test_button_with_partial_emoji():
button = discord.ui.Button(
label="Click me!",
emoji="👍",
)
assert button.label == "Click me!"
assert button.emoji is not None and button.emoji.name == "👍"
def test_button_with_str_emoji():
emoji = discord.PartialEmoji(name="👍")
button = discord.ui.Button(
label="Click me!",
emoji=emoji,
)
assert button.label == "Click me!"
assert button.emoji == emoji
def test_button_with_invalid_emoji():
with pytest.raises(TypeError):
discord.ui.Button(
label="Click me!",
emoji=-0.53, # type: ignore
)
button = discord.ui.Button(
label="Click me!",
)
with pytest.raises(TypeError):
button.emoji = -0.53 # type: ignore
def test_button_setter():
button = discord.ui.Button()
button.label = "Click me!"
assert button.label == "Click me!"
button.style = discord.ButtonStyle.primary
assert button.style == discord.ButtonStyle.primary
button.disabled = True
assert button.disabled == True
button.url = "https://example.com"
assert button.url == "https://example.com"
button.emoji = "👍"
assert button.emoji is not None and button.emoji.name == "👍" # type: ignore
button.custom_id = "test"
assert button.custom_id == "test"
button.sku_id = 1234567890
assert button.sku_id == 1234567890

102
tests/test_ui_modals.py

@ -0,0 +1,102 @@
"""
The MIT License (MIT)
Copyright (c) 2015-present Rapptz
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
import discord
import pytest
@pytest.mark.asyncio
async def test_modal_init():
modal = discord.ui.Modal(
title="Temp Title",
)
assert modal.title == "Temp Title"
assert modal.timeout == None
@pytest.mark.asyncio
async def test_no_title():
with pytest.raises(ValueError) as excinfo:
discord.ui.Modal()
assert str(excinfo.value) == "Modal must have a title"
@pytest.mark.asyncio
async def test_to_dict():
modal = discord.ui.Modal(
title="Temp Title",
)
data = modal.to_dict()
assert data["custom_id"] is not None
assert data["title"] == "Temp Title"
assert data["components"] == []
@pytest.mark.asyncio
async def test_add_item():
modal = discord.ui.Modal(
title="Temp Title",
)
item = discord.ui.TextInput(label="Test")
modal.add_item(item)
assert modal.children == [item]
@pytest.mark.asyncio
async def test_add_item_invalid():
modal = discord.ui.Modal(
title="Temp Title",
)
with pytest.raises(TypeError):
modal.add_item("Not an item") # type: ignore
@pytest.mark.asyncio
async def test_maximum_items():
modal = discord.ui.Modal(
title="Temp Title",
)
max_item_limit = 5
for i in range(max_item_limit):
modal.add_item(discord.ui.TextInput(label=f"Test {i}"))
with pytest.raises(ValueError):
modal.add_item(discord.ui.TextInput(label="Test"))
@pytest.mark.asyncio
async def test_modal_setters():
modal = discord.ui.Modal(
title="Temp Title",
)
modal.title = "New Title"
assert modal.title == "New Title"
modal.timeout = 120
assert modal.timeout == 120
Loading…
Cancel
Save