databaseinterfacetest.cpp 294 KB
Newer Older
1
/*
Matthieu Gallien's avatar
Matthieu Gallien committed
2
3
4
   SPDX-FileCopyrightText: 2015 (c) Matthieu Gallien <matthieu_gallien@yahoo.fr>

   SPDX-License-Identifier: LGPL-3.0-or-later
5
6
 */

7
8
#include "databasetestdata.h"

9
#include "databaseinterface.h"
10
#include "datatypes.h"
11

12
13
#include "config-upnp-qt.h"

14
#include <QObject>
15
16
17
18
19
#include <QUrl>
#include <QString>
#include <QHash>
#include <QVector>
#include <QThread>
20
21
22
#include <QStandardPaths>
#include <QDir>
#include <QFile>
23
#include <QTemporaryFile>
24
25
26

#include <QDebug>

27
#include <QtTest>
28

29
30
#include <algorithm>

31
class DatabaseInterfaceTests: public QObject, public DatabaseTestData
32
33
34
{
    Q_OBJECT

Matthieu Gallien's avatar
Matthieu Gallien committed
35
36
37
38
39
public:

    explicit DatabaseInterfaceTests(QObject *aParent = nullptr) : QObject(aParent)
    {
    }
40

41
private Q_SLOTS:
42

43
    void initTestCase()
44
    {
45
46
47
48
        qRegisterMetaType<QHash<qulonglong,int>>("QHash<qulonglong,int>");
        qRegisterMetaType<QHash<QString,QUrl>>("QHash<QString,QUrl>");
        qRegisterMetaType<QVector<qlonglong>>("QVector<qlonglong>");
        qRegisterMetaType<QHash<qlonglong,int>>("QHash<qlonglong,int>");
49
        qRegisterMetaType<QHash<QUrl,QDateTime>>("QHash<QUrl,QDateTime>");
50
51
52
53
54
55
56
57
        qRegisterMetaType<DataTypes::ListTrackDataType>("ListTrackDataType");
        qRegisterMetaType<DataTypes::ListAlbumDataType>("ListAlbumDataType");
        qRegisterMetaType<DataTypes::ListArtistDataType>("ListArtistDataType");
        qRegisterMetaType<DataTypes::ListGenreDataType>("ListGenreDataType");
        qRegisterMetaType<DataTypes::TrackDataType>("TrackDataType");
        qRegisterMetaType<DataTypes::AlbumDataType>("AlbumDataType");
        qRegisterMetaType<DataTypes::ArtistDataType>("ArtistDataType");
        qRegisterMetaType<DataTypes::GenreDataType>("GenreDataType");
Sven Marquardt's avatar
Sven Marquardt committed
58
	qRegisterMetaType<DataTypes::ListRadioDataType>("ListRadioDataType");
59
60
61
62
63
    }

    void avoidCrashInTrackIdFromTitleAlbumArtist()
    {
        DatabaseInterface musicDb;
64
        musicDb.trackIdFromTitleAlbumTrackDiscNumber(QStringLiteral("track1"), QStringLiteral("artist1"), QStringLiteral("album3"), 1, 1);
65
66
67
68
69
    }

    void avoidCrashInAllArtists()
    {
        DatabaseInterface musicDb;
70
        musicDb.allArtistsData();
71
72
    }

73
    void avoidCrashInallAlbumsData()
74
75
    {
        DatabaseInterface musicDb;
76
        musicDb.allAlbumsData();
Sven Marquardt's avatar
Sven Marquardt committed
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
    }

    void avoidCrashInAllRadios(){
        DatabaseInterface musicDb;
        musicDb.allRadiosData();
    }

      void addOneRadio()
    {
        QTemporaryFile databaseFile;
        databaseFile.open();
        qDebug() << "addOneRadio" << databaseFile.fileName();
        DatabaseInterface musicDb;
        musicDb.init(QStringLiteral("testDb"),databaseFile.fileName());
        QSignalSpy musicDbRadioAddedSpy(&musicDb,&DatabaseInterface::radioAdded);
	//9 because elisa inits the radio table with 9 radio streams
        QCOMPARE(musicDb.allRadiosData().count(),9);

        auto newRadio = DataTypes::TrackDataType();
        newRadio[DataTypes::CommentRole]=QStringLiteral("Test Comment");
        newRadio[DataTypes::ResourceRole]=QUrl::fromEncoded("http://ice2.somafm.com/defcon-128-aac");
        newRadio[DataTypes::TitleRole]=QStringLiteral("Test Radio");
        newRadio[DataTypes::RatingRole]=9;
        newRadio[DataTypes::ImageUrlRole]=QUrl::fromEncoded("https://somafm.com/img3/defcon400.png");
        newRadio[DataTypes::ElementTypeRole]=ElisaUtils::Radio;
        auto newRadios = DataTypes::ListRadioDataType();

        newRadios.push_back(newRadio);
        //Covers are ignored for radios.
        musicDb.insertTracksList(newRadios,mNewCovers);
        musicDbRadioAddedSpy.wait(300);

        QCOMPARE(musicDb.allRadiosData().count(),10);
        QCOMPARE(musicDbRadioAddedSpy.count(),1);
        databaseFile.close();
    }

    void modifyOneRadio(){
        QTemporaryFile databaseFile;
        databaseFile.open();
        qDebug() << "addOneRadio" << databaseFile.fileName();
        DatabaseInterface musicDb;
        musicDb.init(QStringLiteral("testDb"),databaseFile.fileName());
        QSignalSpy musicDbRadioAddedSpy(&musicDb,&DatabaseInterface::radioAdded);
        QSignalSpy musicDbRadioModifiedSpy(&musicDb,&DatabaseInterface::radioModified);

        QCOMPARE(musicDb.allRadiosData().count(),9);

        auto newRadio = DataTypes::TrackDataType();
        newRadio[DataTypes::CommentRole]=QStringLiteral("Test Comment");
        newRadio[DataTypes::ResourceRole]=QUrl::fromEncoded("http://ice2.somafm.com/defcon-128-aac");
        newRadio[DataTypes::TitleRole]=QStringLiteral("Test Radio");
        newRadio[DataTypes::RatingRole]=9;
        newRadio[DataTypes::ImageUrlRole]=QUrl::fromEncoded("https://somafm.com/img3/defcon400.png");
        newRadio[DataTypes::ElementTypeRole]=ElisaUtils::Radio;
        auto newRadios = DataTypes::ListRadioDataType();

        newRadios.push_back(newRadio);
        //Covers are ignored for radios.
        musicDb.insertTracksList(newRadios,mNewCovers);
        musicDbRadioAddedSpy.wait(300);

        QCOMPARE(musicDb.allRadiosData().count(),10);
        QCOMPARE(musicDbRadioAddedSpy.count(),1);


        newRadios.clear();


        auto radioId = musicDb.radioIdFromFileName(QUrl::fromEncoded("http://ice2.somafm.com/defcon-128-aac"));
        auto modifiedRadio = musicDb.radioDataFromDatabaseId(radioId);
        modifiedRadio[DataTypes::CommentRole]=QStringLiteral("News Test Comment");
        newRadios.push_back(modifiedRadio);
        musicDb.insertTracksList(newRadios,mNewCovers);

        QCOMPARE(musicDb.allRadiosData().count(),10);
        QCOMPARE(musicDbRadioAddedSpy.count(),1);
        QCOMPARE(musicDbRadioModifiedSpy.count(),1);
        databaseFile.close();





161
162
    }

163
164
    void addOneTrackWithoutAlbumArtist()
    {
165
166
167
168
169
        QTemporaryFile databaseFile;
        databaseFile.open();

        qDebug() << "addOneTrackWithoutAlbumArtist" << databaseFile.fileName();

170
171
        DatabaseInterface musicDb;

172
        musicDb.init(QStringLiteral("testDb"), databaseFile.fileName());
173

174
        QSignalSpy musicDbArtistAddedSpy(&musicDb, &DatabaseInterface::artistsAdded);
175
        QSignalSpy musicDbAlbumAddedSpy(&musicDb, &DatabaseInterface::albumsAdded);
176
        QSignalSpy musicDbTrackAddedSpy(&musicDb, &DatabaseInterface::tracksAdded);
177
178
179
180
181
        QSignalSpy musicDbArtistRemovedSpy(&musicDb, &DatabaseInterface::artistRemoved);
        QSignalSpy musicDbAlbumRemovedSpy(&musicDb, &DatabaseInterface::albumRemoved);
        QSignalSpy musicDbTrackRemovedSpy(&musicDb, &DatabaseInterface::trackRemoved);
        QSignalSpy musicDbAlbumModifiedSpy(&musicDb, &DatabaseInterface::albumModified);
        QSignalSpy musicDbTrackModifiedSpy(&musicDb, &DatabaseInterface::trackModified);
182
        QSignalSpy musicDbDatabaseErrorSpy(&musicDb, &DatabaseInterface::databaseError);
183

184
        QCOMPARE(musicDb.allAlbumsData().count(), 0);
185
        QCOMPARE(musicDb.allArtistsData().count(), 0);
186
        QCOMPARE(musicDb.allTracksData().count(), 0);
187
188
189
190
191
192
193
194
        QCOMPARE(musicDbArtistAddedSpy.count(), 0);
        QCOMPARE(musicDbAlbumAddedSpy.count(), 0);
        QCOMPARE(musicDbTrackAddedSpy.count(), 0);
        QCOMPARE(musicDbArtistRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 0);
        QCOMPARE(musicDbTrackRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
195
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);
196

197
        auto newTrack = DataTypes::TrackDataType{true, QStringLiteral("$23"), QStringLiteral("0"), QStringLiteral("track6"),
198
199
                QStringLiteral("artist2"), QStringLiteral("album3"), {},
                6, 1, QTime::fromMSecsSinceStartOfDay(23), {QUrl::fromLocalFile(QStringLiteral("/$23"))},
200
201
                QDateTime::fromMSecsSinceEpoch(23),
        {QUrl::fromLocalFile(QStringLiteral("album3"))}, 5, true,
202
                QStringLiteral("genre1"), QStringLiteral("composer1"), QStringLiteral("lyricist1"), false};
203
        auto newTracks = DataTypes::ListTrackDataType();
204
205
        newTracks.push_back(newTrack);

206
        auto newCovers = mNewCovers;
207
        newCovers[QStringLiteral("file:///$23")] = QUrl::fromLocalFile(QStringLiteral("album3"));
208

209
        musicDb.insertTracksList(newTracks, newCovers);
210
211
212

        musicDbTrackAddedSpy.wait(300);

213
        QCOMPARE(musicDb.allAlbumsData().count(), 1);
214
        QCOMPARE(musicDb.allArtistsData().count(), 1);
215
        QCOMPARE(musicDb.allTracksData().count(), 1);
216
217
218
219
220
221
        QCOMPARE(musicDbArtistAddedSpy.count(), 1);
        QCOMPARE(musicDbAlbumAddedSpy.count(), 1);
        QCOMPARE(musicDbTrackAddedSpy.count(), 1);
        QCOMPARE(musicDbArtistRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 0);
        QCOMPARE(musicDbTrackRemovedSpy.count(), 0);
222
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
223
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
224
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);
225

226
227
        auto track = musicDb.trackDataFromDatabaseId(musicDb.trackIdFromTitleAlbumTrackDiscNumber(QStringLiteral("track6"), QStringLiteral("artist2"),
                                                                                                  QStringLiteral("album3"), 6, 1));
228
229
230
231

        QCOMPARE(track.isValid(), true);
        QCOMPARE(track.title(), QStringLiteral("track6"));
        QCOMPARE(track.artist(), QStringLiteral("artist2"));
232
        QCOMPARE(track.album(), QStringLiteral("album3"));
233
        QVERIFY(!track.albumArtist().isEmpty());
234
235
236
        QCOMPARE(track.albumCover(), QUrl::fromLocalFile(QStringLiteral("album3")));
        QCOMPARE(track.trackNumber(), 6);
        QCOMPARE(track.discNumber(), 1);
237
238
        QCOMPARE(track.duration(), QTime::fromMSecsSinceStartOfDay(23));
        QCOMPARE(track.resourceURI(), QUrl::fromLocalFile(QStringLiteral("/$23")));
239
        QCOMPARE(track.rating(), 5);
240
        QCOMPARE(track.genre(), QStringLiteral("genre1"));
241
        QCOMPARE(track.composer(), QStringLiteral("composer1"));
242
        QCOMPARE(track.lyricist(), QStringLiteral("lyricist1"));
243

244
        auto albumId = musicDb.albumIdFromTitleAndArtist(QStringLiteral("album3"), QStringLiteral("artist2"), QStringLiteral("/"));
245
246
247
        QVERIFY(albumId != 0);
        auto album = musicDb.albumDataFromDatabaseId(albumId);
        auto albumData = musicDb.albumData(albumId);
248
249

        QCOMPARE(album.isValid(), true);
250
        QCOMPARE(albumData.count(), 1);
251
        QCOMPARE(album.title(), QStringLiteral("album3"));
252
        QCOMPARE(album.artist(), QStringLiteral("artist2"));
253
        QCOMPARE(album[DataTypes::ColumnsRoles::SecondaryTextRole], QStringLiteral("artist2"));
254
        QVERIFY(album.isValidArtist());
255
256
257
258
        QCOMPARE(album.albumArtURI(), QUrl::fromLocalFile(QStringLiteral("album3")));
        QCOMPARE(album.isSingleDiscAlbum(), true);
    }

