File: test_models.py

package info (click to toggle)
mopidy 3.4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,608 kB
  • sloc: python: 16,656; sh: 159; makefile: 127
file content (1248 lines) | stat: -rw-r--r-- 39,030 bytes parent folder | download | duplicates (3)
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
import json
import unittest

from mopidy.models import (
    Album,
    Artist,
    Image,
    ModelJSONEncoder,
    Playlist,
    Ref,
    SearchResult,
    TlTrack,
    Track,
    model_json_decoder,
)


class InheritanceTest(unittest.TestCase):
    def test_weakref_and_slots_play_nice_in_subclass(self):
        # Check that the following does not happen:
        # TypeError: Error when calling the metaclass bases
        #   __weakref__ slot disallowed: either we already got one...

        class Foo(Track):
            pass

    def test_sub_class_can_have_its_own_slots(self):
        # Needed for things like SpotifyTrack in mopidy-spotify 1.x

        class Foo(Track):
            __slots__ = ("_foo",)

        f = Foo()
        f._foo = 123

    def test_sub_class_can_be_initialized(self):
        # Fails with following error if fields are not handled across classes.
        #   TypeError: __init__() got an unexpected keyword argument "type"
        # Essentially this is testing that sub-classes take parent _fields into
        # account.

        class Foo(Ref):
            pass

        Foo.directory()


class CachingTest(unittest.TestCase):
    def test_same_instance(self):
        assert Track() is Track()

    def test_same_instance_with_values(self):
        assert Track(uri="test") is Track(uri="test")

    def test_different_instance_with_different_values(self):
        assert Track(uri="test1") is not Track(uri="test2")

    def test_different_instance_with_replace(self):
        t = Track(uri="test1")
        assert t is not t.replace(uri="test2")


class GenericReplaceTest(unittest.TestCase):
    def compare(self, orig, other):
        assert orig == other
        assert id(orig) == id(other)

    def test_replace_track(self):
        track = Track()
        self.compare(track, track.replace())

    def test_replace_artist(self):
        artist = Artist()
        self.compare(artist, artist.replace())

    def test_replace_album(self):
        album = Album()
        self.compare(album, album.replace())

    def test_replace_playlist(self):
        playlist = Playlist()
        self.compare(playlist, playlist.replace())

    def test_replace_track_with_basic_values(self):
        track = Track(name="foo", uri="bar")
        other = track.replace(name="baz")
        assert "baz" == other.name
        assert "bar" == other.uri

    def test_replace_track_with_missing_values(self):
        track = Track(uri="bar")
        other = track.replace(name="baz")
        assert "baz" == other.name
        assert "bar" == other.uri

    def test_replace_track_with_private_internal_value(self):
        artist1 = Artist(name="foo")
        artist2 = Artist(name="bar")
        track = Track(artists=[artist1])
        other = track.replace(artists=[artist2])
        assert artist2 in other.artists

    def test_replace_track_with_invalid_key(self):
        with self.assertRaises(TypeError):
            Track().replace(invalid_key=True)

    def test_replace_track_to_remove(self):
        track = Track(name="foo").replace(name=None)
        assert not hasattr(track, "_name")


class RefTest(unittest.TestCase):
    def test_uri(self):
        uri = "an_uri"
        ref = Ref(uri=uri)
        assert ref.uri == uri
        with self.assertRaises(AttributeError):
            ref.uri = None

    def test_name(self):
        name = "a name"
        ref = Ref(name=name)
        assert ref.name == name
        with self.assertRaises(AttributeError):
            ref.name = None

    # TODO: add these for the more of the models?
    def test_del_name(self):
        ref = Ref(name="foo")
        with self.assertRaises(AttributeError):
            del ref.name

    def test_invalid_kwarg(self):
        with self.assertRaises(TypeError):
            Ref(foo="baz")

    def test_repr_without_results(self):
        assert "Ref(name='foo', type='artist', uri='uri')" == repr(
            Ref(uri="uri", name="foo", type="artist")
        )

    def test_serialize_without_results(self):
        self.assertDictEqual(
            {"__model__": "Ref", "uri": "uri"}, Ref(uri="uri").serialize()
        )

    def test_to_json_and_back(self):
        ref1 = Ref(uri="uri")
        serialized = json.dumps(ref1, cls=ModelJSONEncoder)
        ref2 = json.loads(serialized, object_hook=model_json_decoder)
        assert ref1 == ref2

    def test_type_constants(self):
        assert Ref.ALBUM == "album"
        assert Ref.ARTIST == "artist"
        assert Ref.DIRECTORY == "directory"
        assert Ref.PLAYLIST == "playlist"
        assert Ref.TRACK == "track"

    def test_album_constructor(self):
        ref = Ref.album(uri="foo", name="bar")
        assert ref.uri == "foo"
        assert ref.name == "bar"
        assert ref.type == Ref.ALBUM

    def test_artist_constructor(self):
        ref = Ref.artist(uri="foo", name="bar")
        assert ref.uri == "foo"
        assert ref.name == "bar"
        assert ref.type == Ref.ARTIST

    def test_directory_constructor(self):
        ref = Ref.directory(uri="foo", name="bar")
        assert ref.uri == "foo"
        assert ref.name == "bar"
        assert ref.type == Ref.DIRECTORY

    def test_playlist_constructor(self):
        ref = Ref.playlist(uri="foo", name="bar")
        assert ref.uri == "foo"
        assert ref.name == "bar"
        assert ref.type == Ref.PLAYLIST

    def test_track_constructor(self):
        ref = Ref.track(uri="foo", name="bar")
        assert ref.uri == "foo"
        assert ref.name == "bar"
        assert ref.type == Ref.TRACK


