~aleteoryx/muditaos

ref: 78904a6b5d80ab55e1fbf2861ab3a905c96ae94d muditaos/module-db/tests/EventsRecord_tests.cpp -rw-r--r-- 12.3 KiB
78904a6b — Tomas Rogala [EGD-3307] Review changes 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <catch2/catch.hpp>

#include "Interface/EventsRecord.hpp"
#include "Database/Database.hpp"
#include "Databases/EventsDB.hpp"
#include "module-db/queries/calendar/QueryEventsGet.hpp"
#include "module-db/queries/calendar/QueryEventsGetAll.hpp"
#include "module-db/queries/calendar/QueryEventsGetFiltered.hpp"
#include "module-db/queries/calendar/QueryEventsAdd.hpp"
#include "module-db/queries/calendar/QueryEventsRemove.hpp"
#include "module-db/queries/calendar/QueryEventsEdit.hpp"

#include <vfs.hpp>

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

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

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

    EventsDB eventsDb;

    REQUIRE(eventsDb.isInitialized());

    SECTION("Default Constructor")
    {
        EventsRecord testRec;
        REQUIRE(testRec.title == "");
        REQUIRE(testRec.description == "");
        REQUIRE(testRec.date_from == 0);
        REQUIRE(testRec.date_till == 0);
        REQUIRE(testRec.reminder == 0);
        REQUIRE(testRec.repeat == 0);
        REQUIRE(testRec.time_zone == 0);
    }

    SECTION("Constructor from EventsTableRow")
    {
        EventsTableRow tableRow{{.ID = 10}, "Event3", "Desc3", 1910201424, 1910201536, 1, 2, 1};
        EventsRecord testRec(tableRow);
        REQUIRE(testRec.title == "Event3");
        REQUIRE(testRec.description == "Desc3");
        REQUIRE(testRec.date_from == 1910201424);
        REQUIRE(testRec.date_till == 1910201536);
        REQUIRE(testRec.reminder == 1);
        REQUIRE(testRec.repeat == 2);
        REQUIRE(testRec.time_zone == 1);
        REQUIRE(testRec.isValid());
    }

    EventsRecord testRec;
    EventsRecordInterface eventsRecordInterface(&eventsDb);

    auto numberOfEvents = eventsRecordInterface.GetCount();
    REQUIRE(numberOfEvents == 0);

    EventsTableRow tableRow{{.ID = 10}, "Event1", "Desc1", 1910201424, 1910201536, 1, 2, 1};
    auto rec = EventsRecord(tableRow);
    REQUIRE(rec.title == "Event1");
    REQUIRE(rec.description == "Desc1");
    REQUIRE(rec.date_from == 1910201424);
    REQUIRE(rec.date_till == 1910201536);
    REQUIRE(rec.reminder == 1);
    REQUIRE(rec.repeat == 2);
    REQUIRE(rec.time_zone == 1);
    REQUIRE(rec.isValid());

    REQUIRE(eventsRecordInterface.Add(rec));

    numberOfEvents = eventsRecordInterface.GetCount();
    REQUIRE(numberOfEvents == 1);

    SECTION("Get entry by ID")
    {
        auto entry = eventsRecordInterface.GetByID(1);
        REQUIRE(entry.ID == 1);
        REQUIRE(entry.title == "Event1");
        REQUIRE(entry.description == "Desc1");
        REQUIRE(entry.date_from == 1910201424);
        REQUIRE(entry.date_till == 1910201536);
        REQUIRE(entry.reminder == 1);
        REQUIRE(entry.repeat == 2);
        REQUIRE(entry.time_zone == 1);
        REQUIRE(entry.isValid());
    }

    SECTION("Get entry - invalid ID")
    {
        auto entry = eventsRecordInterface.GetByID(100);
        REQUIRE(entry.ID == DB_ID_NONE);
        REQUIRE(entry.title == "");
        REQUIRE(entry.description == "");
        REQUIRE(entry.date_from == 0);
        REQUIRE(entry.date_till == 0);
        REQUIRE(entry.reminder == 0);
        REQUIRE(entry.repeat == 0);
        REQUIRE(entry.time_zone == 0);
        REQUIRE_FALSE(entry.isValid());
    }

    EventsTableRow tableRow2{{.ID = 10}, "Event2", "Desc2", 2510201424, 2510201536, 1, 2, 1};
    auto rec2 = EventsRecord(tableRow2);
    REQUIRE(rec2.title == "Event2");
    REQUIRE(rec2.description == "Desc2");
    REQUIRE(rec2.date_from == 2510201424);
    REQUIRE(rec2.date_till == 2510201536);
    REQUIRE(rec2.reminder == 1);
    REQUIRE(rec2.repeat == 2);
    REQUIRE(rec2.time_zone == 1);
    REQUIRE(rec2.isValid());

    REQUIRE(eventsRecordInterface.Add(rec2));

    numberOfEvents = eventsRecordInterface.GetCount();
    REQUIRE(numberOfEvents == 2);

    SECTION("Get entries")
    {

        SECTION("Get records using valid offset/limit parameters")
        {
            auto retOffsetLimit = eventsRecordInterface.GetLimitOffset(0, numberOfEvents);
            REQUIRE(retOffsetLimit->size() == numberOfEvents);
        }

        SECTION("Get table rows using bigger limit parameters")
        {
            auto retOffsetLimit = eventsRecordInterface.GetLimitOffset(0, 100);
            REQUIRE(retOffsetLimit->size() == numberOfEvents);
        }

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

        SECTION("0 - get all")
        {
            auto retOffsetLimit = eventsRecordInterface.GetLimitOffset(0, 0);
            REQUIRE(retOffsetLimit->size() == numberOfEvents);
        }

        SECTION("Get table rows by SELECT")
        {
            auto retOffsetLimit = eventsRecordInterface.Select(1810201424, 1911201536);
            REQUIRE(retOffsetLimit->size() == 1);
            auto entry = retOffsetLimit->back();
            REQUIRE(entry.ID == 1);
            REQUIRE(entry.title == "Event1");
            REQUIRE(entry.description == "Desc1");
            REQUIRE(entry.date_from == 1910201424);
            REQUIRE(entry.date_till == 1910201536);
            REQUIRE(entry.reminder == 1);
            REQUIRE(entry.repeat == 2);
            REQUIRE(entry.time_zone == 1);
            REQUIRE(entry.isValid());
        }
    }

    SECTION("Entry update value")
    {
        auto entryPre        = eventsRecordInterface.GetByID(1);
        auto checkV          = entryPre.date_from;
        entryPre.title       = "newTitle";
        entryPre.description = "newDesc";
        entryPre.date_from   = 1999999999;
        entryPre.date_till   = 1999999999;
        LOG_DEBUG("LOG ON!!!!!!!!!!!!!!!!!!");
        REQUIRE(eventsRecordInterface.Update(entryPre, checkV));

        auto entry = eventsRecordInterface.GetByID(1);
        REQUIRE(entry.ID == entryPre.ID);
        REQUIRE(entry.title == entryPre.title);
        REQUIRE(entry.description == entryPre.description);
        REQUIRE(entry.date_from == entryPre.date_from);
        REQUIRE(entry.date_till == entryPre.date_till);
        REQUIRE(entry.reminder == entryPre.reminder);
        REQUIRE(entry.repeat == entryPre.repeat);
        REQUIRE(entry.time_zone == entryPre.time_zone);
    }

    EventsTableRow tableRow3{{.ID = 3}, "Event3", "Desc3", 2110201424, 2110201536, 1, 2, 1};
    auto rec3 = EventsRecord(tableRow3);
    REQUIRE(rec3.title == "Event3");
    REQUIRE(rec3.description == "Desc3");
    REQUIRE(rec3.date_from == 2110201424);
    REQUIRE(rec3.date_till == 2110201536);
    REQUIRE(rec3.reminder == 1);
    REQUIRE(rec3.repeat == 2);
    REQUIRE(rec3.time_zone == 1);
    REQUIRE(rec3.isValid());

    REQUIRE(eventsRecordInterface.Add(rec3));

    numberOfEvents = eventsRecordInterface.GetCount();
    REQUIRE(numberOfEvents == 3);

    auto getQuery =
        [&](uint32_t id, std::string title, std::string description, uint32_t date_from, uint32_t date_till) {
            auto query  = std::make_shared<db::query::events::Get>(id);
            auto ret    = eventsRecordInterface.runQuery(query);
            auto result = dynamic_cast<db::query::events::GetResult *>(ret.get());
            REQUIRE(result != nullptr);
            auto record = result->getResult();
            REQUIRE(record.isValid());
            REQUIRE(record.title == title);
            REQUIRE(record.description == description);
            REQUIRE(record.date_from == date_from);
            REQUIRE(record.date_till == date_till);

            return record.date_from;
        };

    auto getFiltered = [&](uint32_t id,
                           std::string title,
                           std::string description,
                           uint32_t date_from,
                           uint32_t date_till,
                           uint32_t filter_from,
                           uint32_t filter_till) {
        auto query  = std::make_shared<db::query::events::GetFiltered>(filter_from, filter_till);
        auto ret    = eventsRecordInterface.runQuery(query);
        auto result = dynamic_cast<db::query::events::GetFilteredResult *>(ret.get());
        REQUIRE(result != nullptr);
        auto records = result->getResult();
        auto record  = records->back();
        REQUIRE(record.isValid());
        REQUIRE(record.title == title);
        REQUIRE(record.description == description);
        REQUIRE(record.date_from == date_from);
        REQUIRE(record.date_till == date_till);
    };

    auto AddQuery =
        [&](uint32_t id, std::string title, std::string description, uint32_t date_from, uint32_t date_till) {
            EventsTableRow tableRow2{{.ID = id}, title, description, date_from, date_till, 1, 2, 1};
            auto record = EventsRecord(tableRow2);
            auto query  = std::make_shared<db::query::events::Add>(record);
            auto ret    = eventsRecordInterface.runQuery(query);
            auto result = dynamic_cast<db::query::events::AddResult *>(ret.get());
            REQUIRE(result != nullptr);
            REQUIRE(result->getResult());
        };

    auto RemoveQuery = [&](uint32_t id) {
        auto query  = std::make_shared<db::query::events::Remove>(id);
        auto ret    = eventsRecordInterface.runQuery(query);
        auto result = dynamic_cast<db::query::events::RemoveResult *>(ret.get());
        REQUIRE(result != nullptr);
        REQUIRE(result->getResult());
    };

    auto EditQuery = [&](uint32_t id,
                         std::string title,
                         std::string description,
                         uint32_t date_from,
                         uint32_t date_till,
                         uint32_t reminder,
                         uint32_t repeat,
                         uint32_t time_zone,
                         uint32_t checkV) {
        EventsTableRow tableRow2{{.ID = id}, title, description, date_from, date_till, reminder, repeat, time_zone};
        auto record = EventsRecord(tableRow2);
        auto query  = std::make_shared<db::query::events::Edit>(record, checkV);
        auto ret    = eventsRecordInterface.runQuery(query);
        auto result = dynamic_cast<db::query::events::EditResult *>(ret.get());
        REQUIRE(result != nullptr);
        REQUIRE(result->getResult());

        auto queryGet  = std::make_shared<db::query::events::Get>(id);
        auto retGet    = eventsRecordInterface.runQuery(queryGet);
        auto resultGet = dynamic_cast<db::query::events::GetResult *>(retGet.get());
        REQUIRE(result != nullptr);
        auto recordGet = resultGet->getResult();
        REQUIRE(recordGet.isValid());
        REQUIRE(recordGet.title == title);
        REQUIRE(recordGet.description == description);
        REQUIRE(recordGet.date_from == date_from);
        REQUIRE(recordGet.date_till == date_till);
        REQUIRE(recordGet.reminder == reminder);
        REQUIRE(recordGet.repeat == repeat);
        REQUIRE(recordGet.time_zone == time_zone);
    };

    SECTION("Get via query")
    {
        getQuery(3, "Event3", "Desc3", 2110201424, 2110201536);
    }

    SECTION("GetFiltered via query")
    {
        getFiltered(1, "Event1", "Desc1", 1910201424, 1910201536, 1810201424, 1912201536);
    }

    SECTION("Add via query")
    {
        AddQuery(3, "Event3", "Desc3", 2610201424, 2210201536);
        getQuery(4, "Event3", "Desc3", 2610201424, 2210201536);
    }

    SECTION("Remove via query")
    {
        RemoveQuery(2);
        auto query  = std::make_shared<db::query::events::GetAll>();
        auto ret    = eventsRecordInterface.runQuery(query);
        auto result = dynamic_cast<db::query::events::GetAllResult *>(ret.get());
        REQUIRE(result != nullptr);
        auto records = result->getResult();
        REQUIRE(records->size() == 3);
    }

    SECTION("Update via query")
    {
        uint32_t checkV = getQuery(2, "Event2", "Desc2", 2510201424, 2510201536);
        EditQuery(2, "Event3", "Desc2", 2110201424, 2110201536, 1, 2, 2, checkV);
    }

    SECTION("Get All via query")
    {
        auto query  = std::make_shared<db::query::events::GetAll>();
        auto ret    = eventsRecordInterface.runQuery(query);
        auto result = dynamic_cast<db::query::events::GetAllResult *>(ret.get());
        REQUIRE(result != nullptr);
        auto records = result->getResult();
        REQUIRE(records->size() == 3);
    }

    Database::deinitialize();
}