259
260
    void addAndRemoveOneTrackWithoutAlbum()
    {
261
262
263
264
265
        QTemporaryFile databaseFile;
        databaseFile.open();

        qDebug() << "addAndRemoveOneTrackWithoutAlbum" << databaseFile.fileName();

266
267
        DatabaseInterface musicDb;

268
        musicDb.init(QStringLiteral("testDb"), databaseFile.fileName());
269
270
271
272
273
274
275
276
277
278
279

        QSignalSpy musicDbArtistAddedSpy(&musicDb, &DatabaseInterface::artistsAdded);
        QSignalSpy musicDbAlbumAddedSpy(&musicDb, &DatabaseInterface::albumsAdded);
        QSignalSpy musicDbTrackAddedSpy(&musicDb, &DatabaseInterface::tracksAdded);
        QSignalSpy musicDbArtistRemovedSpy(&musicDb, &DatabaseInterface::artistRemoved);
        QSignalSpy musicDbAlbumRemovedSpy(&musicDb, &DatabaseInterface::albumRemoved);
        QSignalSpy musicDbTrackRemovedSpy(&musicDb, &DatabaseInterface::trackRemoved);
        QSignalSpy musicDbAlbumModifiedSpy(&musicDb, &DatabaseInterface::albumModified);
        QSignalSpy musicDbTrackModifiedSpy(&musicDb, &DatabaseInterface::trackModified);
        QSignalSpy musicDbDatabaseErrorSpy(&musicDb, &DatabaseInterface::databaseError);

280
        QCOMPARE(musicDb.allAlbumsData().count(), 0);
281
        QCOMPARE(musicDb.allArtistsData().count(), 0);
282
        QCOMPARE(musicDb.allTracksData().count(), 0);
283
284
285
286
287
288
289
290
291
292
        QCOMPARE(musicDbArtistAddedSpy.count(), 0);
        QCOMPARE(musicDbAlbumAddedSpy.count(), 0);
        QCOMPARE(musicDbTrackAddedSpy.count(), 0);
        QCOMPARE(musicDbArtistRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 0);
        QCOMPARE(musicDbTrackRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);

293
        auto newTrack = DataTypes::TrackDataType {true, QStringLiteral("$24"), QStringLiteral("0"), QStringLiteral("track10"),
294
                QStringLiteral("artist8"), {}, QStringLiteral("artist8"),
295
                9, 1, QTime::fromMSecsSinceStartOfDay(24), {QUrl::fromLocalFile(QStringLiteral("/$24"))}, QDateTime::fromMSecsSinceEpoch(24),
296
        {}, 9, true, QStringLiteral("genre1"), QStringLiteral("composer1"), QStringLiteral("lyricist1"), false};
297

298
        auto newTracks = DataTypes::ListTrackDataType();
299
300
301
302
303
        newTracks.push_back(newTrack);

        auto newCovers = mNewCovers;
        newCovers[QStringLiteral("file:///$24")] = QUrl::fromLocalFile(QStringLiteral("album4"));

304
        musicDb.insertTracksList(newTracks, newCovers);
305
306
307

        musicDbTrackAddedSpy.wait(300);

308
        QCOMPARE(musicDb.allAlbumsData().count(), 0);
309
        QCOMPARE(musicDb.allArtistsData().count(), 1);
310
        QCOMPARE(musicDb.allTracksData().count(), 1);
311
312
313
314
315
316
317
318
319
320
        QCOMPARE(musicDbArtistAddedSpy.count(), 1);
        QCOMPARE(musicDbAlbumAddedSpy.count(), 0);
        QCOMPARE(musicDbTrackAddedSpy.count(), 1);
        QCOMPARE(musicDbArtistRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 0);
        QCOMPARE(musicDbTrackRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);

321
322
323
        auto trackId = musicDb.trackIdFromFileName(QUrl::fromLocalFile(QStringLiteral("/$24")));
        QVERIFY(trackId != 0);
        auto track = musicDb.trackDataFromDatabaseId(trackId);
324
325
326
327

        QCOMPARE(track.isValid(), true);
        QCOMPARE(track.title(), QStringLiteral("track10"));
        QCOMPARE(track.artist(), QStringLiteral("artist8"));
328
329
        QCOMPARE(track.album(), QString());
        QVERIFY(!track.albumArtist().isEmpty());
330
331
332
333
334
335
336
337
338
339
340
341
        QCOMPARE(track.albumCover(), QUrl());
        QCOMPARE(track.trackNumber(), 9);
        QCOMPARE(track.discNumber(), 1);
        QCOMPARE(track.duration(), QTime::fromMSecsSinceStartOfDay(24));
        QCOMPARE(track.resourceURI(), QUrl::fromLocalFile(QStringLiteral("/$24")));
        QCOMPARE(track.rating(), 9);
        QCOMPARE(track.genre(), QStringLiteral("genre1"));
        QCOMPARE(track.composer(), QStringLiteral("composer1"));
        QCOMPARE(track.lyricist(), QStringLiteral("lyricist1"));

        musicDb.removeTracksList({track.resourceURI()});

342
        QCOMPARE(musicDb.allAlbumsData().count(), 0);
343
        QCOMPARE(musicDb.allArtistsData().count(), 0);
344
        QCOMPARE(musicDb.allTracksData().count(), 0);
345
346
347
        QCOMPARE(musicDbArtistAddedSpy.count(), 1);
        QCOMPARE(musicDbAlbumAddedSpy.count(), 0);
        QCOMPARE(musicDbTrackAddedSpy.count(), 1);
Matthieu Gallien's avatar
Matthieu Gallien committed
348
        QCOMPARE(musicDbArtistRemovedSpy.count(), 1);
349
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 0);
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
        QCOMPARE(musicDbTrackRemovedSpy.count(), 1);
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);
    }

    void addAndRemoveOneTrackWithoutTrackNumber()
    {
        DatabaseInterface musicDb;

        musicDb.init(QStringLiteral("testDb"));

        QSignalSpy musicDbArtistAddedSpy(&musicDb, &DatabaseInterface::artistsAdded);
        QSignalSpy musicDbAlbumAddedSpy(&musicDb, &DatabaseInterface::albumsAdded);
        QSignalSpy musicDbTrackAddedSpy(&musicDb, &DatabaseInterface::tracksAdded);
        QSignalSpy musicDbArtistRemovedSpy(&musicDb, &DatabaseInterface::artistRemoved);
        QSignalSpy musicDbAlbumRemovedSpy(&musicDb, &DatabaseInterface::albumRemoved);
        QSignalSpy musicDbTrackRemovedSpy(&musicDb, &DatabaseInterface::trackRemoved);
        QSignalSpy musicDbAlbumModifiedSpy(&musicDb, &DatabaseInterface::albumModified);
        QSignalSpy musicDbTrackModifiedSpy(&musicDb, &DatabaseInterface::trackModified);
        QSignalSpy musicDbDatabaseErrorSpy(&musicDb, &DatabaseInterface::databaseError);

372
        QCOMPARE(musicDb.allAlbumsData().count(), 0);
373
        QCOMPARE(musicDb.allArtistsData().count(), 0);
374
        QCOMPARE(musicDb.allTracksData().count(), 0);
375
376
377
378
379
380
381
382
383
384
        QCOMPARE(musicDbArtistAddedSpy.count(), 0);
        QCOMPARE(musicDbAlbumAddedSpy.count(), 0);
        QCOMPARE(musicDbTrackAddedSpy.count(), 0);
        QCOMPARE(musicDbArtistRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 0);
        QCOMPARE(musicDbTrackRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);

385
        auto newTrack = DataTypes::TrackDataType {true, QStringLiteral("$26"), QStringLiteral("0"), QStringLiteral("track12"),
386
387
388
                QStringLiteral("artist8"), QStringLiteral("album4"), QStringLiteral("artist8"),
                -1, 1, QTime::fromMSecsSinceStartOfDay(26), {QUrl::fromLocalFile(QStringLiteral("/$26"))}, QDateTime::fromMSecsSinceEpoch(26),
                QUrl::fromLocalFile(QStringLiteral("file://image$26")), 9, true,
389
                QStringLiteral("genre1"), QStringLiteral("composer1"), QStringLiteral("lyricist1"), false};
390
391
        newTrack.remove(DataTypes::TrackNumberRole);
        auto newTracks = DataTypes::ListTrackDataType();
392
393
394
395
396
        newTracks.push_back(newTrack);

        auto newCovers = mNewCovers;
        newCovers[QStringLiteral("file:///$26")] = QUrl::fromLocalFile(QStringLiteral("album4"));

397
        musicDb.insertTracksList(newTracks, newCovers);
398
399
400

        musicDbTrackAddedSpy.wait(300);

401
        QCOMPARE(musicDb.allAlbumsData().count(), 1);
402
        QCOMPARE(musicDb.allArtistsData().count(), 1);
403
        QCOMPARE(musicDb.allTracksData().count(), 1);
404
405
406
407
408
409
410
411
412
413
        QCOMPARE(musicDbArtistAddedSpy.count(), 1);
        QCOMPARE(musicDbAlbumAddedSpy.count(), 1);
        QCOMPARE(musicDbTrackAddedSpy.count(), 1);
        QCOMPARE(musicDbArtistRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 0);
        QCOMPARE(musicDbTrackRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);

414
        auto track = musicDb.trackDataFromDatabaseId(musicDb.trackIdFromFileName(QUrl::fromLocalFile(QStringLiteral("/$26"))));
415
416
417
418

        QCOMPARE(track.isValid(), true);
        QCOMPARE(track.title(), QStringLiteral("track12"));
        QCOMPARE(track.artist(), QStringLiteral("artist8"));
419
        QCOMPARE(track.album(), QStringLiteral("album4"));
420
421
        QCOMPARE(track.albumArtist(), QStringLiteral("artist8"));
        QCOMPARE(track.albumCover(), QUrl::fromLocalFile(QStringLiteral("album4")));
422
        QCOMPARE(track.hasTrackNumber(), false);
423
424
425
426
427
428
429
430
431
432
        QCOMPARE(track.discNumber(), 1);
        QCOMPARE(track.duration(), QTime::fromMSecsSinceStartOfDay(26));
        QCOMPARE(track.resourceURI(), QUrl::fromLocalFile(QStringLiteral("/$26")));
        QCOMPARE(track.rating(), 9);
        QCOMPARE(track.genre(), QStringLiteral("genre1"));
        QCOMPARE(track.composer(), QStringLiteral("composer1"));
        QCOMPARE(track.lyricist(), QStringLiteral("lyricist1"));

        musicDb.removeTracksList({track.resourceURI()});

433
        QCOMPARE(musicDb.allAlbumsData().count(), 0);
434
        QCOMPARE(musicDb.allArtistsData().count(), 0);
435
        QCOMPARE(musicDb.allTracksData().count(), 0);
436
437
438
439
440
441
442
443
444
445
446
        QCOMPARE(musicDbArtistAddedSpy.count(), 1);
        QCOMPARE(musicDbAlbumAddedSpy.count(), 1);
        QCOMPARE(musicDbTrackAddedSpy.count(), 1);
        QCOMPARE(musicDbArtistRemovedSpy.count(), 1);
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 1);
        QCOMPARE(musicDbTrackRemovedSpy.count(), 1);
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);
    }

