File: test_inputmedia.py

package info (click to toggle)
python-telegram-bot 22.3-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 11,060 kB
  • sloc: python: 90,298; makefile: 176; sh: 4
file content (1258 lines) | stat: -rw-r--r-- 54,439 bytes parent folder | download
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2025
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see [http://www.gnu.org/licenses/].
import asyncio
import copy
import datetime as dtm
from collections.abc import Sequence
from typing import Optional

import pytest

from telegram import (
    InputFile,
    InputMedia,
    InputMediaAnimation,
    InputMediaAudio,
    InputMediaDocument,
    InputMediaPhoto,
    InputMediaVideo,
    InputPaidMediaPhoto,
    InputPaidMediaVideo,
    Message,
    MessageEntity,
    ReplyParameters,
)
from telegram.constants import InputMediaType, ParseMode
from telegram.error import BadRequest
from telegram.request import RequestData
from telegram.warnings import PTBDeprecationWarning
from tests.auxil.files import data_file
from tests.auxil.networking import expect_bad_request
from tests.auxil.slots import mro_slots

from ..auxil.build_messages import make_message


@pytest.fixture(scope="module")
def input_media_video(class_thumb_file):
    return InputMediaVideo(
        media=InputMediaVideoTestBase.media,
        caption=InputMediaVideoTestBase.caption,
        width=InputMediaVideoTestBase.width,
        height=InputMediaVideoTestBase.height,
        duration=InputMediaVideoTestBase.duration,
        parse_mode=InputMediaVideoTestBase.parse_mode,
        caption_entities=InputMediaVideoTestBase.caption_entities,
        thumbnail=class_thumb_file,
        cover=class_thumb_file,
        start_timestamp=InputMediaVideoTestBase.start_timestamp,
        supports_streaming=InputMediaVideoTestBase.supports_streaming,
        has_spoiler=InputMediaVideoTestBase.has_spoiler,
        show_caption_above_media=InputMediaVideoTestBase.show_caption_above_media,
    )


@pytest.fixture(scope="module")
def input_media_photo():
    return InputMediaPhoto(
        media=InputMediaPhotoTestBase.media,
        caption=InputMediaPhotoTestBase.caption,
        parse_mode=InputMediaPhotoTestBase.parse_mode,
        caption_entities=InputMediaPhotoTestBase.caption_entities,
        has_spoiler=InputMediaPhotoTestBase.has_spoiler,
        show_caption_above_media=InputMediaPhotoTestBase.show_caption_above_media,
    )


@pytest.fixture(scope="module")
def input_media_animation(class_thumb_file):
    return InputMediaAnimation(
        media=InputMediaAnimationTestBase.media,
        caption=InputMediaAnimationTestBase.caption,
        parse_mode=InputMediaAnimationTestBase.parse_mode,
        caption_entities=InputMediaAnimationTestBase.caption_entities,
        width=InputMediaAnimationTestBase.width,
        height=InputMediaAnimationTestBase.height,
        thumbnail=class_thumb_file,
        duration=InputMediaAnimationTestBase.duration,
        has_spoiler=InputMediaAnimationTestBase.has_spoiler,
        show_caption_above_media=InputMediaAnimationTestBase.show_caption_above_media,
    )


@pytest.fixture(scope="module")
def input_media_audio(class_thumb_file):
    return InputMediaAudio(
        media=InputMediaAudioTestBase.media,
        caption=InputMediaAudioTestBase.caption,
        duration=InputMediaAudioTestBase.duration,
        performer=InputMediaAudioTestBase.performer,
        title=InputMediaAudioTestBase.title,
        thumbnail=class_thumb_file,
        parse_mode=InputMediaAudioTestBase.parse_mode,
        caption_entities=InputMediaAudioTestBase.caption_entities,
    )


@pytest.fixture(scope="module")
def input_media_document(class_thumb_file):
    return InputMediaDocument(
        media=InputMediaDocumentTestBase.media,
        caption=InputMediaDocumentTestBase.caption,
        thumbnail=class_thumb_file,
        parse_mode=InputMediaDocumentTestBase.parse_mode,
        caption_entities=InputMediaDocumentTestBase.caption_entities,
        disable_content_type_detection=InputMediaDocumentTestBase.disable_content_type_detection,
    )


@pytest.fixture(scope="module")
def input_paid_media_photo():
    return InputPaidMediaPhoto(
        media=InputMediaPhotoTestBase.media,
    )


@pytest.fixture(scope="module")
def input_paid_media_video(class_thumb_file):
    return InputPaidMediaVideo(
        media=InputMediaVideoTestBase.media,
        thumbnail=class_thumb_file,
        cover=class_thumb_file,
        start_timestamp=InputMediaVideoTestBase.start_timestamp,
        width=InputMediaVideoTestBase.width,
        height=InputMediaVideoTestBase.height,
        duration=InputMediaVideoTestBase.duration,
        supports_streaming=InputMediaVideoTestBase.supports_streaming,
    )


class InputMediaVideoTestBase:
    type_ = "video"
    media = "NOTAREALFILEID"
    caption = "My Caption"
    width = 3
    height = 4
    duration = dtm.timedelta(seconds=5)
    start_timestamp = 3
    parse_mode = "HTML"
    supports_streaming = True
    caption_entities = [MessageEntity(MessageEntity.BOLD, 0, 2)]
    has_spoiler = True
    show_caption_above_media = True


class TestInputMediaVideoWithoutRequest(InputMediaVideoTestBase):
    def test_slot_behaviour(self, input_media_video):
        inst = input_media_video
        for attr in inst.__slots__:
            assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
        assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"

    def test_expected_values(self, input_media_video):
        assert input_media_video.type == self.type_
        assert input_media_video.media == self.media
        assert input_media_video.caption == self.caption
        assert input_media_video.width == self.width
        assert input_media_video.height == self.height
        assert input_media_video._duration == self.duration
        assert input_media_video.parse_mode == self.parse_mode
        assert input_media_video.caption_entities == tuple(self.caption_entities)
        assert input_media_video.supports_streaming == self.supports_streaming
        assert isinstance(input_media_video.thumbnail, InputFile)
        assert isinstance(input_media_video.cover, InputFile)
        assert input_media_video.start_timestamp == self.start_timestamp
        assert input_media_video.has_spoiler == self.has_spoiler
        assert input_media_video.show_caption_above_media == self.show_caption_above_media

    def test_caption_entities_always_tuple(self):
        input_media_video = InputMediaVideo(self.media)
        assert input_media_video.caption_entities == ()

    def test_to_dict(self, input_media_video):
        input_media_video_dict = input_media_video.to_dict()
        assert input_media_video_dict["type"] == input_media_video.type
        assert input_media_video_dict["media"] == input_media_video.media
        assert input_media_video_dict["caption"] == input_media_video.caption
        assert input_media_video_dict["width"] == input_media_video.width
        assert input_media_video_dict["height"] == input_media_video.height
        assert input_media_video_dict["duration"] == int(self.duration.total_seconds())
        assert isinstance(input_media_video_dict["duration"], int)
        assert input_media_video_dict["parse_mode"] == input_media_video.parse_mode
        assert input_media_video_dict["caption_entities"] == [
            ce.to_dict() for ce in input_media_video.caption_entities
        ]
        assert input_media_video_dict["supports_streaming"] == input_media_video.supports_streaming
        assert input_media_video_dict["has_spoiler"] == input_media_video.has_spoiler
        assert (
            input_media_video_dict["show_caption_above_media"]
            == input_media_video.show_caption_above_media
        )
        assert input_media_video_dict["cover"] == input_media_video.cover
        assert input_media_video_dict["start_timestamp"] == input_media_video.start_timestamp

    def test_time_period_properties(self, PTB_TIMEDELTA, input_media_video):
        duration = input_media_video.duration

        if PTB_TIMEDELTA:
            assert duration == self.duration
            assert isinstance(duration, dtm.timedelta)
        else:
            assert duration == int(self.duration.total_seconds())
            assert isinstance(duration, int)

    def test_time_period_int_deprecated(self, recwarn, PTB_TIMEDELTA, input_media_video):
        input_media_video.duration

        if PTB_TIMEDELTA:
            assert len(recwarn) == 0
        else:
            assert len(recwarn) == 1
            assert "`duration` will be of type `datetime.timedelta`" in str(recwarn[0].message)
            assert recwarn[0].category is PTBDeprecationWarning

    def test_with_video(self, video, PTB_TIMEDELTA):
        # fixture found in test_video
        input_media_video = InputMediaVideo(video, caption="test 3")
        assert input_media_video.type == self.type_
        assert input_media_video.media == video.file_id
        assert input_media_video.width == video.width
        assert input_media_video.height == video.height
        assert input_media_video.duration == video.duration
        assert input_media_video.caption == "test 3"

    def test_with_video_file(self, video_file):
        # fixture found in test_video
        input_media_video = InputMediaVideo(video_file, caption="test 3")
        assert input_media_video.type == self.type_
        assert isinstance(input_media_video.media, InputFile)
        assert input_media_video.caption == "test 3"

    def test_with_local_files(self):
        input_media_video = InputMediaVideo(
            data_file("telegram.mp4"),
            thumbnail=data_file("telegram.jpg"),
            cover=data_file("telegram.jpg"),
        )
        assert input_media_video.media == data_file("telegram.mp4").as_uri()
        assert input_media_video.thumbnail == data_file("telegram.jpg").as_uri()
        assert input_media_video.cover == data_file("telegram.jpg").as_uri()

    def test_type_enum_conversion(self):
        # Since we have a lot of different test classes for all the input media types, we test this
        # conversion only here. It is independent of the specific class
        assert (
            type(
                InputMedia(
                    media_type="animation",
                    media="media",
                ).type
            )
            is InputMediaType
        )
        assert (
            InputMedia(
                media_type="unknown",
                media="media",
            ).type
            == "unknown"
        )


class InputMediaPhotoTestBase:
    type_ = "photo"
    media = "NOTAREALFILEID"
    caption = "My Caption"
    parse_mode = "Markdown"
    caption_entities = [MessageEntity(MessageEntity.BOLD, 0, 2)]
    has_spoiler = True
    show_caption_above_media = True


class TestInputMediaPhotoWithoutRequest(InputMediaPhotoTestBase):
    def test_slot_behaviour(self, input_media_photo):
        inst = input_media_photo
        for attr in inst.__slots__:
            assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
        assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"

    def test_expected_values(self, input_media_photo):
        assert input_media_photo.type == self.type_
        assert input_media_photo.media == self.media
        assert input_media_photo.caption == self.caption
        assert input_media_photo.parse_mode == self.parse_mode
        assert input_media_photo.caption_entities == tuple(self.caption_entities)
        assert input_media_photo.has_spoiler == self.has_spoiler
        assert input_media_photo.show_caption_above_media == self.show_caption_above_media

    def test_caption_entities_always_tuple(self):
        input_media_photo = InputMediaPhoto(self.media)
        assert input_media_photo.caption_entities == ()

    def test_to_dict(self, input_media_photo):
        input_media_photo_dict = input_media_photo.to_dict()
        assert input_media_photo_dict["type"] == input_media_photo.type
        assert input_media_photo_dict["media"] == input_media_photo.media
        assert input_media_photo_dict["caption"] == input_media_photo.caption
        assert input_media_photo_dict["parse_mode"] == input_media_photo.parse_mode
        assert input_media_photo_dict["caption_entities"] == [
            ce.to_dict() for ce in input_media_photo.caption_entities
        ]
        assert input_media_photo_dict["has_spoiler"] == input_media_photo.has_spoiler
        assert (
            input_media_photo_dict["show_caption_above_media"]
            == input_media_photo.show_caption_above_media
        )

    def test_with_photo(self, photo):
        # fixture found in test_photo
        input_media_photo = InputMediaPhoto(photo, caption="test 2")
        assert input_media_photo.type == self.type_
        assert input_media_photo.media == photo.file_id
        assert input_media_photo.caption == "test 2"

    def test_with_photo_file(self, photo_file):
        # fixture found in test_photo
        input_media_photo = InputMediaPhoto(photo_file, caption="test 2")
        assert input_media_photo.type == self.type_
        assert isinstance(input_media_photo.media, InputFile)
        assert input_media_photo.caption == "test 2"

    def test_with_local_files(self):
        input_media_photo = InputMediaPhoto(data_file("telegram.mp4"))
        assert input_media_photo.media == data_file("telegram.mp4").as_uri()


class InputMediaAnimationTestBase:
    type_ = "animation"
    media = "NOTAREALFILEID"
    caption = "My Caption"
    parse_mode = "Markdown"
    caption_entities = [MessageEntity(MessageEntity.BOLD, 0, 2)]
    width = 30
    height = 30
    duration = dtm.timedelta(seconds=1)
    has_spoiler = True
    show_caption_above_media = True


class TestInputMediaAnimationWithoutRequest(InputMediaAnimationTestBase):
    def test_slot_behaviour(self, input_media_animation):
        inst = input_media_animation
        for attr in inst.__slots__:
            assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
        assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"

    def test_expected_values(self, input_media_animation):
        assert input_media_animation.type == self.type_
        assert input_media_animation.media == self.media
        assert input_media_animation.caption == self.caption
        assert input_media_animation.parse_mode == self.parse_mode
        assert input_media_animation.caption_entities == tuple(self.caption_entities)
        assert isinstance(input_media_animation.thumbnail, InputFile)
        assert input_media_animation.has_spoiler == self.has_spoiler
        assert input_media_animation.show_caption_above_media == self.show_caption_above_media
        assert input_media_animation._duration == self.duration

    def test_caption_entities_always_tuple(self):
        input_media_animation = InputMediaAnimation(self.media)
        assert input_media_animation.caption_entities == ()

    def test_to_dict(self, input_media_animation):
        input_media_animation_dict = input_media_animation.to_dict()
        assert input_media_animation_dict["type"] == input_media_animation.type
        assert input_media_animation_dict["media"] == input_media_animation.media
        assert input_media_animation_dict["caption"] == input_media_animation.caption
        assert input_media_animation_dict["parse_mode"] == input_media_animation.parse_mode
        assert input_media_animation_dict["caption_entities"] == [
            ce.to_dict() for ce in input_media_animation.caption_entities
        ]
        assert input_media_animation_dict["width"] == input_media_animation.width
        assert input_media_animation_dict["height"] == input_media_animation.height
        assert input_media_animation_dict["duration"] == int(self.duration.total_seconds())
        assert isinstance(input_media_animation_dict["duration"], int)
        assert input_media_animation_dict["has_spoiler"] == input_media_animation.has_spoiler
        assert (
            input_media_animation_dict["show_caption_above_media"]
            == input_media_animation.show_caption_above_media
        )

    def test_time_period_properties(self, PTB_TIMEDELTA, input_media_animation):
        duration = input_media_animation.duration

        if PTB_TIMEDELTA:
            assert duration == self.duration
            assert isinstance(duration, dtm.timedelta)
        else:
            assert duration == int(self.duration.total_seconds())
            assert isinstance(duration, int)

    def test_time_period_int_deprecated(self, recwarn, PTB_TIMEDELTA, input_media_animation):
        input_media_animation.duration

        if PTB_TIMEDELTA:
            assert len(recwarn) == 0
        else:
            assert len(recwarn) == 1
            assert "`duration` will be of type `datetime.timedelta`" in str(recwarn[0].message)
            assert recwarn[0].category is PTBDeprecationWarning

    def test_with_animation(self, animation):
        # fixture found in test_animation
        input_media_animation = InputMediaAnimation(animation, caption="test 2")
        assert input_media_animation.type == self.type_
        assert input_media_animation.media == animation.file_id
        assert input_media_animation.caption == "test 2"

    def test_with_animation_file(self, animation_file):
        # fixture found in test_animation
        input_media_animation = InputMediaAnimation(animation_file, caption="test 2")
        assert input_media_animation.type == self.type_
        assert isinstance(input_media_animation.media, InputFile)
        assert input_media_animation.caption == "test 2"

    def test_with_local_files(self):
        input_media_animation = InputMediaAnimation(
            data_file("telegram.mp4"), thumbnail=data_file("telegram.jpg")
        )
        assert input_media_animation.media == data_file("telegram.mp4").as_uri()
        assert input_media_animation.thumbnail == data_file("telegram.jpg").as_uri()


class InputMediaAudioTestBase:
    type_ = "audio"
    media = "NOTAREALFILEID"
    caption = "My Caption"
    duration = dtm.timedelta(seconds=3)
    performer = "performer"
    title = "title"
    parse_mode = "HTML"
    caption_entities = [MessageEntity(MessageEntity.BOLD, 0, 2)]


class TestInputMediaAudioWithoutRequest(InputMediaAudioTestBase):
    def test_slot_behaviour(self, input_media_audio):
        inst = input_media_audio
        for attr in inst.__slots__:
            assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
        assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"

    def test_expected_values(self, input_media_audio):
        assert input_media_audio.type == self.type_
        assert input_media_audio.media == self.media
        assert input_media_audio.caption == self.caption
        assert input_media_audio._duration == self.duration
        assert input_media_audio.performer == self.performer
        assert input_media_audio.title == self.title
        assert input_media_audio.parse_mode == self.parse_mode
        assert input_media_audio.caption_entities == tuple(self.caption_entities)
        assert isinstance(input_media_audio.thumbnail, InputFile)

    def test_caption_entities_always_tuple(self):
        input_media_audio = InputMediaAudio(self.media)
        assert input_media_audio.caption_entities == ()

    def test_to_dict(self, input_media_audio):
        input_media_audio_dict = input_media_audio.to_dict()
        assert input_media_audio_dict["type"] == input_media_audio.type
        assert input_media_audio_dict["media"] == input_media_audio.media
        assert input_media_audio_dict["caption"] == input_media_audio.caption
        assert isinstance(input_media_audio_dict["duration"], int)
        assert input_media_audio_dict["duration"] == int(self.duration.total_seconds())
        assert isinstance(input_media_audio_dict["duration"], int)
        assert input_media_audio_dict["performer"] == input_media_audio.performer
        assert input_media_audio_dict["title"] == input_media_audio.title
        assert input_media_audio_dict["parse_mode"] == input_media_audio.parse_mode
        assert input_media_audio_dict["caption_entities"] == [
            ce.to_dict() for ce in input_media_audio.caption_entities
        ]

    def test_time_period_properties(self, PTB_TIMEDELTA, input_media_audio):
        duration = input_media_audio.duration

        if PTB_TIMEDELTA:
            assert duration == self.duration
            assert isinstance(duration, dtm.timedelta)
        else:
            assert duration == int(self.duration.total_seconds())
            assert isinstance(duration, int)

    def test_time_period_int_deprecated(self, recwarn, PTB_TIMEDELTA, input_media_audio):
        input_media_audio.duration

        if PTB_TIMEDELTA:
            assert len(recwarn) == 0
        else:
            assert len(recwarn) == 1
            assert "`duration` will be of type `datetime.timedelta`" in str(recwarn[0].message)
            assert recwarn[0].category is PTBDeprecationWarning

    def test_with_audio(self, audio):
        # fixture found in test_audio
        input_media_audio = InputMediaAudio(audio, caption="test 3")
        assert input_media_audio.type == self.type_
        assert input_media_audio.media == audio.file_id
        assert input_media_audio.duration == audio.duration
        assert input_media_audio.performer == audio.performer
        assert input_media_audio.title == audio.title
        assert input_media_audio.caption == "test 3"

    def test_with_audio_file(self, audio_file):
        # fixture found in test_audio
        input_media_audio = InputMediaAudio(audio_file, caption="test 3")
        assert input_media_audio.type == self.type_
        assert isinstance(input_media_audio.media, InputFile)
        assert input_media_audio.caption == "test 3"

    def test_with_local_files(self):
        input_media_audio = InputMediaAudio(
            data_file("telegram.mp4"), thumbnail=data_file("telegram.jpg")
        )
        assert input_media_audio.media == data_file("telegram.mp4").as_uri()
        assert input_media_audio.thumbnail == data_file("telegram.jpg").as_uri()


class InputMediaDocumentTestBase:
    type_ = "document"
    media = "NOTAREALFILEID"
    caption = "My Caption"
    parse_mode = "HTML"
    caption_entities = [MessageEntity(MessageEntity.BOLD, 0, 2)]
    disable_content_type_detection = True


class TestInputMediaDocumentWithoutRequest(InputMediaDocumentTestBase):
    def test_slot_behaviour(self, input_media_document):
        inst = input_media_document
        for attr in inst.__slots__:
            assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
        assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"

    def test_expected_values(self, input_media_document):
        assert input_media_document.type == self.type_
        assert input_media_document.media == self.media
        assert input_media_document.caption == self.caption
        assert input_media_document.parse_mode == self.parse_mode
        assert input_media_document.caption_entities == tuple(self.caption_entities)
        assert (
            input_media_document.disable_content_type_detection
            == self.disable_content_type_detection
        )
        assert isinstance(input_media_document.thumbnail, InputFile)

    def test_caption_entities_always_tuple(self):
        input_media_document = InputMediaDocument(self.media)
        assert input_media_document.caption_entities == ()

    def test_to_dict(self, input_media_document):
        input_media_document_dict = input_media_document.to_dict()
        assert input_media_document_dict["type"] == input_media_document.type
        assert input_media_document_dict["media"] == input_media_document.media
        assert input_media_document_dict["caption"] == input_media_document.caption
        assert input_media_document_dict["parse_mode"] == input_media_document.parse_mode
        assert input_media_document_dict["caption_entities"] == [
            ce.to_dict() for ce in input_media_document.caption_entities
        ]
        assert (
            input_media_document["disable_content_type_detection"]
            == input_media_document.disable_content_type_detection
        )

    def test_with_document(self, document):
        # fixture found in test_document
        input_media_document = InputMediaDocument(document, caption="test 3")
        assert input_media_document.type == self.type_
        assert input_media_document.media == document.file_id
        assert input_media_document.caption == "test 3"

    def test_with_document_file(self, document_file):
        # fixture found in test_document
        input_media_document = InputMediaDocument(document_file, caption="test 3")
        assert input_media_document.type == self.type_
        assert isinstance(input_media_document.media, InputFile)
        assert input_media_document.caption == "test 3"

    def test_with_local_files(self):
        input_media_document = InputMediaDocument(
            data_file("telegram.mp4"), thumbnail=data_file("telegram.jpg")
        )
        assert input_media_document.media == data_file("telegram.mp4").as_uri()
        assert input_media_document.thumbnail == data_file("telegram.jpg").as_uri()


class TestInputPaidMediaPhotoWithoutRequest(InputMediaPhotoTestBase):
    def test_slot_behaviour(self, input_paid_media_photo):
        inst = input_paid_media_photo
        for attr in inst.__slots__:
            assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
        assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"

    def test_expected_values(self, input_paid_media_photo):
        assert input_paid_media_photo.type == self.type_
        assert input_paid_media_photo.media == self.media

    def test_to_dict(self, input_paid_media_photo):
        input_paid_media_photo_dict = input_paid_media_photo.to_dict()
        assert input_paid_media_photo_dict["type"] == input_paid_media_photo.type
        assert input_paid_media_photo_dict["media"] == input_paid_media_photo.media

    def test_with_photo(self, photo):
        # fixture found in test_photo
        input_paid_media_photo = InputPaidMediaPhoto(photo)
        assert input_paid_media_photo.type == self.type_
        assert input_paid_media_photo.media == photo.file_id

    def test_with_photo_file(self, photo_file):
        # fixture found in test_photo
        input_paid_media_photo = InputPaidMediaPhoto(photo_file)
        assert input_paid_media_photo.type == self.type_
        assert isinstance(input_paid_media_photo.media, InputFile)

    def test_with_local_files(self):
        input_paid_media_photo = InputPaidMediaPhoto(data_file("telegram.jpg"))
        assert input_paid_media_photo.media == data_file("telegram.jpg").as_uri()


class TestInputPaidMediaVideoWithoutRequest(InputMediaVideoTestBase):
    def test_slot_behaviour(self, input_paid_media_video):
        inst = input_paid_media_video
        for attr in inst.__slots__:
            assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
        assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"

    def test_expected_values(self, input_paid_media_video):
        assert input_paid_media_video.type == self.type_
        assert input_paid_media_video.media == self.media
        assert input_paid_media_video.width == self.width
        assert input_paid_media_video.height == self.height
        assert input_paid_media_video._duration == self.duration
        assert input_paid_media_video.supports_streaming == self.supports_streaming
        assert isinstance(input_paid_media_video.thumbnail, InputFile)
        assert isinstance(input_paid_media_video.cover, InputFile)
        assert input_paid_media_video.start_timestamp == self.start_timestamp

    def test_to_dict(self, input_paid_media_video):
        input_paid_media_video_dict = input_paid_media_video.to_dict()
        assert input_paid_media_video_dict["type"] == input_paid_media_video.type
        assert input_paid_media_video_dict["media"] == input_paid_media_video.media
        assert input_paid_media_video_dict["width"] == input_paid_media_video.width
        assert input_paid_media_video_dict["height"] == input_paid_media_video.height
        assert input_paid_media_video_dict["duration"] == int(self.duration.total_seconds())
        assert isinstance(input_paid_media_video_dict["duration"], int)
        assert (
            input_paid_media_video_dict["supports_streaming"]
            == input_paid_media_video.supports_streaming
        )
        assert input_paid_media_video_dict["thumbnail"] == input_paid_media_video.thumbnail
        assert input_paid_media_video_dict["cover"] == input_paid_media_video.cover
        assert (
            input_paid_media_video_dict["start_timestamp"]
            == input_paid_media_video.start_timestamp
        )

    def test_time_period_properties(self, PTB_TIMEDELTA, input_paid_media_video):
        duration = input_paid_media_video.duration

        if PTB_TIMEDELTA:
            assert duration == self.duration
            assert isinstance(duration, dtm.timedelta)
        else:
            assert duration == int(self.duration.total_seconds())
            assert isinstance(duration, int)

    def test_time_period_int_deprecated(self, recwarn, PTB_TIMEDELTA, input_paid_media_video):
        input_paid_media_video.duration

        if PTB_TIMEDELTA:
            assert len(recwarn) == 0
        else:
            assert len(recwarn) == 1
            assert "`duration` will be of type `datetime.timedelta`" in str(recwarn[0].message)
            assert recwarn[0].category is PTBDeprecationWarning

    def test_with_video(self, video):
        # fixture found in test_video
        input_paid_media_video = InputPaidMediaVideo(video)
        assert input_paid_media_video.type == self.type_
        assert input_paid_media_video.media == video.file_id
        assert input_paid_media_video.width == video.width
        assert input_paid_media_video.height == video.height
        assert input_paid_media_video.duration == video.duration

    def test_with_video_file(self, video_file):
        # fixture found in test_video
        input_paid_media_video = InputPaidMediaVideo(video_file)
        assert input_paid_media_video.type == self.type_
        assert isinstance(input_paid_media_video.media, InputFile)

    def test_with_local_files(self):
        input_paid_media_video = InputPaidMediaVideo(
            data_file("telegram.mp4"),
            thumbnail=data_file("telegram.jpg"),
            cover=data_file("telegram.jpg"),
        )
        assert input_paid_media_video.media == data_file("telegram.mp4").as_uri()
        assert input_paid_media_video.thumbnail == data_file("telegram.jpg").as_uri()
        assert input_paid_media_video.cover == data_file("telegram.jpg").as_uri()


@pytest.fixture(scope="module")
def media_group(photo, thumb):
    return [
        InputMediaPhoto(photo, caption="*photo* 1", parse_mode="Markdown"),
        InputMediaPhoto(thumb, caption="<b>photo</b> 2", parse_mode="HTML"),
        InputMediaPhoto(
            photo, caption="photo 3", caption_entities=[MessageEntity(MessageEntity.BOLD, 0, 5)]
        ),
    ]


@pytest.fixture(scope="module")
def media_group_no_caption_args(photo, thumb):
    return [InputMediaPhoto(photo), InputMediaPhoto(thumb), InputMediaPhoto(photo)]


@pytest.fixture(scope="module")
def media_group_no_caption_only_caption_entities(photo, thumb):
    return [
        InputMediaPhoto(photo, caption_entities=[MessageEntity(MessageEntity.BOLD, 0, 5)]),
        InputMediaPhoto(photo, caption_entities=[MessageEntity(MessageEntity.BOLD, 0, 5)]),
    ]


@pytest.fixture(scope="module")
def media_group_no_caption_only_parse_mode(photo, thumb):
    return [
        InputMediaPhoto(photo, parse_mode="Markdown"),
        InputMediaPhoto(thumb, parse_mode="HTML"),
    ]


class TestSendMediaGroupWithoutRequest:
    async def test_send_media_group_throws_error_with_group_caption_and_individual_captions(
        self,
        offline_bot,
        chat_id,
        media_group,
        media_group_no_caption_only_caption_entities,
        media_group_no_caption_only_parse_mode,
    ):
        for group in (
            media_group,
            media_group_no_caption_only_caption_entities,
            media_group_no_caption_only_parse_mode,
        ):
            with pytest.raises(
                ValueError,
                match="You can only supply either group caption or media with captions.",
            ):
                await offline_bot.send_media_group(chat_id, group, caption="foo")

    async def test_send_media_group_custom_filename(
        self,
        offline_bot,
        chat_id,
        photo_file,
        animation_file,
        audio_file,
        video_file,
        monkeypatch,
    ):
        async def make_assertion(url, request_data: RequestData, *args, **kwargs):
            result = all(
                field_tuple[0] == "custom_filename"
                for field_tuple in request_data.multipart_data.values()
            )
            if result is True:
                raise Exception("Test was successful")

        monkeypatch.setattr(offline_bot.request, "post", make_assertion)

        media = [
            InputMediaAnimation(animation_file, filename="custom_filename"),
            InputMediaAudio(audio_file, filename="custom_filename"),
            InputMediaPhoto(photo_file, filename="custom_filename"),
            InputMediaVideo(video_file, filename="custom_filename"),
        ]

        with pytest.raises(Exception, match="Test was successful"):
            await offline_bot.send_media_group(chat_id, media)

    async def test_send_media_group_with_thumbs(
        self, offline_bot, chat_id, video_file, photo_file, monkeypatch
    ):
        async def make_assertion(method, url, request_data: RequestData, *args, **kwargs):
            files = request_data.multipart_data
            video_check = files[input_video.media.attach_name] == input_video.media.field_tuple
            thumb_check = (
                files[input_video.thumbnail.attach_name] == input_video.thumbnail.field_tuple
            )
            result = video_check and thumb_check
            raise Exception(f"Test was {'successful' if result else 'failing'}")

        monkeypatch.setattr(offline_bot.request, "_request_wrapper", make_assertion)
        input_video = InputMediaVideo(video_file, thumbnail=photo_file)
        with pytest.raises(Exception, match="Test was successful"):
            await offline_bot.send_media_group(chat_id, [input_video, input_video])

    async def test_edit_message_media_with_thumb(
        self, offline_bot, chat_id, video_file, photo_file, monkeypatch
    ):
        async def make_assertion(
            method: str, url: str, request_data: Optional[RequestData] = None, *args, **kwargs
        ):
            files = request_data.multipart_data
            video_check = files[input_video.media.attach_name] == input_video.media.field_tuple
            thumb_check = (
                files[input_video.thumbnail.attach_name] == input_video.thumbnail.field_tuple
            )
            result = video_check and thumb_check
            raise Exception(f"Test was {'successful' if result else 'failing'}")

        monkeypatch.setattr(offline_bot.request, "_request_wrapper", make_assertion)
        input_video = InputMediaVideo(video_file, thumbnail=photo_file)
        with pytest.raises(Exception, match="Test was successful"):
            await offline_bot.edit_message_media(
                chat_id=chat_id, message_id=123, media=input_video
            )

    @pytest.mark.parametrize(
        ("default_bot", "custom"),
        [
            ({"parse_mode": ParseMode.HTML}, None),
            ({"parse_mode": ParseMode.HTML}, ParseMode.MARKDOWN_V2),
            ({"parse_mode": None}, ParseMode.MARKDOWN_V2),
        ],
        indirect=["default_bot"],
    )
    async def test_send_media_group_default_quote_parse_mode(
        self, default_bot, chat_id, media_group, custom, monkeypatch
    ):
        async def make_assertion(url, request_data: RequestData, *args, **kwargs):
            assert request_data.parameters["reply_parameters"].get("quote_parse_mode") == (
                custom or default_bot.defaults.quote_parse_mode
            )
            return [make_message("dummy reply").to_dict()]

        kwargs = {"message_id": 1}
        if custom is not None:
            kwargs["quote_parse_mode"] = custom

        monkeypatch.setattr(default_bot.request, "post", make_assertion)
        await default_bot.send_media_group(
            chat_id, media_group, reply_parameters=ReplyParameters(**kwargs)
        )


class CustomSequence(Sequence):
    def __init__(self, items):
        self.items = items

    def __getitem__(self, index):
        return self.items[index]

    def __len__(self):
        return len(self.items)


class TestSendMediaGroupWithRequest:
    async def test_send_media_group_photo(self, bot, chat_id, media_group):
        messages = await bot.send_media_group(chat_id, media_group)
        assert isinstance(messages, tuple)
        assert len(messages) == 3
        assert all(isinstance(mes, Message) for mes in messages)
        assert all(mes.media_group_id == messages[0].media_group_id for mes in messages)
        assert all(mes.caption == f"photo {idx + 1}" for idx, mes in enumerate(messages))
        assert all(
            mes.caption_entities == (MessageEntity(MessageEntity.BOLD, 0, 5),) for mes in messages
        )

    async def test_send_media_group_new_files(self, bot, chat_id, video_file, photo_file):
        async def func():
            return await bot.send_media_group(
                chat_id,
                [
                    InputMediaVideo(video_file),
                    InputMediaPhoto(photo_file),
                    InputMediaPhoto(data_file("telegram.jpg").read_bytes()),
                ],
            )

        messages = await expect_bad_request(
            func, "Type of file mismatch", "Telegram did not accept the file."
        )

        assert isinstance(messages, tuple)
        assert len(messages) == 3
        assert all(isinstance(mes, Message) for mes in messages)
        assert all(mes.media_group_id == messages[0].media_group_id for mes in messages)

    @pytest.mark.parametrize("sequence_type", [list, tuple, CustomSequence])
    @pytest.mark.parametrize("bot_class", ["raw_bot", "ext_bot"])
    async def test_send_media_group_different_sequences(
        self, bot, chat_id, media_group, sequence_type, bot_class, raw_bot
    ):
        """Test that send_media_group accepts different sequence types. This test ensures that
        Bot._insert_defaults works for arbitrary sequence types."""
        bot = bot if bot_class == "ext_bot" else raw_bot

        messages = await bot.send_media_group(chat_id, sequence_type(media_group))
        assert isinstance(messages, tuple)
        assert len(messages) == 3
        assert all(isinstance(mes, Message) for mes in messages)
        assert all(mes.media_group_id == messages[0].media_group_id for mes in messages)

    async def test_send_media_group_with_message_thread_id(
        self, bot, real_topic, forum_group_id, media_group
    ):
        messages = await bot.send_media_group(
            forum_group_id,
            media_group,
            message_thread_id=real_topic.message_thread_id,
        )
        assert isinstance(messages, tuple)
        assert len(messages) == 3
        assert all(isinstance(mes, Message) for mes in messages)
        assert all(i.message_thread_id == real_topic.message_thread_id for i in messages)

    @pytest.mark.parametrize(
        ("caption", "parse_mode", "caption_entities"),
        [
            # same combinations of caption options as in media_group fixture
            ("*photo* 1", "Markdown", None),
            ("<b>photo</b> 1", "HTML", None),
            ("photo 1", None, [MessageEntity(MessageEntity.BOLD, 0, 5)]),
        ],
    )
    async def test_send_media_group_with_group_caption(
        self,
        bot,
        chat_id,
        media_group_no_caption_args,
        caption,
        parse_mode,
        caption_entities,
    ):
        # prepare a copy to check later on if calling the method has caused side effects
        copied_media_group = media_group_no_caption_args.copy()

        messages = await bot.send_media_group(
            chat_id,
            media_group_no_caption_args,
            caption=caption,
            parse_mode=parse_mode,
            caption_entities=caption_entities,
        )

        # Check that the method had no side effects:
        # original group was not changed and 1st item still points to the same object
        # (1st item must be copied within the method before adding the caption)
        assert media_group_no_caption_args == copied_media_group
        assert media_group_no_caption_args[0] is copied_media_group[0]

        assert not any(item.parse_mode for item in media_group_no_caption_args)

        assert isinstance(messages, tuple)
        assert len(messages) == 3
        assert all(isinstance(mes, Message) for mes in messages)

        first_message, other_messages = messages[0], messages[1:]
        assert all(mes.media_group_id == first_message.media_group_id for mes in messages)

        # Make sure first message got the caption, which will lead
        # to Telegram displaying its caption as group caption
        assert first_message.caption
        assert first_message.caption_entities == (MessageEntity(MessageEntity.BOLD, 0, 5),)

        # Check that other messages have no captions
        assert all(mes.caption is None for mes in other_messages)
        assert not any(mes.caption_entities for mes in other_messages)

    async def test_send_media_group_all_args(self, bot, raw_bot, chat_id, media_group):
        ext_bot = bot
        # We need to test 1) below both the bot and raw_bot and setting this up with
        # pytest.parametrize appears to be difficult ...
        aws = {b.send_message(chat_id, text="test") for b in (ext_bot, raw_bot)}
        for msg_task in asyncio.as_completed(aws):
            m1 = await msg_task
            copied_media_group = copy.copy(media_group)
            messages = await m1.get_bot().send_media_group(
                chat_id,
                media_group,
                disable_notification=True,
                reply_to_message_id=m1.message_id,
                protect_content=True,
            )

            # 1)
            # make sure that the media_group was not modified
            assert media_group == copied_media_group
            assert all(
                a.parse_mode == b.parse_mode for a, b in zip(media_group, copied_media_group)
            )

            assert isinstance(messages, tuple)
            assert len(messages) == 3
            assert all(isinstance(mes, Message) for mes in messages)
            assert all(mes.media_group_id == messages[0].media_group_id for mes in messages)
            assert all(mes.caption == f"photo {idx + 1}" for idx, mes in enumerate(messages))
            assert all(
                mes.caption_entities == (MessageEntity(MessageEntity.BOLD, 0, 5),)
                for mes in messages
            )
            assert all(mes.has_protected_content for mes in messages)

    async def test_send_media_group_with_spoiler(self, bot, chat_id, photo_file, video_file):
        # Media groups can't contain Animations, so that is tested in test_animation.py
        media = [
            InputMediaPhoto(photo_file, has_spoiler=True),
            InputMediaVideo(video_file, has_spoiler=True),
        ]
        messages = await bot.send_media_group(chat_id, media)
        assert isinstance(messages, tuple)
        assert len(messages) == 2
        assert all(isinstance(mes, Message) for mes in messages)
        assert all(mes.media_group_id == messages[0].media_group_id for mes in messages)
        assert all(mes.has_media_spoiler for mes in messages)

    async def test_edit_message_media(self, bot, raw_bot, chat_id, media_group):
        ext_bot = bot
        # We need to test 1) below both the bot and raw_bot and setting this up with
        # pytest.parametrize appears to be difficult ...
        aws = {b.send_media_group(chat_id, media_group) for b in (ext_bot, raw_bot)}
        for msg_task in asyncio.as_completed(aws):
            messages = await msg_task
            cid = messages[-1].chat.id
            mid = messages[-1].message_id
            copied_media = copy.copy(media_group[0])
            new_message = (
                await messages[-1]
                .get_bot()
                .edit_message_media(chat_id=cid, message_id=mid, media=media_group[0])
            )
            assert isinstance(new_message, Message)

            # 1)
            # make sure that the media was not modified
            assert media_group[0].parse_mode == copied_media.parse_mode

    async def test_edit_message_media_new_file(self, bot, chat_id, media_group, thumb_file):
        messages = await bot.send_media_group(chat_id, media_group)
        cid = messages[-1].chat.id
        mid = messages[-1].message_id
        new_message = await bot.edit_message_media(
            chat_id=cid, message_id=mid, media=InputMediaPhoto(thumb_file)
        )
        assert isinstance(new_message, Message)

    @pytest.mark.parametrize(
        ("default_bot", "custom"),
        [
            ({"allow_sending_without_reply": True}, None),
            ({"allow_sending_without_reply": False}, None),
            ({"allow_sending_without_reply": False}, True),
        ],
        indirect=["default_bot"],
    )
    async def test_send_media_group_default_allow_sending_without_reply(
        self, default_bot, chat_id, media_group, custom
    ):
        reply_to_message = await default_bot.send_message(chat_id, "test")
        await reply_to_message.delete()
        if custom is not None:
            messages = await default_bot.send_media_group(
                chat_id,
                media_group,
                allow_sending_without_reply=custom,
                reply_to_message_id=reply_to_message.message_id,
            )
            assert [m.reply_to_message is None for m in messages]
        elif default_bot.defaults.allow_sending_without_reply:
            messages = await default_bot.send_media_group(
                chat_id, media_group, reply_to_message_id=reply_to_message.message_id
            )
            assert [m.reply_to_message is None for m in messages]
        else:
            with pytest.raises(BadRequest, match="Message to be replied not found"):
                await default_bot.send_media_group(
                    chat_id, media_group, reply_to_message_id=reply_to_message.message_id
                )

    @pytest.mark.parametrize("default_bot", [{"protect_content": True}], indirect=True)
    async def test_send_media_group_default_protect_content(
        self, chat_id, media_group, default_bot
    ):
        tasks = asyncio.gather(
            default_bot.send_media_group(chat_id, media_group),
            default_bot.send_media_group(chat_id, media_group, protect_content=False),
        )
        protected, unprotected = await tasks
        assert all(msg.has_protected_content for msg in protected)
        assert not all(msg.has_protected_content for msg in unprotected)

    @pytest.mark.parametrize("default_bot", [{"parse_mode": ParseMode.HTML}], indirect=True)
    async def test_send_media_group_default_parse_mode(
        self, chat_id, media_group_no_caption_args, default_bot
    ):
        default = await default_bot.send_media_group(
            chat_id, media_group_no_caption_args, caption="<b>photo</b> 1"
        )

        # make sure no parse_mode was set as a side effect
        assert not any(item.parse_mode for item in media_group_no_caption_args)

        tasks = asyncio.gather(
            default_bot.send_media_group(
                chat_id,
                media_group_no_caption_args.copy(),
                caption="*photo* 1",
                parse_mode=ParseMode.MARKDOWN_V2,
            ),
            default_bot.send_media_group(
                chat_id,
                media_group_no_caption_args.copy(),
                caption="<b>photo</b> 1",
                parse_mode=None,
            ),
        )
        overridden_markdown_v2, overridden_none = await tasks

        # Make sure first message got the caption, which will lead to Telegram
        # displaying its caption as group caption
        assert overridden_none[0].caption == "<b>photo</b> 1"
        assert not overridden_none[0].caption_entities
        # First messages in these two groups have to have caption "photo 1"
        # because of parse mode (default or explicit)
        for mes_group in (default, overridden_markdown_v2):
            first_message = mes_group[0]
            assert first_message.caption == "photo 1"
            assert first_message.caption_entities == (MessageEntity(MessageEntity.BOLD, 0, 5),)

        # This check is valid for all 3 groups of messages
        for mes_group in (default, overridden_markdown_v2, overridden_none):
            first_message, other_messages = mes_group[0], mes_group[1:]
            assert all(mes.media_group_id == first_message.media_group_id for mes in mes_group)
            # Check that messages from 2nd message onwards have no captions
            assert all(mes.caption is None for mes in other_messages)
            assert not any(mes.caption_entities for mes in other_messages)

    @pytest.mark.parametrize(
        "default_bot", [{"parse_mode": ParseMode.HTML}], indirect=True, ids=["HTML-Bot"]
    )
    @pytest.mark.parametrize("media_type", ["animation", "document", "audio", "photo", "video"])
    async def test_edit_message_media_default_parse_mode(
        self,
        chat_id,
        default_bot,
        media_type,
        animation,
        document,
        audio,
        photo,
        video,
    ):
        html_caption = "<b>bold</b> <i>italic</i> <code>code</code>"
        markdown_caption = "*bold* _italic_ `code`"
        test_caption = "bold italic code"
        test_entities = [
            MessageEntity(MessageEntity.BOLD, 0, 4),
            MessageEntity(MessageEntity.ITALIC, 5, 6),
            MessageEntity(MessageEntity.CODE, 12, 4),
        ]

        def build_media(parse_mode, med_type):
            kwargs = {}
            if parse_mode != ParseMode.HTML:
                kwargs["parse_mode"] = parse_mode
                kwargs["caption"] = markdown_caption
            else:
                kwargs["caption"] = html_caption

            if med_type == "animation":
                return InputMediaAnimation(animation, **kwargs)
            if med_type == "document":
                return InputMediaDocument(document, **kwargs)
            if med_type == "audio":
                return InputMediaAudio(audio, **kwargs)
            if med_type == "photo":
                return InputMediaPhoto(photo, **kwargs)
            if med_type == "video":
                return InputMediaVideo(video, **kwargs)
            return None

        message = await default_bot.send_photo(chat_id, photo)

        media = build_media(parse_mode=ParseMode.HTML, med_type=media_type)
        copied_media = copy.copy(media)
        message = await default_bot.edit_message_media(
            media,
            message.chat_id,
            message.message_id,
        )
        assert message.caption == test_caption
        assert message.caption_entities == tuple(test_entities)
        # make sure that the media was not modified
        assert media.parse_mode == copied_media.parse_mode

        # Remove caption to avoid "Message not changed"
        await message.edit_caption()

        media = build_media(parse_mode=ParseMode.MARKDOWN_V2, med_type=media_type)
        copied_media = copy.copy(media)
        message = await default_bot.edit_message_media(
            media,
            message.chat_id,
            message.message_id,
        )
        assert message.caption == test_caption
        assert message.caption_entities == tuple(test_entities)
        # make sure that the media was not modified
        assert media.parse_mode == copied_media.parse_mode

        # Remove caption to avoid "Message not changed"
        await message.edit_caption()

        media = build_media(parse_mode=None, med_type=media_type)
        copied_media = copy.copy(media)
        message = await default_bot.edit_message_media(
            media,
            message.chat_id,
            message.message_id,
        )
        assert message.caption == markdown_caption
        assert message.caption_entities == ()
        # make sure that the media was not modified
        assert media.parse_mode == copied_media.parse_mode

    async def test_send_paid_media(self, bot, channel_id, photo_file, video_file):
        msg = await bot.send_paid_media(
            chat_id=channel_id,
            star_count=20,
            media=[
                InputPaidMediaPhoto(media=photo_file),
                InputPaidMediaVideo(media=video_file),
            ],
            caption="bye onlyfans",
            show_caption_above_media=True,
        )

        assert isinstance(msg, Message)
        assert msg.caption == "bye onlyfans"
        assert msg.show_caption_above_media
        assert msg.paid_media.star_count == 20