Tests.cpp 2.78 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 "ILabel.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
5
#include "IFile.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
6

7
class MLTest : 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
27
28
29
30
31
32
33
34
35
    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");
        }
};

IMediaLibrary* MLTest::ml;

TEST_F( MLTest, Init )
{
    // only test for correct test fixture behavior
}

TEST_F( MLTest, InsertFile )
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
36
    auto f = ml->addFile( "/dev/null" );
37
38
    ASSERT_TRUE( f != NULL );

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
39
40
41
42
    ASSERT_EQ( f->playCount(), 0 );
    ASSERT_TRUE( f->albumTrack() == NULL );
    ASSERT_TRUE( f->showEpisode() == NULL );

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
43
44
45
    std::vector<std::shared_ptr<IFile>> files;
    bool success = ml->files( files );
    ASSERT_TRUE( success );
46
47
    ASSERT_EQ( files.size(), 1u );
    ASSERT_EQ( files[0]->mrl(), f->mrl() );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
48
49
}

50
51
TEST_F( MLTest, FetchFile )
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
52
53
    auto f = ml->addFile( "/dev/null" );
    auto f2 = ml->file( "/dev/null" );
54
    ASSERT_EQ( f->mrl(), f2->mrl() );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
55
56
    // Basic caching test:
    ASSERT_EQ( f, f2 );
57
58
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
59
60
TEST_F( MLTest, AddLabel )
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
61
    auto f = ml->addFile( "/dev/null" );
62
63
    auto l1 = ml->createLabel( "sea otter" );
    auto l2 = ml->createLabel( "cony the cone" );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
64

65
66
    ASSERT_NE( l1, nullptr);
    ASSERT_NE( l2, nullptr);
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
67

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
68
    auto labels = f->labels();
69
70
71
72
73
74
75
    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
76
77
78
    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
79
}
80
81
82

TEST_F( MLTest, RemoveLabel )
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
83
    auto f = ml->addFile( "/dev/null" );
84
85
86
87
88
    auto l1 = ml->createLabel( "sea otter" );
    auto l2 = ml->createLabel( "cony the cone" );

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

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
90
    auto labels = f->labels();
91
92
93
94
95
96
97
98
99
100
101
    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
102
    auto f2 = ml->file( f->mrl() );
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    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 );
}
123
124