447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
    void addAndRemoveOneTrackWithoutArtist()
    {
        DatabaseInterface musicDb;

        musicDb.init(QStringLiteral("testDb"));

        QSignalSpy musicDbArtistAddedSpy(&musicDb, &DatabaseInterface::artistsAdded);
        QSignalSpy musicDbAlbumAddedSpy(&musicDb, &DatabaseInterface::albumsAdded);
        QSignalSpy musicDbTrackAddedSpy(&musicDb, &DatabaseInterface::tracksAdded);
        QSignalSpy musicDbArtistRemovedSpy(&musicDb, &DatabaseInterface::artistRemoved);
        QSignalSpy musicDbAlbumRemovedSpy(&musicDb, &DatabaseInterface::albumRemoved);
        QSignalSpy musicDbTrackRemovedSpy(&musicDb, &DatabaseInterface::trackRemoved);
        QSignalSpy musicDbAlbumModifiedSpy(&musicDb, &DatabaseInterface::albumModified);
        QSignalSpy musicDbTrackModifiedSpy(&musicDb, &DatabaseInterface::trackModified);
        QSignalSpy musicDbDatabaseErrorSpy(&musicDb, &DatabaseInterface::databaseError);

463
        QCOMPARE(musicDb.allAlbumsData().count(), 0);
464
        QCOMPARE(musicDb.allArtistsData().count(), 0);
465
        QCOMPARE(musicDb.allTracksData().count(), 0);
466
467
468
469
470
471
472
473
474
475
        QCOMPARE(musicDbArtistAddedSpy.count(), 0);
        QCOMPARE(musicDbAlbumAddedSpy.count(), 0);
        QCOMPARE(musicDbTrackAddedSpy.count(), 0);
        QCOMPARE(musicDbArtistRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 0);
        QCOMPARE(musicDbTrackRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);

476
        auto newTrack = DataTypes::TrackDataType {true, QStringLiteral("$26"), QStringLiteral("0"), QStringLiteral("track11"),
477
        {}, QStringLiteral("album4"), {},
478
479
                9, 1, QTime::fromMSecsSinceStartOfDay(26), {QUrl::fromLocalFile(QStringLiteral("/$26"))}, QDateTime::fromMSecsSinceEpoch(26),
                QUrl::fromLocalFile(QStringLiteral("file://image$26")), 9, true,
480
                QStringLiteral("genre1"), QStringLiteral("composer1"), QStringLiteral("lyricist1"), false};
481
        auto newTracks = DataTypes::ListTrackDataType();
482
483
484
485
486
        newTracks.push_back(newTrack);

        auto newCovers = mNewCovers;
        newCovers[QStringLiteral("file:///$26")] = QUrl::fromLocalFile(QStringLiteral("album4"));

487
        musicDb.insertTracksList(newTracks, newCovers);
488
489
490

        musicDbTrackAddedSpy.wait(300);

491
        QCOMPARE(musicDb.allAlbumsData().count(), 1);
492
        QCOMPARE(musicDb.allArtistsData().count(), 0);
493
        QCOMPARE(musicDb.allTracksData().count(), 1);
494
        QCOMPARE(musicDbArtistAddedSpy.count(), 0);
Matthieu Gallien's avatar
Matthieu Gallien committed
495
        QCOMPARE(musicDbAlbumAddedSpy.count(), 1);
496
497
498
499
500
501
502
503
        QCOMPARE(musicDbTrackAddedSpy.count(), 1);
        QCOMPARE(musicDbArtistRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 0);
        QCOMPARE(musicDbTrackRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);

504
        auto track = musicDb.trackDataFromDatabaseId(musicDb.trackIdFromFileName(QUrl::fromLocalFile(QStringLiteral("/$26"))));
505
506
507
508

        QCOMPARE(track.isValid(), true);
        QCOMPARE(track.title(), QStringLiteral("track11"));
        QCOMPARE(track.artist(), QString());
509
510
        QCOMPARE(track.album(), QStringLiteral("album4"));
        QVERIFY(track.albumArtist().isEmpty());
Matthieu Gallien's avatar
Matthieu Gallien committed
511
        QCOMPARE(track.albumCover(), QUrl::fromLocalFile(QStringLiteral("album4")));
512
513
514
515
516
517
518
519
520
521
522
        QCOMPARE(track.trackNumber(), 9);
        QCOMPARE(track.discNumber(), 1);
        QCOMPARE(track.duration(), QTime::fromMSecsSinceStartOfDay(26));
        QCOMPARE(track.resourceURI(), QUrl::fromLocalFile(QStringLiteral("/$26")));
        QCOMPARE(track.rating(), 9);
        QCOMPARE(track.genre(), QStringLiteral("genre1"));
        QCOMPARE(track.composer(), QStringLiteral("composer1"));
        QCOMPARE(track.lyricist(), QStringLiteral("lyricist1"));

        musicDb.removeTracksList({track.resourceURI()});

523
        QCOMPARE(musicDb.allAlbumsData().count(), 0);
524
        QCOMPARE(musicDb.allArtistsData().count(), 0);
525
        QCOMPARE(musicDb.allTracksData().count(), 0);
526
        QCOMPARE(musicDbArtistAddedSpy.count(), 0);
Matthieu Gallien's avatar
Matthieu Gallien committed
527
        QCOMPARE(musicDbAlbumAddedSpy.count(), 1);
528
529
        QCOMPARE(musicDbTrackAddedSpy.count(), 1);
        QCOMPARE(musicDbArtistRemovedSpy.count(), 0);
Matthieu Gallien's avatar
Matthieu Gallien committed
530
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 1);
531
532
533
534
535
536
        QCOMPARE(musicDbTrackRemovedSpy.count(), 1);
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);
    }

