~aleteoryx/muditaos

ref: 9dea864c4a0f8ba49f5a70ca4fcd7bb037a96574 muditaos/module-db/tests/SMSTable_tests.cpp -rw-r--r-- 4.1 KiB
9dea864c — Lucjan Bryndza [EGD-5153] Fix disk manager sector_count 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <vfs.hpp>

#include <catch2/catch.hpp>

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

#include <algorithm>
#include <filesystem>
#include <purefs/filesystem_paths.hpp>
#include <cstring>

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

    const auto smsPath = (purefs::dir::getUserDiskPath() / "sms.db").c_str();
    std::filesystem::remove(smsPath);

    SmsDB smsdb(smsPath);
    REQUIRE(smsdb.isInitialized());

    SMSTableRow testRow1 = {{.ID = 0},
                            .threadID  = 0,
                            .contactID = 0,
                            .date      = 0,
                            .dateSent  = 0,
                            .errorCode = 0,
                            .body      = "Test SMS message 1",
                            .type      = SMSType ::INBOX

    };

    SMSTableRow testRow2 = {{.ID = 0},
                            .threadID  = 0,
                            .contactID = 0,
                            .date      = 0,
                            .dateSent  = 0,
                            .errorCode = 0,
                            .body      = "Test Draft SMS",
                            .type      = SMSType ::DRAFT

    };

    SECTION("SMS Table test")
    {
        // add 4 elements into table
        REQUIRE(smsdb.sms.add(testRow1));
        REQUIRE(smsdb.sms.add(testRow1));
        REQUIRE(smsdb.sms.add(testRow1));
        REQUIRE(smsdb.sms.add(testRow1));

        // Table should have 4 elements
        REQUIRE(smsdb.sms.count() == 4);

        // update existing element in table
        testRow1.ID   = 4;
        testRow1.body = "updated Test SMS message ";
        REQUIRE(smsdb.sms.update(testRow1));

        // Get table row using valid ID & check if it was updated
        auto sms = smsdb.sms.getById(4);
        REQUIRE(sms.body == testRow1.body);

        // Get table row using invalid ID(should return empty smsdb.smsRow)
        auto smsFailed = smsdb.sms.getById(100);
        REQUIRE(smsFailed.body == "");

        // Get table rows using valid offset/limit parameters
        auto retOffsetLimit = smsdb.sms.getLimitOffset(0, 4);
        REQUIRE(retOffsetLimit.size() == 4);

        // Get table rows using valid offset/limit parameters and specific field's ID
        REQUIRE(smsdb.sms.getLimitOffsetByField(0, 4, SMSTableFields::Date, "0").size() == 4);

        // Get table rows using invalid limit parameters(should return 4 elements instead of 100)
        auto retOffsetLimitBigger = smsdb.sms.getLimitOffset(0, 100);
        REQUIRE(retOffsetLimitBigger.size() == 4);

        // Get table rows using invalid offset/limit parameters(should return empty object)
        auto retOffsetLimitFailed = smsdb.sms.getLimitOffset(5, 4);
        REQUIRE(retOffsetLimitFailed.size() == 0);

        // Get count of elements by field's ID
        REQUIRE(smsdb.sms.countByFieldId("thread_id", 0) == 4);

        // Get count of elements by invalid field's ID
        REQUIRE(smsdb.sms.countByFieldId("invalid_field", 0) == 0);

        REQUIRE(smsdb.sms.removeById(2));

        // Table should have now 3 elements
        REQUIRE(smsdb.sms.count() == 3);

        // Remove non existing element
        REQUIRE(smsdb.sms.removeById(100));

        // Remove all elements from table
        REQUIRE(smsdb.sms.removeById(1));
        REQUIRE(smsdb.sms.removeById(3));
        REQUIRE(smsdb.sms.removeById(4));

        // Table should be empty now
        REQUIRE(smsdb.sms.count() == 0);
    }

    SECTION("SMS Draft and Input Table test")
    {
        REQUIRE(smsdb.sms.add(testRow1));
        REQUIRE(smsdb.sms.add(testRow1));
        REQUIRE(smsdb.sms.add(testRow2));

        REQUIRE(smsdb.sms.countWithoutDraftsByThreadId(0) == 2);
        REQUIRE(smsdb.sms.count() == 3);

        REQUIRE(smsdb.sms.getDraftByThreadId(0).body == "Test Draft SMS");

        auto results = smsdb.sms.getByThreadIdWithoutDraftWithEmptyInput(0, 0, 10);

        REQUIRE(results.size() == 3);
        REQUIRE(results.back().type == SMSType ::INPUT);
    }

    Database::deinitialize();
}