-
Notifications
You must be signed in to change notification settings - Fork 200
/
test_library.py
961 lines (753 loc) · 32.4 KB
/
test_library.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
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
# -*- coding: utf-8 -*-
from collections import namedtuple
from datetime import datetime, timedelta
from urllib.parse import quote_plus
import pytest
import plexapi.base
from plexapi.exceptions import BadRequest, NotFound
from . import conftest as utils
def test_library_Library_section(plex):
sections = plex.library.sections()
assert len(sections) >= 3
section_name = plex.library.section("TV Shows")
assert section_name.title == "TV Shows"
with pytest.raises(NotFound):
assert plex.library.section("cant-find-me")
with pytest.raises(NotFound):
assert plex.library.sectionByID(-1)
def test_library_Library_sectionByID_is_equal_section(plex, movies):
# test that sectionByID refreshes the section if the key is missing
# this is needed if there isn't any cached sections
assert plex.library.sectionByID(movies.key).uuid == movies.uuid
def test_library_sectionByID_with_attrs(plex, movies):
assert movies.agent == "tv.plex.agents.movie"
# This seems to fail for some reason.
# my account allow of sync, didn't find any about settings about the library.
# assert movies.allowSync is ("sync" in plex.ownerFeatures)
assert movies.art == "/:/resources/movie-fanart.jpg"
assert utils.is_metadata(
movies.composite, prefix="/library/sections/", contains="/composite/"
)
assert utils.is_datetime(movies.createdAt)
assert movies.filters is True
assert movies._initpath == "/library/sections"
assert utils.is_int(movies.key)
assert movies.language == "en-US"
assert len(movies.locations) == 1
assert len(movies.locations[0]) >= 10
assert movies.refreshing is False
assert movies.scanner == "Plex Movie"
assert movies._server._baseurl == utils.SERVER_BASEURL
assert movies.thumb == "/:/resources/movie.png"
assert movies.title == "Movies"
assert movies.type == "movie"
assert utils.is_datetime(movies.updatedAt)
assert len(movies.uuid) == 36
def test_library_section_get_movie(movies):
assert movies.get("Sita Sings the Blues")
assert movies.get(None, filters={"title": "Big Buck Bunny", "year": 2008})
with pytest.raises(NotFound):
movies.get("invalid title")
def test_library_MovieSection_getGuid(movies, movie):
result = movies.getGuid(guid=movie.guid)
assert result == movie
result = movies.getGuid(guid=movie.guids[0].id)
assert result == movie
with pytest.raises(NotFound):
movies.getGuid(guid='plex://movie/abcdefg')
with pytest.raises(NotFound):
movies.getGuid(guid='imdb://tt00000000')
def test_library_section_movies_all(movies):
assert movies.totalSize == 4
assert len(movies.all(container_start=0, container_size=1, maxresults=1)) == 1
def test_library_section_movies_all_guids(movies):
plexapi.base.USER_DONT_RELOAD_FOR_KEYS.add('guids')
try:
results = movies.all(includeGuids=False)
assert results[0].guids == []
results = movies.all()
assert results[0].guids
movie = movies.get("Sita Sings the Blues")
assert movie.guids
finally:
plexapi.base.USER_DONT_RELOAD_FOR_KEYS.remove('guids')
def test_library_section_totalDuration(tvshows):
assert utils.is_int(tvshows.totalDuration)
def test_library_section_totalStorage(tvshows):
assert utils.is_int(tvshows.totalStorage)
def test_library_section_totalViewSize(tvshows):
assert tvshows.totalViewSize() == 2
assert tvshows.totalViewSize(libtype="show") == 2
assert tvshows.totalViewSize(libtype="season") == 4
assert tvshows.totalViewSize(libtype="episode") == 49
show = tvshows.get("The 100")
show.addCollection("test_view_size")
assert tvshows.totalViewSize() == 3
assert tvshows.totalViewSize(includeCollections=False) == 2
show.removeCollection("test_view_size", locked=False)
def test_library_section_delete(movies, patched_http_call):
movies.delete()
def test_library_fetchItem(plex, movie):
item1 = plex.library.fetchItem(f"/library/metadata/{movie.ratingKey}")
item2 = plex.library.fetchItem(movie.ratingKey)
assert item1.title == "Elephants Dream"
assert item1 == item2 == movie
def test_library_fetchItems_with_maxresults(plex, tvshows):
items = tvshows.searchEpisodes()
assert len(items) > 5
size = len(items) - 5
ratingKeys = [item.ratingKey for item in items]
items1 = plex.fetchItems(ekey=ratingKeys, container_size=size)
items2 = plex.fetchItems(ekey=ratingKeys, container_size=size, maxresults=len(items))
assert items1 == items2 == items
def test_library_onDeck(plex, movie):
movie.updateProgress(movie.duration // 4) # set progress to 25%
assert movie in plex.library.onDeck()
movie.markUnplayed()
def test_library_recentlyAdded(plex):
assert len(list(plex.library.recentlyAdded()))
def test_library_add_edit_delete(plex, movies, photos):
# Create Other Videos library = No external metadata scanning
section_name = "plexapi_test_section"
movie_location = movies.locations[0]
photo_location = photos.locations[0]
plex.library.add(
name=section_name,
type="movie",
agent="com.plexapp.agents.none",
scanner="Plex Video Files Scanner",
language="xn",
location=[movie_location, photo_location]
)
section = plex.library.section(section_name)
assert section.title == section_name
# Create library with an invalid path
error_section_name = "plexapi_error_section"
with pytest.raises(BadRequest):
plex.library.add(
name=error_section_name,
type="movie",
agent="com.plexapp.agents.none",
scanner="Plex Video Files Scanner",
language="xn",
location=[movie_location, photo_location[:-1]]
)
# Create library with no path
with pytest.raises(BadRequest):
plex.library.add(
name=error_section_name,
type="movie",
agent="com.plexapp.agents.none",
scanner="Plex Video Files Scanner",
language="xn",
)
with pytest.raises(NotFound):
plex.library.section(error_section_name)
new_title = "a renamed lib"
section.edit(name=new_title)
section.reload()
assert section.title == new_title
with pytest.raises(BadRequest):
section.addLocations(movie_location[:-1])
with pytest.raises(BadRequest):
section.removeLocations(movie_location[:-1])
section.removeLocations(photo_location)
section.reload()
assert len(section.locations) == 1
section.addLocations(photo_location)
section.reload()
assert len(section.locations) == 2
section.edit(**{'location': movie_location})
section.reload()
assert len(section.locations) == 1
with pytest.raises(BadRequest):
section.edit(**{'location': movie_location[:-1]})
# Attempt to remove all locations
with pytest.raises(BadRequest):
section.removeLocations(section.locations)
section.delete()
assert section not in plex.library.sections()
def test_library_add_advanced_settings(plex, movies):
# Create Other Videos library = No external metadata scanning
section_name = "plexapi_test_advanced_section"
movie_location = movies.locations[0]
advanced_settings = {"enableCinemaTrailers": 0,
"enableBIFGeneration": 0,
"augmentWithProviderContent": 0,
"enableCreditsMarkerGeneration": 0}
plex.library.add(
name=section_name,
type="movie",
agent="com.plexapp.agents.none",
scanner="Plex Video Files Scanner",
language="xn",
location=[movie_location],
**advanced_settings
)
section = plex.library.section(section_name)
assert section.title == section_name
for setting in section.settings():
if setting.value != setting.default:
assert advanced_settings.get(setting.id) == setting.value
section.delete()
assert section not in plex.library.sections()
def test_library_Library_cleanBundle(plex):
plex.library.cleanBundles()
def test_library_Library_optimize(plex):
plex.library.optimize()
def test_library_Library_emptyTrash(plex):
plex.library.emptyTrash()
def _test_library_Library_refresh(plex):
# TODO: fix mangle and proof the sections attrs
plex.library.refresh()
def test_library_Library_update(plex):
plex.library.update()
def test_library_Library_cancelUpdate(plex):
plex.library.cancelUpdate()
def test_library_Library_deleteMediaPreviews(plex):
plex.library.deleteMediaPreviews()
def test_library_Library_all(plex):
assert len(plex.library.all(title__iexact="The 100"))
def test_library_Library_search(plex):
item = plex.library.search("Elephants Dream")[0]
assert item.title == "Elephants Dream"
assert len(plex.library.search(libtype="episode"))
def test_library_Library_tags(plex):
tags = plex.library.tags('genre')
assert len(tags)
with pytest.raises(NotFound):
plex.library.tags('unknown')
def test_library_MovieSection_update(movies):
movies.update()
def test_library_MovieSection_update_path(movies):
movies.update(path=movies.locations[0])
def test_library_MovieSection_refresh(movies, patched_http_call):
movies.refresh()
def test_library_MovieSection_search_genre(movie, movies):
genre = movie.genres[0]
assert len(movies.search(genre=genre)) >= 1
def test_library_MovieSection_cancelUpdate(movies):
movies.cancelUpdate()
def test_library_deleteMediaPreviews(movies):
movies.deleteMediaPreviews()
def test_library_MovieSection_onDeck(movie, movies, tvshows, episode):
movie.updateProgress(movie.duration // 4) # set progress to 25%
assert movie in movies.onDeck()
movie.markUnplayed()
episode.updateProgress(episode.duration // 4)
assert episode in tvshows.onDeck()
episode.markUnplayed()
def test_library_MovieSection_searchMovies(movies):
assert movies.searchMovies(title="Elephants Dream")
def test_library_MovieSection_recentlyAdded(movies, movie):
assert movie in movies.recentlyAdded()
assert movie in movies.recentlyAddedMovies()
def test_library_MovieSection_analyze(movies):
movies.analyze()
def test_library_MovieSection_collections(movies, movie):
try:
collection = movies.createCollection("test_library_MovieSection_collections", movie)
collections = movies.collections()
assert len(collections)
assert collection in collections
c = movies.collection(collection.title)
assert collection == c
finally:
collection.delete()
def test_library_MovieSection_collection_exception(movies):
with pytest.raises(NotFound):
movies.collection("Does Not Exists")
@pytest.mark.authenticated
def test_library_MovieSection_managedHubs(movies):
recommendations = movies.managedHubs()
with pytest.raises(BadRequest):
recommendations[0].remove()
first = recommendations[0]
first.promoteRecommended().promoteHome().promoteShared()
assert first.promotedToRecommended is True
assert first.promotedToOwnHome is True
assert first.promotedToSharedHome is True
first.demoteRecommended().demoteHome().demoteShared()
assert first.promotedToRecommended is False
assert first.promotedToOwnHome is False
assert first.promotedToSharedHome is False
last = recommendations[-1]
last.move()
recommendations = movies.managedHubs()
assert first.identifier == recommendations[1].identifier
assert last.identifier == recommendations[0].identifier
last.move(after=first)
recommendations = movies.managedHubs()
assert first.identifier == recommendations[0].identifier
assert last.identifier == recommendations[1].identifier
movies.resetManagedHubs()
recommendations = movies.managedHubs()
assert first.identifier == recommendations[0].identifier
assert last.identifier == recommendations[-1].identifier
def test_library_MovieSection_PlexWebURL(plex, movies):
tab = 'library'
url = movies.getWebURL(tab=tab)
assert url.startswith('https://app.plex.tv/desktop')
assert plex.machineIdentifier in url
assert f'source={movies.key}' in url
assert f'pivot={tab}' in url
# Test a different base
base = 'https://doesnotexist.com/plex'
url = movies.getWebURL(base=base)
assert url.startswith(base)
def test_library_MovieSection_PlexWebURL_hub(plex, movies):
hubs = movies.hubs()
hub = next(iter(hubs), None)
assert hub is not None
url = hub.section().getWebURL(key=hub.key)
assert url.startswith('https://app.plex.tv/desktop')
assert plex.machineIdentifier in url
assert f'source={movies.key}' in url
assert quote_plus(hub.key) in url
def test_library_ShowSection_all(tvshows):
assert len(tvshows.all(title__iexact="The 100"))
def test_library_ShowSection_searchShows(tvshows):
assert tvshows.searchShows(title="The 100")
def test_library_ShowSection_searchSeasons(tvshows):
assert tvshows.searchSeasons(**{"show.title": "The 100"})
def test_library_ShowSection_searchEpisodes(tvshows):
assert tvshows.searchEpisodes(title="Winter Is Coming")
def test_library_ShowSection_recentlyAdded(tvshows, show):
season = show.season(1)
episode = season.episode(1)
assert show in tvshows.recentlyAdded()
assert show in tvshows.recentlyAddedShows()
assert season in tvshows.recentlyAddedSeasons()
assert episode in tvshows.recentlyAddedEpisodes()
def test_library_ShowSection_playlists(tvshows, show):
episodes = show.episodes()
try:
playlist = tvshows.createPlaylist("test_library_ShowSection_playlists", episodes[:3])
playlists = tvshows.playlists()
assert len(playlists)
assert playlist in playlists
p = tvshows.playlist(playlist.title)
assert playlist == p
playlists = tvshows.playlists(title="test_", sort="mediaCount:asc")
assert playlist in playlists
finally:
playlist.delete()
def test_library_ShowSection_playlist_exception(tvshows):
with pytest.raises(NotFound):
tvshows.playlist("Does Not Exists")
def test_library_MusicSection_albums(music):
assert len(music.albums())
def test_library_MusicSection_stations(music):
assert len(music.stations())
def test_library_MusicSection_searchArtists(music):
assert len(music.searchArtists(title="Broke for Free"))
def test_library_MusicSection_searchAlbums(music):
assert len(music.searchAlbums(title="Layers"))
def test_library_MusicSection_searchTracks(music):
assert len(music.searchTracks(title="As Colourful As Ever"))
def test_library_MusicSection_recentlyAdded(music, artist):
album = artist.albums()[0]
track = album.tracks()[0]
assert artist in music.recentlyAdded()
assert artist in music.recentlyAddedArtists()
assert album in music.recentlyAddedAlbums()
assert track in music.recentlyAddedTracks()
@pytest.mark.authenticated
def test_library_MusicSection_sonicAdventure(account_plexpass, music):
tracks = music.searchTracks()
adventure = music.sonicAdventure(tracks[0], tracks[-1].ratingKey)
assert all(isinstance(t, plexapi.audio.Track) for t in adventure)
def test_library_PhotoSection_searchAlbums(photos, photoalbum):
title = photoalbum.title
assert len(photos.searchAlbums(title=title))
def test_library_PhotoSection_searchPhotos(photos, photoalbum):
title = photoalbum.photos()[0].title
assert len(photos.searchPhotos(title=title))
def test_library_PhotoSection_recentlyAdded(photos, photoalbum):
assert photoalbum in photos.recentlyAddedAlbums()
def test_library_and_section_search_for_movie(plex, movies):
find = "Elephants Dream"
l_search = plex.library.search(find)
s_search = movies.search(find)
assert l_search == s_search
def test_library_settings(movies):
settings = movies.settings()
assert len(settings) >= 1
def test_library_editAdvanced_default(movies):
movies.editAdvanced(hidden=2)
for setting in movies.settings():
if setting.id == "hidden":
assert int(setting.value) == 2
movies.editAdvanced(collectionMode=0)
for setting in movies.settings():
if setting.id == "collectionMode":
assert int(setting.value) == 0
movies.defaultAdvanced()
for setting in movies.settings():
assert str(setting.value) == str(setting.default)
def test_library_lockUnlockAllFields(movies):
for movie in movies.all():
assert 'thumb' not in [f.name for f in movie.fields]
movies.lockAllField('thumb')
for movie in movies.all():
assert 'thumb' in [f.name for f in movie.fields]
movies.unlockAllField('thumb')
for movie in movies.all():
assert 'thumb' not in [f.name for f in movie.fields]
def test_search_with_weird_a(plex, tvshows):
ep_title = "Coup de Grâce"
result_root = plex.search(ep_title)
result_shows = tvshows.searchEpisodes(title=ep_title)
assert result_root
assert result_shows
assert result_root == result_shows
def test_crazy_search(plex, movies, movie):
assert movie in movies.search(
actor=movie.actors[0], sort="titleSort"
), "Unable to search movie by actor."
assert movie in movies.search(
director=movie.directors[0]
), "Unable to search movie by director."
assert movie in movies.search(
year=["2006", "2007"]
), "Unable to search movie by year."
assert movie not in movies.search(year=2007), "Unable to filter movie by year."
assert movie in movies.search(actor=movie.actors[0].tag)
assert len(movies.search(container_start=2, maxresults=1)) == 1
assert len(movies.search(container_size=None)) == 4
assert len(movies.search(container_size=1)) == 4
assert len(movies.search(container_start=9999, container_size=1)) == 0
assert len(movies.search(container_start=2, container_size=1)) == 2
def test_library_section_timeline(plex, movies):
tl = movies.timeline()
assert tl.TAG == "LibraryTimeline"
assert tl.size > 0
assert tl.allowSync is False
assert tl.art == "/:/resources/movie-fanart.jpg"
assert tl.content == "secondary"
assert tl.identifier == "com.plexapp.plugins.library"
assert datetime.fromtimestamp(tl.latestEntryTime).date() == datetime.today().date()
assert tl.mediaTagPrefix == "/system/bundle/media/flags/"
assert tl.mediaTagVersion > 1
assert tl.thumb == "/:/resources/movie.png"
assert tl.title1 == "Movies"
assert utils.is_int(tl.updateQueueSize, gte=0)
assert tl.viewGroup == "secondary"
assert tl.viewMode is None
def test_library_MovieSection_hubSearch(movies):
assert movies.hubSearch("Elephants Dream")
def test_library_MovieSection_search(movies, movie, collection):
movie.addLabel("test_search")
movie.addCollection("test_search")
_test_library_search(movies, movie)
movie.removeLabel("test_search", locked=False)
movie.removeCollection("test_search", locked=False)
_test_library_search(movies, collection)
def test_library_MovieSection_search_FilterChoice(movies, collection):
filterChoice = next(c for c in movies.listFilterChoices("collection") if c.title == collection.title)
results = movies.search(filters={'collection': filterChoice})
movie = collection.items()[0]
assert movie in results
items = filterChoice.items()
assert movie in items
def test_library_MovieSection_advancedSearch(movies, movie):
advancedFilters = {
'and': [
{
'or': [
{'title': 'elephant'},
{'title': 'bunny'}
]
},
{'year>>': 1990},
{'unwatched': True}
]
}
results = movies.search(filters=advancedFilters)
assert movie in results
results = movies.search(limit=1)
assert len(results) == 1
def test_library_ShowSection_search(tvshows, show):
show.addLabel("test_search")
show.addCollection("test_search")
_test_library_search(tvshows, show)
show.removeLabel("test_search", locked=False)
show.removeCollection("test_search", locked=False)
season = show.season(season=1)
_test_library_search(tvshows, season)
episode = season.episode(episode=1)
_test_library_search(tvshows, episode)
# Additional test for mapping field to the correct libtype
assert tvshows.search(unwatched=True) # equal to episode.unwatched=True
def test_library_MusicSection_search(music, artist):
artist.addGenre("test_search")
artist.addStyle("test_search")
artist.addMood("test_search")
artist.addCollection("test_search")
_test_library_search(music, artist)
artist.removeGenre("test_search", locked=False)
artist.removeStyle("test_search", locked=False)
artist.removeMood("test_search", locked=False)
artist.removeCollection("test_search", locked=False)
album = artist.album("Layers")
album.addGenre("test_search")
album.addStyle("test_search")
album.addMood("test_search")
album.addCollection("test_search")
album.addLabel("test_search")
_test_library_search(music, album)
album.removeGenre("test_search", locked=False)
album.removeStyle("test_search", locked=False)
album.removeMood("test_search", locked=False)
album.removeCollection("test_search", locked=False)
album.removeLabel("test_search", locked=False)
track = album.track(track=1)
track.addMood("test_search")
_test_library_search(music, track)
track.removeMood("test_search", locked=False)
def test_library_PhotoSection_search(photos, photoalbum):
photo = photoalbum.photo("photo1")
photo.addTag("test_search")
_test_library_search(photos, photo)
photo.removeTag("test_search")
def test_library_MovieSection_search_sort(movies):
results = movies.search(sort="titleSort")
titleSort = [r.titleSort for r in results]
assert titleSort == sorted(titleSort)
results_asc = movies.search(sort="titleSort:asc")
titleSort_asc = [r.titleSort for r in results_asc]
assert titleSort == titleSort_asc
results_desc = movies.search(sort="titleSort:desc")
titleSort_desc = [r.titleSort for r in results_desc]
assert titleSort_desc == sorted(titleSort_desc, reverse=True)
# Test manually added sorts
results_guid = movies.search(sort="guid")
guid_asc = [r.guid for r in results_guid]
assert guid_asc == sorted(guid_asc)
results_summary = movies.search(sort="summary")
summary_asc = [r.summary for r in results_summary]
assert summary_asc == sorted(summary_asc)
results_tagline = movies.search(sort="tagline")
tagline_asc = [r.tagline for r in results_tagline if r.tagline]
assert tagline_asc == sorted(tagline_asc)
results_updatedAt = movies.search(sort="updatedAt")
updatedAt_asc = [r.updatedAt for r in results_updatedAt]
assert updatedAt_asc == sorted(updatedAt_asc)
# Test multi-sort
results_multi_str = movies.search(sort="year:asc,titleSort:asc")
titleSort_multi_str = [(r.year, r.titleSort) for r in results_multi_str]
assert titleSort_multi_str == sorted(titleSort_multi_str)
results_multi_list = movies.search(sort=["year:desc", "titleSort:desc"])
titleSort_multi_list = [(r.year, r.titleSort) for r in results_multi_list]
assert titleSort_multi_list == sorted(titleSort_multi_list, reverse=True)
# Test sort using FilteringSort object
sortObj = next(s for s in movies.listSorts() if s.key == "year")
results_sortObj = movies.search(sort=sortObj)
sortObj_list = [r.year for r in results_sortObj]
assert sortObj_list == sorted(sortObj_list, reverse=True)
def test_library_ShowSection_search_sort(tvshows):
# Test predefined Plex multi-sort
seasonAsc = "season.index,season.titleSort"
results = tvshows.search(sort=seasonAsc, libtype="season")
sortedResults = sorted(results, key=lambda s: (s.index, s.titleSort))
assert results == sortedResults
seasonShowAsc = "show.titleSort,index"
results = tvshows.search(sort=seasonShowAsc, libtype="season")
sortedResults = sorted(results, key=lambda s: (s.show().titleSort, s.index))
assert results == sortedResults
episodeShowAsc = (
"show.titleSort,season.index:nullsLast,episode.index:nullsLast,"
"episode.originallyAvailableAt:nullsLast,episode.titleSort,episode.id"
)
results = tvshows.search(sort=episodeShowAsc, libtype="episode")
sortedResults = sorted(
results,
key=lambda e: (
e.show().titleSort, e.season().index, e.index,
e.originallyAvailableAt, e.titleSort, e.ratingKey)
)
assert results == sortedResults
episodeShowDesc = (
"show.titleSort:desc,season.index:nullsLast,episode.index:nullsLast,"
"episode.originallyAvailableAt:nullsLast,episode.titleSort,episode.id"
)
results = tvshows.search(sort=episodeShowDesc, libtype="episode")
sortedResults = sorted(
sorted(
results,
key=lambda e: (
e.season().index, e.index,
e.originallyAvailableAt, e.titleSort, e.ratingKey)
),
key=lambda e: e.show().titleSort,
reverse=True
)
assert results == sortedResults
# Test manually added sorts
results_index = tvshows.search(sort="show.index,season.index,episode.index", libtype="episode")
index_asc = [(r.show().index, r.season().index, r.index) for r in results_index]
assert index_asc == sorted(index_asc)
def test_library_MusicSection_search_sort(music):
# Test predefined Plex multi-sort
albumArtistAsc = "artist.titleSort,album.titleSort,album.index,album.id,album.originallyAvailableAt"
results = music.search(sort=albumArtistAsc, libtype="album")
sortedResults = sorted(
results,
key=lambda a: (
a.artist().titleSort, a.titleSort, a.index, a.ratingKey, a.originallyAvailableAt
)
)
assert results == sortedResults
trackAlbumArtistAsc = (
"artist.titleSort,album.titleSort,album.year,"
"track.absoluteIndex,track.index,track.titleSort,track.id"
)
results = music.search(sort=trackAlbumArtistAsc, libtype="track")
sortedResults = sorted(
results,
key=lambda t: (
t.artist().titleSort, t.album().titleSort, t.album().year,
t.index, t.titleSort, t.ratingKey # Skip unknown absoluteIndex
)
)
assert results == sortedResults
def test_library_search_exceptions(movies):
with pytest.raises(BadRequest):
movies.listFilterChoices(field="123abc.title")
with pytest.raises(BadRequest):
movies.search(**{"123abc": True})
with pytest.raises(BadRequest):
movies.search(year="123abc")
with pytest.raises(BadRequest):
movies.search(sort="123abc")
with pytest.raises(BadRequest):
movies.search(filters=[])
with pytest.raises(BadRequest):
movies.search(filters={'and': {'title': 'test'}})
with pytest.raises(BadRequest):
movies.search(filters={'and': [], 'title': 'test'})
with pytest.raises(NotFound):
movies.getFilterType(libtype="show")
with pytest.raises(NotFound):
movies.getFieldType(fieldType="unknown")
with pytest.raises(NotFound):
movies.listFilterChoices(field="unknown")
with pytest.raises(NotFound):
movies.search(unknown="unknown")
with pytest.raises(NotFound):
movies.search(**{"title<>!=": "unknown"})
with pytest.raises(NotFound):
movies.search(sort="unknown")
with pytest.raises(NotFound):
movies.search(sort="titleSort:bad")
def _test_library_search(library, obj): # noqa: C901
# Create & operator
AndOperator = namedtuple("AndOperator", ["key", "title"])
andOp = AndOperator("&=", "and")
fields = library.listFields(obj.type)
for field in fields:
fieldAttr = field.key.split(".")[-1]
if fieldAttr in {"unmatched", "userRating"}:
continue
operators = library.listOperators(field.type)
if field.type in {"tag", "string"}:
operators += [andOp]
for operator in operators:
if (
fieldAttr in {"audienceRating", "rating"} and operator.key in {"=", "!="}
):
continue
value = getattr(obj, fieldAttr, None)
if field.type == "boolean" and value is None:
value = fieldAttr.startswith("unwatched")
if field.type == "tag" and isinstance(value, list) and value and operator.title != "and":
value = value[0]
elif value is None:
continue
if operator.title == "begins with":
searchValue = value[:3]
elif operator.title == "ends with":
searchValue = value[-3:]
elif "contain" in operator.title:
searchValue = value.split(" ")[0]
elif operator.title == "is less than":
searchValue = value + 1
elif operator.title == "is greater than":
searchValue = max(value - 1, 1)
elif operator.title == "is before":
searchValue = value + timedelta(days=1)
elif operator.title == "is after":
searchValue = value - timedelta(days=1)
else:
searchValue = value
_do_test_library_search(library, obj, field, operator, searchValue)
# Test search again using string tag and date
if field.type == "tag" and fieldAttr != "contentRating":
if not isinstance(searchValue, list):
searchValue = [searchValue]
searchValue = [v.tag for v in searchValue]
_do_test_library_search(library, obj, field, operator, searchValue)
elif field.type == "date":
searchValue = searchValue.strftime("%Y-%m-%d")
_do_test_library_search(library, obj, field, operator, searchValue)
searchValue = "1s"
_do_test_library_search(library, obj, field, operator, searchValue)
def _do_test_library_search(library, obj, field, operator, searchValue):
searchFilter = {field.key + operator.key[:-1]: searchValue}
results = library.search(libtype=obj.type, filters=searchFilter)
if operator.key.startswith("!") or operator.key.startswith(">>") and (searchValue == 1 or searchValue == "1s"):
assert obj not in results
else:
assert obj in results, f"Unable to search {obj.type} by {field.key} using {operator.key} and value {searchValue}."
def test_library_common(movies):
items = movies.all()
common = movies.common(items)
assert common.commonType == "movie"
assert common.ratingKeys == [m.ratingKey for m in items]
assert common.items() == items
def test_library_multiedit(movies, tvshows):
movie1, movie2 = movies.all()[:2]
show1, show2 = tvshows.all()[:2]
movie1_title = movie1.title
movie2_title = movie2.title
show1_title = show1.title
# Edit multiple titles
title = "Test Title"
movies.multiEdit([movie1, movie2], **{"title.value": title})
assert movie1.reload().title == title
assert movie2.reload().title == title
# Reset titles
movie1.editTitle(movie1_title, locked=False).reload()
movie2.editTitle(movie2_title, locked=False).reload()
assert movie1.title == movie1_title
assert movie2.title == movie2_title
# Test batch multi-editing
genre = "Test Genre"
tvshows.batchMultiEdits([show1, show2]).addGenre(genre).saveMultiEdits()
assert genre in [g.tag for g in show1.reload().genres]
assert genre in [g.tag for g in show2.reload().genres]
# Reset genres
tvshows.batchMultiEdits([show1, show2]).removeGenre(genre, locked=False).saveMultiEdits()
assert genre not in [g.tag for g in show1.reload().genres]
assert genre not in [g.tag for g in show2.reload().genres]
# Test multi-editing with a single item
tvshows.batchMultiEdits(show1).editTitle(title).saveMultiEdits()
assert show1.reload().title == title
# Reset title
show1.editTitle(show1_title, locked=False).reload()
assert show1.title == show1_title
def test_library_multiedit_exceptions(music, artist, album, photos):
with pytest.raises(BadRequest):
music.multiEdit([])
with pytest.raises(BadRequest):
music.multiEdit([artist, album])
with pytest.raises(BadRequest):
photos.batchMultiEdits(artist)
with pytest.raises(BadRequest):
photos.saveMultiEdits()
with pytest.raises(AttributeError):
photos.editTitle("test")
with pytest.raises(AttributeError):
music.batchMultiEdits(artist).editEdition("test")
with pytest.raises(AttributeError):
music.batchMultiEdits(album).addCountry("test")