537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
    void addOneTrackWithoutAlbumArtistAndAnotherTrackWith()
    {
        DatabaseInterface musicDb;

        musicDb.init(QStringLiteral("testDb"));

        QSignalSpy musicDbArtistAddedSpy(&musicDb, &DatabaseInterface::artistsAdded);
        QSignalSpy musicDbAlbumAddedSpy(&musicDb, &DatabaseInterface::albumsAdded);
        QSignalSpy musicDbTrackAddedSpy(&musicDb, &DatabaseInterface::tracksAdded);
        QSignalSpy musicDbArtistRemovedSpy(&musicDb, &DatabaseInterface::artistRemoved);
        QSignalSpy musicDbAlbumRemovedSpy(&musicDb, &DatabaseInterface::albumRemoved);
        QSignalSpy musicDbTrackRemovedSpy(&musicDb, &DatabaseInterface::trackRemoved);
        QSignalSpy musicDbAlbumModifiedSpy(&musicDb, &DatabaseInterface::albumModified);
        QSignalSpy musicDbTrackModifiedSpy(&musicDb, &DatabaseInterface::trackModified);
        QSignalSpy musicDbDatabaseErrorSpy(&musicDb, &DatabaseInterface::databaseError);

553
        QCOMPARE(musicDb.allAlbumsData().count(), 0);
554
        QCOMPARE(musicDb.allArtistsData().count(), 0);
555
        QCOMPARE(musicDb.allTracksData().count(), 0);
556
557
558
559
560
561
562
563
564
565
        QCOMPARE(musicDbArtistAddedSpy.count(), 0);
        QCOMPARE(musicDbAlbumAddedSpy.count(), 0);
        QCOMPARE(musicDbTrackAddedSpy.count(), 0);
        QCOMPARE(musicDbArtistRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 0);
        QCOMPARE(musicDbTrackRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);

566
        auto newTrack = DataTypes::TrackDataType {true, QStringLiteral("$26"), QStringLiteral("0"), QStringLiteral("track11"),
567
        {}, QStringLiteral("album4"), {},
568
569
                9, 1, QTime::fromMSecsSinceStartOfDay(26), {QUrl::fromLocalFile(QStringLiteral("/$26"))}, QDateTime::fromMSecsSinceEpoch(26),
                QUrl::fromLocalFile(QStringLiteral("file://image$26")), 9, true,
570
                QStringLiteral("genre1"), QStringLiteral("composer1"), QStringLiteral("lyricist1"), false};
571
        auto newTracks = DataTypes::ListTrackDataType();
572
573
574
575
576
        newTracks.push_back(newTrack);

        auto newCovers = mNewCovers;
        newCovers[QStringLiteral("file:///$26")] = QUrl::fromLocalFile(QStringLiteral("album4"));

577
        musicDb.insertTracksList(newTracks, newCovers);
578
579
580

        musicDbTrackAddedSpy.wait(300);

581
        QCOMPARE(musicDb.allAlbumsData().count(), 1);
582
        QCOMPARE(musicDb.allArtistsData().count(), 0);
583
        QCOMPARE(musicDb.allTracksData().count(), 1);
584
585
586
587
588
589
590
591
592
593
        QCOMPARE(musicDbArtistAddedSpy.count(), 0);
        QCOMPARE(musicDbAlbumAddedSpy.count(), 1);
        QCOMPARE(musicDbTrackAddedSpy.count(), 1);
        QCOMPARE(musicDbArtistRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 0);
        QCOMPARE(musicDbTrackRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);

594
        auto track = musicDb.trackDataFromDatabaseId(musicDb.trackIdFromFileName(QUrl::fromLocalFile(QStringLiteral("/$26"))));
595
596
597
598

        QCOMPARE(track.isValid(), true);
        QCOMPARE(track.title(), QStringLiteral("track11"));
        QCOMPARE(track.artist(), QString());
599
        QCOMPARE(track.album(), QStringLiteral("album4"));
600
        QCOMPARE(track.albumArtist(), QString());
601
        QVERIFY(track.albumArtist().isEmpty());
602
603
604
605
606
607
608
609
610
611
        QCOMPARE(track.albumCover(), QUrl::fromLocalFile(QStringLiteral("album4")));
        QCOMPARE(track.trackNumber(), 9);
        QCOMPARE(track.discNumber(), 1);
        QCOMPARE(track.duration(), QTime::fromMSecsSinceStartOfDay(26));
        QCOMPARE(track.resourceURI(), QUrl::fromLocalFile(QStringLiteral("/$26")));
        QCOMPARE(track.rating(), 9);
        QCOMPARE(track.genre(), QStringLiteral("genre1"));
        QCOMPARE(track.composer(), QStringLiteral("composer1"));
        QCOMPARE(track.lyricist(), QStringLiteral("lyricist1"));

612
        newTrack = DataTypes::TrackDataType {true, QStringLiteral("$27"), QStringLiteral("0"), QStringLiteral("track12"),
613
614
615
                QStringLiteral("artist1"), QStringLiteral("album4"), QStringLiteral("artist2"),
                10, 1, QTime::fromMSecsSinceStartOfDay(27), {QUrl::fromLocalFile(QStringLiteral("/autre/$27"))}, QDateTime::fromMSecsSinceEpoch(27),
                QUrl::fromLocalFile(QStringLiteral("file://image$27")), 10, true,
616
                QStringLiteral("genre1"), QStringLiteral("composer1"), QStringLiteral("lyricist1"), false};
617
        newTracks = DataTypes::ListTrackDataType();
618
619
620
621
622
        newTracks.push_back(newTrack);

        newCovers = mNewCovers;
        newCovers[QStringLiteral("file:///autre/$27")] = QUrl::fromLocalFile(QStringLiteral("album4"));

623
        musicDb.insertTracksList(newTracks, newCovers);
624
625
626

        musicDbTrackAddedSpy.wait(300);

627
        QCOMPARE(musicDb.allAlbumsData().count(), 2);
628
        QCOMPARE(musicDb.allArtistsData().count(), 2);
629
        QCOMPARE(musicDb.allTracksData().count(), 2);
630
631
632
633
634
635
636
637
638
639
640
        QCOMPARE(musicDbArtistAddedSpy.count(), 1);
        QCOMPARE(musicDbAlbumAddedSpy.count(), 2);
        QCOMPARE(musicDbTrackAddedSpy.count(), 2);
        QCOMPARE(musicDbArtistRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 0);
        QCOMPARE(musicDbTrackRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);
    }

