~ubuntu-branches/ubuntu/lucid/amarok/lucid-backports

« back to all changes in this revision

Viewing changes to tests/core-impl/collections/db/sql/TestSqlTrack.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2011-03-03 10:27:39 UTC
  • mfrom: (114.1.20 natty)
  • Revision ID: james.westby@ubuntu.com-20110303102739-ar67wpa6mllo59n2
Tags: 2:2.4.0-0ubuntu4~lucid1
* Source backport to lucid (LP: #728447)
  - Drop version requirement on libindicate-qt-dev build-dep

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************************
 
2
 * Copyright (c) 2010 Maximilian Kossick <maximilian.kossick@googlemail.com>       *
 
3
 *                                                                                      *
 
4
 * This program is free software; you can redistribute it and/or modify it under        *
 
5
 * the terms of the GNU General Public License as published by the Free Software        *
 
6
 * Foundation; either version 2 of the License, or (at your option) any later           *
 
7
 * version.                                                                             *
 
8
 *                                                                                      *
 
9
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
 
10
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
 
11
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
 
12
 *                                                                                      *
 
13
 * You should have received a copy of the GNU General Public License along with         *
 
14
 * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
 
15
 ****************************************************************************************/
 
16
 
 
17
#include "TestSqlTrack.h"
 
18
 
 
19
#include "core/support/Debug.h"
 
20
#include "DefaultSqlQueryMakerFactory.h"
 
21
#include "core/meta/Meta.h"
 
22
#include "mysqlecollection/MySqlEmbeddedStorage.h"
 
23
 
 
24
#include "SqlCollection.h"
 
25
#include "SqlMeta.h"
 
26
#include "SqlRegistry.h"
 
27
#include "SqlMountPointManagerMock.h"
 
28
 
 
29
#include "MetaNotificationSpy.h"
 
30
 
 
31
#include <QDateTime>
 
32
#include <QSignalSpy>
 
33
 
 
34
#include <qtest_kde.h>
 
35
 
 
36
QTEST_KDEMAIN_CORE( TestSqlTrack )
 
37
 
 
38
TestSqlTrack::TestSqlTrack()
 
39
    : QObject()
 
40
    , m_collection( 0 )
 
41
    , m_storage( 0 )
 
42
    , m_tmpDir( 0 )
 
43
{
 
44
}
 
45
 
 
46
void
 
47
TestSqlTrack::initTestCase()
 
48
{
 
49
    m_tmpDir = new KTempDir();
 
50
    m_storage = new MySqlEmbeddedStorage( m_tmpDir->name() );
 
51
    m_collection = new Collections::SqlCollection( "testId", "testtrack", m_storage );
 
52
    m_collection->setMountPointManager( new SqlMountPointManagerMock( this, m_storage ) );
 
53
 
 
54
    // I just need the table and not the whole playlist manager
 
55
    m_storage->query( QString( "CREATE TABLE playlist_tracks ("
 
56
            " id " + m_storage->idType() +
 
57
            ", playlist_id INTEGER "
 
58
            ", track_num INTEGER "
 
59
            ", url " + m_storage->exactTextColumnType() +
 
60
            ", title " + m_storage->textColumnType() +
 
61
            ", album " + m_storage->textColumnType() +
 
62
            ", artist " + m_storage->textColumnType() +
 
63
            ", length INTEGER "
 
64
            ", uniqueid " + m_storage->textColumnType(128) + ") ENGINE = MyISAM;" ) );
 
65
 
 
66
}
 
67
 
 
68
void
 
69
TestSqlTrack::cleanupTestCase()
 
70
{
 
71
    delete m_collection;
 
72
    //m_storage is deleted by SqlCollection
 
73
    delete m_tmpDir;
 
74
}
 
75
 
 
76
void
 
77
TestSqlTrack::init()
 
78
{
 
79
    //setup base data
 
80
    m_storage->query( "INSERT INTO artists(id, name) VALUES (1, 'artist1');" );
 
81
    m_storage->query( "INSERT INTO artists(id, name) VALUES (2, 'artist2');" );
 
82
    m_storage->query( "INSERT INTO artists(id, name) VALUES (3, 'artist3');" );
 
83
 
 
84
    m_storage->query( "INSERT INTO albums(id,name,artist) VALUES(1,'album1',1);" );
 
85
    m_storage->query( "INSERT INTO albums(id,name,artist) VALUES(2,'album2',1);" );
 
86
    m_storage->query( "INSERT INTO albums(id,name,artist) VALUES(3,'album3',2);" );
 
87
    m_storage->query( "INSERT INTO albums(id,name,artist) VALUES(4,'album-compilation',0);" );
 
88
 
 
89
    m_storage->query( "INSERT INTO composers(id, name) VALUES (1, 'composer1');" );
 
90
    m_storage->query( "INSERT INTO composers(id, name) VALUES (2, 'composer2');" );
 
91
    m_storage->query( "INSERT INTO composers(id, name) VALUES (3, 'composer3');" );
 
92
 
 
93
    m_storage->query( "INSERT INTO genres(id, name) VALUES (1, 'genre1');" );
 
94
    m_storage->query( "INSERT INTO genres(id, name) VALUES (2, 'genre2');" );
 
95
    m_storage->query( "INSERT INTO genres(id, name) VALUES (3, 'genre3');" );
 
96
 
 
97
    m_storage->query( "INSERT INTO years(id, name) VALUES (1, '1');" );
 
98
    m_storage->query( "INSERT INTO years(id, name) VALUES (2, '2');" );
 
99
    m_storage->query( "INSERT INTO years(id, name) VALUES (3, '3');" );
 
100
 
 
101
    m_storage->query( "INSERT INTO urls(id,deviceid,rpath,uniqueid) VALUES (1, -1, './IDoNotExist.mp3','1');" );
 
102
    m_storage->query( "INSERT INTO urls(id,deviceid,rpath,uniqueid) VALUES (2, -1, './IDoNotExistAsWell.mp3','2');" );
 
103
    m_storage->query( "INSERT INTO urls(id,deviceid,rpath,uniqueid) VALUES (3, -1, './MeNeither.mp3','3');" );
 
104
    m_storage->query( "INSERT INTO urls(id,deviceid,rpath,uniqueid) VALUES (4, -1, './NothingHere.mp3','4');" );
 
105
 
 
106
    m_storage->query( "INSERT INTO tracks(id,url,title,comment,artist,album,genre,year,composer) "
 
107
                      "VALUES(1,1,'track1','comment1',1,1,1,1,1);" );
 
108
    m_storage->query( "INSERT INTO tracks(id,url,title,comment,artist,album,genre,year,composer) "
 
109
                      "VALUES(2,2,'track2','comment2',1,2,1,1,1);" );
 
110
 
 
111
    m_collection->registry()->emptyCache();
 
112
}
 
113
 
 
114
void
 
115
TestSqlTrack::cleanup()
 
116
{
 
117
    m_storage->query( "TRUNCATE TABLE years;" );
 
118
    m_storage->query( "TRUNCATE TABLE genres;" );
 
119
    m_storage->query( "TRUNCATE TABLE composers;" );
 
120
    m_storage->query( "TRUNCATE TABLE albums;" );
 
121
    m_storage->query( "TRUNCATE TABLE artists;" );
 
122
    m_storage->query( "TRUNCATE TABLE tracks;" );
 
123
    m_storage->query( "TRUNCATE TABLE urls;" );
 
124
    m_storage->query( "TRUNCATE TABLE statistics;" );
 
125
    m_storage->query( "TRUNCATE TABLE labels;" );
 
126
    m_storage->query( "TRUNCATE TABLE urls_labels;" );
 
127
}
 
128
 
 
129
 
 
130
void
 
131
TestSqlTrack::setAllValues( Meta::SqlTrack *track )
 
132
{
 
133
    track->setTitle( "New Title" );
 
134
    track->setAlbum( "New Album" );
 
135
    track->setArtist( "New Artist" );
 
136
    track->setComposer( "New Composer" );
 
137
    track->setYear( 1999 );
 
138
    track->setGenre( "New Genre" );
 
139
 
 
140
    track->setUrl( -1, "./new_url", 2 );
 
141
 
 
142
    track->setBpm( 32.0 );
 
143
    track->setComment( "New Comment" );
 
144
 
 
145
    track->setScore( 64.0 );
 
146
    track->setRating( 5 );
 
147
 
 
148
    track->setLength( 5000 );
 
149
    track->setSampleRate( 4400 );
 
150
    track->setBitrate( 128 );
 
151
 
 
152
    track->setTrackNumber( 4 );
 
153
    track->setDiscNumber( 1 );
 
154
 
 
155
    track->setFirstPlayed( QDateTime::fromTime_t(100) );
 
156
    track->setLastPlayed( QDateTime::fromTime_t(200) );
 
157
    track->setPlayCount( 20 );
 
158
 
 
159
    Meta::ReplayGainTag modes[] = { Meta::ReplayGain_Track_Gain,
 
160
        Meta::ReplayGain_Track_Peak,
 
161
        Meta::ReplayGain_Album_Gain,
 
162
        Meta::ReplayGain_Album_Peak };
 
163
 
 
164
    for( int i=0; i<4; i++ )
 
165
        track->setReplayGain( modes[i], qreal(i) );
 
166
 
 
167
    track->addLabel( "New Label" );
 
168
}
 
169
 
 
170
void
 
171
TestSqlTrack::getAllValues( Meta::SqlTrack *track )
 
172
{
 
173
    QCOMPARE( track->name(), QString( "New Title" ) );
 
174
    QCOMPARE( track->album()->name(), QString( "New Album" ) );
 
175
    QCOMPARE( track->artist()->name(), QString( "New Artist" ) );
 
176
    QCOMPARE( track->composer()->name(), QString( "New Composer" ) );
 
177
    QCOMPARE( track->year()->name(), QString( "1999" ) );
 
178
    QCOMPARE( track->genre()->name(), QString( "New Genre" ) );
 
179
 
 
180
    QCOMPARE( track->playableUrl().path(), QString( "/new_url" ) );
 
181
    QCOMPARE( track->bpm(), 32.0 );
 
182
    QCOMPARE( track->comment(), QString( "New Comment" ) );
 
183
 
 
184
    QCOMPARE( track->score(), 64.0 );
 
185
    QCOMPARE( track->rating(), 5 );
 
186
 
 
187
    QCOMPARE( track->length(), qint64(5000) );
 
188
    QCOMPARE( track->sampleRate(), 4400 );
 
189
    QCOMPARE( track->bitrate(), 128 );
 
190
 
 
191
    QCOMPARE( track->trackNumber(), 4 );
 
192
    QCOMPARE( track->discNumber(), 1 );
 
193
 
 
194
    QCOMPARE( track->firstPlayed(), QDateTime::fromTime_t(100) );
 
195
    QCOMPARE( track->lastPlayed(), QDateTime::fromTime_t(200) );
 
196
    QCOMPARE( track->playCount(), 20 );
 
197
 
 
198
    Meta::ReplayGainTag modes[] = { Meta::ReplayGain_Track_Gain,
 
199
        Meta::ReplayGain_Track_Peak,
 
200
        Meta::ReplayGain_Album_Gain,
 
201
        Meta::ReplayGain_Album_Peak };
 
202
 
 
203
    for( int i=0; i<4; i++ )
 
204
            QCOMPARE( track->replayGain( modes[i] ), qreal(i) );
 
205
 
 
206
    QVERIFY( track->labels().count() > 0 );
 
207
    QVERIFY( track->labels().contains( m_collection->registry()->getLabel("New Label") ) );
 
208
}
 
209
 
 
210
/** Check that the registry always returns the same track pointer */
 
211
void
 
212
TestSqlTrack::testGetTrack()
 
213
{
 
214
    {
 
215
        Meta::TrackPtr track1 = m_collection->registry()->getTrack( 1 );
 
216
        Meta::TrackPtr track2 = m_collection->registry()->getTrack( "/IDoNotExist.mp3" );
 
217
        Meta::TrackPtr track3 = m_collection->registry()->getTrackFromUid( "1" );
 
218
 
 
219
        QVERIFY( track1 );
 
220
        QVERIFY( track1 == track2 );
 
221
        QVERIFY( track1 == track3 );
 
222
    }
 
223
 
 
224
    // and also after empty cache
 
225
    m_collection->registry()->emptyCache();
 
226
 
 
227
    // changed order...
 
228
    {
 
229
        Meta::TrackPtr track2 = m_collection->registry()->getTrack( "/IDoNotExist.mp3" );
 
230
        Meta::TrackPtr track3 = m_collection->registry()->getTrackFromUid( "1" );
 
231
        Meta::TrackPtr track1 = m_collection->registry()->getTrack( 1 );
 
232
 
 
233
        QVERIFY( track1 );
 
234
        QVERIFY( track1 == track2 );
 
235
        QVERIFY( track1 == track3 );
 
236
    }
 
237
 
 
238
    // do again creating a new track
 
239
    cleanup();
 
240
    m_collection->registry()->emptyCache();
 
241
 
 
242
    // changed order...
 
243
    {
 
244
        Meta::TrackPtr track1 = m_collection->registry()->getTrack( -1, "./newTrack.mp3", 2, "amarok-sqltrackuid://newuid" );
 
245
        Meta::SqlTrack *sqlTrack1 = static_cast<Meta::SqlTrack*>( track1.data() );
 
246
        sqlTrack1->setBpm( 100 ); // have to commit the new track
 
247
 
 
248
        QVERIFY( track1 );
 
249
        QCOMPARE( track1->playableUrl().path(), QString("/newTrack.mp3" ));
 
250
        QCOMPARE( track1->uidUrl(), QString("amarok-sqltrackuid://newuid" ));
 
251
    }
 
252
 
 
253
    m_collection->registry()->emptyCache();
 
254
 
 
255
    // changed order...
 
256
    {
 
257
        Meta::TrackPtr track1 = m_collection->registry()->getTrackFromUid("amarok-sqltrackuid://newuid");
 
258
 
 
259
        QVERIFY( track1 );
 
260
        QCOMPARE( track1->playableUrl().path(), QString("/newTrack.mp3" ));
 
261
        QCOMPARE( track1->uidUrl(), QString("amarok-sqltrackuid://newuid" ));
 
262
        QCOMPARE( track1->bpm(), 100.0 );
 
263
    }
 
264
}
 
265
 
 
266
void
 
267
TestSqlTrack::testSetAllValuesSingleNotExisting()
 
268
{
 
269
    {
 
270
        // get a new track
 
271
        Meta::TrackPtr track1 = m_collection->registry()->getTrack( -1, "./IamANewTrack.mp3", 0, "mb-1e34fb213489" );
 
272
 
 
273
        QSignalSpy spy( m_collection, SIGNAL(updated()));
 
274
        MetaNotificationSpy metaSpy;
 
275
        metaSpy.subscribeTo( track1 );
 
276
 
 
277
        Meta::SqlTrack *sqlTrack1 = static_cast<Meta::SqlTrack*>( track1.data() );
 
278
        setAllValues( sqlTrack1 );
 
279
        getAllValues( sqlTrack1 );
 
280
 
 
281
        // new track should have an up-to-date create time (not more than 3 seconds old)
 
282
        QVERIFY( track1->createDate().secsTo(QDateTime::currentDateTime()) < 3 );
 
283
 
 
284
        QVERIFY( metaSpy.notificationsFromTracks().count() > 1 ); // we should be notified about the changes
 
285
    }
 
286
 
 
287
    // and also after empty cache
 
288
    m_collection->registry()->emptyCache();
 
289
 
 
290
    {
 
291
        Meta::TrackPtr track1 = m_collection->registry()->getTrack( "/new_url" );
 
292
        Meta::SqlTrack *sqlTrack1 = static_cast<Meta::SqlTrack*>( track1.data() );
 
293
        QVERIFY( track1 );
 
294
        getAllValues( sqlTrack1 );
 
295
    }
 
296
}
 
297
 
 
298
/** Set all track values but before that create them in the registry. */
 
299
void
 
300
TestSqlTrack::testSetAllValuesSingleExisting()
 
301
{
 
302
    {
 
303
        Meta::GenrePtr    genre    = m_collection->registry()->getGenre( "New Genre" );
 
304
        Meta::ComposerPtr composer = m_collection->registry()->getComposer( "New Composer" );
 
305
        Meta::YearPtr     year     = m_collection->registry()->getYear( 1999 );
 
306
        Meta::AlbumPtr    album    = m_collection->registry()->getAlbum( "New Album", "New Artist" );
 
307
        m_collection->registry()->getLabel( "New Label" );
 
308
 
 
309
        Meta::TrackPtr track1 = m_collection->registry()->getTrack( "/IDoNotExist.mp3" );
 
310
 
 
311
        Meta::SqlTrack *sqlTrack1 = static_cast<Meta::SqlTrack*>( track1.data() );
 
312
        setAllValues( sqlTrack1 );
 
313
        getAllValues( sqlTrack1 );
 
314
 
 
315
        // check that the existing object are really updated with the new tracklist
 
316
        QCOMPARE( genre->tracks().count(), 1 );
 
317
        QCOMPARE( genre->tracks().first().data(), track1.data() );
 
318
 
 
319
        QCOMPARE( composer->tracks().count(), 1 );
 
320
        QCOMPARE( composer->tracks().first().data(), track1.data() );
 
321
 
 
322
        QCOMPARE( year->tracks().count(), 1 );
 
323
        QCOMPARE( year->tracks().first().data(), track1.data() );
 
324
 
 
325
        // the logic, how renaming the track artist influences its album is still
 
326
        // unfinished. For sure the track must be in an album with the defined
 
327
        // name
 
328
        QCOMPARE( sqlTrack1->album()->name(), QString("New Album") );
 
329
        QCOMPARE( sqlTrack1->album()->tracks().count(), 1 );
 
330
        QCOMPARE( sqlTrack1->album()->tracks().first().data(), track1.data() );
 
331
    }
 
332
 
 
333
    // and also after empty cache
 
334
    m_collection->registry()->emptyCache();
 
335
 
 
336
    {
 
337
        Meta::TrackPtr track1 = m_collection->registry()->getTrack( "/new_url" );
 
338
        Meta::SqlTrack *sqlTrack1 = static_cast<Meta::SqlTrack*>( track1.data() );
 
339
        QVERIFY( track1 );
 
340
        getAllValues( sqlTrack1 );
 
341
 
 
342
        Meta::GenrePtr    genre    = m_collection->registry()->getGenre( "New Genre" );
 
343
        Meta::ComposerPtr composer = m_collection->registry()->getComposer( "New Composer" );
 
344
        Meta::YearPtr     year     = m_collection->registry()->getYear( 1999 );
 
345
        Meta::AlbumPtr    album    = m_collection->registry()->getAlbum( "New Album", "New Artist" );
 
346
 
 
347
        // check that the existing object are really updated with the new tracklist
 
348
        QCOMPARE( genre->tracks().count(), 1 );
 
349
        QCOMPARE( genre->tracks().first().data(), track1.data() );
 
350
 
 
351
        QCOMPARE( composer->tracks().count(), 1 );
 
352
        QCOMPARE( composer->tracks().first().data(), track1.data() );
 
353
 
 
354
        QCOMPARE( year->tracks().count(), 1 );
 
355
        QCOMPARE( year->tracks().first().data(), track1.data() );
 
356
 
 
357
        // the logic, how renaming the track artist influences its album is still
 
358
        // unfinished. For sure the track must be in an album with the defined
 
359
        // name
 
360
        QCOMPARE( sqlTrack1->album()->name(), QString("New Album") );
 
361
        QCOMPARE( sqlTrack1->album()->tracks().count(), 1 );
 
362
        QCOMPARE( sqlTrack1->album()->tracks().first().data(), track1.data() );
 
363
    }
 
364
}
 
365
 
 
366
void
 
367
TestSqlTrack::testSetAllValuesBatch()
 
368
{
 
369
    {
 
370
        Meta::TrackPtr track1 = m_collection->registry()->getTrack( "/IDoNotExist.mp3" );
 
371
        Meta::SqlTrack *sqlTrack1 = static_cast<Meta::SqlTrack*>( track1.data() );
 
372
 
 
373
        QSignalSpy spy( m_collection, SIGNAL(updated()));
 
374
        MetaNotificationSpy metaSpy;
 
375
        metaSpy.subscribeTo( track1 );
 
376
 
 
377
        sqlTrack1->beginMetaDataUpdate();
 
378
 
 
379
        setAllValues( sqlTrack1 );
 
380
        QCOMPARE( metaSpy.notificationsFromTracks().count(), 1 ); // add label does one notify
 
381
 
 
382
        sqlTrack1->endMetaDataUpdate();
 
383
        QCOMPARE( metaSpy.notificationsFromTracks().count(), 2 ); // only one notificate for all the changes
 
384
 
 
385
        getAllValues( sqlTrack1 );
 
386
    }
 
387
 
 
388
    // and also after empty cache
 
389
    m_collection->registry()->emptyCache();
 
390
 
 
391
    {
 
392
        Meta::TrackPtr track1 = m_collection->registry()->getTrack( "/new_url" );
 
393
        Meta::SqlTrack *sqlTrack1 = static_cast<Meta::SqlTrack*>( track1.data() );
 
394
        QVERIFY( track1 );
 
395
        getAllValues( sqlTrack1 );
 
396
    }
 
397
}
 
398
 
 
399
void
 
400
TestSqlTrack::testUnsetValues()
 
401
{
 
402
    {
 
403
        Meta::TrackPtr track1 = m_collection->registry()->getTrack( "/IDoNotExist.mp3" );
 
404
        Meta::SqlTrack *sqlTrack1 = static_cast<Meta::SqlTrack*>( track1.data() );
 
405
 
 
406
        setAllValues( sqlTrack1 );
 
407
 
 
408
        // now unset the values again
 
409
        sqlTrack1->setAlbum( "" );
 
410
        sqlTrack1->setArtist( "" );
 
411
        sqlTrack1->setComposer( "" );
 
412
        sqlTrack1->setYear( 0 ); // it is not clear what an empty year exacly is
 
413
        sqlTrack1->setGenre( "" );
 
414
 
 
415
        // note: Amarok is still not clear if an empty artist means track->artist() == 0
 
416
        QVERIFY( !track1->album() || track1->album()->name().isEmpty() );
 
417
        QVERIFY( !track1->artist() || track1->artist()->name().isEmpty() );
 
418
        QVERIFY( !track1->composer() || track1->composer()->name().isEmpty() );
 
419
        QVERIFY( !track1->year() || track1->year()->year() == 0 );
 
420
        QVERIFY( !track1->genre() || track1->genre()->name().isEmpty() );
 
421
    }
 
422
 
 
423
    // and also after empty cache
 
424
    m_collection->registry()->emptyCache();
 
425
 
 
426
    {
 
427
        Meta::TrackPtr track1 = m_collection->registry()->getTrack( "/new_url" );
 
428
        QVERIFY( track1 );
 
429
        QVERIFY( !track1->album() || track1->album()->name().isEmpty() );
 
430
        QVERIFY( !track1->artist() || track1->artist()->name().isEmpty() );
 
431
        QVERIFY( !track1->composer() || track1->composer()->name().isEmpty() );
 
432
        QVERIFY( !track1->year() || track1->year()->year() == 0 );
 
433
        QVERIFY( !track1->genre() || track1->genre()->name().isEmpty() );
 
434
    }
 
435
}
 
436
 
 
437
void
 
438
TestSqlTrack::testFinishedPlaying()
 
439
{
 
440
    Meta::TrackPtr track1 = m_collection->registry()->getTrack( "/IDoNotExist.mp3" );
 
441
    Meta::SqlTrack *sqlTrack1 = static_cast<Meta::SqlTrack*>( track1.data() );
 
442
 
 
443
    sqlTrack1->setLength( 5000 );
 
444
 
 
445
    QCOMPARE( sqlTrack1->score(),       0.0 );
 
446
    QCOMPARE( sqlTrack1->playCount(),   0 );
 
447
    QVERIFY( !sqlTrack1->firstPlayed().isValid() );
 
448
    QVERIFY( !sqlTrack1->lastPlayed().isValid() );
 
449
 
 
450
    // now play the track not really
 
451
    sqlTrack1->finishedPlaying( 0.1 );
 
452
 
 
453
    // can't do a statement about the score here
 
454
    QCOMPARE( sqlTrack1->playCount(),   0 );
 
455
    QVERIFY( !sqlTrack1->firstPlayed().isValid() );
 
456
    QVERIFY( !sqlTrack1->lastPlayed().isValid() );
 
457
 
 
458
    // and now really play it
 
459
    sqlTrack1->finishedPlaying( 1.0 );
 
460
 
 
461
    QVERIFY(  sqlTrack1->score() > 0.0 );
 
462
    QCOMPARE( sqlTrack1->playCount(),   1 );
 
463
    QVERIFY(  sqlTrack1->firstPlayed().secsTo( QDateTime::currentDateTime() ) < 2 );
 
464
    QVERIFY(  sqlTrack1->lastPlayed().secsTo( QDateTime::currentDateTime() ) < 2 );
 
465
}
 
466
 
 
467
 
 
468
void
 
469
TestSqlTrack::testAlbumRemaingsNonCompilationAfterChangingAlbumName()
 
470
{
 
471
    m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
 
472
                      "VALUES (3,3,'track1',1,1,1,1,1 );" );
 
473
    m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
 
474
                      "VALUES (4,4,'track2',1,1,1,1,1 );" );
 
