-
-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathtest_clustering.py
165 lines (141 loc) · 5.91 KB
/
test_clustering.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
# -*- coding: utf-8 -*-
#
# Picard, the next-generation MusicBrainz tagger
#
# Copyright (C) 2021 Laurent Monin
# Copyright (C) 2021 Philipp Wolfer
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from test.picardtestcase import PicardTestCase
from picard.cluster import (
Cluster,
FileCluster,
tokenize,
)
from picard.file import File
class TokenizeTest(PicardTestCase):
def test_tokenize(self):
token = tokenize("")
self.assertEqual(token, "")
token = tokenize(" \t ")
self.assertEqual(token, "")
token = tokenize(" A\tWord-test ")
self.assertEqual(token, "awordtest")
class ClusterTest(PicardTestCase):
def setUp(self):
super().setUp()
self.set_config_values({
'windows_compatibility': False,
'va_name': 'Diverse Interpreten',
})
def _create_file(self, album, artist, filename="foo.mp3"):
file = File(filename)
file.metadata['album'] = album
file.metadata['artist'] = artist
return file
def assertClusterEqual(self, album, artist, files, cluster):
self.assertEqual(album, cluster.title)
self.assertEqual(artist, cluster.artist)
self.assertEqual(set(files), set(cluster.files))
def test_cluster_none(self):
clusters = list(Cluster.cluster([]))
# No cluster is being created
self.assertEqual(0, len(clusters))
def test_cluster_single_file(self):
files = [
self._create_file('album foo', 'artist foo'),
]
clusters = list(Cluster.cluster(files))
self.assertEqual(1, len(clusters))
def test_cluster_single_cluster(self):
files = [
self._create_file('album foo', 'artist bar'),
self._create_file('album foo', 'artist foo'),
self._create_file('album foo', 'artist foo'),
]
clusters = list(Cluster.cluster(files))
self.assertEqual(1, len(clusters))
self.assertClusterEqual('album foo', 'artist foo', files, clusters[0])
def test_cluster_multi(self):
files = [
self._create_file('album cluster1', 'artist bar'),
self._create_file('album cluster2', 'artist foo'),
self._create_file('album cluster1', 'artist foo'),
self._create_file('albumcluster2', 'artist bar'),
self._create_file('album single', 'artist bar'),
]
clusters = list(Cluster.cluster(files))
self.assertEqual(3, len(clusters))
self.assertClusterEqual('album cluster1', 'artist bar', {files[0], files[2]}, clusters[0])
self.assertClusterEqual('album cluster2', 'artist foo', {files[1], files[3]}, clusters[1])
self.assertClusterEqual('album single', 'artist bar', {files[4]}, clusters[2])
def test_cluster_by_path(self):
files = [
self._create_file(None, None, 'artist1/album1/foo1.ogg'),
self._create_file(None, None, 'album2/foo1.ogg'),
self._create_file(None, None, 'artist1/album1/foo2.ogg'),
self._create_file(None, None, 'album2/foo2.ogg'),
self._create_file(None, None, 'single/foo.ogg'),
self._create_file(None, None, 'album1/foo3.ogg'),
]
clusters = list(Cluster.cluster(files))
self.assertEqual(3, len(clusters))
self.assertClusterEqual('album1', 'artist1', {files[0], files[2], files[5]}, clusters[0])
self.assertClusterEqual('album2', 'Diverse Interpreten', {files[1], files[3]}, clusters[1])
self.assertClusterEqual('single', 'Diverse Interpreten', {files[4]}, clusters[2])
def test_cluster_no_metadata(self):
files = [
self._create_file(None, None, 'foo1.ogg'),
self._create_file(None, None, 'foo2.ogg'),
self._create_file(None, None, 'foo3.ogg'),
]
clusters = list(Cluster.cluster(files))
self.assertEqual(0, len(clusters))
def test_common_artist_name(self):
files = [
self._create_file('cluster 1', 'artist 1'),
self._create_file('cluster 1', 'artist 2'),
self._create_file('cluster 1', 'artist2'),
self._create_file('cluster 1', 'artist 1'),
self._create_file('cluster 1', 'artist 2'),
]
clusters = list(Cluster.cluster(files))
self.assertEqual(1, len(clusters))
self.assertClusterEqual('cluster 1', 'artist 2', files, clusters[0])
class FileClusterTest(PicardTestCase):
def test_single(self):
file = File('foo')
fc = FileCluster()
fc.add('album 1', 'artist 1', file)
self.assertEqual('album 1', fc.title)
self.assertEqual('artist 1', fc.artist)
self.assertEqual([file], list(fc.files))
def test_multi(self):
files = [
File('foo1'),
File('foo2'),
File('foo3'),
File('foo4'),
File('foo5'),
]
fc = FileCluster()
fc.add('album 1', 'artist1', files[0])
fc.add('Album 1', 'artist 2', files[1])
fc.add('album\t1', 'Artist 1', files[2])
fc.add('Album 1', 'Artist 2', files[3])
fc.add('album 2', 'Artist 1', files[4])
self.assertEqual('Album 1', fc.title)
self.assertEqual('Artist 1', fc.artist)
self.assertEqual(files, list(fc.files))