641
642
    void addTwoTracksWithoutAlbumArtist()
    {
643
644
645
646
647
        QTemporaryFile databaseFile;
        databaseFile.open();

        qDebug() << "addTwoTracksWithoutAlbumArtist" << databaseFile.fileName();

648
649
        DatabaseInterface musicDb;

650
        musicDb.init(QStringLiteral("testDb"), databaseFile.fileName());
651

652
        QSignalSpy musicDbArtistAddedSpy(&musicDb, &DatabaseInterface::artistsAdded);
653
        QSignalSpy musicDbAlbumAddedSpy(&musicDb, &DatabaseInterface::albumsAdded);
654
        QSignalSpy musicDbTrackAddedSpy(&musicDb, &DatabaseInterface::tracksAdded);
655
656
657
658
659
        QSignalSpy musicDbArtistRemovedSpy(&musicDb, &DatabaseInterface::artistRemoved);
        QSignalSpy musicDbAlbumRemovedSpy(&musicDb, &DatabaseInterface::albumRemoved);
        QSignalSpy musicDbTrackRemovedSpy(&musicDb, &DatabaseInterface::trackRemoved);
        QSignalSpy musicDbAlbumModifiedSpy(&musicDb, &DatabaseInterface::albumModified);
        QSignalSpy musicDbTrackModifiedSpy(&musicDb, &DatabaseInterface::trackModified);
660
        QSignalSpy musicDbDatabaseErrorSpy(&musicDb, &DatabaseInterface::databaseError);
661

662
        QCOMPARE(musicDb.allAlbumsData().count(), 0);
663
        QCOMPARE(musicDb.allArtistsData().count(), 0);
664
        QCOMPARE(musicDb.allTracksData().count(), 0);
665
666
667
668
669
670
671
672
        QCOMPARE(musicDbArtistAddedSpy.count(), 0);
        QCOMPARE(musicDbAlbumAddedSpy.count(), 0);
        QCOMPARE(musicDbTrackAddedSpy.count(), 0);
        QCOMPARE(musicDbArtistRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 0);
        QCOMPARE(musicDbTrackRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
673
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);
674

675
        auto newTracks = DataTypes::ListTrackDataType();
676
677

        newTracks = {{true, QStringLiteral("$19"), QStringLiteral("0"), QStringLiteral("track6"),
678
679
                      QStringLiteral("artist2"), QStringLiteral("album3"), {}, 6, 1, QTime::fromMSecsSinceStartOfDay(19),
                      {QUrl::fromLocalFile(QStringLiteral("/$19"))},
680
                      QDateTime::fromMSecsSinceEpoch(23),
681
                      {QUrl::fromLocalFile(QStringLiteral("album3"))}, 5, true,
682
                      QStringLiteral("genre1"), QStringLiteral("composer1"), QStringLiteral("lyricist1"), true},
683
                     {true, QStringLiteral("$20"), QStringLiteral("0"), QStringLiteral("track7"),
684
685
                      QStringLiteral("artist3"), QStringLiteral("album3"), {}, 7, 1, QTime::fromMSecsSinceStartOfDay(20),
                      {QUrl::fromLocalFile(QStringLiteral("/$20"))},
686
                      QDateTime::fromMSecsSinceEpoch(23),
687
                      {QUrl::fromLocalFile(QStringLiteral("album3"))}, 5, true,
688
                      QStringLiteral("genre1"), QStringLiteral("composer1"), QStringLiteral("lyricist1"), false}};
689

690
        auto newCovers = mNewCovers;
691
        newCovers[QStringLiteral("file:///$23")] = QUrl::fromLocalFile(QStringLiteral("album3"));
692
693
        newCovers[QStringLiteral("file:///$20")] = QUrl::fromLocalFile(QStringLiteral("album3"));

694
        musicDb.insertTracksList(newTracks, newCovers);
695
696
697

        musicDbTrackAddedSpy.wait(300);

698
        QCOMPARE(musicDb.allAlbumsData().count(), 1);
699
        QCOMPARE(musicDb.allArtistsData().count(), 2);
700
        QCOMPARE(musicDb.allTracksData().count(), 2);
701
        QCOMPARE(musicDbArtistAddedSpy.count(), 1);
702
        QCOMPARE(musicDbAlbumAddedSpy.count(), 1);
703
        QCOMPARE(musicDbTrackAddedSpy.count(), 1);
704
705
706
        QCOMPARE(musicDbArtistRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 0);
        QCOMPARE(musicDbTrackRemovedSpy.count(), 0);
707
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
708
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
709
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);
710