475
 
 
476
    Meta::TrackPtr track1 = m_collection->registry()->getTrack( 3 );
 
477
    Meta::TrackPtr track2 = m_collection->registry()->getTrack( 4 );
 
478
 
 
479
    QCOMPARE( track1->album()->name(), QString( "album1" ) );
 
480
    QVERIFY( track1->album()->hasAlbumArtist() );
 
481
    QCOMPARE( track1->album().data(), track2->album().data() );
 
482
 
 
483
    Meta::SqlTrack *sqlTrack1 = static_cast<Meta::SqlTrack*>( track1.data() );
 
484
    sqlTrack1->setAlbum( "album2" );
 
485
    Meta::SqlTrack *sqlTrack2 = static_cast<Meta::SqlTrack*>( track2.data() );
 
486
    sqlTrack2->beginMetaDataUpdate();
 
487
    sqlTrack2->setAlbum( "album2" );
 
488
    sqlTrack2->endMetaDataUpdate();
 
489
 
 
490
    QCOMPARE( track1->album()->name(), QString( "album2" ) );
 
491
    QVERIFY( track1->album()->hasAlbumArtist() );
 
492
    QVERIFY( track1->album() == track2->album() );
 
493
}
 
494
 
 
495
void
 
496
TestSqlTrack::testAlbumRemainsCompilationAfterChangingAlbumName()
 
497
{
 
498
    m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
 
499
                      "VALUES (3,3,'track1',1,4,1,1,1 );" );
 
500
    m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
 
501
                      "VALUES (4,4,'track2',1,4,1,1,1 );" );
 
502
 
 
503
    Meta::TrackPtr track1 = m_collection->registry()->getTrack( 3 );
 
504
    Meta::TrackPtr track2 = m_collection->registry()->getTrack( 4 );
 
505
 
 
506
    QVERIFY( track1 );
 
507
    QVERIFY( track1->album() );
 
508
    QVERIFY( track2 );
 
509
    QVERIFY( track2->album() );
 
510
    QCOMPARE( track1->album()->name(), QString( "album-compilation" ) );
 
511
    QVERIFY( track1->album()->isCompilation() );
 
512
    QVERIFY( track1->album().data() == track2->album().data() );
 
513
 
 
514
    Meta::SqlTrack *sqlTrack1 = static_cast<Meta::SqlTrack*>( track1.data() );
 
515
    Meta::SqlTrack *sqlTrack2 = static_cast<Meta::SqlTrack*>( track2.data() );
 
516
    sqlTrack1->setAlbum( "album2" );
 
517
    sqlTrack2->beginMetaDataUpdate();
 
518
    sqlTrack2->setAlbum( "album2" );
 
