~aleteoryx/muditaos

ref: sign_test muditaos/module-db/tests/SMSTemplateRecord_tests.cpp -rw-r--r-- 3.8 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
// Copyright (c) 2017-2022, 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 "Interface/SMSTemplateRecord.hpp"
#include "module-db/databases/SmsDB.hpp"

#include <algorithm>
#include <cstring>

TEST_CASE("SMS templates Record tests")
{
    db::tests::DatabaseUnderTest<SmsDB> smsDb{"sms.db", db::tests::getPurePhoneScriptsPath()};

    SMSTemplateRecordInterface SMSTemplateRecordInterface(&smsDb.get());
    SMSTemplateRecord testRec;
    testRec.text               = "Test text";
    testRec.lastUsageTimestamp = 100;

    const auto smsRecords = SMSTemplateRecordInterface.GetCount();
    // clear sms table
    for (std::uint32_t id = 1; id <= smsRecords; id++) {
        REQUIRE(SMSTemplateRecordInterface.RemoveByID(id));
    }
    // Add 4 records
    REQUIRE(SMSTemplateRecordInterface.Add(testRec));
    REQUIRE(SMSTemplateRecordInterface.Add(testRec));
    REQUIRE(SMSTemplateRecordInterface.Add(testRec));
    REQUIRE(SMSTemplateRecordInterface.Add(testRec));

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

    SECTION("Get entry by ID")
    {
        auto templ = SMSTemplateRecordInterface.GetByID(4);
        REQUIRE(templ.ID == 4);
        REQUIRE(templ.text == testRec.text);
        REQUIRE(templ.lastUsageTimestamp == testRec.lastUsageTimestamp);
    }

    SECTION("Check entry order")
    {
        for (std::uint32_t templateNumber = 1; templateNumber <= 4; templateNumber++) {
            auto messageTemplate = SMSTemplateRecordInterface.GetByID(templateNumber);
            REQUIRE(messageTemplate.ID == templateNumber);
            REQUIRE(messageTemplate.order == templateNumber);
        }
    }

    SECTION("Entry update")
    {
        testRec.ID                 = 4;
        testRec.text               = "New text";
        testRec.order              = 9;
        testRec.lastUsageTimestamp = 200;
        REQUIRE(SMSTemplateRecordInterface.Update(testRec));
        auto templ = SMSTemplateRecordInterface.GetByID(4);
        REQUIRE(templ.ID == 4);
        REQUIRE(templ.text == testRec.text);
        REQUIRE(templ.order == testRec.order);
        REQUIRE(templ.lastUsageTimestamp == testRec.lastUsageTimestamp);
    }

    SECTION("Get entry - invalid ID")
    {
        auto templ = SMSTemplateRecordInterface.GetByID(100);
        REQUIRE(templ.ID == DB_ID_NONE);
        REQUIRE(templ.text == "");
        REQUIRE(templ.lastUsageTimestamp == 0);
    }

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

        // Get table rows using invalid limit parameters(should return 4 elements instead of 100)
        auto retOffsetLimitBigger = SMSTemplateRecordInterface.GetLimitOffset(0, 100);
        REQUIRE(retOffsetLimitBigger->size() == 4);

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

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

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

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

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

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