2010-03-01 23:00:15 +01:00
|
|
|
#include "test_utils.h"
|
|
|
|
#include "gtest/gtest.h"
|
2010-03-03 00:37:12 +01:00
|
|
|
#include "gmock/gmock.h"
|
2010-03-01 23:00:15 +01:00
|
|
|
|
|
|
|
#include "librarybackend.h"
|
2010-03-03 00:37:12 +01:00
|
|
|
#include "song.h"
|
2010-03-01 23:00:15 +01:00
|
|
|
|
|
|
|
#include <boost/scoped_ptr.hpp>
|
|
|
|
|
|
|
|
#include <QtDebug>
|
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
using ::testing::_;
|
2010-03-02 14:35:39 +01:00
|
|
|
using ::testing::AtMost;
|
2010-03-01 23:00:15 +01:00
|
|
|
using ::testing::Invoke;
|
|
|
|
using ::testing::Return;
|
|
|
|
|
|
|
|
void PrintTo(const ::QString& str, std::ostream& os) {
|
|
|
|
os << str.toStdString();
|
|
|
|
}
|
|
|
|
|
|
|
|
class LibraryBackendTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
virtual void SetUp() {
|
2010-03-03 00:37:12 +01:00
|
|
|
backend_.reset(new LibraryBackend(NULL, ":memory:"));
|
|
|
|
|
|
|
|
connection_name_ = "thread_" + QString::number(
|
|
|
|
reinterpret_cast<quint64>(QThread::currentThread()));
|
|
|
|
database_ = QSqlDatabase::database(connection_name_);
|
2010-03-01 23:00:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown() {
|
|
|
|
// Make sure Qt does not re-use the connection.
|
2010-03-03 00:37:12 +01:00
|
|
|
database_ = QSqlDatabase();
|
|
|
|
QSqlDatabase::removeDatabase(connection_name_);
|
2010-03-01 23:00:15 +01:00
|
|
|
}
|
|
|
|
|
2010-03-03 00:37:12 +01:00
|
|
|
Song MakeDummySong(int directory_id) {
|
|
|
|
// Returns a valid song with all the required fields set
|
|
|
|
Song ret;
|
|
|
|
ret.set_directory_id(directory_id);
|
|
|
|
ret.set_filename("foo.mp3");
|
|
|
|
ret.set_mtime(0);
|
|
|
|
ret.set_ctime(0);
|
|
|
|
ret.set_filesize(0);
|
|
|
|
return ret;
|
2010-03-02 14:35:39 +01:00
|
|
|
}
|
|
|
|
|
2010-03-03 00:37:12 +01:00
|
|
|
boost::scoped_ptr<LibraryBackend> backend_;
|
|
|
|
QString connection_name_;
|
|
|
|
QSqlDatabase database_;
|
2010-03-02 14:35:39 +01:00
|
|
|
};
|
2010-03-01 23:00:15 +01:00
|
|
|
|
|
|
|
TEST_F(LibraryBackendTest, DatabaseInitialises) {
|
2010-03-03 00:37:12 +01:00
|
|
|
// Check that these tables exist
|
|
|
|
QStringList tables = database_.tables();
|
|
|
|
EXPECT_TRUE(tables.contains("songs"));
|
|
|
|
EXPECT_TRUE(tables.contains("directories"));
|
|
|
|
ASSERT_TRUE(tables.contains("schema_version"));
|
|
|
|
|
|
|
|
// Check the schema version is correct
|
|
|
|
QSqlQuery q("SELECT version FROM schema_version", database_);
|
|
|
|
ASSERT_TRUE(q.exec());
|
|
|
|
ASSERT_TRUE(q.next());
|
|
|
|
EXPECT_EQ(2, q.value(0).toInt());
|
|
|
|
EXPECT_FALSE(q.next());
|
2010-03-01 23:00:15 +01:00
|
|
|
}
|
|
|
|
|
2010-03-03 00:37:12 +01:00
|
|
|
TEST_F(LibraryBackendTest, EmptyDatabase) {
|
|
|
|
// Check the database is empty to start with
|
|
|
|
QStringList artists = backend_->GetAllArtists();
|
|
|
|
EXPECT_TRUE(artists.isEmpty());
|
2010-03-02 14:35:39 +01:00
|
|
|
|
2010-03-03 00:37:12 +01:00
|
|
|
LibraryBackend::AlbumList albums = backend_->GetAllAlbums();
|
|
|
|
EXPECT_TRUE(albums.isEmpty());
|
2010-03-01 23:00:15 +01:00
|
|
|
}
|
2010-03-02 14:35:39 +01:00
|
|
|
|
2010-03-03 00:37:12 +01:00
|
|
|
TEST_F(LibraryBackendTest, AddSong) {
|
|
|
|
// Add a directory
|
|
|
|
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 artist
|
|
|
|
QStringList artists = backend_->GetAllArtists();
|
|
|
|
ASSERT_EQ(1, artists.size());
|
|
|
|
EXPECT_EQ(s.artist(), artists[0]);
|
|
|
|
|
|
|
|
// Check the various album getters
|
|
|
|
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);
|
|
|
|
|
|
|
|
albums = backend_->GetAlbumsByArtist("Bar");
|
|
|
|
ASSERT_EQ(1, albums.size());
|
|
|
|
EXPECT_EQ(s.album(), albums[0].album_name);
|
|
|
|
EXPECT_EQ(s.artist(), albums[0].artist);
|
|
|
|
|
|
|
|
LibraryBackend::Album album = backend_->GetAlbumArt("Bar", "Meep");
|
|
|
|
EXPECT_EQ(s.album(), album.album_name);
|
|
|
|
EXPECT_EQ(s.artist(), album.artist);
|
|
|
|
|
|
|
|
// Check we can get the song back
|
|
|
|
SongList songs = backend_->GetSongs("Bar", "Meep");
|
|
|
|
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(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());
|
|
|
|
}
|
2010-03-02 14:35:39 +01:00
|
|
|
|
2010-03-03 00:37:12 +01:00
|
|
|
TEST_F(LibraryBackendTest, AddSongWithoutFilename) {
|
|
|
|
}
|
2010-03-02 14:35:39 +01:00
|
|
|
|
2010-03-03 00:37:12 +01:00
|
|
|
TEST_F(LibraryBackendTest, GetAlbumArtNonExistent) {
|
2010-03-02 14:35:39 +01:00
|
|
|
}
|