711
712
        auto firstTrack = musicDb.trackDataFromDatabaseId(musicDb.trackIdFromTitleAlbumTrackDiscNumber(QStringLiteral("track6"), QStringLiteral("artist2"),
                                                                                                       QStringLiteral("album3"), 6, 1));
713
714
715
716

        QCOMPARE(firstTrack.isValid(), true);
        QCOMPARE(firstTrack.title(), QStringLiteral("track6"));
        QCOMPARE(firstTrack.artist(), QStringLiteral("artist2"));
717
        QCOMPARE(firstTrack.album(), QStringLiteral("album3"));
718
        QVERIFY(!firstTrack.albumArtist().isEmpty());
719
720
721
722
723
724
        QCOMPARE(firstTrack.albumCover(), QUrl::fromLocalFile(QStringLiteral("album3")));
        QCOMPARE(firstTrack.trackNumber(), 6);
        QCOMPARE(firstTrack.discNumber(), 1);
        QCOMPARE(firstTrack.duration(), QTime::fromMSecsSinceStartOfDay(19));
        QCOMPARE(firstTrack.resourceURI(), QUrl::fromLocalFile(QStringLiteral("/$19")));
        QCOMPARE(firstTrack.rating(), 5);
725
        QCOMPARE(firstTrack.genre(), QStringLiteral("genre1"));
726
        QCOMPARE(firstTrack.composer(), QStringLiteral("composer1"));
727
        QCOMPARE(firstTrack.lyricist(), QStringLiteral("lyricist1"));
728
        QCOMPARE(firstTrack.hasEmbeddedCover(), true);
729

730
731
        auto secondTrack = musicDb.trackDataFromDatabaseId(musicDb.trackIdFromTitleAlbumTrackDiscNumber(QStringLiteral("track7"), QStringLiteral("artist3"),
                                                                                                        QStringLiteral("album3"), 7, 1));
