Commit 3750a1ce authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Rework test file creations to get rid of stand alone files concept

parent 994c490d
......@@ -275,11 +275,6 @@ bool Media::save()
return true;
}
bool Media::isStandAlone()
{
return m_folderId == 0;
}
unsigned int Media::lastModificationDate()
{
return m_lastModificationDate;
......
......@@ -91,7 +91,6 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
void setSnapshot( const std::string& snapshot );
bool save();
bool isStandAlone();
unsigned int lastModificationDate();
/// Explicitely mark a file as fully parsed, meaning no metadata service needs to run anymore.
......
......@@ -180,7 +180,7 @@ std::vector<MediaPtr> MediaLibrary::videoFiles()
return Media::fetchAll<IMedia>( m_dbConnection.get(), req, IMedia::Type::VideoType );
}
std::shared_ptr<Media> MediaLibrary::addFile( const std::string& path, Folder* parentFolder, fs::IDirectory* parentFolderFs )
std::shared_ptr<Media> MediaLibrary::addFile( const std::string& path, Folder& parentFolder, fs::IDirectory& parentFolderFs )
{
std::unique_ptr<fs::IFile> file;
try
......@@ -213,9 +213,8 @@ std::shared_ptr<Media> MediaLibrary::addFile( const std::string& path, Folder* p
return nullptr;
LOG_INFO( "Adding ", path );
auto fptr = Media::create( m_dbConnection.get(), type, file.get(),
parentFolder != nullptr ? parentFolder->id() : 0,
parentFolderFs != nullptr ? parentFolderFs->device()->isRemovable() : false );
auto fptr = Media::create( m_dbConnection.get(), type, file.get(), parentFolder.id(),
parentFolderFs.device()->isRemovable() );
if ( fptr == nullptr )
{
LOG_ERROR( "Failed to add file ", file->fullPath(), " to the media library" );
......
......@@ -53,7 +53,7 @@ class MediaLibrary : public IMediaLibrary
std::vector<MediaPtr> files();
virtual std::vector<MediaPtr> audioFiles() override;
virtual std::vector<MediaPtr> videoFiles() override;
std::shared_ptr<Media> addFile(const std::string& path, Folder* parentFolder, fs::IDirectory* parentFolderFs);
std::shared_ptr<Media> addFile( const std::string& path, Folder& parentFolder, fs::IDirectory& parentFolderFs );
virtual bool deleteFile(const Media* file );
bool deleteFolder( const Folder* folder );
......
......@@ -66,7 +66,7 @@ bool FsDiscoverer::discover( const std::string &entryPoint )
auto blist = blacklist();
if ( isBlacklisted( *fsDir, blist ) == true )
return false;
return addFolder( fsDir.get(), nullptr, blist );
return addFolder( *fsDir, nullptr, blist );
}
void FsDiscoverer::reload()
......@@ -84,8 +84,8 @@ void FsDiscoverer::reload()
m_ml->deleteFolder( f.get() );
continue;
}
checkSubfolders( folder.get(), f.get(), blist );
checkFiles( folder.get(), f.get() );
checkSubfolders( *folder, *f, blist );
checkFiles( *folder, *f );
}
}
......@@ -108,12 +108,12 @@ void FsDiscoverer::checkDevices()
}
}
bool FsDiscoverer::checkSubfolders( fs::IDirectory* folder, Folder* parentFolder, const std::vector<std::shared_ptr<Folder>> blacklist ) const
bool FsDiscoverer::checkSubfolders( fs::IDirectory& folder, Folder& parentFolder, const std::vector<std::shared_ptr<Folder>> blacklist ) const
{
// Load the folders we already know of:
LOG_INFO( "Checking for modifications in ", folder->path() );
auto subFoldersInDB = Folder::fetchAll( m_dbConn, parentFolder->id() );
for ( const auto& subFolderPath : folder->dirs() )
LOG_INFO( "Checking for modifications in ", folder.path() );
auto subFoldersInDB = Folder::fetchAll( m_dbConn, parentFolder.id() );
for ( const auto& subFolderPath : folder.dirs() )
{
auto subFolder = m_fsFactory->createDirectory( subFolderPath );
if ( subFolder == nullptr )
......@@ -132,15 +132,15 @@ bool FsDiscoverer::checkSubfolders( fs::IDirectory* folder, Folder* parentFolder
continue;
}
LOG_INFO( "New folder detected: ", subFolderPath );
addFolder( subFolder.get(), parentFolder, blacklist );
addFolder( *subFolder, &parentFolder, blacklist );
continue;
}
auto folderInDb = *it;
// In any case, check for modifications, as a change related to a mountpoint might
// not update the folder modification date.
// Also, relying on the modification date probably isn't portable
checkSubfolders( subFolder.get(), folderInDb.get(), blacklist );
checkFiles( subFolder.get(), folderInDb.get() );
checkSubfolders( *subFolder, *folderInDb, blacklist );
checkFiles( *subFolder, *folderInDb );
subFoldersInDB.erase( it );
}
// Now all folders we had in DB but haven't seen from the FS must have been deleted.
......@@ -149,17 +149,17 @@ bool FsDiscoverer::checkSubfolders( fs::IDirectory* folder, Folder* parentFolder
LOG_INFO( "Folder ", f->path(), " not found in FS, deleting it" );
m_ml->deleteFolder( f.get() );
}
LOG_INFO( "Done checking subfolders in ", folder->path() );
LOG_INFO( "Done checking subfolders in ", folder.path() );
return true;
}
void FsDiscoverer::checkFiles( fs::IDirectory* folder, Folder* parentFolder ) const
void FsDiscoverer::checkFiles( fs::IDirectory& folder, Folder& parentFolder ) const
{
LOG_INFO( "Checking file in ", folder->path() );
LOG_INFO( "Checking file in ", folder.path() );
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name
+ " WHERE folder_id = ?";
auto files = Media::fetchAll<Media>( m_dbConn, req, parentFolder->id() );
for ( const auto& filePath : folder->files() )
auto files = Media::fetchAll<Media>( m_dbConn, req, parentFolder.id() );
for ( const auto& filePath : folder.files() )
{
auto it = std::find_if( begin( files ), end( files ), [filePath](const std::shared_ptr<IMedia>& f) {
return f->mrl() == filePath;
......@@ -186,7 +186,7 @@ void FsDiscoverer::checkFiles( fs::IDirectory* folder, Folder* parentFolder ) co
LOG_INFO( "File ", file->mrl(), " not found on filesystem, deleting it" );
m_ml->deleteFile( file.get() );
}
LOG_INFO( "Done checking files ", folder->path() );
LOG_INFO( "Done checking files ", folder.path() );
}
std::vector<std::shared_ptr<Folder> > FsDiscoverer::blacklist() const
......@@ -210,9 +210,9 @@ bool FsDiscoverer::isBlacklisted( const fs::IDirectory& directory, const std::ve
}) != end( blacklist );
}
bool FsDiscoverer::addFolder( fs::IDirectory* folder, Folder* parentFolder, const std::vector<std::shared_ptr<Folder>>& blacklist ) const
bool FsDiscoverer::addFolder( fs::IDirectory& folder, Folder* parentFolder, const std::vector<std::shared_ptr<Folder>>& blacklist ) const
{
auto deviceFs = folder->device();
auto deviceFs = folder.device();
// We are creating a folder, there has to be a device containing it.
assert( deviceFs != nullptr );
auto device = Device::fromUuid( m_dbConn, deviceFs->uuid() );
......@@ -222,12 +222,13 @@ bool FsDiscoverer::addFolder( fs::IDirectory* folder, Folder* parentFolder, cons
device = Device::create( m_dbConn, deviceFs->uuid(), deviceFs->isRemovable() );
}
auto f = Folder::create( m_dbConn, folder->path(), parentFolder != nullptr ? parentFolder->id() : 0,
auto f = Folder::create( m_dbConn, folder.path(),
parentFolder != nullptr ? parentFolder->id() : 0,
*device, *deviceFs );
if ( f == nullptr )
return false;
checkFiles( folder, f.get() );
checkSubfolders( folder, f.get(), blacklist );
checkFiles( folder, *f );
checkSubfolders( folder, *f, blacklist );
return true;
}
......@@ -39,11 +39,11 @@ public:
virtual void reload() override;
private:
bool checkSubfolders( fs::IDirectory *folder, Folder* parentFolder, const std::vector<std::shared_ptr<Folder>> blacklist ) const;
void checkFiles( fs::IDirectory *folder, Folder* parentFolder ) const;
bool checkSubfolders(fs::IDirectory& folder, Folder& parentFolder, const std::vector<std::shared_ptr<Folder>> blacklist ) const;
void checkFiles(fs::IDirectory& folder, Folder& parentFolder ) const;
std::vector<std::shared_ptr<Folder>> blacklist() const;
bool isBlacklisted( const fs::IDirectory& directory, const std::vector<std::shared_ptr<Folder>>& blacklist ) const;
bool addFolder(fs::IDirectory* folder, Folder* parentFolder, const std::vector<std::shared_ptr<Folder> >& blacklist ) const;
bool addFolder(fs::IDirectory& folder, Folder* parentFolder, const std::vector<std::shared_ptr<Folder> >& blacklist ) const;
void checkDevices();
private:
......
......@@ -534,6 +534,54 @@ public:
}
};
class NoopDevice : public fs::IDevice
{
public:
virtual const std::string&uuid() const override
{
assert(false);
}
virtual bool isRemovable() const override
{
return false;
}
virtual bool isPresent() const override
{
return true;
}
virtual const std::string& mountpoint() const override
{
assert(false);
}
};
// We just need a valid instance of this one
class NoopDirectory : public fs::IDirectory
{
virtual const std::string& path() const override
{
assert(false);
}
virtual const std::vector<std::string>&files() override
{
assert(false);
}
virtual const std::vector<std::string>&dirs() override
{
assert(false);
}
virtual std::shared_ptr<fs::IDevice> device() const override
{
return std::make_shared<NoopDevice>();
}
};
class NoopFsFactory : public factory::IFileSystem
{
public:
......
......@@ -59,7 +59,7 @@ TEST_F( Albums, Fetch )
TEST_F( Albums, AddTrack )
{
auto a = ml->createAlbum( "albumtag" );
auto f = ml->addFile( "track.mp3", nullptr, nullptr );
auto f = ml->addFile( "track.mp3" );
auto track = a->addTrack( f, 10, 0 );
ASSERT_NE( track, nullptr );
......@@ -79,7 +79,7 @@ TEST_F( Albums, NbTracks )
auto a = ml->createAlbum( "albumtag" );
for ( auto i = 1u; i <= 10; ++i )
{
auto f = ml->addFile( "track" + std::to_string(i) + ".mp3", nullptr, nullptr );
auto f = ml->addFile( "track" + std::to_string(i) + ".mp3" );
auto track = a->addTrack( f, i, i );
ASSERT_NE( track, nullptr );
}
......@@ -96,7 +96,7 @@ TEST_F( Albums, NbTracks )
TEST_F( Albums, SetGenre )
{
auto a = ml->createAlbum( "album" );
auto f = ml->addFile( "track.mp3", nullptr, nullptr );
auto f = ml->addFile( "track.mp3" );
auto t = a->addTrack( f, 1, 0 );
t->setGenre( "happy underground post progressive death metal" );
......@@ -167,7 +167,7 @@ TEST_F( Albums, SetArtworkUrl )
TEST_F( Albums, FetchAlbumFromTrack )
{
auto a = ml->createAlbum( "album" );
auto f = ml->addFile( "file.mp3", nullptr, nullptr );
auto f = ml->addFile( "file.mp3" );
auto t = a->addTrack( f, 1, 0 );
f->setAlbumTrack( t );
......
......@@ -34,7 +34,7 @@ class AlbumTracks : public Tests
TEST_F( AlbumTracks, Create )
{
auto album = ml->createAlbum( "album" );
auto f = ml->addFile( "track1.mp3", nullptr, nullptr );
auto f = ml->addFile( "track1.mp3" );
auto track = album->addTrack( f, 1, 10 );
ASSERT_NE( nullptr, track );
ASSERT_EQ( 10u, track->discNumber() );
......@@ -48,7 +48,7 @@ TEST_F( AlbumTracks, Create )
TEST_F( AlbumTracks, Artist )
{
auto album = ml->createAlbum( "album" );
auto f = ml->addFile( "track1.mp3", nullptr, nullptr );
auto f = ml->addFile( "track1.mp3" );
auto track = album->addTrack( f, 1, 0 );
ASSERT_EQ( track->artist(), "" );
......@@ -66,7 +66,7 @@ TEST_F( AlbumTracks, Artist )
TEST_F( AlbumTracks, SetReleaseYear )
{
auto a = ml->createAlbum( "album" );
auto m = ml->addFile( "test.mp3", nullptr, nullptr );
auto m = ml->addFile( "test.mp3" );
auto t = a->addTrack( m, 1, 0 );
ASSERT_EQ( 0u, t->releaseYear() );
......
......@@ -118,7 +118,7 @@ TEST_F( Artists, AllSongs )
for (auto i = 1; i <= 3; ++i)
{
auto f = ml->addFile( "song" + std::to_string(i) + ".mp3", nullptr, nullptr );
auto f = ml->addFile( "song" + std::to_string(i) + ".mp3" );
auto res = artist->addMedia( f.get() );
ASSERT_TRUE( res );
}
......
......@@ -31,14 +31,14 @@ class AudioTracks : public Tests
TEST_F( AudioTracks, AddTrack )
{
auto f = std::static_pointer_cast<Media>( ml->addFile( "file.mp3", nullptr, nullptr ) );
auto f = std::static_pointer_cast<Media>( ml->addFile( "file.mp3" ) );
bool res = f->addAudioTrack( "PCM", 128, 44100, 2, "fr", "test" );
ASSERT_TRUE( res );
}
TEST_F( AudioTracks, GetSetProperties )
{
auto f = std::static_pointer_cast<Media>( ml->addFile( "file.mp3", nullptr, nullptr ) );
auto f = std::static_pointer_cast<Media>( ml->addFile( "file.mp3" ) );
ASSERT_NE( f, nullptr );
f->addAudioTrack( "PCM", 128, 44100, 2, "en", "test desc" );
auto tracks = f->audioTracks();
......@@ -68,7 +68,7 @@ TEST_F( AudioTracks, GetSetProperties )
TEST_F( AudioTracks, FetchTracks )
{
auto f = std::static_pointer_cast<Media>( ml->addFile( "file.mp3", nullptr, nullptr ) );
auto f = std::static_pointer_cast<Media>( ml->addFile( "file.mp3" ) );
f->addAudioTrack( "PCM", 128, 44100, 2, "en", "test desc" );
f->addAudioTrack( "WMA", 128, 48000, 2, "fr", "test desc 2" );
......
......@@ -66,7 +66,6 @@ TEST_F( Folders, Add )
auto files = ml->files();
ASSERT_EQ( files.size(), 3u );
ASSERT_FALSE( std::static_pointer_cast<Media>( files[0] )->isStandAlone() );
}
TEST_F( Folders, Delete )
......@@ -116,8 +115,6 @@ TEST_F( Folders, Load )
auto files = ml->files();
ASSERT_EQ( files.size(), 3u );
for ( auto& f : files )
ASSERT_FALSE( std::static_pointer_cast<Media>( f )->isStandAlone() );
}
TEST_F( Folders, InvalidPath )
......@@ -238,7 +235,6 @@ TEST_F( Folders, NewFileInSubFolder )
f = ml->folder( mock::FileSystemFactory::SubFolder );
ASSERT_EQ( 2u, f->files().size() );
ASSERT_NE( nullptr, file );
ASSERT_FALSE( std::static_pointer_cast<Media>( file )->isStandAlone() );
}
TEST_F( Folders, RemoveFileFromDirectory )
......
......@@ -32,7 +32,7 @@ class Labels : public Tests
TEST_F( Labels, Add )
{
auto f = ml->addFile( "media.avi", nullptr, nullptr );
auto f = ml->addFile( "media.avi" );
auto l1 = ml->createLabel( "sea otter" );
auto l2 = ml->createLabel( "cony the cone" );
......@@ -54,7 +54,7 @@ TEST_F( Labels, Add )
TEST_F( Labels, Remove )
{
auto f = ml->addFile( "media.avi", nullptr, nullptr );
auto f = ml->addFile( "media.avi" );
auto l1 = ml->createLabel( "sea otter" );
auto l2 = ml->createLabel( "cony the cone" );
......@@ -97,9 +97,9 @@ TEST_F( Labels, Remove )
TEST_F( Labels, Files )
{
auto f = ml->addFile( "media.avi", nullptr, nullptr );
auto f2 = ml->addFile( "file.mp3", nullptr, nullptr );
auto f3 = ml->addFile( "otter.mkv", nullptr, nullptr );
auto f = ml->addFile( "media.avi" );
auto f2 = ml->addFile( "file.mp3" );
auto f3 = ml->addFile( "otter.mkv" );
auto l1 = ml->createLabel( "label1" );
auto l2 = ml->createLabel( "label2" );
......@@ -124,7 +124,7 @@ TEST_F( Labels, Files )
TEST_F( Labels, Delete )
{
auto f = ml->addFile( "media.avi", nullptr, nullptr );
auto f = ml->addFile( "media.avi" );
auto l1 = ml->createLabel( "sea otter" );
auto l2 = ml->createLabel( "cony the cone" );
......
......@@ -40,13 +40,12 @@ TEST_F( Medias, Init )
TEST_F( Medias, Create )
{
auto f = ml->addFile( "media.avi", nullptr, nullptr );
auto f = ml->addFile( "media.avi" );
ASSERT_NE( f, nullptr );
ASSERT_EQ( f->playCount(), 0 );
ASSERT_EQ( f->albumTrack(), nullptr );
ASSERT_EQ( f->showEpisode(), nullptr );
ASSERT_TRUE( f->isStandAlone() );
ASSERT_FALSE( f->isParsed() );
ASSERT_EQ( f->duration(), -1 );
ASSERT_NE( 0u, f->insertionDate() );
......@@ -58,7 +57,7 @@ TEST_F( Medias, Create )
TEST_F( Medias, Fetch )
{
auto f = ml->addFile( "media.avi", nullptr, nullptr );
auto f = ml->addFile( "media.avi" );
auto f2 = std::static_pointer_cast<Media>( ml->media( f->id() ) );
ASSERT_EQ( f->mrl(), f2->mrl() );
ASSERT_EQ( f, f2 );
......@@ -68,12 +67,11 @@ TEST_F( Medias, Fetch )
f2 = std::static_pointer_cast<Media>( ml->media( f->id() ) );
ASSERT_EQ( f->mrl(), f2->mrl() );
ASSERT_TRUE( f2->isStandAlone() );
}
TEST_F( Medias, Delete )
{
auto f = ml->addFile( "media.avi", nullptr, nullptr );
auto f = ml->addFile( "media.avi" );
auto f2 = ml->media( f->id() );
ASSERT_EQ( f, f2 );
......@@ -85,7 +83,7 @@ TEST_F( Medias, Delete )
TEST_F( Medias, LastModificationDate )
{
auto f = ml->addFile( "media.avi", nullptr, nullptr );
auto f = ml->addFile( "media.avi" );
ASSERT_NE( 0u, f->lastModificationDate() );
Reload();
......@@ -95,7 +93,7 @@ TEST_F( Medias, LastModificationDate )
TEST_F( Medias, Duration )
{
auto f = ml->addFile( "media.avi", nullptr, nullptr );
auto f = ml->addFile( "media.avi" );
ASSERT_EQ( f->duration(), -1 );
// Use a value that checks we're using a 64bits value
......@@ -114,7 +112,7 @@ TEST_F( Medias, Duration )
TEST_F( Medias, Artist )
{
auto f = std::static_pointer_cast<Media>( ml->addFile( "media.avi", nullptr, nullptr ) );
auto f = std::static_pointer_cast<Media>( ml->addFile( "media.avi" ) );
ASSERT_EQ( f->artist(), "" );
std::string newArtist( "Rage Against The Otters" );
......@@ -131,7 +129,7 @@ TEST_F( Medias, Artist )
TEST_F( Medias, Snapshot )
{
auto f = ml->addFile( "media.avi", nullptr, nullptr );
auto f = ml->addFile( "media.avi" );
ASSERT_EQ( f->snapshot(), "" );
std::string newSnapshot( "/path/to/snapshot" );
......@@ -148,7 +146,7 @@ TEST_F( Medias, Snapshot )
TEST_F( Medias, PlayCount )
{
auto f = ml->addFile( "media.avi", nullptr, nullptr );
auto f = ml->addFile( "media.avi" );
ASSERT_EQ( 0, f->playCount() );
f->increasePlayCount();
ASSERT_EQ( 1, f->playCount() );
......
......@@ -105,7 +105,7 @@ TEST_F( Movies, SetImdbId )
TEST_F( Movies, AssignToFile )
{
auto f = ml->addFile( "file.avi", nullptr, nullptr );
auto f = ml->addFile( "file.avi" );
auto m = ml->createMovie( "movie" );
ASSERT_EQ( f->movie(), nullptr );
......
......@@ -130,7 +130,7 @@ TEST_F( Shows, FetchShowFromEpisode )
{
auto s = ml->createShow( "show" );
auto e = s->addEpisode( "episode 1", 1 );
auto f = ml->addFile( "file.avi", nullptr, nullptr );
auto f = ml->addFile( "file.avi" );
f->setShowEpisode( e );
f->save();
......@@ -216,7 +216,7 @@ TEST_F( Shows, FileSetShowEpisode )
{
auto show = ml->createShow( "show" );
auto e = show->addEpisode( "episode 1", 1 );
auto f = ml->addFile( "file.avi", nullptr, nullptr );
auto f = ml->addFile( "file.avi" );
ASSERT_EQ( f->showEpisode(), nullptr );
f->setShowEpisode( e );
......
......@@ -74,6 +74,12 @@ void Tests::InstantiateMediaLibrary()
::testing::Environment* const env = ::testing::AddGlobalTestEnvironment(new TestEnv);
MediaLibraryTester::MediaLibraryTester()
: dummyDirectory( new mock::NoopDirectory )
, dummyFolder( "./", 0, 0, false )
{
}
std::shared_ptr<Media> MediaLibraryTester::media( unsigned int id )
{
return Media::fetch( m_dbConnection.get(), id );
......@@ -102,3 +108,8 @@ std::shared_ptr<Folder> MediaLibraryTester::folder( const std::string& path )
}
return nullptr;
}
std::shared_ptr<Media> MediaLibraryTester::addFile( const std::string& path )
{
return MediaLibrary::addFile( path, dummyFolder, *dummyDirectory );
}
......@@ -24,13 +24,20 @@
#include "factory/IFileSystem.h"
#include "MediaLibrary.h"
#include "Folder.h"
class MediaLibraryTester : public MediaLibrary
{
public:
MediaLibraryTester();
std::shared_ptr<Media> media( unsigned int id );
MediaPtr media( const std::string& path );
std::shared_ptr<Folder> folder( const std::string& path );
std::shared_ptr<Media> addFile( const std::string& path );
private:
std::unique_ptr<fs::IDirectory> dummyDirectory;
Folder dummyFolder;
};
class MediaLibraryWithoutParser : public MediaLibraryTester
......
......@@ -31,14 +31,14 @@ class VideoTracks : public Tests
TEST_F( VideoTracks, AddTrack )
{
auto f = std::static_pointer_cast<Media>( ml->addFile( "file.avi", nullptr, nullptr ) );
auto f = std::static_pointer_cast<Media>( ml->addFile( "file.avi" ) );
bool res = f->addVideoTrack( "H264", 1920, 1080, 29.97 );
ASSERT_TRUE( res );
}
TEST_F( VideoTracks, FetchTracks )
{
auto f = std::static_pointer_cast<Media>( ml->addFile( "file.avi", nullptr, nullptr ) );
auto f = std::static_pointer_cast<Media>( ml->addFile( "file.avi" ) );
f->addVideoTrack( "H264", 1920, 1080, 29.97 );
f->addVideoTrack( "VP80", 640, 480, 29.97 );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment