4
from datetime import datetime, timedelta
8
from disnake import Color, Embed, File, embeds
9
from disnake.utils import MISSING, utcnow
11
_BASE = {"type": "rich"}
16
time = utcnow() + timedelta(days=42)
20
description="what a cool embed",
21
url="http://endless.horse",
29
return File(io.BytesIO(b"abcd"), filename="data.txt")
32
def test_init_empty() -> None:
34
assert embed.title is None
35
assert embed.description is None
36
assert embed.url is None
37
assert embed.timestamp is None
39
assert embed.to_dict() == _BASE
40
assert not bool(embed)
42
assert embed.fields == []
45
def test_init_all(embed: Embed) -> None:
46
assert embed.timestamp
47
assert embed.to_dict() == {
50
"description": "what a cool embed",
51
"url": "http://endless.horse",
52
"timestamp": embed.timestamp.isoformat(),
58
def test_type_default() -> None:
60
assert Embed().type == "rich"
63
assert Embed.from_dict({}).type is None
66
def test_timestamp_naive(embed: Embed) -> None:
67
embed.timestamp = datetime.now()
68
assert embed.timestamp.tzinfo is not None
71
def test_len(embed: Embed) -> None:
72
assert len(embed) == 20
74
embed.set_footer(text="hmm", icon_url="https://localhost")
75
assert len(embed) == 23
77
embed.set_author(name="someone", url="https://127.0.0.1", icon_url="https://127.0.0.2")
78
assert len(embed) == 30
80
embed.add_field("field name", "field value")
81
embed.add_field("another field", "woooo")
82
assert len(embed) == 69
87
embed_1, embed_2 = Embed(), Embed()
88
assert embed_1 == embed_2
90
embed_1.title, embed_2.title = None, ""
91
assert embed_1 == embed_2
94
embed_1, embed_2 = Embed(), Embed()
95
embed_1.color = Color(123456)
96
assert not embed_1 == embed_2
98
embed_1.color = MISSING
99
assert embed_1 == embed_2
101
embed_1, embed_2 = Embed(color=None), Embed()
102
assert embed_1 == embed_2
105
Embed.set_default_color(123456)
106
assert not embed_1 == embed_2
108
Embed.set_default_color(None)
111
embed_1, embed_2 = Embed(), Embed()
112
embed_1.add_field(name="This is a test field", value="69 test 69")
113
embed_2.add_field(name="This is a test field", value="69 test 69", inline=False)
114
assert not embed_1 == embed_2
116
embed_1, embed_2 = Embed(), Embed()
118
embed_2._fields = None
119
assert embed_1 == embed_2
122
def test_embed_proxy_eq() -> None:
123
embed_1, embed_2 = Embed(), Embed()
125
embed_1.set_image("https://disnake.dev/assets/disnake-logo.png")
126
embed_2.set_image(None)
127
assert not embed_1.image == embed_2.image
129
embed_2.set_image("https://disnake.dev/assets/disnake-logo.png")
130
assert embed_1.image == embed_2.image
133
def test_color_zero() -> None:
140
assert e.to_dict() == {"color": 0, **_BASE}
141
assert e != Embed(color=None)
144
def test_default_color() -> None:
147
Embed.set_default_color(123456)
149
assert embed.color == Color(123456)
152
assert not bool(embed)
156
assert embed.color == Color(987654)
157
assert embed.to_dict() == {"color": 987654, **_BASE}
162
assert embed.color == Color(123456)
165
Embed.set_default_color(None)
166
assert embed.color is None
168
Embed.set_default_color(None)
171
def test_default_color_copy(embed: Embed) -> None:
173
assert embed.copy().color == Color(0xF8A8B8)
176
assert embed.copy().color is None
180
Embed.set_default_color(123456)
182
assert c._colour is MISSING
184
assert c.color == Color(123456)
186
Embed.set_default_color(None)
189
def test_attr_proxy() -> None:
191
author = embed.author
192
assert len(author) == 0
194
embed.set_author(name="someone")
195
author = embed.author
196
assert len(author) == 1
197
assert author.name == "someone"
198
assert embed.to_dict() == {"author": {"name": "someone"}, **_BASE}
199
assert author.icon_url is None
201
embed.set_author(name="abc", icon_url="https://xyz")
202
assert len(embed.author) == 2
204
embed.remove_author()
205
assert len(embed.author) == 0
206
assert embed.to_dict() == _BASE
209
def test_image_init() -> None:
212
assert embed._files == {}
213
assert embed.to_dict() == _BASE
216
def test_image_none() -> None:
219
embed.set_image(None)
220
assert embed._files == {}
221
assert embed.to_dict() == _BASE
224
def test_image_url() -> None:
226
embed.set_image("https://disnake.dev")
227
assert embed._files == {}
228
assert embed.to_dict() == {"image": {"url": "https://disnake.dev"}, **_BASE}
231
def test_image_file(file: File) -> None:
233
embed.set_image(file=file)
234
assert embed._files == {"image": file}
235
assert embed.to_dict() == {"image": {"url": "attachment://data.txt"}, **_BASE}
238
def test_image_remove(file: File) -> None:
240
embed.set_image(file=file)
241
embed.set_image(None)
242
assert embed._files == {}
243
assert embed.to_dict() == _BASE
246
def test_file_params(file: File) -> None:
248
with pytest.raises(TypeError):
249
embed.set_image("https://disnake.dev/assets/disnake-logo.png", file=file)
251
assert embed._files == {}
252
assert embed.to_dict() == _BASE
255
def test_file_filename(file: File) -> None:
258
with pytest.raises(TypeError):
259
embed.set_image(file=file)
262
def test_file_overwrite_url(file: File) -> None:
265
embed.set_image(file=file)
266
embed.set_image("https://abc")
267
assert embed._files == {}
268
assert embed.to_dict() == {"image": {"url": "https://abc"}, **_BASE}
271
def test_file_overwrite_file(file: File) -> None:
274
file2 = File(io.BytesIO(), filename="empty.dat")
275
embed.set_image(file=file)
276
embed.set_image(file=file2)
277
assert embed._files == {"image": file2}
278
assert embed.to_dict() == {"image": {"url": "attachment://empty.dat"}, **_BASE}
281
def test_file_multiple(file: File) -> None:
284
embed.set_image(file=file)
285
embed.set_thumbnail(file=file)
286
assert embed._files == {"image": file, "thumbnail": file}
287
assert embed.to_dict() == {
288
"image": {"url": "attachment://data.txt"},
289
"thumbnail": {"url": "attachment://data.txt"},
294
def test_fields() -> None:
297
embed.insert_field_at(42, "a", "b")
298
assert embed.to_dict() == {"fields": [{"name": "a", "value": "b", "inline": True}], **_BASE}
300
embed.insert_field_at(0, "c", "d")
301
assert embed.to_dict() == {
303
{"name": "c", "value": "d", "inline": True},
304
{"name": "a", "value": "b", "inline": True},
309
embed.set_field_at(-1, "e", "f", inline=False)
310
assert embed.to_dict() == {
312
{"name": "c", "value": "d", "inline": True},
313
{"name": "e", "value": "f", "inline": False},
318
embed.remove_field(0)
319
assert embed.to_dict() == {"fields": [{"name": "e", "value": "f", "inline": False}], **_BASE}
322
assert embed.to_dict() == _BASE
325
def test_fields_exceptions() -> None:
329
embed.remove_field(42)
332
embed.add_field("a", "b")
333
embed.remove_field(5)
335
with pytest.raises(IndexError):
336
embed.set_field_at(42, "x", "y")
339
with pytest.raises(IndexError):
340
embed.set_field_at(0, "x", "y")
343
def test_field_restraints() -> None:
344
embed = Embed(title="T" * 256)
345
embed.set_footer(text="T" * 2048)
346
embed.add_field(name="A" * 256, value="B" * 1024)
347
embed.add_field(name="A" * 256, value="B" * 1024)
348
embed.add_field(name="A" * 112, value="B" * 1024)
349
assert len(embed) == 6000
352
embed.add_field(name="A", value="B")
353
with pytest.raises(ValueError, match="^Embed total size"):
355
embed.remove_field(3)
358
embed.insert_field_at(index=3, name="A", value="B")
359
with pytest.raises(ValueError, match="^Embed total size"):
361
embed.remove_field(3)
363
embed.set_field_at(index=2, name="A" * 113, value="B" * 1024)
364
assert len(embed) == 6001
365
with pytest.raises(ValueError, match="^Embed total size"):
367
embed.set_field_at(index=2, name="A" * 112, value="B" * 1024)
368
assert len(embed) == 6000
370
embed.set_author(name="A")
371
with pytest.raises(ValueError, match="^Embed total size"):
373
embed.remove_author()
376
text="T" * 2048 + " " * 500
380
embed = Embed(title="Too many fields :WAYTOODANK:")
382
embed.add_field(name="OK", value=":D")
385
embed.add_field(name="NOTOK", value="D:")
386
with pytest.raises(ValueError, match="Embeds cannot have more than 25 fields"):
389
embed = Embed(title="author_check")
390
embed.set_author(name="A" * 256)
392
embed.set_author(name="B" * 257)
393
with pytest.raises(ValueError, match="^Embed author"):
396
embed = Embed(title="footer_check")
397
embed.set_footer(text="A" * 2048)
399
embed.set_footer(text="B" * 2049)
400
with pytest.raises(ValueError, match="^Embed footer"):
403
embed = Embed(title="title_check" + "A" * 245)
405
embed = Embed(title="title_check" + "A" * 246)
406
with pytest.raises(ValueError, match="^Embed title"):
409
embed = Embed(description="desc_check" + "A" * 4086)
411
embed = Embed(description="desc_check" + "A" * 4087)
412
with pytest.raises(ValueError, match="^Embed description"):
416
def test_copy(embed: Embed, file: File) -> None:
417
embed.set_footer(text="hi there", icon_url="https://localhost")
418
embed.set_author(name="someone", url="https://127.0.0.1", icon_url="https://127.0.0.2")
419
embed.add_field("field name", "field value")
420
embed.add_field("another field", "woooo")
421
embed.set_thumbnail("https://thumbnail.url")
422
embed.set_image(file=file)
426
assert embed.to_dict() == copy.to_dict()
429
assert embed._files == copy._files
430
assert embed._files is not copy._files
431
assert embed._fields == copy._fields
432
assert embed._fields is not copy._fields
435
def test_copy_empty() -> None:
436
e = Embed.from_dict({})
438
assert e.to_dict() == copy.to_dict() == {}
441
assert e._files == copy._files
442
assert e._files is not copy._files
443
assert e._fields is None
444
assert copy._fields is None
447
def test_copy_fields(embed: Embed) -> None:
448
embed.add_field("things", "stuff")
453
embed.insert_field_at(0, "w", "x")
455
embed.remove_field(0)
456
assert embed._fields == []
457
assert copy._fields == [{"name": "w", "value": "x", "inline": True}]
459
embed.insert_field_at(0, "w", "x")
461
embed.insert_field_at(1, "y", "z")
462
embed.set_field_at(0, "abc", "def", inline=False)
464
assert embed._fields == [
465
{"name": "abc", "value": "def", "inline": False},
466
{"name": "y", "value": "z", "inline": True},
468
assert copy._fields == [{"name": "w", "value": "x", "inline": True}]
472
def test_emptyembed() -> None:
473
with pytest.warns(DeprecationWarning):
474
assert embeds.EmptyEmbed is None
475
with pytest.warns(DeprecationWarning):
476
assert Embed.Empty is None
477
with pytest.warns(DeprecationWarning):
478
assert Embed().Empty is None
481
with pytest.raises(AttributeError):
482
_ = embeds.this_does_not_exist