732
733
734
735

        QCOMPARE(secondTrack.isValid(), true);
        QCOMPARE(secondTrack.title(), QStringLiteral("track7"));
        QCOMPARE(secondTrack.artist(), QStringLiteral("artist3"));
736
        QCOMPARE(secondTrack.album(), QStringLiteral("album3"));
737
        QVERIFY(!secondTrack.albumArtist().isEmpty());
738
739
740
741
742
743
        QCOMPARE(secondTrack.albumCover(), QUrl::fromLocalFile(QStringLiteral("album3")));
        QCOMPARE(secondTrack.trackNumber(), 7);
        QCOMPARE(secondTrack.discNumber(), 1);
        QCOMPARE(secondTrack.duration(), QTime::fromMSecsSinceStartOfDay(20));
        QCOMPARE(secondTrack.resourceURI(), QUrl::fromLocalFile(QStringLiteral("/$20")));
        QCOMPARE(secondTrack.rating(), 5);
744
        QCOMPARE(secondTrack.genre(), QStringLiteral("genre1"));
745
        QCOMPARE(secondTrack.composer(), QStringLiteral("composer1"));
746
        QCOMPARE(secondTrack.lyricist(), QStringLiteral("lyricist1"));
747
        QCOMPARE(secondTrack.hasEmbeddedCover(), false);
748

749
        auto albumId = musicDb.albumIdFromTitleAndArtist(QStringLiteral("album3"), {}, QStringLiteral("/"));
750
751
        auto album = musicDb.albumDataFromDatabaseId(albumId);
        auto albumData = musicDb.albumData(albumId);
752
753

        QCOMPARE(album.isValid(), true);
754
        QCOMPARE(albumData.count(), 2);
755
756
        QCOMPARE(album.title(), QStringLiteral("album3"));
        QVERIFY(album.isValidArtist());
757
758
759
760
        QCOMPARE(album.albumArtURI(), QUrl::fromLocalFile(QStringLiteral("album3")));
        QCOMPARE(album.isSingleDiscAlbum(), true);
    }

