~aleteoryx/muditaos

ref: sign_test muditaos/module-db/tests/NotificationsTable_tests.cpp -rw-r--r-- 5.7 KiB
a217eeb3 — Dawid Wojtas [BH-2024] Fix lack of alarm directory after updating software 1 year, 5 months 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>
#include "Helpers.hpp"

#include "Database/Database.hpp"
#include "module-db/databases/NotificationsDB.hpp"
#include "Interface/NotificationsRecord.hpp"
#include "Tables/NotificationsTable.hpp"

#include <stdint.h>
#include <string>
#include <algorithm>
#include <iostream>
#include <filesystem>

TEST_CASE("Notifications Table tests")
{
    db::tests::DatabaseUnderTest<NotificationsDB> notificationDb{"notifications.db",
                                                                 db::tests::getPurePhoneScriptsPath()};

    auto &notificationsTbl        = notificationDb.get().notifications;
    const auto notificationsCount = notificationsTbl.count() + 1;
    // clear notifications table
    for (std::size_t id = 1; id <= notificationsCount; id++) {
        REQUIRE(notificationsTbl.removeById(id));
    }
    REQUIRE(notificationsTbl.count() == 0);

    NotificationsTableRow callsRow{
        Record(DB_ID_NONE), .key = static_cast<uint32_t>(NotificationsRecord::Key::Calls), .value = 0};
    REQUIRE(notificationsTbl.add(callsRow));

    NotificationsTableRow smsRow{
        Record(DB_ID_NONE), .key = static_cast<uint32_t>(NotificationsRecord::Key::Sms), .value = 0};
    REQUIRE(notificationsTbl.add(smsRow));

    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(testRow.contactID == DB_ID_NONE);
        REQUIRE_FALSE(testRow.isValid());
    }

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

    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.contactID == 100);
        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.contactID == DB_ID_NONE);
        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({Record(3), .key = 100, .value = 200, .contactID = 300}));
        auto entry = notificationsTbl.getById(3);
        REQUIRE(entry.ID == 3);
        REQUIRE(entry.key == 100);
        REQUIRE(entry.value == 200);
        REQUIRE(entry.contactID == 300);
    }

    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);
        REQUIRE(entry.ID == DB_ID_NONE);
    }

    SECTION("Get by invalid key")
    {
        auto entry = notificationsTbl.getByKey(100);
        REQUIRE(entry.ID == DB_ID_NONE);
        REQUIRE(entry.key == 0);
        REQUIRE(entry.value == 0);
        REQUIRE(entry.ID == DB_ID_NONE);
        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({Record(0), .key = 3, .value = 100, .contactID = 200}));
        REQUIRE(notificationsTbl.count() == 4);
        auto entry = notificationsTbl.getByKey(3);
        REQUIRE(entry.isValid());
        REQUIRE(entry.ID == 3);
        REQUIRE(entry.key == 3);
        REQUIRE(entry.value == 8);
        REQUIRE(entry.contactID == DB_ID_NONE);
    }
}