~aleteoryx/muditaos

ref: sign_test muditaos/module-db/tests/ContactGroups_tests.cpp -rw-r--r-- 6.6 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
// 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 "Helpers.hpp"

#include "module-db/databases/ContactsDB.hpp"
#include <Tables/ContactsTable.hpp>
#include <Tables/ContactsGroups.hpp>

namespace consts
{
    uint32_t favouritesId = 1;
    uint32_t iceId        = 2;
    uint32_t blocked      = 3;
    uint32_t temporaryId  = 4;
} // namespace consts

void addSomeContacts(ContactsDB &contactsDb);

TEST_CASE("Contact Groups tests")
{
    db::tests::DatabaseUnderTest<ContactsDB> contactsDb{"contacts.db", db::tests::getPurePhoneScriptsPath()};
    ContactsGroupsTable contactGroupsTable = ContactsGroupsTable(&contactsDb.get());
    REQUIRE(contactGroupsTable.create());

    SECTION("Adding checking standard groups")
    {
        REQUIRE(contactGroupsTable.count() == 4);
        REQUIRE(contactGroupsTable.favouritesId() == consts::favouritesId);
        REQUIRE(contactGroupsTable.iceId() == consts::iceId);
        REQUIRE(contactGroupsTable.blockedId() == consts::blocked);
        REQUIRE(contactGroupsTable.temporaryId() == consts::temporaryId);
    }

    SECTION("CRUD")
    {
        const std::string sendbajtName("#sendbajt");
        const std::string sendbajt2Name("send'bajt");
        const std::string sendbajtWrongName("sentbyte");
        const std::string sendbajtProperName("sendbajt");

        // test # in name
        ContactsGroupsTableRow sendbajtGroup(sendbajtName);
        REQUIRE(contactGroupsTable.add(sendbajtGroup));
        REQUIRE(contactGroupsTable.getId(sendbajtName) != 0);

        // test ' in name
        ContactsGroupsTableRow sendbajt2Group(sendbajt2Name);
        REQUIRE(contactGroupsTable.add(sendbajt2Group));
        REQUIRE(contactGroupsTable.getId(sendbajt2Name) != 0);

        // add wrong naame
        ContactsGroupsTableRow wrongNameGroup(sendbajtWrongName);
        REQUIRE(contactGroupsTable.add(wrongNameGroup));
        ContactsGroupsTableRow newNameGroup(sendbajtProperName);
        newNameGroup.ID = contactGroupsTable.getId(sendbajtWrongName);

        // update group name
        REQUIRE(contactGroupsTable.update(newNameGroup));
        REQUIRE(contactGroupsTable.getId(sendbajtProperName) == newNameGroup.ID);
        REQUIRE(contactGroupsTable.getById(newNameGroup.ID).name == sendbajtProperName);

        // test remove statement
        uint32_t currentItemsCount = contactGroupsTable.count();
        REQUIRE(contactGroupsTable.removeById(newNameGroup.ID));

        // test if removing works
        REQUIRE(contactGroupsTable.count() == --currentItemsCount);
        REQUIRE(contactGroupsTable.getId(newNameGroup.name) == 0);
        REQUIRE(contactGroupsTable.getById(newNameGroup.ID).name.empty());

        // adding more items to check offset and limit works;

        for (unsigned int i = 0; i < 100; ++i) {
            std::stringstream name;
            name << "group_" << std::setw(3) << std::setfill('0') << i;
            ContactsGroupsTableRow row(name.str());
            contactGroupsTable.add(row);
            currentItemsCount++;
        }

        REQUIRE(contactGroupsTable.count() == currentItemsCount);
        REQUIRE(contactGroupsTable.getLimitOffset(0, 10).size() == 10);
        REQUIRE(contactGroupsTable.getLimitOffset(0, 150).size() == currentItemsCount);
        REQUIRE(contactGroupsTable.getAllRows().size() == currentItemsCount);

        uint32_t group_000_id = contactGroupsTable.getId("group_000");
        REQUIRE(group_000_id == 7);

        auto someGroups = contactGroupsTable.getLimitOffset(7, 10);
        REQUIRE(someGroups.size() == 10);
        REQUIRE(someGroups[0].name == "group_001");
        REQUIRE(someGroups[9].name == "group_010");
        REQUIRE(someGroups[9].ID == 17);

        INFO("Adding some contacts");
        addSomeContacts(contactsDb.get());
    }

    SECTION("Update Groups")
    {
        addSomeContacts(contactsDb.get());

        ContactsGroupsTableRow favouritesGroup(contactGroupsTable.getById(contactGroupsTable.favouritesId()));
        ContactsGroupsTableRow iceGroup(contactGroupsTable.getById(contactGroupsTable.iceId()));
        ContactsGroupsTableRow blockedGroup(contactGroupsTable.getById(contactGroupsTable.blockedId()));
        ContactsGroupsTableRow familyGroup("Family");
        ContactsGroupsTableRow friendsGroup("Friends");
        ContactsGroupsTableRow workGroup("Work");

        // adding extra groups db
        contactGroupsTable.add(familyGroup);
        familyGroup.ID = contactGroupsTable.getId(familyGroup.name);
        contactGroupsTable.add(friendsGroup);
        friendsGroup.ID = contactGroupsTable.getId(friendsGroup.name);
        contactGroupsTable.add(workGroup);
        workGroup.ID = contactGroupsTable.getId(workGroup.name);

        // adding contact to some groups
        contactGroupsTable.addContactToGroup(1, contactGroupsTable.favouritesId());
        contactGroupsTable.addContactToGroup(1, contactGroupsTable.iceId());
        contactGroupsTable.addContactToGroup(1, contactGroupsTable.blockedId());
        contactGroupsTable.addContactToGroup(1, familyGroup.ID);
        contactGroupsTable.addContactToGroup(1, workGroup.ID);

        // checking if adding was successful
        REQUIRE(contactGroupsTable.getGroupsForContact(1).size() == 5);

        // creating new groups set
        std::set<ContactsGroupsTableRow> newGrupsSet;
        newGrupsSet.insert(blockedGroup);
        newGrupsSet.insert(familyGroup);
        newGrupsSet.insert(workGroup);
        newGrupsSet.insert(friendsGroup);

        // this function is tested
        contactGroupsTable.updateGroups(1, newGrupsSet);

        // getting new grups
        auto checkGroupsSet = contactGroupsTable.getGroupsForContact(1);
        auto checkEnd       = checkGroupsSet.end();

        // checking if update was successful
        REQUIRE(checkGroupsSet.size() == 4);
        REQUIRE(checkGroupsSet.find(blockedGroup) != checkEnd);
        REQUIRE(checkGroupsSet.find(familyGroup) != checkEnd);
        REQUIRE(checkGroupsSet.find(workGroup) != checkEnd);
        REQUIRE(checkGroupsSet.find(friendsGroup) != checkEnd);

        REQUIRE(checkGroupsSet.find(favouritesGroup) == checkEnd);
        REQUIRE(checkGroupsSet.find(iceGroup) == checkEnd);
    }
}

void addSomeContacts(ContactsDB &contactsDb)
{
    ContactsTableRow testRow1 = {
        Record(0), .nameID = 0, .numbersID = "0 1 2 3 4", .ringID = 0, .addressID = 0, .speedDial = "666"

    };

    // add 4 elements into table
    contactsDb.contacts.add(testRow1);
    contactsDb.contacts.add(testRow1);
    contactsDb.contacts.add(testRow1);
    contactsDb.contacts.add(testRow1);
}