-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathtest_embeds.py
482 lines (364 loc) · 13.7 KB
/
test_embeds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# SPDX-License-Identifier: MIT
import io
from datetime import datetime, timedelta
import pytest
from disnake import Color, Embed, File, embeds
from disnake.utils import MISSING, utcnow
_BASE = {"type": "rich"}
@pytest.fixture
def embed() -> Embed:
time = utcnow() + timedelta(days=42)
return Embed(
type="link",
title="wow",
description="what a cool embed",
url="http://endless.horse",
timestamp=time,
color=0xF8A8B8,
)
@pytest.fixture
def file() -> File:
return File(io.BytesIO(b"abcd"), filename="data.txt")
def test_init_empty() -> None:
embed = Embed()
assert embed.title is None
assert embed.description is None
assert embed.url is None
assert embed.timestamp is None
assert embed.to_dict() == _BASE
assert not bool(embed)
assert embed.fields == []
def test_init_all(embed: Embed) -> None:
assert embed.timestamp
assert embed.to_dict() == {
"type": "link",
"title": "wow",
"description": "what a cool embed",
"url": "http://endless.horse",
"timestamp": embed.timestamp.isoformat(),
"color": 0xF8A8B8,
}
assert bool(embed)
def test_type_default() -> None:
# type for new embeds should be set to default value
assert Embed().type == "rich"
# type shouldn't be set in from_dict if dict didn't contain a type
assert Embed.from_dict({}).type is None
def test_timestamp_naive(embed: Embed) -> None:
embed.timestamp = datetime.now() # noqa: DTZ005 # the point of this is to test naive dts
assert embed.timestamp.tzinfo is not None
def test_len(embed: Embed) -> None:
assert len(embed) == 20
embed.set_footer(text="hmm", icon_url="https://localhost")
assert len(embed) == 23
embed.set_author(name="someone", url="https://127.0.0.1", icon_url="https://127.0.0.2")
assert len(embed) == 30
embed.add_field("field name", "field value")
embed.add_field("another field", "woooo")
assert len(embed) == 69
def test_eq() -> None:
# basic test
embed_1, embed_2 = Embed(), Embed()
assert embed_1 == embed_2
embed_1.title, embed_2.title = None, ""
assert embed_1 == embed_2
# color tests
embed_1, embed_2 = Embed(), Embed()
embed_1.color = Color(123456)
assert not embed_1 == embed_2
embed_1.color = MISSING
assert embed_1 == embed_2
embed_1, embed_2 = Embed(color=None), Embed()
assert embed_1 == embed_2
try:
Embed.set_default_color(123456)
assert not embed_1 == embed_2
finally:
Embed.set_default_color(None)
# test fields
embed_1, embed_2 = Embed(), Embed()
embed_1.add_field(name="This is a test field", value="69 test 69")
embed_2.add_field(name="This is a test field", value="69 test 69", inline=False)
assert not embed_1 == embed_2
embed_1, embed_2 = Embed(), Embed()
embed_1._fields = []
embed_2._fields = None
assert embed_1 == embed_2
def test_embed_proxy_eq() -> None:
embed_1, embed_2 = Embed(), Embed()
embed_1.set_image("https://disnake.dev/assets/disnake-logo.png")
embed_2.set_image(None)
assert not embed_1.image == embed_2.image
embed_2.set_image("https://disnake.dev/assets/disnake-logo.png")
assert embed_1.image == embed_2.image
def test_color_zero() -> None:
e = Embed()
assert not e
# color=0 should be applied to to_dict, __bool__, and __eq__
e.color = 0
assert e
assert e.to_dict() == {"color": 0, **_BASE}
assert e != Embed(color=None)
def test_default_color() -> None:
try:
# color applies if set before init
Embed.set_default_color(123456)
embed = Embed()
assert embed.color == Color(123456)
# default color should not affect __bool__
assert not bool(embed)
# custom value overrides default
embed.color = 987654
assert embed.color == Color(987654)
assert embed.to_dict() == {"color": 987654, **_BASE}
assert bool(embed)
# removing custom value resets to default
del embed.color
assert embed.color == Color(123456)
# clearing default changes color to `None`
Embed.set_default_color(None)
assert embed.color is None
finally:
Embed.set_default_color(None)
def test_default_color_copy(embed: Embed) -> None:
# copy should retain color
assert embed.copy().color == Color(0xF8A8B8)
del embed.color
assert embed.copy().color is None
try:
# copying with default value should not set attribute
Embed.set_default_color(123456)
c = embed.copy()
assert c._colour is MISSING
assert c.color == Color(123456)
finally:
Embed.set_default_color(None)
def test_attr_proxy() -> None:
embed = Embed()
author = embed.author
assert len(author) == 0
embed.set_author(name="someone")
author = embed.author
assert len(author) == 1
assert author.name == "someone"
assert embed.to_dict() == {"author": {"name": "someone"}, **_BASE}
assert author.icon_url is None
embed.set_author(name="abc", icon_url="https://xyz")
assert len(embed.author) == 2
embed.remove_author()
assert len(embed.author) == 0
assert embed.to_dict() == _BASE
def test_image_init() -> None:
# should be empty initially
embed = Embed()
assert embed._files == {}
assert embed.to_dict() == _BASE
def test_image_none() -> None:
# removing image shouldn't change dict
embed = Embed()
embed.set_image(None)
assert embed._files == {}
assert embed.to_dict() == _BASE
def test_image_url() -> None:
embed = Embed()
embed.set_image("https://disnake.dev")
assert embed._files == {}
assert embed.to_dict() == {"image": {"url": "https://disnake.dev"}, **_BASE}
def test_image_file(file: File) -> None:
embed = Embed()
embed.set_image(file=file)
assert embed._files == {"image": file}
assert embed.to_dict() == {"image": {"url": "attachment://data.txt"}, **_BASE}
def test_image_remove(file: File) -> None:
embed = Embed()
embed.set_image(file=file)
embed.set_image(None)
assert embed._files == {}
assert embed.to_dict() == _BASE
def test_file_params(file: File) -> None:
embed = Embed()
with pytest.raises(TypeError):
embed.set_image("https://disnake.dev/assets/disnake-logo.png", file=file) # type: ignore
assert embed._files == {}
assert embed.to_dict() == _BASE
def test_file_filename(file: File) -> None:
embed = Embed()
file.filename = None
with pytest.raises(TypeError):
embed.set_image(file=file)
def test_file_overwrite_url(file: File) -> None:
embed = Embed()
# setting url should remove file
embed.set_image(file=file)
embed.set_image("https://abc")
assert embed._files == {}
assert embed.to_dict() == {"image": {"url": "https://abc"}, **_BASE}
def test_file_overwrite_file(file: File) -> None:
embed = Embed()
# setting file twice should keep second one
file2 = File(io.BytesIO(), filename="empty.dat")
embed.set_image(file=file)
embed.set_image(file=file2)
assert embed._files == {"image": file2}
assert embed.to_dict() == {"image": {"url": "attachment://empty.dat"}, **_BASE}
def test_file_multiple(file: File) -> None:
embed = Embed()
# setting multiple files should work correctly
embed.set_image(file=file)
embed.set_thumbnail(file=file)
assert embed._files == {"image": file, "thumbnail": file}
assert embed.to_dict() == {
"image": {"url": "attachment://data.txt"},
"thumbnail": {"url": "attachment://data.txt"},
**_BASE,
}
def test_fields() -> None:
embed = Embed()
embed.insert_field_at(42, "a", "b")
assert embed.to_dict() == {"fields": [{"name": "a", "value": "b", "inline": True}], **_BASE}
embed.insert_field_at(0, "c", "d")
assert embed.to_dict() == {
"fields": [
{"name": "c", "value": "d", "inline": True},
{"name": "a", "value": "b", "inline": True},
],
**_BASE,
}
embed.set_field_at(-1, "e", "f", inline=False)
assert embed.to_dict() == {
"fields": [
{"name": "c", "value": "d", "inline": True},
{"name": "e", "value": "f", "inline": False},
],
**_BASE,
}
embed.remove_field(0)
assert embed.to_dict() == {"fields": [{"name": "e", "value": "f", "inline": False}], **_BASE}
embed.clear_fields()
assert embed.to_dict() == _BASE
def test_fields_exceptions() -> None:
embed = Embed()
# shouldn't raise
embed.remove_field(42)
# also shouldn't raise
embed.add_field("a", "b")
embed.remove_field(5)
with pytest.raises(IndexError):
embed.set_field_at(42, "x", "y")
embed.clear_fields()
with pytest.raises(IndexError):
embed.set_field_at(0, "x", "y")
def test_field_restraints() -> None:
embed = Embed(title="T" * 256) # Size = 256
embed.set_footer(text="T" * 2048) # Size = 2304
embed.add_field(name="A" * 256, value="B" * 1024) # Size = 3584
embed.add_field(name="A" * 256, value="B" * 1024) # Size = 4864
embed.add_field(name="A" * 112, value="B" * 1024) # Size = 6000
assert len(embed) == 6000
embed.check_limits()
embed.add_field(name="A", value="B") # Breaks limit of 6000 chars
with pytest.raises(ValueError, match="^Embed total size"):
embed.check_limits()
embed.remove_field(3)
embed.check_limits()
embed.insert_field_at(index=3, name="A", value="B") # Breaks limit of 6000 chars
with pytest.raises(ValueError, match="^Embed total size"):
embed.check_limits()
embed.remove_field(3)
embed.set_field_at(index=2, name="A" * 113, value="B" * 1024) # Breaks limit of 6000 chars
assert len(embed) == 6001
with pytest.raises(ValueError, match="^Embed total size"):
embed.check_limits()
embed.set_field_at(index=2, name="A" * 112, value="B" * 1024)
assert len(embed) == 6000
embed.set_author(name="A") # Breaks limit of 6000 chars
with pytest.raises(ValueError, match="^Embed total size"):
embed.check_limits()
embed.remove_author()
embed.set_footer(
text="T" * 2048 + " " * 500
) # Would break the 6000 limit, but leading + trailing whitespace doesn't count
embed.check_limits()
embed = Embed(title="Too many fields :WAYTOODANK:")
for _ in range(25):
embed.add_field(name="OK", value=":D")
embed.check_limits()
embed.add_field(name="NOTOK", value="D:")
with pytest.raises(ValueError, match="Embeds cannot have more than 25 fields"):
embed.check_limits()
embed = Embed(title="author_check")
embed.set_author(name="A" * 256)
embed.check_limits()
embed.set_author(name="B" * 257)
with pytest.raises(ValueError, match="^Embed author"):
embed.check_limits()
embed = Embed(title="footer_check")
embed.set_footer(text="A" * 2048)
embed.check_limits()
embed.set_footer(text="B" * 2049)
with pytest.raises(ValueError, match="^Embed footer"):
embed.check_limits()
embed = Embed(title="title_check" + "A" * 245)
embed.check_limits()
embed = Embed(title="title_check" + "A" * 246)
with pytest.raises(ValueError, match="^Embed title"):
embed.check_limits()
embed = Embed(description="desc_check" + "A" * 4086)
embed.check_limits()
embed = Embed(description="desc_check" + "A" * 4087)
with pytest.raises(ValueError, match="^Embed description"):
embed.check_limits()
def test_copy(embed: Embed, file: File) -> None:
embed.set_footer(text="hi there", icon_url="https://localhost")
embed.set_author(name="someone", url="https://127.0.0.1", icon_url="https://127.0.0.2")
embed.add_field("field name", "field value")
embed.add_field("another field", "woooo")
embed.set_thumbnail("https://thumbnail.url")
embed.set_image(file=file)
# copying should keep exact dict representation
copy = embed.copy()
assert embed.to_dict() == copy.to_dict()
# shallow copy, but `_files` and `_fields` should be copied
assert embed._files == copy._files
assert embed._files is not copy._files
assert embed._fields == copy._fields
assert embed._fields is not copy._fields
def test_copy_empty() -> None:
e = Embed.from_dict({})
copy = e.copy()
assert e.to_dict() == copy.to_dict() == {}
# shallow copy, but `_files` and `_fields` should be copied
assert e._files == copy._files
assert e._files is not copy._files
assert e._fields is None
assert copy._fields is None
def test_copy_fields(embed: Embed) -> None:
embed.add_field("things", "stuff")
copy = embed.copy()
embed.clear_fields()
assert copy._fields
embed.insert_field_at(0, "w", "x")
copy = embed.copy()
embed.remove_field(0)
assert embed._fields == []
assert copy._fields == [{"name": "w", "value": "x", "inline": True}]
embed.insert_field_at(0, "w", "x")
copy = embed.copy()
embed.insert_field_at(1, "y", "z")
embed.set_field_at(0, "abc", "def", inline=False)
assert embed._fields == [
{"name": "abc", "value": "def", "inline": False},
{"name": "y", "value": "z", "inline": True},
]
assert copy._fields == [{"name": "w", "value": "x", "inline": True}]
# backwards compatibility
def test_emptyembed() -> None:
with pytest.warns(DeprecationWarning):
assert embeds.EmptyEmbed is None # type: ignore
with pytest.warns(DeprecationWarning):
assert Embed.Empty is None # type: ignore
with pytest.warns(DeprecationWarning):
assert Embed().Empty is None # type: ignore
# make sure unknown module attrs continue to raise
with pytest.raises(AttributeError):
_ = embeds.this_does_not_exist # type: ignore