Some more backend tests
This commit is contained in:
parent
3296bf7b7d
commit
3f861654ec
@ -15,6 +15,7 @@ set(QT_USE_QTOPENGL 1)
|
|||||||
set(QT_USE_QTSQL 1)
|
set(QT_USE_QTSQL 1)
|
||||||
set(QT_USE_QTNETWORK 1)
|
set(QT_USE_QTNETWORK 1)
|
||||||
set(QT_USE_QTXML 1)
|
set(QT_USE_QTXML 1)
|
||||||
|
set(QT_USE_QTTEST 1)
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
set(QT_USE_PHONON 1)
|
set(QT_USE_PHONON 1)
|
||||||
endif(WIN32)
|
endif(WIN32)
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QMetaType>
|
||||||
|
|
||||||
class QSqlQuery;
|
class QSqlQuery;
|
||||||
|
|
||||||
@ -10,7 +11,9 @@ struct Directory {
|
|||||||
QString path;
|
QString path;
|
||||||
int id;
|
int id;
|
||||||
};
|
};
|
||||||
|
Q_DECLARE_METATYPE(Directory);
|
||||||
|
|
||||||
typedef QList<Directory> DirectoryList;
|
typedef QList<Directory> DirectoryList;
|
||||||
|
Q_DECLARE_METATYPE(DirectoryList);
|
||||||
|
|
||||||
#endif // DIRECTORY_H
|
#endif // DIRECTORY_H
|
||||||
|
@ -78,7 +78,6 @@ QSqlDatabase LibraryBackend::Connect() {
|
|||||||
}
|
}
|
||||||
if (schema_version < kSchemaVersion) {
|
if (schema_version < kSchemaVersion) {
|
||||||
// Update the schema
|
// Update the schema
|
||||||
qDebug() << "Updating database schema from" << schema_version << "to" << kSchemaVersion;
|
|
||||||
for (int v=schema_version+1 ; v<= kSchemaVersion ; ++v) {
|
for (int v=schema_version+1 ; v<= kSchemaVersion ; ++v) {
|
||||||
UpdateDatabaseSchema(v, db);
|
UpdateDatabaseSchema(v, db);
|
||||||
}
|
}
|
||||||
@ -100,8 +99,6 @@ void LibraryBackend::UpdateDatabaseSchema(int version, QSqlDatabase &db) {
|
|||||||
qFatal("Couldn't open schema file %s", filename.toUtf8().constData());
|
qFatal("Couldn't open schema file %s", filename.toUtf8().constData());
|
||||||
QString schema(QString::fromUtf8(schema_file.readAll()));
|
QString schema(QString::fromUtf8(schema_file.readAll()));
|
||||||
|
|
||||||
qDebug() << "Applying database schema version" << version;
|
|
||||||
|
|
||||||
// Run each command
|
// Run each command
|
||||||
QStringList commands(schema.split(";\n\n"));
|
QStringList commands(schema.split(";\n\n"));
|
||||||
db.transaction();
|
db.transaction();
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#include <QSharedDataPointer>
|
#include <QSharedDataPointer>
|
||||||
#include <QSqlQuery>
|
#include <QSqlQuery>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QMetaType>
|
||||||
|
|
||||||
#include "engine_fwd.h"
|
#include "engine_fwd.h"
|
||||||
|
|
||||||
@ -152,7 +153,9 @@ class Song {
|
|||||||
private:
|
private:
|
||||||
QSharedDataPointer<SongData> d;
|
QSharedDataPointer<SongData> d;
|
||||||
};
|
};
|
||||||
|
Q_DECLARE_METATYPE(Song);
|
||||||
|
|
||||||
typedef QList<Song> SongList;
|
typedef QList<Song> SongList;
|
||||||
|
Q_DECLARE_METATYPE(SongList);
|
||||||
|
|
||||||
#endif // SONG_H
|
#endif // SONG_H
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include <QtDebug>
|
#include <QtDebug>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
|
#include <QSignalSpy>
|
||||||
|
|
||||||
using ::testing::_;
|
using ::testing::_;
|
||||||
using ::testing::AtMost;
|
using ::testing::AtMost;
|
||||||
@ -75,51 +76,128 @@ TEST_F(LibraryBackendTest, EmptyDatabase) {
|
|||||||
EXPECT_TRUE(albums.isEmpty());
|
EXPECT_TRUE(albums.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(LibraryBackendTest, AddSong) {
|
TEST_F(LibraryBackendTest, AddDirectory) {
|
||||||
// Add a directory
|
QSignalSpy spy(backend_.get(), SIGNAL(DirectoriesDiscovered(DirectoryList)));
|
||||||
|
|
||||||
backend_->AddDirectory("/test");
|
backend_->AddDirectory("/test");
|
||||||
|
|
||||||
// Add the song
|
// Check the signal was emitted correctly
|
||||||
Song s = MakeDummySong(1);
|
ASSERT_EQ(1, spy.count());
|
||||||
s.set_title("Foo");
|
DirectoryList list = spy[0][0].value<DirectoryList>();
|
||||||
s.set_artist("Bar");
|
ASSERT_EQ(1, list.size());
|
||||||
s.set_album("Meep");
|
EXPECT_EQ("/test", list[0].path);
|
||||||
backend_->AddOrUpdateSongs(SongList() << s);
|
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();
|
QStringList artists = backend_->GetAllArtists();
|
||||||
ASSERT_EQ(1, artists.size());
|
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();
|
LibraryBackend::AlbumList albums = backend_->GetAllAlbums();
|
||||||
ASSERT_EQ(1, albums.size());
|
ASSERT_EQ(1, albums.size());
|
||||||
EXPECT_EQ(s.album(), albums[0].album_name);
|
EXPECT_EQ(song_.album(), albums[0].album_name);
|
||||||
EXPECT_EQ(s.artist(), albums[0].artist);
|
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());
|
ASSERT_EQ(1, albums.size());
|
||||||
EXPECT_EQ(s.album(), albums[0].album_name);
|
EXPECT_EQ(song_.album(), albums[0].album_name);
|
||||||
EXPECT_EQ(s.artist(), albums[0].artist);
|
EXPECT_EQ(song_.artist(), albums[0].artist);
|
||||||
|
}
|
||||||
|
|
||||||
LibraryBackend::Album album = backend_->GetAlbumArt("Bar", "Meep");
|
TEST_F(SingleSong, GetAlbumArt) {
|
||||||
EXPECT_EQ(s.album(), album.album_name);
|
LibraryBackend::Album album = backend_->GetAlbumArt("Artist", "Album");
|
||||||
EXPECT_EQ(s.artist(), album.artist);
|
EXPECT_EQ(song_.album(), album.album_name);
|
||||||
|
EXPECT_EQ(song_.artist(), album.artist);
|
||||||
|
}
|
||||||
|
|
||||||
// Check we can get the song back
|
TEST_F(SingleSong, GetSongs) {
|
||||||
SongList songs = backend_->GetSongs("Bar", "Meep");
|
SongList songs = backend_->GetSongs("Artist", "Album");
|
||||||
ASSERT_EQ(1, songs.size());
|
ASSERT_EQ(1, songs.size());
|
||||||
EXPECT_EQ(s.album(), songs[0].album());
|
EXPECT_EQ(song_.album(), songs[0].album());
|
||||||
EXPECT_EQ(s.artist(), songs[0].artist());
|
EXPECT_EQ(song_.artist(), songs[0].artist());
|
||||||
EXPECT_EQ(s.title(), songs[0].title());
|
EXPECT_EQ(song_.title(), songs[0].title());
|
||||||
EXPECT_EQ(1, songs[0].id());
|
EXPECT_EQ(1, songs[0].id());
|
||||||
|
}
|
||||||
|
|
||||||
// Check we can get the song by ID
|
TEST_F(SingleSong, GetSongById) {
|
||||||
Song song2 = backend_->GetSongById(1);
|
Song song = backend_->GetSongById(1);
|
||||||
EXPECT_EQ(s.album(), song2.album());
|
EXPECT_EQ(song_.album(), song.album());
|
||||||
EXPECT_EQ(s.artist(), song2.artist()); // This is an error - song2.artist() should obviously be "Blur"
|
EXPECT_EQ(song_.artist(), song.artist());
|
||||||
EXPECT_EQ(s.title(), song2.title());
|
EXPECT_EQ(song_.title(), song.title());
|
||||||
EXPECT_EQ(1, song2.id());
|
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) {
|
TEST_F(LibraryBackendTest, AddSongWithoutFilename) {
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
#include <gmock/gmock.h>
|
#include <gmock/gmock.h>
|
||||||
|
|
||||||
#include "resources_env.h"
|
#include "resources_env.h"
|
||||||
|
#include "metatypes_env.h"
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
testing::InitGoogleMock(&argc, argv);
|
testing::InitGoogleMock(&argc, argv);
|
||||||
|
|
||||||
testing::AddGlobalTestEnvironment(new ResourcesEnvironment);
|
testing::AddGlobalTestEnvironment(new ResourcesEnvironment);
|
||||||
|
testing::AddGlobalTestEnvironment(new MetatypesEnvironment);
|
||||||
|
|
||||||
return RUN_ALL_TESTS();
|
return RUN_ALL_TESTS();
|
||||||
}
|
}
|
||||||
|
19
tests/metatypes_env.h
Normal file
19
tests/metatypes_env.h
Normal 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
|
Loading…
x
Reference in New Issue
Block a user