class ImageTest(unittest.TestCase):
    def test_uri(self):
        uri = "an_uri"
        image = Image(uri=uri)
        assert image.uri == uri
        with self.assertRaises(AttributeError):
            image.uri = None

    def test_width(self):
        image = Image(width=100)
        assert image.width == 100
        with self.assertRaises(AttributeError):
            image.width = None

    def test_height(self):
        image = Image(height=100)
        assert image.height == 100
        with self.assertRaises(AttributeError):
            image.height = None

    def test_invalid_kwarg(self):
        with self.assertRaises(TypeError):
            Image(foo="baz")


class ArtistTest(unittest.TestCase):
    def test_uri(self):
        uri = "an_uri"
        artist = Artist(uri=uri)
        assert artist.uri == uri
        with self.assertRaises(AttributeError):
            artist.uri = None

    def test_name(self):
        name = "a name"
        artist = Artist(name=name)
        assert artist.name == name
        with self.assertRaises(AttributeError):
            artist.name = None

    def test_musicbrainz_id(self):
        mb_id = "mb-id"
        artist = Artist(musicbrainz_id=mb_id)
        assert artist.musicbrainz_id == mb_id
        with self.assertRaises(AttributeError):
            artist.musicbrainz_id = None

    def test_invalid_kwarg(self):
        with self.assertRaises(TypeError):
            Artist(foo="baz")

    def test_invalid_kwarg_with_name_matching_method(self):
        with self.assertRaises(TypeError):
            Artist(replace="baz")

        with self.assertRaises(TypeError):
            Artist(serialize="baz")

    def test_repr(self):
        assert "Artist(name='name', uri='uri')" == repr(
            Artist(uri="uri", name="name")
        )

    def test_serialize(self):
        self.assertDictEqual(
            {"__model__": "Artist", "uri": "uri", "name": "name"},
            Artist(uri="uri", name="name").serialize(),
        )

    def test_serialize_falsy_values(self):
        self.assertDictEqual(
            {"__model__": "Artist", "uri": "", "name": ""},
            Artist(uri="", name="").serialize(),
        )

    def test_to_json_and_back(self):
        artist1 = Artist(uri="uri", name="name")
        serialized = json.dumps(artist1, cls=ModelJSONEncoder)
        artist2 = json.loads(serialized, object_hook=model_json_decoder)
        assert artist1 == artist2

    def test_to_json_and_back_with_unknown_field(self):
        artist = Artist(uri="uri", name="name").serialize()
        artist["foo"] = "foo"
        serialized = json.dumps(artist)
        with self.assertRaises(TypeError):
            json.loads(serialized, object_hook=model_json_decoder)

    def test_to_json_and_back_with_field_matching_method(self):
        artist = Artist(uri="uri", name="name").serialize()
        artist["copy"] = "foo"
        serialized = json.dumps(artist)
        with self.assertRaises(TypeError):
            json.loads(serialized, object_hook=model_json_decoder)

    def test_to_json_and_back_with_field_matching_internal_field(self):
        artist = Artist(uri="uri", name="name").serialize()
        artist["__mro__"] = "foo"
        serialized = json.dumps(artist)
        with self.assertRaises(TypeError):
            json.loads(serialized, object_hook=model_json_decoder)

    def test_eq_name(self):
        artist1 = Artist(name="name")
        artist2 = Artist(name="name")
        assert artist1 == artist2
        assert hash(artist1) == hash(artist2)

    def test_eq_uri(self):
        artist1 = Artist(uri="uri")
        artist2 = Artist(uri="uri")
        assert artist1 == artist2
        assert hash(artist1) == hash(artist2)

    def test_eq_musibrainz_id(self):
        artist1 = Artist(musicbrainz_id="id")
        artist2 = Artist(musicbrainz_id="id")
        assert artist1 == artist2
        assert hash(artist1) == hash(artist2)

    def test_eq(self):
        artist1 = Artist(uri="uri", name="name", musicbrainz_id="id")
        artist2 = Artist(uri="uri", name="name", musicbrainz_id="id")
        assert artist1 == artist2
        assert hash(artist1) == hash(artist2)

    def test_eq_none(self):
        assert Artist() is not None

    def test_eq_other(self):
        assert Artist() != "other"

    def test_ne_name(self):
        artist1 = Artist(name="name1")
        artist2 = Artist(name="name2")
        assert artist1 != artist2
        assert hash(artist1) != hash(artist2)

    def test_ne_uri(self):
        artist1 = Artist(uri="uri1")
        artist2 = Artist(uri="uri2")
        assert artist1 != artist2
        assert hash(artist1) != hash(artist2)

    def test_ne_musicbrainz_id(self):
        artist1 = Artist(musicbrainz_id="id1")
        artist2 = Artist(musicbrainz_id="id2")
        assert artist1 != artist2
        assert hash(artist1) != hash(artist2)

    def test_ne(self):
        artist1 = Artist(uri="uri1", name="name1", musicbrainz_id="id1")
        artist2 = Artist(uri="uri2", name="name2", musicbrainz_id="id2")
        assert artist1 != artist2
        assert hash(artist1) != hash(artist2)


