~aleteoryx/muditaos

ref: 2276ceed679b93a3a891e4f5739ade9e13991c5a muditaos/module-db/tests/SettingsTable_v2_tests.cpp -rw-r--r-- 2.7 KiB
2276ceed — Radoslaw Wicik [EGD-3743] Update copyrights in fies 5 years ago
                                                                                
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
85
86
87
88
89
90
91
92
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "vfs.hpp"

#include <catch2/catch.hpp>

#include "Database/Database.hpp"
#include "Databases/SettingsDB.hpp"

#include "Tables/SettingsTable.hpp"

TEST_CASE("Settings Table version 2 tests")
{
    Database::initialize();

    vfs.remove(SettingsDB::GetDBName());

    SettingsDB settingsDb;
    REQUIRE(settingsDb.isInitialized());

    auto &settingsTable_v2 = settingsDb.settings_v2;

    SECTION("Default Constructor")
    {
        SettingsTableRow_v2 testRow;
        REQUIRE(testRow.ID == DB_ID_NONE);
        REQUIRE(testRow.path == "");
        REQUIRE(testRow.value == "");
    }

    const std::vector<std::string> testPaths{"test/path1", "test/path2", "test/path3", "test/path4"};

    for (const auto &entry : testPaths) {
        SettingsTableRow_v2 testRow = {{.ID = 0}, .path = entry, .value = "abc"};

        REQUIRE(settingsTable_v2.add(testRow));
    }

    SECTION("Get entry - invalid ID")
    {
        auto settingsEntry = settingsTable_v2.getById(100);
        REQUIRE(settingsEntry.ID == DB_ID_NONE);
        REQUIRE(settingsEntry.path == "");
        REQUIRE(settingsEntry.value == "");
    }

    SECTION("Remove entries")
    {
        const auto &cnt = settingsTable_v2.count();

        SECTION("Remove by ID")
        {
            // Table should have now cnt - 1 elements
            REQUIRE(settingsTable_v2.removeById(1));
            REQUIRE(settingsTable_v2.count() == cnt - 1);

            // Remove non existing element
            REQUIRE(settingsTable_v2.removeById(100));

            // Table should have now cnt - 2 elements
            REQUIRE(settingsTable_v2.removeById(2));
            REQUIRE(settingsTable_v2.count() == cnt - 2);
        }

        SECTION("Remove by Field")
        {
            // Table should have now cnt - 1 elements
            REQUIRE(settingsTable_v2.removeByField(SettingsTableFields_v2::Path, "test/path1"));
            REQUIRE(settingsTable_v2.count() == cnt - 1);

            // Table should have now 0 elements
            REQUIRE(settingsTable_v2.removeByField(SettingsTableFields_v2::Value, "abc"));
            REQUIRE(settingsTable_v2.count() == 0);
        }
    }

    SECTION("Entry update")
    {
        auto settingsPre  = settingsTable_v2.getById(1);
        settingsPre.path  = "Test/test";
        settingsPre.value = "TestValue";

        REQUIRE(settingsTable_v2.update(settingsPre));

        auto settingsPost = settingsTable_v2.getById(1);
        REQUIRE(settingsPost.path == settingsPre.path);
        REQUIRE(settingsPost.value == settingsPre.value);
    }

    Database::deinitialize();
}