~aleteoryx/muditaos

ref: 57c7672f8f4324526dd63c614b6089fa1a9fc021 muditaos/module-db/tests/NotificationsTable_tests.cpp -rw-r--r-- 4.6 KiB
57c7672f — Piotr Tanski [EGD-4366] Global data cleanup. (#999) 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>

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

#include "Tables/NotificationsTable.hpp"

#include <vfs.hpp>
#include <stdint.h>
#include <string>
#include <algorithm>
#include <iostream>

TEST_CASE("Notifications Table tests")
{
    Database::initialize();

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

    NotificationsDB notificationsDb;
    REQUIRE(notificationsDb.isInitialized());

    auto &notificationsTbl = notificationsDb.notifications;
    REQUIRE(notificationsTbl.count() == 2); // it already got some entries Calls(1) and Sms(2)

    SECTION("Default Constructor")
    {
        NotificationsTableRow testRow;
        REQUIRE(testRow.ID == DB_ID_NONE);
        REQUIRE(testRow.key == 0);
        REQUIRE(testRow.value == 0);
        REQUIRE_FALSE(testRow.isValid());
    }

    REQUIRE(notificationsTbl.add({{.ID = 0}, .key = 3, .value = 8}));
    REQUIRE(notificationsTbl.add({{.ID = 0}, .key = 4, .value = 16}));

    REQUIRE(notificationsTbl.count() == 4);

    SECTION("Get entry by ID")
    {
        auto entry = notificationsTbl.getById(4);
        REQUIRE(entry.ID == 4);
        REQUIRE(entry.key == 4);
        REQUIRE(entry.value == 16);
        REQUIRE(entry.isValid());
    }

    SECTION("Get by key")
    {
        auto entry = notificationsTbl.getById(3);
        REQUIRE(entry.ID == 3);
        REQUIRE(entry.key == 3);
        REQUIRE(entry.value == 8);
        REQUIRE(entry.isValid());
    }

    SECTION("Get table rows")
    {
        auto retOffsetLimitSmaller = notificationsTbl.getLimitOffset(0, 2);
        REQUIRE(retOffsetLimitSmaller.size() == 2);

        auto retOffsetLimit = notificationsTbl.getLimitOffset(0, 4);
        REQUIRE(retOffsetLimit.size() == 4);

        auto retOffsetLimitBigger = notificationsTbl.getLimitOffset(0, 100);
        REQUIRE(retOffsetLimitBigger.size() == 4);

        auto retOffsetLimitFailed = notificationsTbl.getLimitOffset(5, 4);
        REQUIRE(retOffsetLimitFailed.size() == 0);
    }

    SECTION("Entry update")
    {
        REQUIRE(notificationsTbl.update({{.ID = 3}, .key = 100, .value = 200}));
        auto entry = notificationsTbl.getById(3);
        REQUIRE(entry.ID == 3);
        REQUIRE(entry.key == 100);
        REQUIRE(entry.value == 200);
    }

    SECTION("Get entry - invalid ID")
    {
        auto entry = notificationsTbl.getById(100);
        REQUIRE_FALSE(entry.isValid());
        REQUIRE(entry.ID == DB_ID_NONE);
        REQUIRE(entry.key == 0);
        REQUIRE(entry.value == 0);
    }

    SECTION("Get by invalid key")
    {
        auto entry = notificationsTbl.getById(100);
        REQUIRE(entry.ID == DB_ID_NONE);
        REQUIRE(entry.key == 0);
        REQUIRE(entry.value == 0);
        REQUIRE_FALSE(entry.isValid());
    }

    SECTION("Remove entries")
    {
        REQUIRE(notificationsTbl.removeById(2));
        REQUIRE(notificationsTbl.count() == 3);

        // Remove non exist
        REQUIRE(notificationsTbl.removeById(100));

        // Remove all elements from table
        REQUIRE(notificationsTbl.removeById(1));
        REQUIRE(notificationsTbl.removeById(3));
        REQUIRE(notificationsTbl.removeById(4));

        // Table should be empty now
        REQUIRE(notificationsTbl.count() == 0);
    }

    SECTION("Remove entries by field")
    {
        REQUIRE(notificationsTbl.removeByField(NotificationsTableFields::key, "2"));
        REQUIRE(notificationsTbl.count() == 3);

        REQUIRE(notificationsTbl.removeByField(NotificationsTableFields::key, "1"));
        REQUIRE(notificationsTbl.count() == 2);

        REQUIRE(notificationsTbl.removeByField(NotificationsTableFields::key, "1"));
        REQUIRE(notificationsTbl.count() == 2); // no change

        REQUIRE(notificationsTbl.removeByField(NotificationsTableFields::key, "100"));
        REQUIRE(notificationsTbl.count() == 2); // no change

        REQUIRE(notificationsTbl.removeByField(NotificationsTableFields::key, "3"));
        REQUIRE(notificationsTbl.count() == 1);

        REQUIRE(notificationsTbl.removeByField(NotificationsTableFields::key, "4"));
        REQUIRE(notificationsTbl.count() == 0);
    }

    SECTION("Check uniqueness")
    {
        REQUIRE(notificationsTbl.add({{.ID = 0}, .key = 3, .value = 100}));
        REQUIRE(notificationsTbl.count() == 4);
        auto entry = notificationsTbl.getById(3);
        REQUIRE(entry.ID == 3);
        REQUIRE(entry.key == 3);
        REQUIRE(entry.value == 8);
        REQUIRE(entry.isValid());
    }

    Database::deinitialize();
}