class AlbumTest(unittest.TestCase):
    def test_uri(self):
        uri = "an_uri"
        album = Album(uri=uri)
        assert album.uri == uri
        with self.assertRaises(AttributeError):
            album.uri = None

    def test_name(self):
        name = "a name"
        album = Album(name=name)
        assert album.name == name
        with self.assertRaises(AttributeError):
            album.name = None

    def test_artists(self):
        artist = Artist()
        album = Album(artists=[artist])
        assert artist in album.artists
        with self.assertRaises(AttributeError):
            album.artists = None

    def test_artists_none(self):
        assert set() == Album(artists=None).artists

    def test_num_tracks(self):
        num_tracks = 11
        album = Album(num_tracks=num_tracks)
        assert album.num_tracks == num_tracks
        with self.assertRaises(AttributeError):
            album.num_tracks = None

    def test_num_discs(self):
        num_discs = 2
        album = Album(num_discs=num_discs)
        assert album.num_discs == num_discs
        with self.assertRaises(AttributeError):
            album.num_discs = None

    def test_date(self):
        date = "1977-01-01"
        album = Album(date=date)
        assert album.date == date
        with self.assertRaises(AttributeError):
            album.date = None

    def test_musicbrainz_id(self):
        mb_id = "mb-id"
        album = Album(musicbrainz_id=mb_id)
        assert album.musicbrainz_id == mb_id
        with self.assertRaises(AttributeError):
            album.musicbrainz_id = None

    def test_invalid_kwarg(self):
        with self.assertRaises(TypeError):
            Album(foo="baz")

    def test_repr_without_artists(self):
        assert "Album(name='name', uri='uri')" == repr(
            Album(uri="uri", name="name")
        )

    def test_repr_with_artists(self):
        assert (
            "Album(artists=[Artist(name='foo')], name='name', uri='uri')"
            == repr(Album(uri="uri", name="name", artists=[Artist(name="foo")]))
        )

    def test_serialize_without_artists(self):
        self.assertDictEqual(
            {"__model__": "Album", "uri": "uri", "name": "name"},
            Album(uri="uri", name="name").serialize(),
        )

    def test_serialize_with_artists(self):
        artist = Artist(name="foo")
        self.assertDictEqual(
            {
                "__model__": "Album",
                "uri": "uri",
                "name": "name",
                "artists": [artist.serialize()],
            },
            Album(uri="uri", name="name", artists=[artist]).serialize(),
        )

    def test_to_json_and_back(self):
        album1 = Album(uri="uri", name="name", artists=[Artist(name="foo")])
        serialized = json.dumps(album1, cls=ModelJSONEncoder)
        album2 = json.loads(serialized, object_hook=model_json_decoder)
        assert album1 == album2

    def test_eq_name(self):
        album1 = Album(name="name")
        album2 = Album(name="name")
        assert album1 == album2
        assert hash(album1) == hash(album2)

    def test_eq_uri(self):
        album1 = Album(uri="uri")
        album2 = Album(uri="uri")
        assert album1 == album2
        assert hash(album1) == hash(album2)

    def test_eq_artists(self):
        artists = [Artist()]
        album1 = Album(artists=artists)
        album2 = Album(artists=artists)
        assert album1 == album2
        assert hash(album1) == hash(album2)

    def test_eq_artists_order(self):
        artist1 = Artist(name="name1")
        artist2 = Artist(name="name2")
        album1 = Album(artists=[artist1, artist2])
        album2 = Album(artists=[artist2, artist1])
        assert album1 == album2
        assert hash(album1) == hash(album2)

    def test_eq_num_tracks(self):
        album1 = Album(num_tracks=2)
        album2 = Album(num_tracks=2)
        assert album1 == album2
        assert hash(album1) == hash(album2)

    def test_eq_date(self):
        date = "1977-01-01"
        album1 = Album(date=date)
        album2 = Album(date=date)
        assert album1 == album2
        assert hash(album1) == hash(album2)

    def test_eq_musibrainz_id(self):
        album1 = Album(musicbrainz_id="id")
        album2 = Album(musicbrainz_id="id")
        assert album1 == album2
        assert hash(album1) == hash(album2)

    def test_eq(self):
        artists = [Artist()]
        album1 = Album(
            name="name",
            uri="uri",
            artists=artists,
            num_tracks=2,
            musicbrainz_id="id",
        )
        album2 = Album(
            name="name",
            uri="uri",
            artists=artists,
            num_tracks=2,
            musicbrainz_id="id",
        )
        assert album1 == album2
        assert hash(album1) == hash(album2)

    def test_eq_none(self):
        assert Album() is not None

    def test_eq_other(self):
        assert Album() != "other"

    def test_ne_name(self):
        album1 = Album(name="name1")
        album2 = Album(name="name2")
        assert album1 != album2
        assert hash(album1) != hash(album2)

    def test_ne_uri(self):
        album1 = Album(uri="uri1")
        album2 = Album(uri="uri2")
        assert album1 != album2
        assert hash(album1) != hash(album2)

    def test_ne_artists(self):
        album1 = Album(artists=[Artist(name="name1")])
        album2 = Album(artists=[Artist(name="name2")])
        assert album1 != album2
        assert hash(album1) != hash(album2)

    def test_ne_num_tracks(self):
        album1 = Album(num_tracks=1)
        album2 = Album(num_tracks=2)
        assert album1 != album2
        assert hash(album1) != hash(album2)

    def test_ne_date(self):
        album1 = Album(date="1977-01-01")
        album2 = Album(date="1977-01-02")
        assert album1 != album2
        assert hash(album1) != hash(album2)

    def test_ne_musicbrainz_id(self):
        album1 = Album(musicbrainz_id="id1")
        album2 = Album(musicbrainz_id="id2")
        assert album1 != album2
        assert hash(album1) != hash(album2)

    def test_ne(self):
        album1 = Album(
            name="name1",
            uri="uri1",
            artists=[Artist(name="name1")],
            num_tracks=1,
            musicbrainz_id="id1",
        )
        album2 = Album(
            name="name2",
            uri="uri2",
            artists=[Artist(name="name2")],
            num_tracks=2,
            musicbrainz_id="id2",
        )
        assert album1 != album2
        assert hash(album1) != hash(album2)


