Extend unit tests

This commit is contained in:
Jonas Kvinge 2021-09-26 00:08:45 +02:00
parent b952256988
commit ddfc9d911b
2 changed files with 267 additions and 37 deletions

View File

@ -116,34 +116,37 @@ TEST_F(CollectionBackendTest, AddInvalidSong) {
// Adding a song without certain fields set should fail // Adding a song without certain fields set should fail
backend_->AddDirectory("/tmp"); backend_->AddDirectory("/tmp");
Song s; Song s;
//s.set_url(QUrl::fromLocalFile("foo.flac")); s.set_url(QUrl::fromLocalFile("foo.flac"));
s.set_directory_id(1); s.set_directory_id(1);
QSignalSpy spy(database_.get(), &Database::Error); QSignalSpy spy(database_.get(), &Database::Error);
backend_->AddOrUpdateSongs(SongList() << s); backend_->AddOrUpdateSongs(SongList() << s);
//ASSERT_EQ(1, spy.count()); spy.takeFirst(); ASSERT_EQ(1, spy.count());
spy.takeFirst();
s.set_url(QUrl::fromLocalFile("foo.flac")); s.set_url(QUrl::fromLocalFile("foo.flac"));
backend_->AddOrUpdateSongs(SongList() << s); backend_->AddOrUpdateSongs(SongList() << s);
//ASSERT_EQ(1, spy.count()); spy.takeFirst(); ASSERT_EQ(1, spy.count());
spy.takeFirst();
s.set_filesize(100); s.set_filesize(100);
backend_->AddOrUpdateSongs(SongList() << s); backend_->AddOrUpdateSongs(SongList() << s);
//ASSERT_EQ(1, spy.count()); spy.takeFirst(); ASSERT_EQ(1, spy.count());
spy.takeFirst();
s.set_mtime(100); s.set_mtime(100);
backend_->AddOrUpdateSongs(SongList() << s); backend_->AddOrUpdateSongs(SongList() << s);
//ASSERT_EQ(1, spy.count()); spy.takeFirst(); ASSERT_EQ(1, spy.count());
spy.takeFirst();
s.set_ctime(100); s.set_ctime(100);
backend_->AddOrUpdateSongs(SongList() << s); backend_->AddOrUpdateSongs(SongList() << s);
//ASSERT_EQ(0, spy.count()); ASSERT_EQ(0, spy.count());
} }
TEST_F(CollectionBackendTest, GetAlbumArtNonExistent) { TEST_F(CollectionBackendTest, GetAlbumArtNonExistent) {}
}
// Test adding a single song to the database, then getting various information back about it. // Test adding a single song to the database, then getting various information back about it.
class SingleSong : public CollectionBackendTest { class SingleSong : public CollectionBackendTest {
@ -189,19 +192,21 @@ class SingleSong : public CollectionBackendTest {
TEST_F(SingleSong, GetSongWithNoAlbum) { TEST_F(SingleSong, GetSongWithNoAlbum) {
song_.set_album(""); song_.set_album("");
AddDummySong(); if (HasFatalFailure()) return; AddDummySong();
if (HasFatalFailure()) return;
EXPECT_EQ(1, backend_->GetAllArtists().size()); EXPECT_EQ(1, backend_->GetAllArtists().size());
//CollectionBackend::AlbumList albums = backend_->GetAllAlbums(); CollectionBackend::AlbumList albums = backend_->GetAllAlbums();
//EXPECT_EQ(1, albums.size()); EXPECT_EQ(1, albums.size());
//EXPECT_EQ("Artist", albums[0].artist); EXPECT_EQ("Artist", albums[0].album_artist);
//EXPECT_EQ("", albums[0].album); EXPECT_EQ("", albums[0].album);
} }
TEST_F(SingleSong, GetAllArtists) { TEST_F(SingleSong, GetAllArtists) {
AddDummySong(); if (HasFatalFailure()) return; AddDummySong();
if (HasFatalFailure()) return;
QStringList artists = backend_->GetAllArtists(); QStringList artists = backend_->GetAllArtists();
ASSERT_EQ(1, artists.size()); ASSERT_EQ(1, artists.size());
@ -211,7 +216,8 @@ TEST_F(SingleSong, GetAllArtists) {
TEST_F(SingleSong, GetAllAlbums) { TEST_F(SingleSong, GetAllAlbums) {
AddDummySong(); if (HasFatalFailure()) return; AddDummySong();
if (HasFatalFailure()) return;
CollectionBackend::AlbumList albums = backend_->GetAllAlbums(); CollectionBackend::AlbumList albums = backend_->GetAllAlbums();
ASSERT_EQ(1, albums.size()); ASSERT_EQ(1, albums.size());
@ -222,7 +228,8 @@ TEST_F(SingleSong, GetAllAlbums) {
TEST_F(SingleSong, GetAlbumsByArtist) { TEST_F(SingleSong, GetAlbumsByArtist) {
AddDummySong(); if (HasFatalFailure()) return; AddDummySong();
if (HasFatalFailure()) return;
CollectionBackend::AlbumList albums = backend_->GetAlbumsByArtist("Artist"); CollectionBackend::AlbumList albums = backend_->GetAlbumsByArtist("Artist");
ASSERT_EQ(1, albums.size()); ASSERT_EQ(1, albums.size());
@ -233,7 +240,8 @@ TEST_F(SingleSong, GetAlbumsByArtist) {
TEST_F(SingleSong, GetAlbumArt) { TEST_F(SingleSong, GetAlbumArt) {
AddDummySong(); if (HasFatalFailure()) return; AddDummySong();
if (HasFatalFailure()) return;
CollectionBackend::Album album = backend_->GetAlbumArt("Artist", "Album"); CollectionBackend::Album album = backend_->GetAlbumArt("Artist", "Album");
EXPECT_EQ(song_.album(), album.album); EXPECT_EQ(song_.album(), album.album);
@ -243,7 +251,8 @@ TEST_F(SingleSong, GetAlbumArt) {
TEST_F(SingleSong, GetSongs) { TEST_F(SingleSong, GetSongs) {
AddDummySong(); if (HasFatalFailure()) return; AddDummySong();
if (HasFatalFailure()) return;
SongList songs = backend_->GetAlbumSongs("Artist", "Album"); SongList songs = backend_->GetAlbumSongs("Artist", "Album");
ASSERT_EQ(1, songs.size()); ASSERT_EQ(1, songs.size());
@ -256,7 +265,8 @@ TEST_F(SingleSong, GetSongs) {
TEST_F(SingleSong, GetSongById) { TEST_F(SingleSong, GetSongById) {
AddDummySong(); if (HasFatalFailure()) return; AddDummySong();
if (HasFatalFailure()) return;
Song song = backend_->GetSongById(1); Song song = backend_->GetSongById(1);
EXPECT_EQ(song_.album(), song.album()); EXPECT_EQ(song_.album(), song.album());
@ -268,7 +278,8 @@ TEST_F(SingleSong, GetSongById) {
TEST_F(SingleSong, FindSongsInDirectory) { TEST_F(SingleSong, FindSongsInDirectory) {
AddDummySong(); if (HasFatalFailure()) return; AddDummySong();
if (HasFatalFailure()) return;
SongList songs = backend_->FindSongsInDirectory(1); SongList songs = backend_->FindSongsInDirectory(1);
ASSERT_EQ(1, songs.size()); ASSERT_EQ(1, songs.size());
@ -281,7 +292,8 @@ TEST_F(SingleSong, FindSongsInDirectory) {
TEST_F(SingleSong, UpdateSong) { TEST_F(SingleSong, UpdateSong) {
AddDummySong(); if (HasFatalFailure()) return; AddDummySong();
if (HasFatalFailure()) return;
Song new_song(song_); Song new_song(song_);
new_song.set_id(1); new_song.set_id(1);
@ -308,7 +320,8 @@ TEST_F(SingleSong, UpdateSong) {
TEST_F(SingleSong, DeleteSongs) { TEST_F(SingleSong, DeleteSongs) {
AddDummySong(); if (HasFatalFailure()) return; AddDummySong();
if (HasFatalFailure()) return;
Song new_song(song_); Song new_song(song_);
new_song.set_id(1); new_song.set_id(1);
@ -340,7 +353,8 @@ TEST_F(SingleSong, DeleteSongs) {
TEST_F(SingleSong, MarkSongsUnavailable) { TEST_F(SingleSong, MarkSongsUnavailable) {
AddDummySong(); if (HasFatalFailure()) return; AddDummySong();
if (HasFatalFailure()) return;
Song new_song(song_); Song new_song(song_);
new_song.set_id(1); new_song.set_id(1);
@ -370,7 +384,15 @@ TEST_F(SingleSong, MarkSongsUnavailable) {
} }
TEST_F(SingleSong, TestUrls) { class TestUrls : public CollectionBackendTest {
protected:
void SetUp() override {
CollectionBackendTest::SetUp();
backend_->AddDirectory("/mnt/music");
}
};
TEST_F(TestUrls, TestUrls) {
QStringList strings = QStringList() << "file:///mnt/music/01 - Pink Floyd - Echoes.flac" QStringList strings = QStringList() << "file:///mnt/music/01 - Pink Floyd - Echoes.flac"
<< "file:///mnt/music/02 - Björn Afzelius - Det räcker nu.flac" << "file:///mnt/music/02 - Björn Afzelius - Det räcker nu.flac"
@ -378,12 +400,11 @@ TEST_F(SingleSong, TestUrls) {
<< "file:///mnt/music/Test !#$%&'()-@^_`{}~..flac"; << "file:///mnt/music/Test !#$%&'()-@^_`{}~..flac";
QList<QUrl> urls = QUrl::fromStringList(strings); QList<QUrl> urls = QUrl::fromStringList(strings);
SongList songs;
for (const QUrl &url : urls) { for (const QUrl &url : urls) {
QString str = url.toString(QUrl::FullyEncoded); EXPECT_EQ(url, QUrl::fromEncoded(url.toString(QUrl::FullyEncoded).toUtf8()));
QUrl test_url = QUrl::fromEncoded(str.toUtf8()); EXPECT_EQ(url.toString(QUrl::FullyEncoded), url.toEncoded());
EXPECT_EQ(url, test_url);
Song song(Song::Source_Collection); Song song(Song::Source_Collection);
song.set_directory_id(1); song.set_directory_id(1);
@ -397,10 +418,22 @@ TEST_F(SingleSong, TestUrls) {
song.set_filesize(1); song.set_filesize(1);
song.set_valid(true); song.set_valid(true);
backend_->AddOrUpdateSongs(SongList() << song); songs << song;
if (HasFatalFailure()) continue;
SongList songs = backend_->GetSongsByUrl(url); }
QSignalSpy spy(backend_.get(), &CollectionBackend::SongsDiscovered);
backend_->AddOrUpdateSongs(songs);
if (HasFatalFailure()) return;
ASSERT_EQ(1, spy.count());
SongList new_songs = spy[0][0].value<SongList>();
EXPECT_EQ(new_songs.count(), strings.count());
for (const QUrl &url : urls) {
songs = backend_->GetSongsByUrl(url);
EXPECT_EQ(1, songs.count()); EXPECT_EQ(1, songs.count());
if (songs.count() < 1) continue; if (songs.count() < 1) continue;
@ -420,7 +453,7 @@ TEST_F(SingleSong, TestUrls) {
q.prepare(QString("SELECT url FROM %1 WHERE url = :url").arg(SCollection::kSongsTable)); q.prepare(QString("SELECT url FROM %1 WHERE url = :url").arg(SCollection::kSongsTable));
q.bindValue(":url", url.toString(QUrl::FullyEncoded)); q.bindValue(":url", url.toString(QUrl::FullyEncoded));
q.exec(); EXPECT_TRUE(q.exec());
while (q.next()) { while (q.next()) {
EXPECT_EQ(url, q.value(0).toUrl()); EXPECT_EQ(url, q.value(0).toUrl());
@ -431,7 +464,15 @@ TEST_F(SingleSong, TestUrls) {
} }
TEST_F(CollectionBackendTest, UpdateSongsBySongID) { class UpdateSongsBySongID : public CollectionBackendTest {
protected:
void SetUp() override {
CollectionBackendTest::SetUp();
backend_->AddDirectory("/mnt/music");
}
};
TEST_F(UpdateSongsBySongID, UpdateSongsBySongID) {
QStringList song_ids = QStringList() << "song1" QStringList song_ids = QStringList() << "song1"
<< "song2" << "song2"

View File

@ -2,6 +2,7 @@
* Strawberry Music Player * Strawberry Music Player
* This file was part of Clementine. * This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com> * Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
* *
* Strawberry is free software: you can redistribute it and/or modify * Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -27,25 +28,213 @@
#include "test_utils.h" #include "test_utils.h"
#include "core/utilities.h" #include "core/utilities.h"
#include "core/logging.h"
TEST(UtilitiesTest, PrettyTimeDelta) {
ASSERT_EQ(Utilities::PrettyTimeDelta(60), "+1:00");
ASSERT_EQ(Utilities::PrettyTimeDelta(3600), "+1:00:00");
ASSERT_EQ(Utilities::PrettyTimeDelta(9600), "+2:40:00");
}
TEST(UtilitiesTest, PrettyTime) {
ASSERT_EQ(Utilities::PrettyTime(60), "1:00");
ASSERT_EQ(Utilities::PrettyTime(3600), "1:00:00");
ASSERT_EQ(Utilities::PrettyTime(9600), "2:40:00");
}
TEST(UtilitiesTest, PrettyTimeNanosec) {}
TEST(UtilitiesTest, WordyTime) {
ASSERT_EQ(Utilities::WordyTime(787200), "9 days 2:40:00");
}
TEST(UtilitiesTest, WordyTimeNanosec) {}
TEST(UtilitiesTest, Ago) {
ASSERT_EQ(Utilities::Ago(QDateTime::currentDateTime().toSecsSinceEpoch() - 604800, QLocale()), "7 days ago");
}
TEST(UtilitiesTest, PrettyFutureDate) {}
TEST(UtilitiesTest, PrettySize) {
ASSERT_EQ(Utilities::PrettySize(787200), "787.2 KB");
}
TEST(UtilitiesTest, ColorToRgba) {
ASSERT_EQ(Utilities::ColorToRgba(QColor(33, 22, 128)), "rgba(33, 22, 128, 255)");
}
TEST(UtilitiesTest, HmacFunctions) { TEST(UtilitiesTest, HmacFunctions) {
QString key("key"); QString key("key");
QString data("The quick brown fox jumps over the lazy dog"); QString data("The quick brown fox jumps over the lazy dog");
// Test Hmac Md5
// Test Hmac MD5
QString result_hash_md5 = Utilities::HmacMd5(key.toLocal8Bit(), data.toLocal8Bit()).toHex(); QString result_hash_md5 = Utilities::HmacMd5(key.toLocal8Bit(), data.toLocal8Bit()).toHex();
bool result_md5 = result_hash_md5 == QString("80070713463e7749b90c2dc24911e275"); bool result_md5 = result_hash_md5 == QString("80070713463e7749b90c2dc24911e275");
EXPECT_TRUE(result_md5); EXPECT_TRUE(result_md5);
// Test Hmac Sha256
// Test Hmac SHA256
QString result_hash_sha256 = Utilities::HmacSha256(key.toLocal8Bit(), data.toLocal8Bit()).toHex(); QString result_hash_sha256 = Utilities::HmacSha256(key.toLocal8Bit(), data.toLocal8Bit()).toHex();
bool result_sha256 = result_hash_sha256 == QString("f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"); bool result_sha256 = result_hash_sha256 == QString("f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8");
EXPECT_TRUE(result_sha256); EXPECT_TRUE(result_sha256);
} }
TEST(UtilitiesTest, ParseRFC822DateTim) { TEST(UtilitiesTest, PrettySize2) {
ASSERT_EQ(Utilities::PrettySize(QSize(22, 32)), "22x32");
}
TEST(UtilitiesTest, ParseRFC822DateTime) {
QDateTime result_DateTime = Utilities::ParseRFC822DateTime(QString("22 Feb 2008 00:16:17 GMT")); QDateTime result_DateTime = Utilities::ParseRFC822DateTime(QString("22 Feb 2008 00:16:17 GMT"));
EXPECT_TRUE(result_DateTime.isValid()); EXPECT_TRUE(result_DateTime.isValid());
result_DateTime = Utilities::ParseRFC822DateTime(QString("Thu, 13 Dec 2012 13:27:52 +0000")); result_DateTime = Utilities::ParseRFC822DateTime(QString("Thu, 13 Dec 2012 13:27:52 +0000"));
EXPECT_TRUE(result_DateTime.isValid()); EXPECT_TRUE(result_DateTime.isValid());
result_DateTime = Utilities::ParseRFC822DateTime(QString("Mon, 12 March 2012 20:00:00 +0100")); result_DateTime = Utilities::ParseRFC822DateTime(QString("Mon, 12 March 2012 20:00:00 +0100"));
EXPECT_TRUE(result_DateTime.isValid()); EXPECT_TRUE(result_DateTime.isValid());
}
TEST(UtilitiesTest, DecodeHtmlEntities) {
ASSERT_EQ(Utilities::DecodeHtmlEntities("&amp;"), "&");
ASSERT_EQ(Utilities::DecodeHtmlEntities("&#38;"), "&");
ASSERT_EQ(Utilities::DecodeHtmlEntities("&quot;"), "\"");
ASSERT_EQ(Utilities::DecodeHtmlEntities("&#34;"), "\"");
ASSERT_EQ(Utilities::DecodeHtmlEntities("&apos;"), "'");
ASSERT_EQ(Utilities::DecodeHtmlEntities("&#39;"), "'");
ASSERT_EQ(Utilities::DecodeHtmlEntities("&lt;"), "<");
ASSERT_EQ(Utilities::DecodeHtmlEntities("&#60;"), "<");
ASSERT_EQ(Utilities::DecodeHtmlEntities("&gt;"), ">");
ASSERT_EQ(Utilities::DecodeHtmlEntities("&#62;"), ">");
}
TEST(UtilitiesTest, PathWithoutFilenameExtension) {
ASSERT_EQ(Utilities::PathWithoutFilenameExtension("/home/jonas/test/filename.txt"), "/home/jonas/test/filename");
}
TEST(UtilitiesTest, FiddleFileExtension) {
ASSERT_EQ(Utilities::FiddleFileExtension("/home/jonas/test/filename.txt", "db"), "/home/jonas/test/filename.db");
}
TEST(UtilitiesTest, SetEnvGetEnv) {
QString var = "STRAWBERRY_UNIT_TEST_" + Utilities::GetRandomStringWithCharsAndNumbers(20);
QString value = "STRAWBERRY_UNIT_TEST_" + Utilities::GetRandomStringWithCharsAndNumbers(20);
Utilities::SetEnv(var.toUtf8(), value);
ASSERT_EQ(Utilities::GetEnv(var), value);
Utilities::SetEnv(var.toUtf8(), "");
}
TEST(UtilitiesTest, Random) {
EXPECT_FALSE(Utilities::GetRandomStringWithChars(20) == Utilities::GetRandomStringWithChars(20));
EXPECT_FALSE(Utilities::GetRandomStringWithCharsAndNumbers(20) == Utilities::GetRandomStringWithCharsAndNumbers(20));
EXPECT_FALSE(Utilities::CryptographicRandomString(20) == Utilities::CryptographicRandomString(20));
EXPECT_FALSE(Utilities::GetRandomString(20, "&%XVBGQ") == Utilities::GetRandomString(20, "&%XVBGQ"));
}
TEST(UtilitiesTest, UnicodeToAscii) {
ASSERT_EQ(Utilities::UnicodeToAscii("ÆØÅ"), "AEOA");
}
TEST(UtilitiesTest, ReplaceVariable) {
Song song;
song.set_title(Utilities::GetRandomStringWithChars(8));
song.set_album(Utilities::GetRandomStringWithChars(8));
song.set_artist(Utilities::GetRandomStringWithChars(8));
song.set_albumartist(Utilities::GetRandomStringWithChars(8));
song.set_track(5);
song.set_disc(2);
song.set_year(1999);
song.set_originalyear(2000);
song.set_genre(Utilities::GetRandomStringWithChars(8));
song.set_composer(Utilities::GetRandomStringWithChars(8));
song.set_performer(Utilities::GetRandomStringWithChars(8));
song.set_grouping(Utilities::GetRandomStringWithChars(8));
song.set_length_nanosec(900000000000);
song.set_url(QUrl("file:///home/jonas/Music/test_song.flac"));
song.set_skipcount(20);
song.set_playcount(90);
song.set_rating(1.0);
ASSERT_EQ(Utilities::ReplaceVariable("%title%", song, ""), song.title());
ASSERT_EQ(Utilities::ReplaceVariable("%album%", song, ""), song.album());
ASSERT_EQ(Utilities::ReplaceVariable("%artist%", song, ""), song.artist());
ASSERT_EQ(Utilities::ReplaceVariable("%albumartist%", song, ""), song.effective_albumartist());
ASSERT_EQ(Utilities::ReplaceVariable("%track%", song, ""), QString::number(song.track()));
ASSERT_EQ(Utilities::ReplaceVariable("%disc%", song, ""), QString::number(song.disc()));
ASSERT_EQ(Utilities::ReplaceVariable("%year%", song, ""), QString::number(song.year()));
ASSERT_EQ(Utilities::ReplaceVariable("%originalyear%", song, ""), QString::number(song.originalyear()));
ASSERT_EQ(Utilities::ReplaceVariable("%genre%", song, ""), song.genre());
ASSERT_EQ(Utilities::ReplaceVariable("%composer%", song, ""), song.composer());
ASSERT_EQ(Utilities::ReplaceVariable("%performer%", song, ""), song.performer());
ASSERT_EQ(Utilities::ReplaceVariable("%grouping%", song, ""), song.grouping());
ASSERT_EQ(Utilities::ReplaceVariable("%length%", song, ""), song.PrettyLength());
ASSERT_EQ(Utilities::ReplaceVariable("%filename%", song, ""), song.basefilename());
ASSERT_EQ(Utilities::ReplaceVariable("%url%", song, ""), song.url().toString());
ASSERT_EQ(Utilities::ReplaceVariable("%playcount%", song, ""), QString::number(song.playcount()));
ASSERT_EQ(Utilities::ReplaceVariable("%skipcount%", song, ""), QString::number(song.skipcount()));
ASSERT_EQ(Utilities::ReplaceVariable("%rating%", song, ""), song.PrettyRating());
}
TEST(UtilitiesTest, ReplaceMessage) {
Song song;
song.set_title(Utilities::GetRandomStringWithChars(8));
song.set_album(Utilities::GetRandomStringWithChars(8));
song.set_artist(Utilities::GetRandomStringWithChars(8));
song.set_albumartist(Utilities::GetRandomStringWithChars(8));
song.set_track(5);
song.set_disc(2);
song.set_year(1999);
song.set_originalyear(2000);
song.set_genre(Utilities::GetRandomStringWithChars(8));
song.set_composer(Utilities::GetRandomStringWithChars(8));
song.set_performer(Utilities::GetRandomStringWithChars(8));
song.set_grouping(Utilities::GetRandomStringWithChars(8));
song.set_length_nanosec(900000000000);
song.set_url(QUrl("file:///home/jonas/Music/test_song.flac"));
song.set_skipcount(20);
song.set_playcount(90);
song.set_rating(1.0);
ASSERT_EQ(Utilities::ReplaceMessage("%title% - %artist%", song, ""), song.title() + " - " + song.artist());
} }