~aleteoryx/muditaos

ref: a4406ec5a70b9e0f0eb8243c432e4bc0a89c5845 muditaos/module-db/tests/Helpers.cpp -rw-r--r-- 5.4 KiB
a4406ec5 — Bartosz [MOS-783] Fixed tests and simulator for Harmony 3 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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "Helpers.hpp"

#include <Utils.hpp>
#include <set>
#include <algorithm>
#include <utility>

namespace
{
    std::string readContent(const std::string &filename) noexcept
    {
        auto getFileSize = [](FILE *fd) -> std::size_t {
            std::fseek(fd, 0, SEEK_END);
            const auto size = std::ftell(fd);
            std::rewind(fd);
            return size;
        };

        std::vector<char> fileContent;
        if (const auto fp = std::fopen(filename.c_str(), "r")) {
            const auto fileSize = getFileSize(fp);

            fileContent.reserve(fileSize + 1);
            std::fread(fileContent.data(), 1, fileSize, fp);
            std::fclose(fp);
            fileContent[fileSize] = '\0';
            return fileContent.data();
        }
        return {};
    }

    std::vector<std::string> readCommands(const std::filesystem::path &filePath)
    {
        const auto fileContent = readContent(filePath.c_str());
        std::string currentStatement{};
        std::vector<std::string> statements{};

        std::string line{};
        for (const auto &c : fileContent) {
            if (c != '\n') {
                line += c;
            }
            else {
                if (line.empty() || utils::startsWith(line, "--")) {
                    line.clear();
                    continue;
                }
                if (utils::endsWith(line, ";")) {
                    statements.push_back(currentStatement + line);
                    currentStatement.clear();
                    line.clear();
                    continue;
                }
                currentStatement += line;

                line.clear();
            }
        }
        return statements;
    }

    std::array<std::string, 3> splitFilename(const std::string &filename)
    {
        const auto name    = filename.substr(0, filename.find("."));
        const auto prefix  = name.substr(0, name.find_last_of("_"));
        const auto postfix = name.substr(name.find_last_of("_") + 1, std::string::npos);

        return {name, prefix, postfix};
    }

    bool isOnExcludedList(const std::string &name, const std::vector<std::string> &excludedList)
    {
        return std::any_of(excludedList.begin(), excludedList.end(), [&name](const auto &entry) {
            return name.find(entry) != std::string::npos;
        });
    }

    std::vector<std::filesystem::path> listFiles(const std::filesystem::path &path,
                                                 const std::string &prefix,
                                                 const std::vector<std::string> &excludedList)
    {
        std::set<std::pair<int, std::filesystem::path>> orderedFiles;
        for (const auto &entry : std::filesystem::directory_iterator(path)) {
            if (!entry.is_directory() && entry.path().has_filename()) {
                try {
                    const auto parts       = splitFilename(entry.path().filename().string());
                    const auto &filePrefix = parts[1];
                    if (filePrefix == prefix and not isOnExcludedList(parts[0], excludedList)) {
                        orderedFiles.insert({std::stoi(parts[2]), entry.path()});
                    }
                }
                catch (std::invalid_argument &e) {
                    printf("Ignoring file: %s", entry.path().c_str());
                }
            }
        }

        std::vector<std::filesystem::path> files;
        std::for_each(orderedFiles.begin(), orderedFiles.end(), [&](auto item) { files.push_back(item.second); });
        return files;
    }

    bool executeOnDb(Database &db, const std::vector<std::string> &statements)
    {
        return std::all_of(statements.begin(), statements.end(), [&db](const auto &statement) {
            return db.execute(statement.c_str());
        });
    }
} // namespace

namespace db::tests
{
    std::filesystem::path currentFileLocation()
    {
        const std::string path = __FILE__;
        return std::filesystem::path{path.substr(0, path.rfind('/'))};
    }

    std::filesystem::path getScriptsPath()
    {
        return currentFileLocation() / "../databases/scripts";
    }

    /// TODO:
    /// This function is obviously wrong and should not exist in the first place. Unfortunately, due to the
    /// current directory structure, product specific modules and some unit tests are placed in common directories.
    /// After proper module-db and service-db split it wont' be needed as every product-specific db unit test will be
    /// placed in the correct directory.
    std::filesystem::path getPurePhoneScriptsPath()
    {
        return currentFileLocation() / "../../products/PurePhone/services/db/databases/scripts";
    }

    bool DatabaseScripts::operator()(Database &db)
    {
        const std::filesystem::path dbpath = db.getName();
        const std::string dbname           = dbpath.filename().replace_extension();

        const auto scripts = listFiles(directory, dbname, exclude);
        return std::all_of(scripts.begin(), scripts.end(), [&db](const auto &script) {
            return executeOnDb(db, readCommands(script));
        });
    }
    DatabaseScripts::DatabaseScripts(std::filesystem::path directory, std::vector<std::string> &&exclude)
        : directory{std::move(directory)}, extension{".sql"}, exclude{std::move(exclude)}
    {}
} // namespace db::tests