class TrackTest(unittest.TestCase):
    def test_uri(self):
        uri = "an_uri"
        track = Track(uri=uri)
        assert track.uri == uri
        with self.assertRaises(AttributeError):
            track.uri = None

    def test_name(self):
        name = "a name"
        track = Track(name=name)
        assert track.name == name
        with self.assertRaises(AttributeError):
            track.name = None

    def test_artists(self):
        artists = [Artist(name="name1"), Artist(name="name2")]
        track = Track(artists=artists)
        assert set(track.artists) == set(artists)
        with self.assertRaises(AttributeError):
            track.artists = None

    def test_artists_none(self):
        assert set() == Track(artists=None).artists

    def test_composers(self):
        artists = [Artist(name="name1"), Artist(name="name2")]
        track = Track(composers=artists)
        assert set(track.composers) == set(artists)
        with self.assertRaises(AttributeError):
            track.composers = None

    def test_composers_none(self):
        assert set() == Track(composers=None).composers

    def test_performers(self):
        artists = [Artist(name="name1"), Artist(name="name2")]
        track = Track(performers=artists)
        assert set(track.performers) == set(artists)
        with self.assertRaises(AttributeError):
            track.performers = None

    def test_performers_none(self):
        assert set() == Track(performers=None).performers

    def test_album(self):
        album = Album()
        track = Track(album=album)
        assert track.album == album
        with self.assertRaises(AttributeError):
            track.album = None

    def test_track_no(self):
        track_no = 7
        track = Track(track_no=track_no)
        assert track.track_no == track_no
        with self.assertRaises(AttributeError):
            track.track_no = None

    def test_disc_no(self):
        disc_no = 2
        track = Track(disc_no=disc_no)
        assert track.disc_no == disc_no
        with self.assertRaises(AttributeError):
            track.disc_no = None

    def test_date(self):
        date = "1977-01-01"
        track = Track(date=date)
        assert track.date == date
        with self.assertRaises(AttributeError):
            track.date = None

    def test_length(self):
        length = 137000
        track = Track(length=length)
        assert track.length == length
        with self.assertRaises(AttributeError):
            track.length = None

    def test_bitrate(self):
        bitrate = 160
        track = Track(bitrate=bitrate)
        assert track.bitrate == bitrate
        with self.assertRaises(AttributeError):
            track.bitrate = None

    def test_musicbrainz_id(self):
        mb_id = "mb-id"
        track = Track(musicbrainz_id=mb_id)
        assert track.musicbrainz_id == mb_id
        with self.assertRaises(AttributeError):
            track.musicbrainz_id = None

    def test_invalid_kwarg(self):
        with self.assertRaises(TypeError):
            Track(foo="baz")

    def test_repr_without_artists(self):
        assert "Track(name='name', uri='uri')" == repr(
            Track(uri="uri", name="name")
        )

    def test_repr_with_artists(self):
        assert (
            "Track(artists=[Artist(name='foo')], name='name', uri='uri')"
            == repr(Track(uri="uri", name="name", artists=[Artist(name="foo")]))
        )

    def test_serialize_without_artists(self):
        self.assertDictEqual(
            {"__model__": "Track", "uri": "uri", "name": "name"},
            Track(uri="uri", name="name").serialize(),
        )

    def test_serialize_with_artists(self):
        artist = Artist(name="foo")
        self.assertDictEqual(
            {
                "__model__": "Track",
                "uri": "uri",
                "name": "name",
                "artists": [artist.serialize()],
            },
            Track(uri="uri", name="name", artists=[artist]).serialize(),
        )

    def test_serialize_with_album(self):
        album = Album(name="foo")
        self.assertDictEqual(
            {
                "__model__": "Track",
                "uri": "uri",
                "name": "name",
                "album": album.serialize(),
            },
            Track(uri="uri", name="name", album=album).serialize(),
        )

    def test_to_json_and_back(self):
        track1 = Track(
            uri="uri",
            name="name",
            album=Album(name="foo"),
            artists=[Artist(name="foo")],
        )
        serialized = json.dumps(track1, cls=ModelJSONEncoder)
        track2 = json.loads(serialized, object_hook=model_json_decoder)
        assert track1 == track2

    def test_eq_uri(self):
        track1 = Track(uri="uri1")
        track2 = Track(uri="uri1")
        assert track1 == track2
        assert hash(track1) == hash(track2)

    def test_eq_name(self):
        track1 = Track(name="name1")
        track2 = Track(name="name1")
        assert track1 == track2
        assert hash(track1) == hash(track2)

    def test_eq_artists(self):
        artists = [Artist()]
        track1 = Track(artists=artists)
        track2 = Track(artists=artists)
        assert track1 == track2
        assert hash(track1) == hash(track2)

    def test_eq_artists_order(self):
        artist1 = Artist(name="name1")
        artist2 = Artist(name="name2")
        track1 = Track(artists=[artist1, artist2])
        track2 = Track(artists=[artist2, artist1])
        assert track1 == track2
        assert hash(track1) == hash(track2)

    def test_eq_album(self):
        album = Album()
        track1 = Track(album=album)
        track2 = Track(album=album)
        assert track1 == track2
        assert hash(track1) == hash(track2)

    def test_eq_track_no(self):
        track1 = Track(track_no=1)
        track2 = Track(track_no=1)
        assert track1 == track2
        assert hash(track1) == hash(track2)

    def test_eq_date(self):
        date = "1977-01-01"
        track1 = Track(date=date)
        track2 = Track(date=date)
        assert track1 == track2
        assert hash(track1) == hash(track2)

    def test_eq_length(self):
        track1 = Track(length=100)
        track2 = Track(length=100)
        assert track1 == track2
        assert hash(track1) == hash(track2)

    def test_eq_bitrate(self):
        track1 = Track(bitrate=100)
        track2 = Track(bitrate=100)
        assert track1 == track2
        assert hash(track1) == hash(track2)

    def test_eq_musibrainz_id(self):
        track1 = Track(musicbrainz_id="id")
        track2 = Track(musicbrainz_id="id")
        assert track1 == track2
        assert hash(track1) == hash(track2)

    def test_eq(self):
        date = "1977-01-01"
        artists = [Artist()]
        album = Album()
        track1 = Track(
            uri="uri",
            name="name",
            artists=artists,
            album=album,
            track_no=1,
            date=date,
            length=100,
            bitrate=100,
            musicbrainz_id="id",
        )
        track2 = Track(
            uri="uri",
            name="name",
            artists=artists,
            album=album,
            track_no=1,
            date=date,
            length=100,
            bitrate=100,
            musicbrainz_id="id",
        )
        assert track1 == track2
        assert hash(track1) == hash(track2)

    def test_eq_none(self):
        assert Track() is not None

    def test_eq_other(self):
        assert Track() != "other"

    def test_ne_uri(self):
        track1 = Track(uri="uri1")
        track2 = Track(uri="uri2")
        assert track1 != track2
        assert hash(track1) != hash(track2)

    def test_ne_name(self):
        track1 = Track(name="name1")
        track2 = Track(name="name2")
        assert track1 != track2
        assert hash(track1) != hash(track2)

    def test_ne_artists(self):
        track1 = Track(artists=[Artist(name="name1")])
        track2 = Track(artists=[Artist(name="name2")])
        assert track1 != track2
        assert hash(track1) != hash(track2)

    def test_ne_album(self):
        track1 = Track(album=Album(name="name1"))
        track2 = Track(album=Album(name="name2"))
        assert track1 != track2
        assert hash(track1) != hash(track2)

    def test_ne_track_no(self):
        track1 = Track(track_no=1)
        track2 = Track(track_no=2)
        assert track1 != track2
        assert hash(track1) != hash(track2)

    def test_ne_date(self):
        track1 = Track(date="1977-01-01")
        track2 = Track(date="1977-01-02")
        assert track1 != track2
        assert hash(track1) != hash(track2)

    def test_ne_length(self):
        track1 = Track(length=100)
        track2 = Track(length=200)
        assert track1 != track2
        assert hash(track1) != hash(track2)

    def test_ne_bitrate(self):
        track1 = Track(bitrate=100)
        track2 = Track(bitrate=200)
        assert track1 != track2
        assert hash(track1) != hash(track2)

    def test_ne_musicbrainz_id(self):
        track1 = Track(musicbrainz_id="id1")
        track2 = Track(musicbrainz_id="id2")
        assert track1 != track2
        assert hash(track1) != hash(track2)

    def test_ne(self):
        track1 = Track(
            uri="uri1",
            name="name1",
            artists=[Artist(name="name1")],
            album=Album(name="name1"),
            track_no=1,
            date="1977-01-01",
            length=100,
            bitrate=100,
            musicbrainz_id="id1",
        )
        track2 = Track(
            uri="uri2",
            name="name2",
            artists=[Artist(name="name2")],
            album=Album(name="name2"),
            track_no=2,
            date="1977-01-02",
            length=200,
            bitrate=200,
            musicbrainz_id="id2",
        )
        assert track1 != track2
        assert hash(track1) != hash(track2)

    def test_ignores_values_with_default_value_none(self):
        track1 = Track(name="name1")
        track2 = Track(name="name1", album=None)
        assert track1 == track2
        assert hash(track1) == hash(track2)

    def test_replace_can_reset_to_default_value(self):
        track1 = Track(name="name1")
        track2 = Track(name="name1", album=Album()).replace(album=None)
        assert track1 == track2
        assert hash(track1) == hash(track2)


