~aleteoryx/muditaos

ref: 78904a6b5d80ab55e1fbf2861ab3a905c96ae94d muditaos/module-db/tests/EventsTable_tests.cpp -rw-r--r-- 6.4 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
#include <catch2/catch.hpp>

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

#include "Tables/EventsTable.hpp"

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

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

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

    EventsDB eventsDb;
    REQUIRE(eventsDb.isInitialized());

    auto &eventsTbl = eventsDb.events;
    REQUIRE(eventsTbl.count() == 0);

    SECTION("Default Constructor")
    {
        EventsTableRow testRow;
        REQUIRE(testRow.ID == DB_ID_NONE);
        REQUIRE(testRow.title == "");
        REQUIRE(testRow.description == "");
        REQUIRE(testRow.date_from == 0);
        REQUIRE(testRow.date_till == 0);
        REQUIRE(testRow.reminder == 0);
        REQUIRE(testRow.repeat == 0);
        REQUIRE(testRow.time_zone == 0);
        REQUIRE_FALSE(testRow.isValid());
    }

    REQUIRE(eventsTbl.add({{.ID = 0},
                           .title       = "Event3",
                           .description = "Desc3",
                           .date_from   = 1910201424,
                           .date_till   = 1910201536,
                           .reminder    = 1,
                           .repeat      = 2,
                           .time_zone   = 1}));
    REQUIRE(eventsTbl.add({{.ID = 0},
                           .title       = "Event4",
                           .description = "Desc4",
                           .date_from   = 2104191224,
                           .date_till   = 2104201536,
                           .reminder    = 0,
                           .repeat      = 3,
                           .time_zone   = 0}));

    REQUIRE(eventsTbl.count() == 2);

    SECTION("Get entry by ID")
    {
        auto entry = eventsTbl.getById(2);
        REQUIRE(entry.ID == 2);
        REQUIRE(entry.title == "Event4");
        REQUIRE(entry.description == "Desc4");
        REQUIRE(entry.date_from == 2104191224);
        REQUIRE(entry.date_till == 2104201536);
        REQUIRE(entry.reminder == 0);
        REQUIRE(entry.repeat == 3);
        REQUIRE(entry.time_zone == 0);
        REQUIRE(entry.isValid());
    }

    SECTION("Remove entry by ID")
    {
        auto response = eventsTbl.removeById(2);
        REQUIRE(response);
        REQUIRE_FALSE(eventsTbl.count() == 2);
        REQUIRE_NOTHROW(eventsTbl.getById(2));
        auto entry = eventsTbl.getById(1);
        REQUIRE(entry.ID == 1);
        REQUIRE(entry.title == "Event3");
        REQUIRE(entry.description == "Desc3");
        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("Update entry by ID")
    {
        auto entry =
            EventsTableRow({{.ID = 2}, "TestUpdateEvent", "Tested update event", 1910201500, 1910201854, 0, 2, 1});
        REQUIRE(eventsTbl.update(entry));
        auto record = eventsTbl.getById(2);
        REQUIRE(record.ID == 2);
        REQUIRE(record.title == "TestUpdateEvent");
        REQUIRE(record.description == "Tested update event");
        REQUIRE(record.date_from == 1910201500);
        REQUIRE(record.date_till == 1910201854);
        REQUIRE(record.reminder == 0);
        REQUIRE(record.repeat == 2);
        REQUIRE(record.time_zone == 1);
        REQUIRE(record.isValid());
    }

    SECTION("Select entry by date")
    {
        REQUIRE(eventsTbl.add({{.ID = 0},
                               .title       = "Event5",
                               .description = "Desc5",
                               .date_from   = 1910201424,
                               .date_till   = 1910201536,
                               .reminder    = 1,
                               .repeat      = 2,
                               .time_zone   = 1}));
        REQUIRE(eventsTbl.add({{.ID = 0},
                               .title       = "Event6",
                               .description = "Desc6",
                               .date_from   = 2104191224,
                               .date_till   = 2104201536,
                               .reminder    = 0,
                               .repeat      = 3,
                               .time_zone   = 0}));
        REQUIRE(eventsTbl.add({{.ID = 0},
                               .title       = "Event7",
                               .description = "Desc7",
                               .date_from   = 1910201524,
                               .date_till   = 1910201536,
                               .reminder    = 1,
                               .repeat      = 2,
                               .time_zone   = 1}));
        REQUIRE(eventsTbl.add({{.ID = 0},
                               .title       = "Event8",
                               .description = "Desc8",
                               .date_from   = 1504191224,
                               .date_till   = 1504201536,
                               .reminder    = 0,
                               .repeat      = 3,
                               .time_zone   = 0}));
        REQUIRE(eventsTbl.add({{.ID = 0},
                               .title       = "Event9",
                               .description = "Desc9",
                               .date_from   = 2510201424,
                               .date_till   = 2510201536,
                               .reminder    = 1,
                               .repeat      = 2,
                               .time_zone   = 1}));
        REQUIRE(eventsTbl.add({{.ID = 0},
                               .title       = "Event10",
                               .description = "Desc10",
                               .date_from   = 2112191224,
                               .date_till   = 2112201536,
                               .reminder    = 0,
                               .repeat      = 3,
                               .time_zone   = 0}));

        auto entries = eventsTbl.selectByDatePeriod(1910201500, 2104201500);
        auto entry   = entries.back();
        REQUIRE(entry.ID == 5);
        REQUIRE(entry.title == "Event7");
        REQUIRE(entry.description == "Desc7");
        REQUIRE(entry.date_from == 1910201524);
        REQUIRE(entry.date_till == 1910201536);
        REQUIRE(entry.reminder == 1);
        REQUIRE(entry.repeat == 2);
        REQUIRE(entry.time_zone == 1);
        REQUIRE(entry.isValid());
    }

    Database::deinitialize();
}