761
762
763
764
765
766
    void addThreeTracksWithoutAlbumArtistButSameArtist()
    {
        DatabaseInterface musicDb;

        musicDb.init(QStringLiteral("testDb"));

767
        QSignalSpy musicDbArtistAddedSpy(&musicDb, &DatabaseInterface::artistsAdded);
768
        QSignalSpy musicDbAlbumAddedSpy(&musicDb, &DatabaseInterface::albumsAdded);
769
        QSignalSpy musicDbTrackAddedSpy(&musicDb, &DatabaseInterface::tracksAdded);
770
771
772
773
774
        QSignalSpy musicDbArtistRemovedSpy(&musicDb, &DatabaseInterface::artistRemoved);
        QSignalSpy musicDbAlbumRemovedSpy(&musicDb, &DatabaseInterface::albumRemoved);
        QSignalSpy musicDbTrackRemovedSpy(&musicDb, &DatabaseInterface::trackRemoved);
        QSignalSpy musicDbAlbumModifiedSpy(&musicDb, &DatabaseInterface::albumModified);
        QSignalSpy musicDbTrackModifiedSpy(&musicDb, &DatabaseInterface::trackModified);
775
        QSignalSpy musicDbDatabaseErrorSpy(&musicDb, &DatabaseInterface::databaseError);
776

777
        QCOMPARE(musicDb.allAlbumsData().count(), 0);
778
        QCOMPARE(musicDb.allArtistsData().count(), 0);
779
        QCOMPARE(musicDb.allTracksData().count(), 0);
780
781
782
783
784
785
786
787
        QCOMPARE(musicDbArtistAddedSpy.count(), 0);
        QCOMPARE(musicDbAlbumAddedSpy.count(), 0);
        QCOMPARE(musicDbTrackAddedSpy.count(), 0);
        QCOMPARE(musicDbArtistRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 0);
        QCOMPARE(musicDbTrackRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
788
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);
789

790
        auto newTracks = DataTypes::ListTrackDataType();
791
792

        newTracks = {{true, QStringLiteral("$19"), QStringLiteral("0"), QStringLiteral("track6"),
793
794
                      QStringLiteral("artist2"), QStringLiteral("album3"), {}, 6, 1,
                      QTime::fromMSecsSinceStartOfDay(19), {QUrl::fromLocalFile(QStringLiteral("/$19"))},
795
                      QDateTime::fromMSecsSinceEpoch(19),
796
                      {QUrl::fromLocalFile(QStringLiteral("album3"))}, 5, true,
797
                      QStringLiteral("genre1"), QStringLiteral("composer1"), QStringLiteral("lyricist1"), false},
798
                     {true, QStringLiteral("$20"), QStringLiteral("0"), QStringLiteral("track7"),
799
800
                      QStringLiteral("artist2"), QStringLiteral("album3"), {}, 7, 1,
                      QTime::fromMSecsSinceStartOfDay(20), {QUrl::fromLocalFile(QStringLiteral("/$20"))},
801
                      QDateTime::fromMSecsSinceEpoch(20),
802
                      {QUrl::fromLocalFile(QStringLiteral("album3"))}, 4, true,
803
                      QStringLiteral("genre1"), QStringLiteral("composer1"), QStringLiteral("lyricist1"), false},
804
                     {true, QStringLiteral("$21"), QStringLiteral("0"), QStringLiteral("track8"),
805
806
                      QStringLiteral("artist2"), QStringLiteral("album3"), {}, 8, 1,
                      QTime::fromMSecsSinceStartOfDay(21), {QUrl::fromLocalFile(QStringLiteral("/$21"))},
807
                      QDateTime::fromMSecsSinceEpoch(21),
808
                      {QUrl::fromLocalFile(QStringLiteral("album3"))}, 3, true,
809
                      QStringLiteral("genre1"), QStringLiteral("composer1"), QStringLiteral("lyricist1"), false}};
810

811
        auto newCovers = mNewCovers;
812
        newCovers[QStringLiteral("file:///$19")] = QUrl::fromLocalFile(QStringLiteral("album3"));
813
814
815
        newCovers[QStringLiteral("file:///$20")] = QUrl::fromLocalFile(QStringLiteral("album3"));
        newCovers[QStringLiteral("file:///$21")] = QUrl::fromLocalFile(QStringLiteral("album3"));

816
        musicDb.insertTracksList(newTracks, newCovers);
817
818
819

        musicDbTrackAddedSpy.wait(300);

820
        QCOMPARE(musicDb.allAlbumsData().count(), 1);
821
        QCOMPARE(musicDb.allArtistsData().count(), 1);
822
        QCOMPARE(musicDb.allTracksData().count(), 3);
823
824
        QCOMPARE(musicDbArtistAddedSpy.count(), 1);
        QCOMPARE(musicDbAlbumAddedSpy.count(), 1);
825
        QCOMPARE(musicDbTrackAddedSpy.count(), 1);
826
827
828
        QCOMPARE(musicDbArtistRemovedSpy.count(), 0);
        QCOMPARE(musicDbAlbumRemovedSpy.count(), 0);
        QCOMPARE(musicDbTrackRemovedSpy.count(), 0);
829
        QCOMPARE(musicDbAlbumModifiedSpy.count(), 0);
830
        QCOMPARE(musicDbTrackModifiedSpy.count(), 0);
831
        QCOMPARE(musicDbDatabaseErrorSpy.count(), 0);
832

833
834
        auto firstTrack = musicDb.trackDataFromDatabaseId(musicDb.trackIdFromTitleAlbumTrackDiscNumber(QStringLiteral("track6"), QStringLiteral("artist2"),
                                                                                                       QStringLiteral("album3"), 6, 1));
835
836
837
838

        QCOMPARE(firstTrack.isValid(), true);
        QCOMPARE(firstTrack.title(), QStringLiteral("track6"));
        QCOMPARE(firstTrack.artist(), QStringLiteral("artist2"));
839
        QCOMPARE(firstTrack.album(), QStringLiteral("album3"));
840
        QVERIFY(!firstTrack.albumArtist().isEmpty());
841
842
843
844
845
846
        QCOMPARE(firstTrack.albumCover(), QUrl::fromLocalFile(QStringLiteral("album3")));
        QCOMPARE(firstTrack.trackNumber(), 6);
        QCOMPARE(firstTrack.discNumber(), 1);
        QCOMPARE(firstTrack.duration(), QTime::fromMSecsSinceStartOfDay(19));
        QCOMPARE(firstTrack.resourceURI(), QUrl::fromLocalFile(QStringLiteral("/$19")));
        QCOMPARE(firstTrack.rating(), 5);
847
        QCOMPARE(firstTrack.genre(), QStringLiteral("genre1"));
848
        QCOMPARE(firstTrack.composer(), QStringLiteral("composer1"));
849
        QCOMPARE(firstTrack.lyricist(), QStringLiteral("lyricist1"));
850
        QCOMPARE(firstTrack.isSingleDiscAlbum(), true);
851

852
853
        auto secondTrack = musicDb.trackDataFromDatabaseId(musicDb.trackIdFromTitleAlbumTrackDiscNumber(QStringLiteral("track7"), QStringLiteral("artist2"),
                                                                                                        QStringLiteral("album3"), 7, 1));
854
855
856
857

        QCOMPARE(secondTrack.isValid(), true);
        QCOMPARE(secondTrack.title(), QStringLiteral("track7"));
        QCOMPARE(secondTrack.artist(), QStringLiteral("artist2"));
858
        QCOMPARE(secondTrack.album(), QStringLiteral("album3"));
859
        QVERIFY(!secondTrack.albumArtist().isEmpty());
860
861
862
863
864
865
        QCOMPARE(secondTrack.albumCover(), QUrl::fromLocalFile(QStringLiteral("album3")));
        QCOMPARE(secondTrack.trackNumber(), 7);
        QCOMPARE(secondTrack.discNumber(), 1);
        QCOMPARE(secondTrack.duration(), QTime::fromMSecsSinceStartOfDay(20));
        QCOMPARE(secondTrack.resourceURI(), QUrl::fromLocalFile(QStringLiteral("/$20")));
        QCOMPARE(secondTrack.rating(), 4);
866
        QCOMPARE(secondTrack.genre(), QStringLiteral("genre1"));
867
        QCOMPARE(secondTrack.composer(), QStringLiteral("composer1"));
868
        QCOMPARE(secondTrack.lyricist(), QStringLiteral("lyricist1"));
869
        QCOMPARE(secondTrack.isSingleDiscAlbum(), true);
870
        QCOMPARE(secondTrack.hasEmbeddedCover(), false);
871

872
873
        auto thirdTrack = musicDb.trackDataFromDatabaseId(musicDb.trackIdFromTitleAlbumTrackDiscNumber(QStringLiteral("track8"), QStringLiteral("artist2"),
                                                                                                       QStringLiteral("album3"), 8, 1));
874
875
876
877

        QCOMPARE(thirdTrack.isValid(), true);
        QCOMPARE(thirdTrack.title(), QStringLiteral("track8"));
        QCOMPARE(thirdTrack.artist(), QStringLiteral("artist2"));
878
        QCOMPARE(thirdTrack.album(), QStringLiteral("album3"));
879
        QVERIFY(!thirdTrack.albumArtist().isEmpty());
880
881
882
883
884
885
        QCOMPARE(thirdTrack.albumCover(), QUrl::fromLocalFile(QStringLiteral("album3")));
        QCOMPARE(thirdTrack.trackNumber(), 8);
        QCOMPARE(thirdTrack.discNumber(), 1);
        QCOMPARE(thirdTrack.duration(), QTime::fromMSecsSinceStartOfDay(21));
        QCOMPARE(thirdTrack.resourceURI(), QUrl::fromLocalFile(QStringLiteral("/$21")));
        QCOMPARE(thirdTrack.rating(), 3);
886
        QCOMPARE(thirdTrack.genre(), QStringLiteral("genre1"));
887
        QCOMPARE(thirdTrack.composer(), QStringLiteral("composer1"));
888
        QCOMPARE(thirdTrack.lyricist(), QStringLiteral("lyricist1"));
889
        QCOMPARE(thirdTrack.isSingleDiscAlbum(), true);
890

891
        auto albumId = musicDb.albumIdFromTitleAndArtist(QStringLiteral("album3"), {}, QStringLiteral("/"));
892
893
        auto album = musicDb.albumDataFromDatabaseId(albumId);
        auto albumData = musicDb.albumData(albumId);
894
895

        QCOMPARE(album.isValid(), true);
896
        QCOMPARE(albumData.count(), 3);
897
        QCOMPARE(album.title(), QStringLiteral("album3"));
898
        QVERIFY(album.isValidArtist());
899
900
901
902
        QCOMPARE(album.albumArtURI(), QUrl::fromLocalFile(QStringLiteral("album3")));
        QCOMPARE(album.isSingleDiscAlbum(), true);
    }

903
904
    void addTwoTracksWithPartialAlbumArtist()
    {
905
906
907
908
909
        QTemporaryFile databaseFile;
        databaseFile.open();

        qDebug() << "addTwoTracksWithPartialAlbumArtist" << databaseFile.fileName();

910
911
        DatabaseInterface musicDb;

912
        musicDb.init(QStringLiteral("testDb"), databaseFile.fileName());
913

914
        QSignalSpy musicDbArtistAddedSpy(&musicDb, &DatabaseInterface::artistsAdded);
915
        QSignalSpy musicDbAlbumAddedSpy(&musicDb, &DatabaseInterface::albumsAdded);
916
        QSignalSpy musicDbTrackAddedSpy(&musicDb, &DatabaseInterface::tracksAdded);
917
918
919
920
921
        QSignalSpy musicDbArtistRemovedSpy(&musicDb, &DatabaseInterface::artistRemoved);
        QSignalSpy musicDbAlbumRemovedSpy(&musicDb, &DatabaseInterface::albumRemoved);
        QSignalSpy musicDbTrackRemovedSpy(&musicDb, &DatabaseInterface::trackRemoved);
        QSignalSpy musicDbAlbumModifiedSpy(&musicDb, &DatabaseInterface::albumModified);
        QSignalSpy musicDbTrackModifiedSpy(&musicDb, &DatabaseInterface::trackModified);
922
        QSignalSpy musicDbDatabaseErrorSpy(&musicDb, &DatabaseInterface::databaseError);
923

924
        QCOMPARE(musicDb.allAlbumsData().count(), 0);