class TlTrackTest(unittest.TestCase):
    def test_tlid(self):
        tlid = 123
        tl_track = TlTrack(tlid=tlid)
        assert tl_track.tlid == tlid
        with self.assertRaises(AttributeError):
            tl_track.tlid = None

    def test_track(self):
        track = Track()
        tl_track = TlTrack(track=track)
        assert tl_track.track == track
        with self.assertRaises(AttributeError):
            tl_track.track = None

    def test_invalid_kwarg(self):
        with self.assertRaises(TypeError):
            TlTrack(foo="baz")

    def test_positional_args(self):
        tlid = 123
        track = Track()
        tl_track = TlTrack(tlid, track)
        assert tl_track.tlid == tlid
        assert tl_track.track == track

    def test_iteration(self):
        tlid = 123
        track = Track()
        tl_track = TlTrack(tlid, track)
        (tlid2, track2) = tl_track
        assert tlid2 == tlid
        assert track2 == track

    def test_repr(self):
        assert "TlTrack(tlid=123, track=Track(uri='uri'))" == repr(
            TlTrack(tlid=123, track=Track(uri="uri"))
        )

    def test_serialize(self):
        track = Track(uri="uri", name="name")
        self.assertDictEqual(
            {"__model__": "TlTrack", "tlid": 123, "track": track.serialize()},
            TlTrack(tlid=123, track=track).serialize(),
        )

    def test_to_json_and_back(self):
        tl_track1 = TlTrack(tlid=123, track=Track(uri="uri", name="name"))
        serialized = json.dumps(tl_track1, cls=ModelJSONEncoder)
        tl_track2 = json.loads(serialized, object_hook=model_json_decoder)
        assert tl_track1 == tl_track2

    def test_eq(self):
        tlid = 123
        track = Track()
        tl_track1 = TlTrack(tlid=tlid, track=track)
        tl_track2 = TlTrack(tlid=tlid, track=track)
        assert tl_track1 == tl_track2
        assert hash(tl_track1) == hash(tl_track2)

    def test_eq_none(self):
        assert TlTrack() is not None

    def test_eq_other(self):
        assert TlTrack() != "other"

    def test_ne_tlid(self):
        tl_track1 = TlTrack(tlid=123)
        tl_track2 = TlTrack(tlid=321)
        assert tl_track1 != tl_track2
        assert hash(tl_track1) != hash(tl_track2)

    def test_ne_track(self):
        tl_track1 = TlTrack(track=Track(uri="a"))
        tl_track2 = TlTrack(track=Track(uri="b"))
        assert tl_track1 != tl_track2
        assert hash(tl_track1) != hash(tl_track2)


