Tests.cpp 3.53 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
#include "Tests.h"
24
25
26

#include <memory>

27
#include "File.h"
28
29
#include "filesystem/IFile.h"
#include "filesystem/IDirectory.h"
30
#include "utils/Filename.h"
31
#include "discoverer/FsDiscoverer.h"
32
#include "mocks/FileSystem.h"
33
#include "Media.h"
34
#include "Folder.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
35
#include "Playlist.h"
36
37
38
39
40
41
42
43
44
45
46

class TestEnv : public ::testing::Environment
{
    public:
        virtual void SetUp()
        {
            // Always clean the DB in case a previous test crashed
            unlink("test.db");
        }
};

47
48
49
50
51
void Tests::TearDown()
{
    ml.reset();
}

52
void Tests::Reload(std::shared_ptr<factory::IFileSystem> fs /*= nullptr*/, IMediaLibraryCb* metadataCb /*= nullptr*/ )
53
{
54
    InstantiateMediaLibrary();
55
    if ( fs == nullptr )
56
57
58
    {
        fs = std::shared_ptr<factory::IFileSystem>( new mock::NoopFsFactory );
    }
59
    ml->setFsFactory( fs );
60
    ml->setVerbosity( LogLevel::Error );
61
    bool res = ml->initialize( "test.db", "/tmp", metadataCb );
62
63
64
65
66
67
    ASSERT_TRUE( res );
}


void Tests::SetUp()
{
68
    unlink("test.db");
69
70
71
    Reload();
}

72
73
74
75
76
void Tests::InstantiateMediaLibrary()
{
    ml.reset( new MediaLibraryWithoutBackground );
}

77
::testing::Environment* const env = ::testing::AddGlobalTestEnvironment(new TestEnv);
78

79
80
81
82
83
84
MediaLibraryTester::MediaLibraryTester()
    : dummyDirectory( new mock::NoopDirectory )
    , dummyFolder( "./", 0, 0, false )
{
}

85
86
87
88
89
90
91
92
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();
93
    for ( auto& m : medias )
94
    {
95
96
97
98
99
100
        auto files = m->files();
        for ( auto& f : files )
        {
            if ( f->mrl() == path )
                return m;
        }
101
102
103
    }
    return nullptr;
}
104
105
106
107

std::shared_ptr<Folder> MediaLibraryTester::folder( const std::string& path )
{
    static const std::string req = "SELECT * FROM " + policy::FolderTable::Name +
108
            " WHERE is_blacklisted = 0 AND is_present = 1";
109
110
111
112
113
114
115
116
    auto folders = Folder::DatabaseHelpers::fetchAll<Folder>( m_dbConnection.get(), req );
    for ( auto &f : folders )
    {
        if ( f->path() == path )
            return f;
    }
    return nullptr;
}
117
118
119
120
121

std::shared_ptr<Media> MediaLibraryTester::addFile( const std::string& path )
{
    return MediaLibrary::addFile( path, dummyFolder, *dummyDirectory );
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
122
123
124
125
126

std::shared_ptr<Playlist> MediaLibraryTester::playlist(unsigned int playlistId)
{
    return Playlist::fetch( m_dbConnection.get(), playlistId );
}