~aleteoryx/muditaos

ref: a92f163afccc43e0e7e850ee9ee06ef93a1da0db muditaos/module-db/tests/NotesTable_tests.cpp -rw-r--r-- 1.9 KiB
a92f163a — Wiktor S. Ovalle Correa [EGD-5137] Change iosyscalls symbols 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>

#include <Tables/NotesTable.hpp>
#include "Database/Database.hpp"
#include "Databases/NotesDB.hpp"
#include <purefs/filesystem_paths.hpp>

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

    auto notesDb = std::make_unique<NotesDB>((purefs::dir::getUserDiskPath() / "notes.db").c_str());
    REQUIRE(notesDb->isInitialized());

    NotesTable table{notesDb.get()};
    table.removeAll();
    REQUIRE(table.count() == 0);

    constexpr auto testSnippet = "TEST SNIPPET";

    NotesTableRow rowIn;
    rowIn.snippet = testSnippet;

    table.add(rowIn);
    REQUIRE(table.count() == 1);

    SECTION("Get notes")
    {
        const auto &records = table.getLimitOffset(0, table.count());
        REQUIRE(records.size() == table.count());
        REQUIRE(records[0].snippet == testSnippet);
    }

    SECTION("Get notes by text query")
    {
        constexpr auto testSearch = "TEST";
        const auto &records       = table.getByText(testSearch);
        REQUIRE(records.size() == 1);
        REQUIRE(records[0].snippet == testSnippet);
    }

    SECTION("Add a note")
    {
        NotesTableRow row;
        row.snippet = testSnippet;
        table.add(row);
        REQUIRE(table.count() == 2);
    }

    SECTION("Update a note")
    {
        constexpr auto testId             = 1;
        constexpr auto testSnippetUpdated = "UPDATED TEST SNIPPET";
        NotesTableRow row;
        row.ID      = testId;
        row.snippet = testSnippetUpdated;
        table.update(row);
        REQUIRE(table.count() == 1);

        const auto &record = table.getById(testId);
        REQUIRE(record.snippet == testSnippetUpdated);
    }

    SECTION("Remove a note")
    {
        table.removeById(1);
        REQUIRE(table.count() == 0);
    }

    Database::deinitialize();
}