class PlaylistTest(unittest.TestCase):
    def test_uri(self):
        uri = "an_uri"
        playlist = Playlist(uri=uri)
        assert playlist.uri == uri
        with self.assertRaises(AttributeError):
            playlist.uri = None

    def test_name(self):
        name = "a name"
        playlist = Playlist(name=name)
        assert playlist.name == name
        with self.assertRaises(AttributeError):
            playlist.name = None

    def test_tracks(self):
        tracks = [Track(), Track(), Track()]
        playlist = Playlist(tracks=tracks)
        assert list(playlist.tracks) == tracks
        with self.assertRaises(AttributeError):
            playlist.tracks = None

    def test_length(self):
        tracks = [Track(), Track(), Track()]
        playlist = Playlist(tracks=tracks)
        assert playlist.length == 3

    def test_last_modified(self):
        last_modified = 1390942873000
        playlist = Playlist(last_modified=last_modified)
        assert playlist.last_modified == last_modified
        with self.assertRaises(AttributeError):
            playlist.last_modified = None

    def test_with_new_uri(self):
        tracks = [Track()]
        last_modified = 1390942873000
        playlist = Playlist(
            uri="an uri",
            name="a name",
            tracks=tracks,
            last_modified=last_modified,
        )
        new_playlist = playlist.replace(uri="another uri")
        assert new_playlist.uri == "another uri"
        assert new_playlist.name == "a name"
        assert list(new_playlist.tracks) == tracks
        assert new_playlist.last_modified == last_modified

    def test_with_new_name(self):
        tracks = [Track()]
        last_modified = 1390942873000
        playlist = Playlist(
            uri="an uri",
            name="a name",
            tracks=tracks,
            last_modified=last_modified,
        )
        new_playlist = playlist.replace(name="another name")
        assert new_playlist.uri == "an uri"
        assert new_playlist.name == "another name"
        assert list(new_playlist.tracks) == tracks
        assert new_playlist.last_modified == last_modified

    def test_with_new_tracks(self):
        tracks = [Track()]
        last_modified = 1390942873000
        playlist = Playlist(
            uri="an uri",
            name="a name",
            tracks=tracks,
            last_modified=last_modified,
        )
        new_tracks = [Track(), Track()]
        new_playlist = playlist.replace(tracks=new_tracks)
        assert new_playlist.uri == "an uri"
        assert new_playlist.name == "a name"
        assert list(new_playlist.tracks) == new_tracks
        assert new_playlist.last_modified == last_modified

    def test_with_new_last_modified(self):
        tracks = [Track()]
        last_modified = 1390942873000
        new_last_modified = last_modified + 1000
        playlist = Playlist(
            uri="an uri",
            name="a name",
            tracks=tracks,
            last_modified=last_modified,
        )
        new_playlist = playlist.replace(last_modified=new_last_modified)
        assert new_playlist.uri == "an uri"
        assert new_playlist.name == "a name"
        assert list(new_playlist.tracks) == tracks
        assert new_playlist.last_modified == new_last_modified

    def test_invalid_kwarg(self):
        with self.assertRaises(TypeError):
            Playlist(foo="baz")

    def test_repr_without_tracks(self):
        assert "Playlist(name='name', uri='uri')" == repr(
            Playlist(uri="uri", name="name")
        )

    def test_repr_with_tracks(self):
        assert (
            "Playlist(name='name', tracks=[Track(name='foo')], uri='uri')"
            == repr(
                Playlist(uri="uri", name="name", tracks=[Track(name="foo")])
            )
        )

    def test_serialize_without_tracks(self):
        self.assertDictEqual(
            {"__model__": "Playlist", "uri": "uri", "name": "name"},
            Playlist(uri="uri", name="name").serialize(),
        )

    def test_serialize_with_tracks(self):
        track = Track(name="foo")
        self.assertDictEqual(
            {
                "__model__": "Playlist",
                "uri": "uri",
                "name": "name",
                "tracks": [track.serialize()],
            },
            Playlist(uri="uri", name="name", tracks=[track]).serialize(),
        )

    def test_to_json_and_back(self):
        playlist1 = Playlist(uri="uri", name="name")
        serialized = json.dumps(playlist1, cls=ModelJSONEncoder)
        playlist2 = json.loads(serialized, object_hook=model_json_decoder)
        assert playlist1 == playlist2

    def test_eq_name(self):
        playlist1 = Playlist(name="name")
        playlist2 = Playlist(name="name")
        assert playlist1 == playlist2
        assert hash(playlist1) == hash(playlist2)

    def test_eq_uri(self):
        playlist1 = Playlist(uri="uri")
        playlist2 = Playlist(uri="uri")
        assert playlist1 == playlist2
        assert hash(playlist1) == hash(playlist2)

    def test_eq_tracks(self):
        tracks = [Track()]
        playlist1 = Playlist(tracks=tracks)
        playlist2 = Playlist(tracks=tracks)
        assert playlist1 == playlist2
        assert hash(playlist1) == hash(playlist2)

    def test_eq_last_modified(self):
        playlist1 = Playlist(last_modified=1)
        playlist2 = Playlist(last_modified=1)
        assert playlist1 == playlist2
        assert hash(playlist1) == hash(playlist2)

    def test_eq(self):
        tracks = [Track()]
        playlist1 = Playlist(
            uri="uri", name="name", tracks=tracks, last_modified=1
        )
        playlist2 = Playlist(
            uri="uri", name="name", tracks=tracks, last_modified=1
        )
        assert playlist1 == playlist2
        assert hash(playlist1) == hash(playlist2)

    def test_eq_none(self):
        assert Playlist() is not None

    def test_eq_other(self):
        assert Playlist() != "other"

    def test_ne_name(self):
        playlist1 = Playlist(name="name1")
        playlist2 = Playlist(name="name2")
        assert playlist1 != playlist2
        assert hash(playlist1) != hash(playlist2)

    def test_ne_uri(self):
        playlist1 = Playlist(uri="uri1")
        playlist2 = Playlist(uri="uri2")
        assert playlist1 != playlist2
        assert hash(playlist1) != hash(playlist2)

    def test_ne_tracks(self):
        playlist1 = Playlist(tracks=[Track(uri="uri1")])
        playlist2 = Playlist(tracks=[Track(uri="uri2")])
        assert playlist1 != playlist2
        assert hash(playlist1) != hash(playlist2)

    def test_ne_last_modified(self):
        playlist1 = Playlist(last_modified=1)
        playlist2 = Playlist(last_modified=2)
        assert playlist1 != playlist2
        assert hash(playlist1) != hash(playlist2)

    def test_ne(self):
        playlist1 = Playlist(
            uri="uri1",
            name="name1",
            tracks=[Track(uri="uri1")],
            last_modified=1,
        )
        playlist2 = Playlist(
            uri="uri2",
            name="name2",
            tracks=[Track(uri="uri2")],
            last_modified=2,
        )
        assert playlist1 != playlist2
        assert hash(playlist1) != hash(playlist2)


