Commit 24c4b431 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

MediaLibrary: Don't expose a file/media getter.

This is now only part of the test implementation, and allows us to
remove quite a bit of overhead
parent 694e9e7f
......@@ -38,16 +38,12 @@
#include "database/SqliteTools.h"
#include "VideoTrack.h"
#include "filesystem/IFile.h"
#include "filesystem/IDevice.h"
#include "filesystem/IDirectory.h"
#include "utils/Filename.h"
const std::string policy::MediaTable::Name = "Media";
const std::string policy::MediaTable::PrimaryKeyColumn = "id_media";
unsigned int Media::* const policy::MediaTable::PrimaryKey = &Media::m_id;
std::shared_ptr<factory::IFileSystem> Media::FsFactory;
Media::Media( DBConnection dbConnection, sqlite::Row& row )
: m_dbConnection( dbConnection )
, m_changed( false )
......@@ -294,11 +290,6 @@ bool Media::isParsed() const
return m_isParsed;
}
void Media::setFileSystemFactory(std::shared_ptr<factory::IFileSystem> fsFactory)
{
FsFactory = fsFactory;
}
void Media::markParsed()
{
if ( m_isParsed == true )
......@@ -374,37 +365,6 @@ bool Media::createTable( DBConnection connection )
sqlite::Tools::executeRequest( connection, triggerReq );
}
MediaPtr Media::fromPath( DBConnection connection, const std::string& fullPath )
{
auto folderPath = utils::file::directory( fullPath );
auto folderFs = FsFactory->createDirectory( folderPath );
if ( folderFs != nullptr )
{
auto deviceFs = folderFs->device();
if ( deviceFs->isRemovable() == false )
{
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name +
" WHERE mrl = ? AND is_present = 1";
return Media::fetch( connection, req, fullPath );
}
}
auto folder = Folder::fromPath( connection, folderPath );
auto folderId = folder != nullptr ? folder->id() : 0;
if ( folderId != 0 )
{
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name +
" WHERE mrl = ? AND folder_id = ? AND is_present = 1";
auto fileName = utils::file::fileName( fullPath );
return Media::fetch( connection, req, fileName, folderId );
}
else
{
static const std::string req = "SELECT * FROM " + policy::MediaTable::Name +
" WHERE mrl = ? AND folder_id IS NULL AND is_present = 1";
return Media::fetch( connection, req, fullPath );
}
}
bool Media::addLabel( LabelPtr label )
{
if ( m_id == 0 || label->id() == 0 )
......
......@@ -58,8 +58,6 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
static std::shared_ptr<Media> create( DBConnection dbConnection, Type type, const fs::IFile* file , unsigned int folderId, bool isRemovable );
static bool createTable( DBConnection connection );
static MediaPtr fromPath( DBConnection connection, const std::string& fullPath );
static void setFileSystemFactory( std::shared_ptr<factory::IFileSystem> fsFactory );
virtual unsigned int id() const override;
virtual Type type() override;
......@@ -102,9 +100,6 @@ class Media : public IMedia, public DatabaseHelpers<Media, policy::MediaTable>
void markParsed();
bool isParsed() const;
private:
static std::shared_ptr<factory::IFileSystem> FsFactory;
private:
DBConnection m_dbConnection;
......
......@@ -115,7 +115,6 @@ bool MediaLibrary::initialize( const std::string& dbPath, const std::string& sna
{
if ( m_fsFactory == nullptr )
m_fsFactory.reset( new factory::FileSystemFactory );
Media::setFileSystemFactory( m_fsFactory );
Folder::setFileSystemFactory( m_fsFactory );
m_snapshotPath = snapshotPath;
m_callback = mlCallback;
......@@ -181,11 +180,6 @@ std::vector<MediaPtr> MediaLibrary::videoFiles()
return Media::fetchAll<IMedia>( m_dbConnection.get(), req, IMedia::Type::VideoType );
}
MediaPtr MediaLibrary::file( const std::string& path )
{
return Media::fromPath( m_dbConnection.get(), path );
}
std::shared_ptr<Media> MediaLibrary::addFile( const std::string& path, Folder* parentFolder, fs::IDirectory* parentFolderFs )
{
std::unique_ptr<fs::IFile> file;
......
......@@ -53,7 +53,6 @@ class MediaLibrary : public IMediaLibrary
std::vector<MediaPtr> files();
virtual std::vector<MediaPtr> audioFiles() override;
virtual std::vector<MediaPtr> videoFiles() override;
MediaPtr file( const std::string& path );
std::shared_ptr<Media> addFile(const std::string& path, Folder* parentFolder, fs::IDirectory* parentFolderFs);
virtual bool deleteFile(const Media* file );
......@@ -103,7 +102,7 @@ class MediaLibrary : public IMediaLibrary
virtual void startParser();
virtual void startDiscoverer();
private:
protected:
std::unique_ptr<SqliteConnection> m_dbConnection;
std::shared_ptr<factory::IFileSystem> m_fsFactory;
std::string m_snapshotPath;
......
......@@ -166,19 +166,18 @@ TEST_F( Albums, SetArtworkUrl )
TEST_F( Albums, FetchAlbumFromTrack )
{
{
auto a = ml->createAlbum( "album" );
auto f = ml->addFile( "file.mp3", nullptr, nullptr );
auto t = a->addTrack( f, 1, 0 );
f->setAlbumTrack( t );
}
auto a = ml->createAlbum( "album" );
auto f = ml->addFile( "file.mp3", nullptr, nullptr );
auto t = a->addTrack( f, 1, 0 );
f->setAlbumTrack( t );
Reload();
auto f = ml->file( "file.mp3" );
auto t = f->albumTrack();
auto a = t->album();
ASSERT_NE( a, nullptr );
ASSERT_EQ( a->title(), "album" );
f = ml->media( f->id() );
auto t2 = f->albumTrack();
auto a2 = t2->album();
ASSERT_NE( a2, nullptr );
ASSERT_EQ( a2->title(), "album" );
}
TEST_F( Albums, Artists )
......
......@@ -41,7 +41,7 @@ TEST_F( AlbumTracks, Create )
Reload();
f = std::static_pointer_cast<Media>( ml->file( "track1.mp3" ) );
f = std::static_pointer_cast<Media>( ml->media( f->id() ) );
ASSERT_EQ( 10u, f->albumTrack()->discNumber() );
}
......@@ -58,7 +58,7 @@ TEST_F( AlbumTracks, Artist )
Reload();
// Don't reuse the "track" and "f" variable, their type differ
auto file = ml->file( "track1.mp3" );
auto file = ml->media( f->id() );
auto albumTrack = file->albumTrack();
ASSERT_EQ( albumTrack->artist(), "artist" );
}
......@@ -76,7 +76,7 @@ TEST_F( AlbumTracks, SetReleaseYear )
Reload();
auto m2 = ml->file( "test.mp3" );
auto m2 = ml->media( m->id() );
auto t2 = m2->albumTrack();
ASSERT_EQ( t->releaseYear(), t2->releaseYear() );
}
......@@ -54,7 +54,7 @@ TEST_F( AudioTracks, GetSetProperties )
Reload();
auto f2 = ml->file( "file.mp3" );
auto f2 = ml->media( f->id() );
tracks = f2->audioTracks();
ASSERT_EQ( tracks.size(), 1u );
t = tracks[0];
......
......@@ -114,7 +114,7 @@ TEST_F( DeviceFs, RemoveDisk )
auto files = ml->files();
ASSERT_EQ( 5u, files.size() );
auto file = ml->file( RemovableDeviceMountpoint + "removablefile.mp3" );
auto file = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
ASSERT_NE( nullptr, file );
fsMock->removeDevice( RemovableDeviceUuid );
......@@ -127,7 +127,7 @@ TEST_F( DeviceFs, RemoveDisk )
files = ml->files();
ASSERT_EQ( 3u, files.size() );
file = ml->file( RemovableDeviceMountpoint + "removablefile.mp3" );
file = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
ASSERT_EQ( nullptr, file );
}
......@@ -141,7 +141,7 @@ TEST_F( DeviceFs, UnmountDisk )
auto files = ml->files();
ASSERT_EQ( 5u, files.size() );
auto file = ml->file( RemovableDeviceMountpoint + "removablefile.mp3" );
auto file = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
ASSERT_NE( nullptr, file );
auto device = std::static_pointer_cast<mock::Device>( fsMock->createDevice( RemovableDeviceUuid ) );
......@@ -155,7 +155,7 @@ TEST_F( DeviceFs, UnmountDisk )
files = ml->files();
ASSERT_EQ( 3u, files.size() );
file = ml->file( RemovableDeviceMountpoint + "removablefile.mp3" );
file = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
ASSERT_EQ( nullptr, file );
}
......@@ -169,7 +169,7 @@ TEST_F( DeviceFs, ReplugDisk )
auto files = ml->files();
ASSERT_EQ( 5u, files.size() );
auto file = ml->file( RemovableDeviceMountpoint + "removablefile.mp3" );
auto file = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
ASSERT_NE( nullptr, file );
auto device = fsMock->removeDevice( RemovableDeviceUuid );
......@@ -182,7 +182,7 @@ TEST_F( DeviceFs, ReplugDisk )
files = ml->files();
ASSERT_EQ( 3u, files.size() );
file = ml->file( RemovableDeviceMountpoint + "removablefile.mp3" );
file = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
ASSERT_EQ( nullptr, file );
fsMock->addDevice( device );
......@@ -194,7 +194,7 @@ TEST_F( DeviceFs, ReplugDisk )
files = ml->files();
ASSERT_EQ( 5u, files.size() );
file = ml->file( RemovableDeviceMountpoint + "removablefile.mp3" );
file = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
ASSERT_NE( nullptr, file );
}
......@@ -240,7 +240,7 @@ TEST_F( DeviceFs, RemoveAlbum )
// Create an album on a non-removable device
{
auto album = std::static_pointer_cast<Album>( ml->createAlbum( "album" ) );
auto file = ml->file( mock::FileSystemFactory::Root + "audio.mp3" );
auto file = ml->media( mock::FileSystemFactory::Root + "audio.mp3" );
album->addTrack( std::static_pointer_cast<Media>( file ), 1, 1 );
auto artist = ml->createArtist( "artist" );
album->setAlbumArtist( artist.get() );
......@@ -248,8 +248,8 @@ TEST_F( DeviceFs, RemoveAlbum )
// And an album that will disappear, along with its artist
{
auto album = std::static_pointer_cast<Album>( ml->createAlbum( "album 2" ) );
auto file = ml->file( RemovableDeviceMountpoint + "removablefile.mp3" );
auto file2 = ml->file( RemovableDeviceMountpoint + "removablefile2.mp3" );
auto file = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
ml->media( RemovableDeviceMountpoint + "removablefile2.mp3" );
album->addTrack( std::static_pointer_cast<Media>( file ), 1, 1 );
album->addTrack( std::static_pointer_cast<Media>( file ), 2, 1 );
auto artist = ml->createArtist( "artist 2" );
......@@ -283,8 +283,8 @@ TEST_F( DeviceFs, PartialAlbumRemoval )
{
auto album = std::static_pointer_cast<Album>( ml->createAlbum( "album" ) );
auto file = ml->file( mock::FileSystemFactory::SubFolder + "subfile.mp4" );
auto file2 = ml->file( RemovableDeviceMountpoint + "removablefile2.mp3" );
auto file = ml->media( mock::FileSystemFactory::SubFolder + "subfile.mp4" );
auto file2 = ml->media( RemovableDeviceMountpoint + "removablefile2.mp3" );
album->addTrack( std::static_pointer_cast<Media>( file ), 1, 1 );
album->addTrack( std::static_pointer_cast<Media>( file2 ), 2, 1 );
auto newArtist = ml->createArtist( "artist" );
......@@ -322,7 +322,7 @@ TEST_F( DeviceFs, ChangeDevice )
ASSERT_TRUE( discovered );
// Fetch a removable file's ID
auto f = ml->file( RemovableDeviceMountpoint + "removablefile.mp3" );
auto f = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
ASSERT_NE( f, nullptr );
auto firstRemovableFileId = f->id();
auto firstRemovableFilePath = f->mrl();
......@@ -341,7 +341,7 @@ TEST_F( DeviceFs, ChangeDevice )
// Check that new files with the same name have different IDs
// but the same "full path"
f = ml->file( RemovableDeviceMountpoint + "removablefile.mp3" );
f = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
ASSERT_NE( nullptr, f );
ASSERT_EQ( firstRemovableFilePath, f->mrl() );
ASSERT_NE( firstRemovableFileId, f->id() );
......@@ -354,7 +354,7 @@ TEST_F( DeviceFs, ChangeDevice )
reloaded = cbMock->waitForReload();
ASSERT_TRUE( reloaded );
f = ml->file( RemovableDeviceMountpoint + "removablefile.mp3" );
f = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
ASSERT_NE( nullptr, f );
ASSERT_EQ( firstRemovableFileId, f->id() );
}
......@@ -92,10 +92,6 @@ TEST_F( Folders, Delete )
files = ml->files();
ASSERT_EQ( files.size(), 0u );
// Check the file isn't cached anymore:
auto file = ml->file( filePath );
ASSERT_EQ( nullptr, file );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
......@@ -212,8 +208,8 @@ TEST_F( Folders, NewFolderWithFile )
ASSERT_TRUE( reloaded );
ASSERT_EQ( 4u, ml->files().size() );
auto file = ml->file( newFolder + "newfile.avi" );
ASSERT_NE( nullptr, file );
auto f = ml->media( newFolder + "newfile.avi" );
ASSERT_NE( nullptr, f );
}
// This is expected to fail until we fix the file system modifications detection
......@@ -238,7 +234,7 @@ TEST_F( Folders, NewFileInSubFolder )
ASSERT_TRUE( reloaded );
ASSERT_EQ( 4u, ml->files().size() );
auto file = ml->file( mock::FileSystemFactory::SubFolder + "newfile.avi" );
auto file = ml->media( mock::FileSystemFactory::SubFolder + "newfile.avi" );
f = ml->folder( mock::FileSystemFactory::SubFolder );
ASSERT_EQ( 2u, f->files().size() );
ASSERT_NE( nullptr, file );
......@@ -263,7 +259,7 @@ TEST_F( Folders, RemoveFileFromDirectory )
ASSERT_TRUE( reloaded );
ASSERT_EQ( 2u, ml->files().size() );
auto file = ml->file( mock::FileSystemFactory::SubFolder + "subfile.mp4" );
auto file = ml->media( mock::FileSystemFactory::SubFolder + "subfile.mp4" );
auto f = ml->folder( mock::FileSystemFactory::SubFolder );
ASSERT_EQ( 0u, f->files().size() );
ASSERT_EQ( nullptr, file );
......@@ -287,7 +283,7 @@ TEST_F( Folders, RemoveDirectory )
ASSERT_TRUE( reloaded );
ASSERT_EQ( 2u, ml->files().size() );
auto file = ml->file( mock::FileSystemFactory::SubFolder + "subfile.mp4" );
auto file = ml->media( mock::FileSystemFactory::SubFolder + "subfile.mp4" );
auto f = ml->folder( mock::FileSystemFactory::SubFolder );
ASSERT_EQ( nullptr, f );
ASSERT_EQ( nullptr, file );
......@@ -301,7 +297,7 @@ TEST_F( Folders, UpdateFile )
ASSERT_TRUE( discovered );
auto filePath = mock::FileSystemFactory::SubFolder + "subfile.mp4";
auto f = ml->file( filePath );
auto f = ml->media( filePath );
ASSERT_NE( f, nullptr );
auto id = f->id();
......@@ -313,7 +309,7 @@ TEST_F( Folders, UpdateFile )
bool reloaded = cbMock->waitForReload();
ASSERT_TRUE( reloaded );
f = ml->file( filePath );
f = ml->media( filePath );
ASSERT_NE( nullptr, f );
// The file is expected to be deleted and re-added since it changed, so the
// id should have changed
......@@ -348,7 +344,7 @@ TEST_F( Folders, BlacklistAfterDiscovery )
ASSERT_EQ( nullptr, f2 );
for ( auto& file : files )
{
auto m = ml->file( file->mrl() );
auto m = ml->media( file->mrl() );
ASSERT_EQ( nullptr, m );
}
......
......@@ -73,7 +73,7 @@ TEST_F( Labels, Remove )
ASSERT_EQ( labels[0]->name(), "cony the cone" );
// And now clean fetch another instance of the file & check again for DB replication
auto f2 = ml->file( f->mrl() );
auto f2 = ml->media( f->id() );
labels = f2->labels();
ASSERT_EQ( labels.size(), 1u );
ASSERT_EQ( labels[0]->name(), "cony the cone" );
......@@ -90,7 +90,7 @@ TEST_F( Labels, Remove )
ASSERT_EQ( labels.size(), 0u );
// Check again for DB replication
f2 = ml->file( f->mrl() );
f2 = ml->media( f->id() );
labels = f2->labels();
ASSERT_EQ( labels.size(), 0u );
}
......
......@@ -59,14 +59,14 @@ TEST_F( Medias, Create )
TEST_F( Medias, Fetch )
{
auto f = ml->addFile( "media.avi", nullptr, nullptr );
auto f2 = std::static_pointer_cast<Media>( ml->file( "media.avi" ) );
auto f2 = std::static_pointer_cast<Media>( ml->media( f->id() ) );
ASSERT_EQ( f->mrl(), f2->mrl() );
ASSERT_EQ( f, f2 );
// Flush cache and fetch from DB
Reload();
f2 = std::static_pointer_cast<Media>( ml->file( "media.avi" ) );
f2 = std::static_pointer_cast<Media>( ml->media( f->id() ) );
ASSERT_EQ( f->mrl(), f2->mrl() );
ASSERT_TRUE( f2->isStandAlone() );
}
......@@ -74,12 +74,12 @@ TEST_F( Medias, Fetch )
TEST_F( Medias, Delete )
{
auto f = ml->addFile( "media.avi", nullptr, nullptr );
auto f2 = ml->file( "media.avi" );
auto f2 = ml->media( f->id() );
ASSERT_EQ( f, f2 );
ml->deleteFile( f.get() );
f2 = ml->file( "media.avi" );
f2 = ml->media( f->id() );
ASSERT_EQ( f2, nullptr );
}
......@@ -89,7 +89,7 @@ TEST_F( Medias, LastModificationDate )
ASSERT_NE( 0u, f->lastModificationDate() );
Reload();
auto f2 = std::static_pointer_cast<Media>( ml->file( "media.avi" ) );
auto f2 = std::static_pointer_cast<Media>( ml->media( f->id() ) );
ASSERT_EQ( f->lastModificationDate(), f2->lastModificationDate() );
}
......@@ -107,7 +107,7 @@ TEST_F( Medias, Duration )
Reload();
auto f2 = ml->file( "media.avi" );
auto f2 = ml->media( f->id() );
ASSERT_EQ( f2->duration(), d );
}
......@@ -125,7 +125,7 @@ TEST_F( Medias, Artist )
Reload();
auto f2 = ml->file( "media.avi" );
auto f2 = ml->media( f->id() );
ASSERT_EQ( f2->artist(), newArtist );
}
......@@ -142,7 +142,7 @@ TEST_F( Medias, Snapshot )
Reload();
auto f2 = ml->file( "media.avi" );
auto f2 = ml->media( f->id() );
ASSERT_EQ( f2->snapshot(), newSnapshot );
}
......@@ -156,6 +156,6 @@ TEST_F( Medias, PlayCount )
Reload();
f = std::static_pointer_cast<Media>( ml->file( "media.avi" ) );
f = std::static_pointer_cast<Media>( ml->media( f->id() ) );
ASSERT_EQ( 1, f->playCount() );
}
......@@ -115,7 +115,7 @@ TEST_F( Movies, AssignToFile )
Reload();
auto f2 = ml->file( "file.avi" );
auto f2 = ml->media( f->id() );
auto m2 = f2->movie();
ASSERT_NE( m2, nullptr );
ASSERT_EQ( m2->title(), "movie" );
......
......@@ -141,7 +141,7 @@ TEST_F( Shows, FetchShowFromEpisode )
Reload();
f = std::static_pointer_cast<Media>( ml->file( "file.avi" ) );
f = std::static_pointer_cast<Media>( ml->media( f->id() ) );
ASSERT_NE( nullptr, f->showEpisode() );
s2 = f->showEpisode()->show();
ASSERT_NE( s2, nullptr );
......@@ -225,7 +225,7 @@ TEST_F( Shows, FileSetShowEpisode )
Reload();
f = std::static_pointer_cast<Media>( ml->file( "file.avi" ) );
f = std::static_pointer_cast<Media>( ml->media( f->id() ) );
auto e2 = f->showEpisode();
ASSERT_NE( e2, nullptr );
ASSERT_EQ( e2->name(), "episode 1" );
......
......@@ -29,6 +29,7 @@
#include "utils/Filename.h"
#include "discoverer/FsDiscoverer.h"
#include "mocks/FileSystem.h"
#include "Media.h"
class TestEnv : public ::testing::Environment
{
......@@ -71,3 +72,19 @@ void Tests::InstantiateMediaLibrary()
}
::testing::Environment* const env = ::testing::AddGlobalTestEnvironment(new TestEnv);
std::shared_ptr<Media> MediaLibraryTester::media( unsigned int id )
{
return Media::fetch( m_dbConnection.get(), id );
}
MediaPtr MediaLibraryTester::media( const std::string& path )
{
auto medias = files();
for ( auto& f : medias )
{
if ( f->mrl() == path )
return f;
}
return nullptr;
}
......@@ -25,7 +25,14 @@
#include "factory/IFileSystem.h"
#include "MediaLibrary.h"
class MediaLibraryWithoutParser : public MediaLibrary
class MediaLibraryTester : public MediaLibrary
{
public:
std::shared_ptr<Media> media( unsigned int id );
MediaPtr media( const std::string& path );
};
class MediaLibraryWithoutParser : public MediaLibraryTester
{
virtual void startParser() override {}
};
......@@ -38,7 +45,7 @@ class MediaLibraryWithoutBackground : public MediaLibraryWithoutParser
class Tests : public testing::Test
{
protected:
std::unique_ptr<MediaLibrary> ml;
std::unique_ptr<MediaLibraryTester> ml;
virtual void SetUp() override;
virtual void InstantiateMediaLibrary();
......
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