519
    sqlTrack2->endMetaDataUpdate();
 
520
 
 
521
    QCOMPARE( track1->album()->name(), QString( "album2" ) );
 
522
    QVERIFY( track1->album()->isCompilation() );
 
523
    QVERIFY( track1->album() == track2->album() );
 
524
}
 
525
 
 
526
void
 
527
TestSqlTrack::testRemoveLabelFromTrack()
 
528
{
 
529
    Meta::TrackPtr track = m_collection->registry()->getTrack( "/IDoNotExist.mp3" );
 
530
    Meta::LabelPtr label = m_collection->registry()->getLabel( "A" );
 
531
    track->addLabel( label );
 
532
    QCOMPARE( track->labels().count(), 1 );
 
533
 
 
534
    track->removeLabel( label );
 
535
    QCOMPARE( track->labels().count(), 0 );
 
536
 
 
537
    QStringList urlsLabelsCount = m_storage->query( "SELECT COUNT(*) FROM urls_labels;" );
 
538
    QCOMPARE( urlsLabelsCount.first().toInt(), 0 );
 
539
}
 
540
 
 
541
void
 
542
TestSqlTrack::testRemoveLabelFromTrackWhenNotInCache()
 
543
{
 
544
    m_storage->query( "INSERT INTO labels(id,label) VALUES (1,'A');" );
 
545
    m_storage->query( "INSERT INTO urls_labels(url,label) VALUES (1,1);" );
 
546
 
 
547
    Meta::TrackPtr track = m_collection->registry()->getTrack( "/IDoNotExist.mp3" );
 
548
    Meta::LabelPtr label = m_collection->registry()->getLabel( "A" );
 
549
 
 
550
    track->removeLabel( label );
 
551
    QCOMPARE( track->labels().count(), 0 );
 
552
 
 
553
    QStringList urlsLabelsCount = m_storage->query( "SELECT COUNT(*) FROM urls_labels;" );
 
554
    QCOMPARE( urlsLabelsCount.first().toInt(), 0 );
 
555
}
 
556
 
 
557
#include "TestSqlTrack.moc"