class SearchResultTest(unittest.TestCase):
    def test_uri(self):
        uri = "an_uri"
        result = SearchResult(uri=uri)
        assert result.uri == uri
        with self.assertRaises(AttributeError):
            result.uri = None

    def test_tracks(self):
        tracks = [Track(), Track(), Track()]
        result = SearchResult(tracks=tracks)
        assert list(result.tracks) == tracks
        with self.assertRaises(AttributeError):
            result.tracks = None

    def test_artists(self):
        artists = [Artist(), Artist(), Artist()]
        result = SearchResult(artists=artists)
        assert list(result.artists) == artists
        with self.assertRaises(AttributeError):
            result.artists = None

    def test_albums(self):
        albums = [Album(), Album(), Album()]
        result = SearchResult(albums=albums)
        assert list(result.albums) == albums
        with self.assertRaises(AttributeError):
            result.albums = None

    def test_invalid_kwarg(self):
        with self.assertRaises(TypeError):
            SearchResult(foo="baz")

    def test_repr_without_results(self):
        assert "SearchResult(uri='uri')" == repr(SearchResult(uri="uri"))

    def test_serialize_without_results(self):
        self.assertDictEqual(
            {"__model__": "SearchResult", "uri": "uri"},
            SearchResult(uri="uri").serialize(),
        )