Commit 02346c13 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Tests: Simplify test classes creation

Use a default FS mock for all tests
Do not duplicate setup code
parent 8bc3ae5f
#include "gtest/gtest.h"
#include "Tests.h"
#include "IAlbum.h"
#include "IAlbumTrack.h"
#include "IFile.h"
#include "IMediaLibrary.h"
class Albums : public testing::Test
class Albums : public Tests
{
public:
static std::unique_ptr<IMediaLibrary> ml;
protected:
virtual void SetUp()
{
ml.reset( MediaLibraryFactory::create() );
bool res = ml->initialize( "test.db" );
ASSERT_TRUE( res );
}
virtual void TearDown()
{
ml.reset();
unlink("test.db");
}
};
std::unique_ptr<IMediaLibrary> Albums::ml;
TEST_F( Albums, Create )
{
auto a = ml->createAlbum( "album" );
......@@ -42,7 +24,7 @@ TEST_F( Albums, Fetch )
auto a = ml->createAlbum( "album" );
// Clear the cache
SetUp();
Reload();
auto a2 = ml->album( "album" );
// The shared pointer are expected to point to a different instance
......@@ -61,7 +43,7 @@ TEST_F( Albums, AddTrack )
ASSERT_EQ( tracks.size(), 1u );
ASSERT_EQ( tracks[0], track );
SetUp();
Reload();
a = ml->album( "albumtag" );
tracks = a->tracks();
......@@ -81,7 +63,7 @@ TEST_F( Albums, AssignTrack )
ASSERT_NE( f->albumTrack(), nullptr );
ASSERT_EQ( f->albumTrack(), t );
SetUp();
Reload();
f = ml->file( "file" );
t = f->albumTrack();
......@@ -111,7 +93,7 @@ TEST_F( Albums, SetGenre )
t->setGenre( "happy underground post progressive death metal" );
ASSERT_EQ( t->genre(), "happy underground post progressive death metal" );
SetUp();
Reload();
a = ml->album( "album" );
auto tracks = a->tracks();
......@@ -126,7 +108,7 @@ TEST_F( Albums, SetReleaseDate )
a->setReleaseDate( 1234 );
ASSERT_EQ( a->releaseDate(), 1234 );
SetUp();
Reload();
auto a2 = ml->album( "album" );
ASSERT_EQ( a->releaseDate(), a2->releaseDate() );
......@@ -139,7 +121,7 @@ TEST_F( Albums, SetShortSummary )
a->setShortSummary( "summary" );
ASSERT_EQ( a->shortSummary(), "summary" );
SetUp();
Reload();
auto a2 = ml->album( "album" );
ASSERT_EQ( a->shortSummary(), a2->shortSummary() );
......@@ -152,7 +134,7 @@ TEST_F( Albums, SetArtworkUrl )
a->setArtworkUrl( "artwork" );
ASSERT_EQ( a->artworkUrl(), "artwork" );
SetUp();
Reload();
auto a2 = ml->album( "album" );
ASSERT_EQ( a->artworkUrl(), a2->artworkUrl() );
......@@ -166,7 +148,7 @@ TEST_F( Albums, SetArtist )
t->setArtist( "The undead otters" );
ASSERT_EQ( t->artist(), "The undead otters" );
SetUp();
Reload();
auto a2 = ml->album( "album" );
auto tracks = a2->tracks();
......@@ -182,7 +164,7 @@ TEST_F( Albums, FetchAlbumFromTrack )
auto t = a->addTrack( "track 1", 1 );
f->setAlbumTrack( t );
}
SetUp();
Reload();
auto f = ml->file( "file" );
auto t = f->albumTrack();
......
#include "gtest/gtest.h"
#include "Tests.h"
#include "IFile.h"
#include "IAudioTrack.h"
class AudioTracks : public testing::Test
class AudioTracks : public Tests
{
public:
static std::unique_ptr<IMediaLibrary> ml;
protected:
virtual void SetUp()
{
ml.reset( MediaLibraryFactory::create() );
bool res = ml->initialize( "test.db" );
ASSERT_TRUE( res );
}
virtual void TearDown()
{
ml.reset();
unlink("test.db");
}
};
std::unique_ptr<IMediaLibrary> AudioTracks::ml;
TEST_F( AudioTracks, AddTrack )
{
auto f = ml->addFile( "file" );
......
......@@ -43,7 +43,7 @@ list(APPEND TEST_SRCS
VideoTracks.cpp
AudioTracks.cpp
VLCMetadataServices.cpp
FsUtilsTests.cpp
FsUtilsTests.cpp
)
add_executable(unittest ${TEST_SRCS})
......
#include "gtest/gtest.h"
#include "Tests.h"
#include "IMediaLibrary.h"
#include "IFile.h"
class Files : public testing::Test
class Files : public Tests
{
public:
static std::unique_ptr<IMediaLibrary> ml;
protected:
virtual void SetUp()
{
ml.reset( MediaLibraryFactory::create() );
bool res = ml->initialize( "test.db" );
ASSERT_TRUE( res );
}
virtual void TearDown()
{
ml.reset();
unlink("test.db");
}
};
std::unique_ptr<IMediaLibrary> Files::ml;
TEST_F( Files, Init )
{
......@@ -53,7 +36,7 @@ TEST_F( Files, Fetch )
ASSERT_EQ( f, f2 );
// Flush cache and fetch from DB
SetUp();
Reload();
f2 = ml->file( "/dev/null" );
ASSERT_EQ( f->mrl(), f2->mrl() );
......@@ -89,7 +72,7 @@ TEST_F( Files, LastModificationDate )
auto f = ml->addFile( "/dev/seaotter" );
ASSERT_NE( 0u, f->lastModificationDate() );
SetUp();
Reload();
auto f2 = ml->file( "/dev/seaotter" );
ASSERT_EQ( f->lastModificationDate(), f2->lastModificationDate() );
}
#include "gtest/gtest.h"
#include "Tests.h"
#include "IFile.h"
#include "IFolder.h"
......@@ -243,34 +243,24 @@ struct FileSystemFactory : public factory::IFileSystem
}
class Folders : public testing::Test
class Folders : public Tests
{
public:
static std::unique_ptr<IMediaLibrary> ml;
protected:
std::shared_ptr<mock::FileSystemFactory> fsMock;
protected:
virtual void Reload()
{
ml.reset( MediaLibraryFactory::create() );
bool res = ml->initialize( "test.db", fsMock );
ASSERT_TRUE( res );
}
virtual void SetUp()
virtual void SetUp() override
{
fsMock.reset( new mock::FileSystemFactory );
Reload();
Tests::Reload( fsMock );
}
virtual void TearDown()
virtual void Reload()
{
ml.reset();
unlink("test.db");
Tests::Reload( fsMock );
}
};
std::unique_ptr<IMediaLibrary> Folders::ml;
};
TEST_F( Folders, Add )
{
......@@ -307,7 +297,7 @@ TEST_F( Folders, Delete )
auto file = ml->file( filePath );
ASSERT_EQ( nullptr, file );
SetUp();
Reload();
// Recheck folder deletion from DB:
f = ml->folder( folderPath );
......@@ -319,7 +309,7 @@ TEST_F( Folders, Load )
auto f = ml->addFolder( "." );
ASSERT_NE( f, nullptr );
SetUp();
Reload();
auto files = ml->files();
ASSERT_EQ( files.size(), 3u );
......@@ -344,7 +334,7 @@ TEST_F( Folders, List )
auto files = f->files();
ASSERT_EQ( files.size(), 2u );
SetUp();
Reload();
f = ml->folder( f->path() );
files = f->files();
......@@ -371,7 +361,7 @@ TEST_F( Folders, ListFolders )
ASSERT_EQ( std::string{ mock::FileSystemFactory::SubFolder } + "subfile.mp4", file->mrl() );
// Now again, without cache
SetUp();
Reload();
f = ml->folder( f->path() );
subFolders = f->folders();
......@@ -392,7 +382,7 @@ TEST_F( Folders, LastModificationDate )
auto subFolders = f->folders();
ASSERT_NE( 0u, subFolders[0]->lastModificationDate() );
SetUp();
Reload();
f = ml->folder( f->path() );
ASSERT_NE( 0u, f->lastModificationDate() );
......
#include "gtest/gtest.h"
#include "Tests.h"
#include "IMediaLibrary.h"
#include "IFile.h"
#include "ILabel.h"
class Labels : public testing::Test
class Labels : public Tests
{
public:
static std::unique_ptr<IMediaLibrary> ml;
protected:
virtual void SetUp()
{
ml.reset( MediaLibraryFactory::create() );
bool res = ml->initialize( "test.db" );
ASSERT_TRUE( res );
}
virtual void TearDown()
{
ml.reset();
unlink("test.db");
}
};
std::unique_ptr<IMediaLibrary> Labels::ml;
TEST_F( Labels, Add )
{
auto f = ml->addFile( "/dev/null" );
......
#include "gtest/gtest.h"
#include "Tests.h"
#include "IMediaLibrary.h"
#include "IMovie.h"
#include "IFile.h"
class Movies : public testing::Test
class Movies : public Tests
{
public:
static std::unique_ptr<IMediaLibrary> ml;
protected:
virtual void SetUp()
{
ml.reset( MediaLibraryFactory::create() );
bool res = ml->initialize( "test.db" );
ASSERT_TRUE( res );
}
virtual void TearDown()
{
ml.reset();
unlink("test.db");
}
};
std::unique_ptr<IMediaLibrary> Movies::ml;
TEST_F( Movies, Create )
{
auto m = ml->createMovie( "movie" );
......@@ -40,7 +22,7 @@ TEST_F( Movies, Fetch )
ASSERT_EQ( m, m2 );
SetUp();
Reload();
m2 = ml->movie( "movie" );
ASSERT_NE( m2, nullptr );
......@@ -54,7 +36,7 @@ TEST_F( Movies, SetReleaseDate )
m->setReleaseDate( 1234 );
ASSERT_EQ( m->releaseDate(), 1234u );
SetUp();
Reload();
m = ml->movie( "movie" );
ASSERT_EQ( m->releaseDate(), 1234u );
......@@ -67,7 +49,7 @@ TEST_F( Movies, SetShortSummary )
m->setShortSummary( "great movie" );
ASSERT_EQ( m->shortSummary(), "great movie" );
SetUp();
Reload();
m = ml->movie( "movie" );
ASSERT_EQ( m->shortSummary(), "great movie" );
......@@ -80,7 +62,7 @@ TEST_F( Movies, SetArtworkUrl )
m->setArtworkUrl( "artwork" );
ASSERT_EQ( m->artworkUrl(), "artwork" );
SetUp();
Reload();
m = ml->movie( "movie" );
ASSERT_EQ( m->artworkUrl(), "artwork" );
......@@ -93,7 +75,7 @@ TEST_F( Movies, SetImdbId )
m->setImdbId( "id" );
ASSERT_EQ( m->imdbId(), "id" );
SetUp();
Reload();
m = ml->movie( "movie" );
ASSERT_EQ( m->imdbId(), "id" );
......@@ -108,7 +90,7 @@ TEST_F( Movies, AssignToFile )
f->setMovie( m );
ASSERT_EQ( f->movie(), m );
SetUp();
Reload();
f = ml->file( "file" );
m = f->movie();
......@@ -127,7 +109,7 @@ TEST_F( Movies, DestroyMovie )
f = ml->file( "file" );
ASSERT_EQ( f, nullptr );
SetUp();
Reload();
f = ml->file( "file" );
ASSERT_EQ( f, nullptr );
......
#include "gtest/gtest.h"
#include "Tests.h"
#include "IFile.h"
#include "IMediaLibrary.h"
#include "IShow.h"
#include "IShowEpisode.h"
class Shows : public testing::Test
class Shows : public Tests
{
public:
static std::unique_ptr<IMediaLibrary> ml;
protected:
virtual void SetUp()
{
ml.reset( MediaLibraryFactory::create() );
bool res = ml->initialize( "test.db" );
ASSERT_TRUE( res );
}
virtual void TearDown()
{
ml.reset();
unlink("test.db");
}
};
std::unique_ptr<IMediaLibrary> Shows::ml;
TEST_F( Shows, Create )
{
auto s = ml->createShow( "show" );
......@@ -41,7 +23,7 @@ TEST_F( Shows, Fetch )
auto s = ml->createShow( "show" );
// Clear the cache
SetUp();
Reload();
auto s2 = ml->show( "show" );
// The shared pointers are expected to point to different instances
......@@ -57,7 +39,7 @@ TEST_F( Shows, SetReleaseDate )
s->setReleaseDate( 1234 );
ASSERT_EQ( s->releaseDate(), 1234 );
SetUp();
Reload();
auto s2 = ml->show( "show" );
ASSERT_EQ( s->releaseDate(), s2->releaseDate() );
......@@ -71,7 +53,7 @@ TEST_F( Shows, SetShortSummary )
ASSERT_EQ( s->shortSummary(), "summary" );
SetUp();
Reload();
auto s2 = ml->show( "show" );
ASSERT_EQ( s->shortSummary(), s2->shortSummary() );
......@@ -84,7 +66,7 @@ TEST_F( Shows, SetArtworkUrl )
s->setArtworkUrl( "artwork" );
ASSERT_EQ( s->artworkUrl(), "artwork" );
SetUp();
Reload();
auto s2 = ml->show( "show" );
ASSERT_EQ( s->artworkUrl(), s2->artworkUrl() );
......@@ -97,7 +79,7 @@ TEST_F( Shows, SetTvdbId )
s->setTvdbId( "TVDBID" );
ASSERT_EQ( s->tvdbId(), "TVDBID" );
SetUp();
Reload();
auto s2 = ml->show( "show" );
ASSERT_EQ( s->tvdbId(), s2->tvdbId() );
......@@ -134,7 +116,7 @@ TEST_F( Shows, FetchShowFromEpisode )
ASSERT_NE( s2, nullptr );
ASSERT_EQ( s, s2 );
SetUp();
Reload();
f = ml->file( "file" );
s2 = f->showEpisode()->show();
......@@ -150,7 +132,7 @@ TEST_F( Shows, SetEpisodeArtwork )
ASSERT_TRUE( res );
ASSERT_EQ( e->artworkUrl(), "path-to-snapshot" );
SetUp();
Reload();
show = ml->show( "show" );
auto episodes = show->episodes();
......@@ -165,7 +147,7 @@ TEST_F( Shows, SetEpisodeSeasonNumber )
ASSERT_TRUE( res );
ASSERT_EQ( e->seasonNumber(), 42u );
SetUp();
Reload();
show = ml->show( "show" );
auto episodes = show->episodes();
......@@ -180,7 +162,7 @@ TEST_F( Shows, SetEpisodeSummary )
ASSERT_TRUE( res );
ASSERT_EQ( e->shortSummary(), "Insert spoilers here" );
SetUp();
Reload();
show = ml->show( "show" );
auto episodes = show->episodes();
......@@ -195,7 +177,7 @@ TEST_F( Shows, SetEpisodeTvdbId )
ASSERT_TRUE( res );
ASSERT_EQ( e->tvdbId(), "TVDBID" );
SetUp();
Reload();
show = ml->show( "show" );
auto episodes = show->episodes();
......@@ -216,7 +198,7 @@ TEST_F( Shows, FileSetShowEpisode )
f->setShowEpisode( e );
ASSERT_EQ( f->showEpisode(), e );
SetUp();
Reload();
f = ml->file( "file" );
e = f->showEpisode();
......@@ -236,7 +218,7 @@ TEST_F( Shows, DeleteShowEpisode )
f = ml->file( "file" );
ASSERT_EQ( f, nullptr );
SetUp();
Reload();
f = ml->file( "file" );
ASSERT_EQ( f, nullptr );
......
#include "gtest/gtest.h"
#include "Tests.h"
#include "filesystem/IFile.h"
#include "filesystem/IDirectory.h"
#include "Utils.h"
class TestEnv : public ::testing::Environment
{
......@@ -10,4 +13,87 @@ class TestEnv : public ::testing::Environment
}
};
namespace mock
{
namespace defaults
{
class File : public fs::IFile
{
std::string m_path;
std::string m_fileName;
std::string m_extension;
public:
File( const std::string& file )
: m_path( file )
, m_fileName( utils::file::fileName( file ) )
, m_extension( utils::file::extension( file ) )
{
}
virtual const std::string& name() const
{
return m_fileName;
}
virtual const std::string& path() const
{
return m_path;
}
virtual const std::string& fullPath() const
{
return m_path;
}
virtual const std::string& extension() const
{
return m_extension;
}
virtual unsigned int lastModificationDate() const
{
// Ensure a non-0 value so tests can easily verify that the value
// is initialized
return 123;
}
};
class FileSystemFactory : public factory::IFileSystem
{
virtual std::unique_ptr<fs::IDirectory> createDirectory(const std::string&)
{
return nullptr;
}
virtual std::unique_ptr<fs::IFile> createFile(const std::string& fileName)
{
return std::unique_ptr<fs::IFile>( new File( fileName ) );
}
};
}
}
void Tests::TearDown()
{
ml.reset();
unlink("test.db");
}
void Tests::Reload(std::shared_ptr<factory::IFileSystem> fs /* = nullptr */ )
{
ml.reset( MediaLibraryFactory::create() );
bool res = ml->initialize( "test.db", fs ? fs : defaultFs );
ASSERT_TRUE( res );
}