Labels.cpp 3.39 KB
Newer Older
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
1 2
#include "gtest/gtest.h"

3
#include "IMediaLibrary.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
4
#include "IFile.h"
5
#include "ILabel.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
6

7
class Labels : public testing::Test
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
8
{
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    public:
        static IMediaLibrary* ml;

    protected:
        virtual void SetUp()
        {
            ml = MediaLibraryFactory::create();
            bool res = ml->initialize( "test.db" );
            ASSERT_TRUE( res );
        }

        virtual void TearDown()
        {
            delete ml;
            unlink("test.db");
        }
};

27
IMediaLibrary* Labels::ml;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
28

29
TEST_F( Labels, Add )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
30
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
31
    auto f = ml->addFile( "/dev/null" );
32 33
    auto l1 = ml->createLabel( "sea otter" );
    auto l2 = ml->createLabel( "cony the cone" );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
34

35 36
    ASSERT_NE( l1, nullptr);
    ASSERT_NE( l2, nullptr);
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
37

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
38
    auto labels = f->labels();
39 40 41 42 43 44 45
    ASSERT_EQ( labels.size(), 0u );

    f->addLabel( l1 );
    f->addLabel( l2 );

    labels = f->labels();

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
46 47 48
    ASSERT_EQ( labels.size(), 2u );
    ASSERT_EQ( labels[0]->name(), "sea otter" );
    ASSERT_EQ( labels[1]->name(), "cony the cone" );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
49
}
50

51
TEST_F( Labels, Remove )
52
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
53
    auto f = ml->addFile( "/dev/null" );
54 55 56 57 58
    auto l1 = ml->createLabel( "sea otter" );
    auto l2 = ml->createLabel( "cony the cone" );

    f->addLabel( l1 );
    f->addLabel( l2 );
59

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
60
    auto labels = f->labels();
61 62 63 64 65 66 67 68 69 70 71
    ASSERT_EQ( labels.size(), 2u );

    bool res = f->removeLabel( l1 );
    ASSERT_TRUE( res );

    // Check for existing file first
    labels = f->labels();
    ASSERT_EQ( labels.size(), 1u );
    ASSERT_EQ( labels[0]->name(), "cony the cone" );

    // And now clean fetch another instance of the file & check again for DB replication
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
72
    auto f2 = ml->file( f->mrl() );
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    labels = f2->labels();
    ASSERT_EQ( labels.size(), 1u );
    ASSERT_EQ( labels[0]->name(), "cony the cone" );

    // Remove a non-linked label
    res = f->removeLabel( l1 );
    ASSERT_FALSE( res );

    // Remove the last label
    res = f->removeLabel( l2 );
    ASSERT_TRUE( res );

    labels = f->labels();
    ASSERT_EQ( labels.size(), 0u );

    // Check again for DB replication
    f2 = ml->file( f->mrl() );
    labels = f2->labels();
    ASSERT_EQ( labels.size(), 0u );
}
93

94
TEST_F( Labels, Files )
95 96 97 98 99 100 101 102 103 104 105
{
    auto f = ml->addFile( "/dev/null" );
    auto f2 = ml->addFile( "/dev/moulaf" );
    auto f3 = ml->addFile( "/sea/otter" );

    auto l1 = ml->createLabel( "label1" );
    auto l2 = ml->createLabel( "label2" );

    f->addLabel( l1 );
    f2->addLabel( l2 );
    f3->addLabel( l1 );
106

107 108 109 110 111 112 113
    std::vector<FilePtr> label1Files;
    std::vector<FilePtr> label2Files;

    bool res = l1->files( label1Files );
    ASSERT_TRUE( res );
    res = l2->files( label2Files );
    ASSERT_TRUE( res );
114 115 116 117 118 119 120 121 122 123 124

    ASSERT_EQ( label1Files.size(), 2u );
    ASSERT_EQ( label2Files.size(), 1u );

    ASSERT_EQ( label2Files[0], f2 );

    for (auto labelFile : label1Files )
    {
        ASSERT_TRUE( labelFile == f || labelFile == f3 );
    }
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
125

126
TEST_F( Labels, Delete )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
{
    auto f = ml->addFile( "/dev/null" );
    auto l1 = ml->createLabel( "sea otter" );
    auto l2 = ml->createLabel( "cony the cone" );

    f->addLabel( l1 );
    f->addLabel( l2 );

    auto labels = f->labels();
    ASSERT_EQ( labels.size(), 2u );

    ml->deleteLabel( "sea otter" );
    labels = f->labels();
    ASSERT_EQ( labels.size(), 1u );

    ml->deleteLabel( l2 );
    labels = f->labels();
    ASSERT_EQ( labels.size(), 0u );

    // Nothing to delete anymore, this should fail gracefuly
    bool res = ml->deleteLabel( l1 );
    ASSERT_FALSE( res );
}
150