MediaLibraryTester.cpp 3.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

23 24 25 26
#if HAVE_CONFIG_H
# include "config.h"
#endif

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include "MediaLibraryTester.h"

#include "Album.h"
#include "AlbumTrack.h"
#include "Artist.h"
#include "Device.h"
#include "Playlist.h"
#include "Genre.h"
#include "Media.h"
#include "Folder.h"
#include "mocks/FileSystem.h"


MediaLibraryTester::MediaLibraryTester()
    : dummyDirectory( new mock::NoopDirectory )
    , dummyFolder( nullptr, "./", 0, 0, false )
{
}

46
std::shared_ptr<Media> MediaLibraryTester::media( int64_t id )
47 48 49 50
{
    return Media::fetch( this, id );
}

51
MediaPtr MediaLibraryTester::media( const std::string& path )
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
{
    auto medias = files();
    for ( auto& m : medias )
    {
        auto files = m->files();
        for ( auto& f : files )
        {
            if ( f->mrl() == path )
                return m;
        }
    }
    return nullptr;
}

std::shared_ptr<Folder> MediaLibraryTester::folder( const std::string& path )
{
    static const std::string req = "SELECT * FROM " + policy::FolderTable::Name +
            " WHERE is_blacklisted = 0 AND is_present = 1";
    auto folders = Folder::DatabaseHelpers::fetchAll<Folder>( this, req );
    for ( auto &f : folders )
    {
        if ( f->path() == path )
            return f;
    }
    return nullptr;
}

std::shared_ptr<Media> MediaLibraryTester::addFile( const std::string& path )
{
    mock::NoopFile file( path );
    return MediaLibrary::addFile( file, dummyFolder, *dummyDirectory );
}

85 86 87 88 89
std::shared_ptr<Media> MediaLibraryTester::addFile( fs::IFile& file )
{
    return MediaLibrary::addFile( file, dummyFolder, *dummyDirectory );
}

90
std::shared_ptr<Playlist> MediaLibraryTester::playlist( int64_t playlistId )
91 92 93 94
{
    return Playlist::fetch( this, playlistId );
}

95
void MediaLibraryTester::deleteAlbum( int64_t albumId )
96 97 98 99 100 101 102 103 104
{
    Album::destroy( this, albumId );
}

std::shared_ptr<Genre> MediaLibraryTester::createGenre( const std::string& name )
{
    return Genre::create( this, name );
}

105
void MediaLibraryTester::deleteGenre( int64_t genreId )
106 107 108 109
{
    Genre::destroy( this, genreId );
}

110
void MediaLibraryTester::deleteArtist( int64_t artistId )
111 112 113 114 115 116 117 118 119 120 121 122 123 124
{
    Artist::destroy( this, artistId );
}

std::shared_ptr<Device> MediaLibraryTester::addDevice( const std::string& uuid, bool isRemovable )
{
    return Device::create( this, uuid, isRemovable );
}

void MediaLibraryTester::setFsFactory(std::shared_ptr<factory::IFileSystem> fsFactory)
{
    m_fsFactory = fsFactory;
}

125
void MediaLibraryTester::deleteTrack( int64_t trackId )
126 127 128 129
{
    AlbumTrack::destroy( this, trackId );
}

130
std::shared_ptr<AlbumTrack> MediaLibraryTester::albumTrack( int64_t id )
131 132 133
{
    return AlbumTrack::fetch( this, id );
}
134

135
std::vector<MediaPtr> MediaLibraryTester::files()
136 137
{
    static const std::string req = "SELECT * FROM " + policy::MediaTable::Name + " WHERE is_present = 1";
138
    return Media::fetchAll<IMedia>( this, req );
139
}