Some more backend tests

This commit is contained in:
David Sansome 2010-03-03 00:33:31 +00:00
parent 3296bf7b7d
commit 3f861654ec
7 changed files with 136 additions and 33 deletions

View File

@ -15,6 +15,7 @@ set(QT_USE_QTOPENGL 1)
set(QT_USE_QTSQL 1)
set(QT_USE_QTNETWORK 1)
set(QT_USE_QTXML 1)
set(QT_USE_QTTEST 1)
if(WIN32)
set(QT_USE_PHONON 1)
endif(WIN32)

View File

@ -3,6 +3,7 @@
#include <QList>
#include <QString>
#include <QMetaType>
class QSqlQuery;
@ -10,7 +11,9 @@ struct Directory {
QString path;
int id;
};
Q_DECLARE_METATYPE(Directory);
typedef QList<Directory> DirectoryList;
Q_DECLARE_METATYPE(DirectoryList);
#endif // DIRECTORY_H

View File

@ -78,7 +78,6 @@ QSqlDatabase LibraryBackend::Connect() {
}
if (schema_version < kSchemaVersion) {
// Update the schema
qDebug() << "Updating database schema from" << schema_version << "to" << kSchemaVersion;
for (int v=schema_version+1 ; v<= kSchemaVersion ; ++v) {
UpdateDatabaseSchema(v, db);
}
@ -100,8 +99,6 @@ void LibraryBackend::UpdateDatabaseSchema(int version, QSqlDatabase &db) {
qFatal("Couldn't open schema file %s", filename.toUtf8().constData());
QString schema(QString::fromUtf8(schema_file.readAll()));
qDebug() << "Applying database schema version" << version;
// Run each command
QStringList commands(schema.split(";\n\n"));
db.transaction();

View File

@ -7,6 +7,7 @@
#include <QSharedDataPointer>
#include <QSqlQuery>
#include <QString>
#include <QMetaType>
#include "engine_fwd.h"
@ -152,7 +153,9 @@ class Song {
private:
QSharedDataPointer<SongData> d;
};
Q_DECLARE_METATYPE(Song);
typedef QList<Song> SongList;
Q_DECLARE_METATYPE(SongList);
#endif // SONG_H

View File

@ -9,6 +9,7 @@
#include <QtDebug>
#include <QThread>
#include <QSignalSpy>
using ::testing::_;
using ::testing::AtMost;
@ -75,51 +76,128 @@ TEST_F(LibraryBackendTest, EmptyDatabase) {
EXPECT_TRUE(albums.isEmpty());
}
TEST_F(LibraryBackendTest, AddSong) {
// Add a directory
TEST_F(LibraryBackendTest, AddDirectory) {
QSignalSpy spy(backend_.get(), SIGNAL(DirectoriesDiscovered(DirectoryList)));
backend_->AddDirectory("/test");
// Add the song
Song s = MakeDummySong(1);
s.set_title("Foo");
s.set_artist("Bar");
s.set_album("Meep");
backend_->AddOrUpdateSongs(SongList() << s);
// Check the signal was emitted correctly
ASSERT_EQ(1, spy.count());
DirectoryList list = spy[0][0].value<DirectoryList>();
ASSERT_EQ(1, list.size());
EXPECT_EQ("/test", list[0].path);
EXPECT_EQ(1, list[0].id);
}
// Check the artist
TEST_F(LibraryBackendTest, RemoveDirectory) {
// Add a directory
Directory dir;
dir.id = 1;
dir.path = "/test";
backend_->AddDirectory(dir.path);
QSignalSpy spy(backend_.get(), SIGNAL(DirectoriesDeleted(DirectoryList)));
// Remove the directory again
backend_->RemoveDirectory(dir);
// Check the signal was emitted correctly
ASSERT_EQ(1, spy.count());
DirectoryList list = spy[0][0].value<DirectoryList>();
ASSERT_EQ(1, list.size());
EXPECT_EQ("/test", list[0].path);
EXPECT_EQ(1, list[0].id);
}
// Test adding a single song to the database, then getting various information
// back about it.
class SingleSong : public LibraryBackendTest {
protected:
virtual void SetUp() {
LibraryBackendTest::SetUp();
// Add a directory - this will get ID 1
backend_->AddDirectory("/test");
// Make a song in that directory
song_ = MakeDummySong(1);
song_.set_title("Title");
song_.set_artist("Artist");
song_.set_album("Album");
QSignalSpy added_spy(backend_.get(), SIGNAL(SongsDiscovered(SongList)));
QSignalSpy deleted_spy(backend_.get(), SIGNAL(SongsDeleted(SongList)));
// Add the song
backend_->AddOrUpdateSongs(SongList() << song_);
// Check the correct signals were emitted
EXPECT_EQ(0, deleted_spy.count());
ASSERT_EQ(1, added_spy.count());
SongList list = added_spy[0][0].value<SongList>();
ASSERT_EQ(1, list.count());
EXPECT_EQ(song_.title(), list[0].title());
EXPECT_EQ(song_.artist(), list[0].artist());
EXPECT_EQ(song_.album(), list[0].album());
EXPECT_EQ(1, list[0].id());
EXPECT_EQ(1, list[0].directory_id());
}
Song song_;
};
TEST_F(SingleSong, GetAllArtists) {
QStringList artists = backend_->GetAllArtists();
ASSERT_EQ(1, artists.size());
EXPECT_EQ(s.artist(), artists[0]);
EXPECT_EQ(song_.artist(), artists[0]);
}
// Check the various album getters
TEST_F(SingleSong, GetAllAlbums) {
LibraryBackend::AlbumList albums = backend_->GetAllAlbums();
ASSERT_EQ(1, albums.size());
EXPECT_EQ(s.album(), albums[0].album_name);
EXPECT_EQ(s.artist(), albums[0].artist);
EXPECT_EQ(song_.album(), albums[0].album_name);
EXPECT_EQ(song_.artist(), albums[0].artist);
}
albums = backend_->GetAlbumsByArtist("Bar");
TEST_F(SingleSong, GetAlbumsByArtist) {
LibraryBackend::AlbumList albums = backend_->GetAlbumsByArtist("Artist");
ASSERT_EQ(1, albums.size());
EXPECT_EQ(s.album(), albums[0].album_name);
EXPECT_EQ(s.artist(), albums[0].artist);
EXPECT_EQ(song_.album(), albums[0].album_name);
EXPECT_EQ(song_.artist(), albums[0].artist);
}
LibraryBackend::Album album = backend_->GetAlbumArt("Bar", "Meep");
EXPECT_EQ(s.album(), album.album_name);
EXPECT_EQ(s.artist(), album.artist);
TEST_F(SingleSong, GetAlbumArt) {
LibraryBackend::Album album = backend_->GetAlbumArt("Artist", "Album");
EXPECT_EQ(song_.album(), album.album_name);
EXPECT_EQ(song_.artist(), album.artist);
}
// Check we can get the song back
SongList songs = backend_->GetSongs("Bar", "Meep");
TEST_F(SingleSong, GetSongs) {
SongList songs = backend_->GetSongs("Artist", "Album");
ASSERT_EQ(1, songs.size());
EXPECT_EQ(s.album(), songs[0].album());
EXPECT_EQ(s.artist(), songs[0].artist());
EXPECT_EQ(s.title(), songs[0].title());
EXPECT_EQ(song_.album(), songs[0].album());
EXPECT_EQ(song_.artist(), songs[0].artist());
EXPECT_EQ(song_.title(), songs[0].title());
EXPECT_EQ(1, songs[0].id());
}
// Check we can get the song by ID
Song song2 = backend_->GetSongById(1);
EXPECT_EQ(s.album(), song2.album());
EXPECT_EQ(s.artist(), song2.artist()); // This is an error - song2.artist() should obviously be "Blur"
EXPECT_EQ(s.title(), song2.title());
EXPECT_EQ(1, song2.id());
TEST_F(SingleSong, GetSongById) {
Song song = backend_->GetSongById(1);
EXPECT_EQ(song_.album(), song.album());
EXPECT_EQ(song_.artist(), song.artist());
EXPECT_EQ(song_.title(), song.title());
EXPECT_EQ(1, song.id());
}
TEST_F(SingleSong, FindSongsInDirectory) {
SongList songs = backend_->FindSongsInDirectory(1);
ASSERT_EQ(1, songs.size());
EXPECT_EQ(song_.album(), songs[0].album());
EXPECT_EQ(song_.artist(), songs[0].artist());
EXPECT_EQ(song_.title(), songs[0].title());
EXPECT_EQ(1, songs[0].id());
}
TEST_F(LibraryBackendTest, AddSongWithoutFilename) {

View File

@ -1,11 +1,13 @@
#include <gmock/gmock.h>
#include "resources_env.h"
#include "metatypes_env.h"
int main(int argc, char** argv) {
testing::InitGoogleMock(&argc, argv);
testing::AddGlobalTestEnvironment(new ResourcesEnvironment);
testing::AddGlobalTestEnvironment(new MetatypesEnvironment);
return RUN_ALL_TESTS();
}

19
tests/metatypes_env.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef METATYPES_ENV_H
#define METATYPES_ENV_H
#include <gtest/gtest.h>
#include <QMetaType>
#include "directory.h"
#include "song.h"
class MetatypesEnvironment : public ::testing::Environment {
public:
void SetUp() {
qRegisterMetaType<DirectoryList>("DirectoryList");
qRegisterMetaType<SongList>("SongList");
}
};
#endif // RESOURCES_ENV_H