~aleteoryx/muditaos

ref: 5c2032ae929d48b62c2bcd9c0a27a38970350bfb muditaos/module-db/tests/CalllogRecord_tests.cpp -rw-r--r-- 6.5 KiB
5c2032ae — Maciej-Mudita [MOS-836] Fix for selecting SIM during onboarding 3 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>

#include "Interface/CalllogRecord.hpp"
#include "Database/Database.hpp"
#include "Databases/CalllogDB.hpp"

#include <cstdint>
#include <cstring>
#include <filesystem>
#include <algorithm>
#include <iostream>

TEST_CASE("Calllog Record tests")
{
    Database::initialize();

    const auto calllogPath  = (std::filesystem::path{"sys/user"} / "calllog.db");
    const auto contactsPath = (std::filesystem::path{"sys/user"} / "contacts.db");
    if (std::filesystem::exists(calllogPath)) {
        REQUIRE(std::filesystem::remove(calllogPath));
    }
    if (std::filesystem::exists(contactsPath)) {
        REQUIRE(std::filesystem::remove(contactsPath));
    }

    CalllogDB calllogDb(calllogPath.c_str());
    ContactsDB contactsDb(contactsPath.c_str());

    REQUIRE(calllogDb.isInitialized());
    REQUIRE(contactsDb.isInitialized());

    SECTION("Default Constructor")
    {
        CalllogRecord testRec;
        REQUIRE(testRec.ID == DB_ID_NONE);
        REQUIRE(testRec.presentation == PresentationType::PR_UNKNOWN);
        REQUIRE(testRec.date == 0);
        REQUIRE(testRec.duration == 0);
        REQUIRE(testRec.type == CallType::CT_NONE);
        REQUIRE(!testRec.phoneNumber.isValid());
        REQUIRE(testRec.isRead == true);
    }

    CalllogRecordInterface calllogRecordInterface(&calllogDb, &contactsDb);
    CalllogRecord testRec;
    testRec.presentation = PresentationType::PR_ALLOWED;
    testRec.date         = 100;
    testRec.duration     = 100;
    testRec.type         = CallType::CT_INCOMING;
    testRec.phoneNumber  = utils::PhoneNumber("600123456").getView();
    testRec.isRead       = false;

    const auto callsCount = calllogRecordInterface.GetCount() + 1;
    // clear calllog table
    for (std::size_t id = 1; id < callsCount; id++) {
        REQUIRE(calllogRecordInterface.RemoveByID(id));
    }
    // Add 4 records
    REQUIRE(calllogRecordInterface.Add(testRec));
    REQUIRE(calllogRecordInterface.Add(testRec));
    REQUIRE(calllogRecordInterface.Add(testRec));
    REQUIRE(calllogRecordInterface.Add(testRec));

    REQUIRE(calllogRecordInterface.GetCount() == 4);

    SECTION("Get entry by ID")
    {
        auto call = calllogRecordInterface.GetByID(1);
        REQUIRE(call.ID == 1);
        REQUIRE(call.presentation == testRec.presentation);
        REQUIRE(call.date == testRec.date);
        REQUIRE(call.duration == testRec.duration);
        REQUIRE(call.type == testRec.type);
        REQUIRE(call.phoneNumber == testRec.phoneNumber);
        REQUIRE(call.isRead == testRec.isRead);
    }

    SECTION("Entry update")
    {
        auto callPre         = calllogRecordInterface.GetByID(1);
        callPre.presentation = PresentationType::PR_PAYPHONE;
        callPre.date         = callPre.date + 100;
        callPre.duration     = callPre.duration + 100;
        callPre.type         = CallType::CT_MISSED;
        callPre.isRead       = false;

        REQUIRE(calllogRecordInterface.Update(callPre));

        auto callPost = calllogRecordInterface.GetByID(1);
        REQUIRE(callPost.presentation == callPre.presentation);
        REQUIRE(callPost.date == callPre.date);
        REQUIRE(callPost.duration == callPre.duration);
        REQUIRE(callPost.type == callPre.type);
        REQUIRE(callPost.isRead == callPre.isRead);
        REQUIRE(callPost.phoneNumber == callPre.phoneNumber);
    }

    SECTION("Get entry - invalid ID")
    {
        auto call = calllogRecordInterface.GetByID(100);
        REQUIRE(call.ID == DB_ID_NONE);
        REQUIRE(call.presentation == PresentationType::PR_UNKNOWN);
        REQUIRE(call.date == 0);
        REQUIRE(call.duration == 0);
        REQUIRE(call.type == CallType::CT_NONE);
        REQUIRE(!call.phoneNumber.isValid());
        REQUIRE(call.isRead == true);
    }

    SECTION("Get entries")
    {
        SECTION("Get table rows using valid offset/limit parameters")
        {
            auto retOffsetLimit = calllogRecordInterface.GetLimitOffset(0, 4);
            REQUIRE(retOffsetLimit->size() == 4);
        }

        SECTION("Get table rows using invalid limit parameters(should return 4 elements instead of 100)")
        {
            auto retOffsetLimit = calllogRecordInterface.GetLimitOffset(0, 100);
            REQUIRE(retOffsetLimit->size() == 4);
        }

        SECTION("Get table rows using invalid offset/limit parameters(should return empty object)")
        {
            auto retOffsetLimit = calllogRecordInterface.GetLimitOffset(5, 4);
            REQUIRE(retOffsetLimit->size() == 0);
        }
    }

    SECTION("Remove entries")
    {
        REQUIRE(calllogRecordInterface.RemoveByID(2));

        // Table should have now 3 elements
        REQUIRE(calllogRecordInterface.GetCount() == 3);

        // Remove non existing element
        REQUIRE(!calllogRecordInterface.RemoveByID(100));

        // Remove all elements from table
        REQUIRE(calllogRecordInterface.RemoveByID(1));
        REQUIRE(calllogRecordInterface.RemoveByID(3));
        REQUIRE(calllogRecordInterface.RemoveByID(4));

        // Table should be empty now
        REQUIRE(calllogRecordInterface.GetCount() == 0);
    }

    SECTION("Get Count")
    {
        REQUIRE(calllogRecordInterface.GetCount() == 4);
        REQUIRE(calllogRecordInterface.GetCount(EntryState::ALL) == 4);
        REQUIRE(calllogRecordInterface.GetCount(EntryState::READ) == 0);
        REQUIRE(calllogRecordInterface.GetCount(EntryState::UNREAD) == 4);
    }

    SECTION("Set All Read")
    {
        REQUIRE(calllogRecordInterface.GetCount(EntryState::UNREAD) == 4);
        REQUIRE(calllogRecordInterface.GetCount(EntryState::READ) == 0);
        REQUIRE(calllogRecordInterface.SetAllRead());
        REQUIRE(calllogRecordInterface.GetCount(EntryState::UNREAD) == 0);
        REQUIRE(calllogRecordInterface.GetCount(EntryState::READ) == 4);
    }

    SECTION("Set All Read via query")
    {
        REQUIRE(calllogRecordInterface.GetCount(EntryState::UNREAD) == 4);
        REQUIRE(calllogRecordInterface.GetCount(EntryState::READ) == 0);

        auto query  = std::make_shared<db::query::calllog::SetAllRead>();
        auto ret    = calllogRecordInterface.runQuery(query);
        auto result = dynamic_cast<db::query::calllog::SetAllReadResult *>(ret.get());
        REQUIRE(result != nullptr);
        REQUIRE(result->ret == true);

        REQUIRE(calllogRecordInterface.GetCount(EntryState::UNREAD) == 0);
        REQUIRE(calllogRecordInterface.GetCount(EntryState::READ) == 4);
    }

    Database::deinitialize();
}