MiscTests.cpp 6.44 KB
Newer Older
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2017 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.
 *****************************************************************************/

#if HAVE_CONFIG_H
# include "config.h"
#endif

27 28
#include <fstream>

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
29
#include "Tests.h"
30 31
#include "database/SqliteTools.h"
#include "database/SqliteConnection.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
32

33
#include "Artist.h"
34
#include "Media.h"
35

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
36 37 38 39 40 41 42 43 44 45 46 47
class Misc : public Tests
{
};

TEST_F( Misc, FileExtensions )
{
    const auto supportedExtensions = ml->getSupportedExtensions();
    for ( auto i = 0u; i < supportedExtensions.size() - 1; i++ )
    {
        ASSERT_LT( strcmp( supportedExtensions[i], supportedExtensions[i + 1] ), 0 );
    }
}
48

49
class DbModel : public testing::Test
50
{
51 52 53 54
protected:
    std::unique_ptr<MediaLibraryTester> ml;
    std::unique_ptr<mock::NoopCallback> cbMock;

55 56 57 58
public:
    virtual void SetUp() override
    {
        unlink("test.db");
59
        ml.reset( new MediaLibraryTester );
60 61 62 63 64 65
        cbMock.reset( new mock::NoopCallback );
    }

    void LoadFakeDB( const char* dbPath )
    {
        std::ifstream file{ dbPath };
66
        {
67 68
            // Don't use our Sqlite wrapper to open a connection. We don't want
            // to mess with per-thread connections.
69
            medialibrary::sqlite::Connection::Handle conn;
70 71
            sqlite3_open( "test.db", &conn );
            std::unique_ptr<sqlite3, int(*)(sqlite3*)> dbPtr{ conn, &sqlite3_close };
72 73 74 75
            // The backup file already contains a transaction
            char buff[2048];
            while( file.getline( buff, sizeof( buff ) ) )
            {
76
                medialibrary::sqlite::Statement stmt( conn, buff );
77 78 79 80 81
                stmt.execute();
                while ( stmt.row() != nullptr )
                    ;
            }
            // Ensure we are doing a migration
82 83
            {
                medialibrary::sqlite::Statement stmt{ conn,
84
                        "SELECT * FROM Settings" };
85 86 87 88 89 90
                stmt.execute();
                auto row = stmt.row();
                uint32_t dbVersion;
                row >> dbVersion;
                ASSERT_NE( dbVersion, Settings::DbModelVersion );
            }
91 92
            // Keep address sanitizer/memleak detection happy
            medialibrary::sqlite::Statement::FlushStatementCache();
93
        }
94 95
    }

96 97 98 99 100 101 102 103 104 105 106
    void CheckNbTriggers( uint32_t expected )
    {
        medialibrary::sqlite::Statement stmt{ ml->getDbConn()->handle(),
                "SELECT COUNT(*) FROM sqlite_master WHERE type='trigger'" };
        stmt.execute();
        auto row = stmt.row();
        uint32_t nbTriggers;
        row >> nbTriggers;
        ASSERT_EQ( nbTriggers, expected );
    }

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    virtual void TearDown() override
    {
        medialibrary::sqlite::Connection::Handle conn;
        sqlite3_open( "test.db", &conn );
        std::unique_ptr<sqlite3, int(*)(sqlite3*)> dbPtr{ conn, &sqlite3_close };
        {
            medialibrary::sqlite::Statement stmt{ conn,
                    "SELECT * FROM Settings" };
            stmt.execute();
            auto row = stmt.row();
            uint32_t dbVersion;
            row >> dbVersion;
            ASSERT_EQ( dbVersion, Settings::DbModelVersion );
        }
        medialibrary::sqlite::Statement::FlushStatementCache();
122 123 124
    }
};

125
TEST_F( DbModel, Upgrade3to5 )
126
{
127 128
    LoadFakeDB( SRC_DIR "/test/unittest/db_v3.sql" );
    auto res = ml->initialize( "test.db", "/tmp", cbMock.get() );
129
    ASSERT_EQ( InitializeResult::Success, res );
130 131 132
    // All is done during the database initialization, we only care about no
    // exception being thrown, and MediaLibrary::initialize() returning true
}
133 134 135 136 137

TEST_F( DbModel, Upgrade4to5 )
{
    LoadFakeDB( SRC_DIR "/test/unittest/db_v4.sql" );
    auto res = ml->initialize( "test.db", "/tmp", cbMock.get() );
138
    ASSERT_EQ( InitializeResult::DbReset, res );
139 140 141 142 143 144 145 146

    // The culprit  with V4 was an invalid migration, leading to missing fields
    // in File and most likely Playlist tables. Simply try to create/fetch a file
    auto m = ml->addFile( "test.mkv" );
    ASSERT_NE( m, nullptr );
    auto files = ml->files();
    ASSERT_NE( files.size(), 0u );
}
147 148 149 150 151 152

TEST_F( DbModel, Upgrade7to8 )
{
    LoadFakeDB( SRC_DIR "/test/unittest/db_v7.sql" );
    auto res = ml->initialize( "test.db", "/tmp", cbMock.get() );
    ASSERT_EQ( InitializeResult::Success, res );
153 154
    // Removed post migration tests starting with V9, since we force a re-scan,
    // there is no content left to test
155
}
156 157 158 159 160 161 162 163 164 165

TEST_F( DbModel, Upgrade8to9 )
{
    LoadFakeDB( SRC_DIR "/test/unittest/db_v8.sql" );
    auto res = ml->initialize( "test.db", "/tmp", cbMock.get() );
    ASSERT_EQ( InitializeResult::Success, res );
    // We expect the file-orphaned media to have been deleted
    auto media = ml->files();
    ASSERT_EQ( 1u, media.size() );
}
166 167 168 169 170 171

TEST_F( DbModel, Upgrade12to13 )
{
    LoadFakeDB( SRC_DIR "/test/unittest/db_v12.sql" );
    auto res = ml->initialize( "test.db", "/tmp", cbMock.get() );
    ASSERT_EQ( InitializeResult::Success, res );
172 173
    // We can't check for the number of albums anymore since they are deleted
    // as part of 13 -> 14 migration
174 175

    CheckNbTriggers( 31 );
176 177 178 179 180 181 182
}

TEST_F( DbModel, Upgrade13to14 )
{
    LoadFakeDB( SRC_DIR "/test/unittest/db_v13.sql" );
    auto res = ml->initialize( "test.db", "/tmp", cbMock.get() );
    ASSERT_EQ( InitializeResult::Success, res );
183 184 185 186 187 188 189 190 191
    auto media = ml->files();
    ASSERT_EQ( 2u, media.size() );
    auto m = media[0];
    ASSERT_EQ( m->thumbnail(), "/path/to/thumbnail" );
    ASSERT_TRUE( m->isThumbnailGenerated() );

    m = media[1];
    ASSERT_EQ( m->thumbnail(), "" );
    ASSERT_FALSE( m->isThumbnailGenerated() );
192 193

    CheckNbTriggers( 31 );
194
}