Files
MuditaOS/module-db/tests/NotesRecord_tests.cpp
RobertPiet cafeb52103 [EGD-5317] Module-db UT solved
Vfs init.
Convertion time string to unix time returns 1 hour later than should.
DatabaseInitializer stub that just creates empty dbs with table
structure but without data. Notification table initial records moved
to Database Initilize section. UT run with a stub of Database
Initialize - new test section introduced to test original
initializer: test-initializer.
Added missing data for contacts db to run the sorted list test
and test fix.
2021-02-04 16:45:50 +01:00

85 lines
2.9 KiB
C++

// 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 <Interface/NotesRecord.hpp>
#include <queries/notes/QueryNotesGet.hpp>
#include <queries/notes/QueryNotesGetByText.hpp>
#include <queries/notes/QueryNoteRemove.hpp>
#include <queries/notes/QueryNoteStore.hpp>
#include <purefs/filesystem_paths.hpp>
#include "Database/Database.hpp"
#include "Databases/NotesDB.hpp"
#include <vfs.hpp>
TEST_CASE("Notes Record tests")
{
vfs.Init();
Database::initialize();
const auto notesDbPath = purefs::dir::getUserDiskPath() / "notes.db";
NotesDB notesDb{notesDbPath.c_str()};
REQUIRE(notesDb.isInitialized());
NotesRecordInterface notesRecordInterface{&notesDb};
notesRecordInterface.RemoveAll(); // Empty the notes database.
constexpr auto testSnippet = "TEST SNIPPET";
NotesRecord recordIn;
recordIn.snippet = testSnippet;
notesRecordInterface.Add(recordIn);
REQUIRE(notesRecordInterface.GetCount() == 1);
SECTION("Get notes query")
{
auto query = std::make_unique<db::query::QueryNotesGet>(0, notesRecordInterface.GetCount());
auto response = notesRecordInterface.runQuery(std::move(query));
auto getResult = static_cast<db::query::NotesGetResult *>(response.get());
REQUIRE(getResult->getRecords().size() == notesRecordInterface.GetCount());
REQUIRE(getResult->getCount() == notesRecordInterface.GetCount());
REQUIRE(getResult->getRecords()[0].snippet == testSnippet);
}
SECTION("Get notes by text query")
{
constexpr auto testSearch = "TEST";
auto query = std::make_unique<db::query::QueryNotesGetByText>(testSearch);
auto response = notesRecordInterface.runQuery(std::move(query));
auto getResult = static_cast<db::query::NotesGetByTextResult *>(response.get());
REQUIRE(getResult->getRecords().size() == 1);
REQUIRE(getResult->getRecords()[0].snippet == testSnippet);
}
SECTION("Add a note")
{
NotesRecord record;
record.snippet = testSnippet;
auto query = std::make_unique<db::query::QueryNoteStore>(record);
auto response = notesRecordInterface.runQuery(std::move(query));
auto addResult = static_cast<db::query::NoteStoreResult *>(response.get());
REQUIRE(addResult->succeed());
REQUIRE(notesRecordInterface.GetCount() == 2);
}
SECTION("Remove a note")
{
auto query = std::make_unique<db::query::QueryNoteRemove>(1);
auto response = notesRecordInterface.runQuery(std::move(query));
auto removeResult = static_cast<db::query::NoteRemoveResult *>(response.get());
REQUIRE(removeResult->succeed());
REQUIRE(notesRecordInterface.GetCount() == 0);
}
Database::deinitialize();
};