~aleteoryx/muditaos

ref: 8109062f8dc3db5d7151bd73e88f8f0398e14e50 muditaos/module-db/tests/test-initializer/unittest.cpp -rw-r--r-- 10.8 KiB
8109062f — rrandomsky [MOS-723] Fix for french translation for SIM cards 2 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>

#include "Database/Database.hpp"
#include "Database/DatabaseInitializer.hpp"

#include "Tables/SMSTable.hpp"

#include <algorithm>
#include <fstream>
#include <iostream>

#include <cstdint>
#include <cstdio>
#include <cstring>

TEST_CASE("Create and destroy simple database")
{
    Database::initialize();

    SECTION("Create database")
    {

        Database testDB("test.db");

        const char *media_album_table = "CREATE TABLE IF NOT EXISTS albums("
                                        "_id	        		INTEGER PRIMARY KEY,"
                                        "artist_id	            INTEGER,"
                                        "name	        		TEXT UNIQUE,"
                                        "FOREIGN KEY(artist_id) REFERENCES artists(_id)"
                                        ");";

        const char *media_artist_table = "CREATE TABLE IF NOT EXISTS artists("
                                         "_id	                INTEGER PRIMARY KEY,"
                                         "name   	            TEXT UNIQUE"
                                         ");";

        const char *media_songs_table = "CREATE TABLE IF NOT EXISTS tracks("
                                        "_id	                INTEGER PRIMARY KEY,"
                                        "filename	            TEXT UNIQUE,"
                                        "name	            	TEXT,"
                                        "duration	            INTEGER,"
                                        "artist_id	            INTEGER,"
                                        "album_id	            INTEGER,"
                                        "cover  	            INTEGER,"
                                        "FOREIGN KEY(artist_id) REFERENCES artists(_id),"
                                        "FOREIGN KEY(album_id) 	REFERENCES albums(_id)"
                                        ");";

        const char *testdb_queries[] = {media_artist_table, media_album_table, media_songs_table};

        // execute all commands from the array
        for (uint32_t i = 0; i < sizeof(testdb_queries) / sizeof(char *); i++) {
            REQUIRE(testDB.execute(testdb_queries[i]) == true);
        }
    }

    SECTION("Add records to database")
    {
        Database testDB("test.db");

        REQUIRE(testDB.execute("insert or ignore into artists ( name ) VALUES ( '%q');", "Mati Patus") == true);
        REQUIRE(testDB.execute("insert or ignore into artists ( name ) VALUES ( '%q');", "Mati Patus2") == true);
        REQUIRE(testDB.execute("insert or ignore into artists ( name ) VALUES ( '%q');", "Mati Patus3") == true);
        REQUIRE(testDB.execute("insert or ignore into artists ( name ) VALUES ( '%q');", "Mati Patus4") == true);
        REQUIRE(testDB.execute("insert or ignore into artists ( name ) VALUES ( '%q');", "Mati Patus5") == true);
        REQUIRE(testDB.execute("insert or ignore into artists ( name ) VALUES ( '%q');", "Mati Patus6") == true);
    }

    SECTION("query database")
    {
        Database testDB("test.db");

        auto queryRes = testDB.query("SELECT * from artists;");

        REQUIRE(queryRes->getFieldCount() == 2);
        REQUIRE(queryRes->getRowCount() == 6);
    }

    SECTION("Store database into backup file")
    {
        std::string backupPathDB = "testbackup.db";
        std::remove(backupPathDB.c_str());
        Database testDB("test.db");
        REQUIRE(testDB.storeIntoFile(backupPathDB) == true);
        std::ifstream f(backupPathDB);
        REQUIRE(f.good() == true);
    }

    Database::deinitialize();
}

class ScopedDir
{
  public:
    explicit ScopedDir(const std::string &p) : path(p)
    {
        if (!(std::filesystem::exists(path.c_str()))) {
            REQUIRE(std::filesystem::create_directory(path.c_str()));
        }
    }

    ~ScopedDir()
    {
        if (std::filesystem::exists(path.c_str())) {
            REQUIRE(std::filesystem::remove_all(path.c_str()) > 0);
        }
    }

    auto operator()(const std::string &file = "") -> std::filesystem::path
    {
        return path / file;
    }

  private:
    std::filesystem::path path;
};

TEST_CASE("Database initialization scripts")
{
    Database::initialize();

    const std::string script_create = "CREATE TABLE IF NOT EXISTS tracks("
                                      "_id	                INTEGER PRIMARY KEY,"
                                      "filename	            TEXT UNIQUE,"
                                      "name	            	TEXT,"
                                      "duration	            INTEGER,"
                                      "artist_id	            INTEGER,"
                                      "album_id	            INTEGER,"
                                      "cover  	            INTEGER,"
                                      "FOREIGN KEY(artist_id) REFERENCES artists(_id),"
                                      "FOREIGN KEY(album_id) 	REFERENCES albums(_id)"
                                      ");\n";

    const std::string script_insert = "insert or ignore into tracks(name) VALUES('ala ma kota');\n";

    const std::string script_comment = "--insert or ignore into tracks(name) VALUES('ula');\n"
                                       "insert or ignore into tracks(name) VALUES('ala ma kota');\n"
                                       "--insert or ignore into tracks(name) VALUES('basia');\n"
                                       "insert or ignore into tracks(name) VALUES('ala ma kota');\n";

    const std::string script_invalid = "inserts error(name) VALUES('super');\n";

    SECTION("list files")
    {
        ScopedDir dir("scripts");

        auto file = std::fopen(dir("test_1.sql").c_str(), "w");
        std::fclose(file);

        file = std::fopen(dir("test_021.sql").c_str(), "w");
        std::fclose(file);

        file = std::fopen(dir("test_011.sql").c_str(), "w");
        std::fclose(file);

        file = std::fopen(dir("test_013.sql").c_str(), "w");
        std::fclose(file);

        file = std::fopen(dir("noprefix_003.sql").c_str(), "w");
        std::fclose(file);

        Database db(dir("test.db").c_str());
        DatabaseInitializer initializer(&db);
        auto files = initializer.listFiles("scripts", "test", "sql");

        REQUIRE(files.size() == 4);
        REQUIRE(files[0] == (std::filesystem::path{"scripts"} / "test_1.sql"));
        REQUIRE(files[1] == (std::filesystem::path{"scripts"} / "test_011.sql"));
        REQUIRE(files[2] == (std::filesystem::path{"scripts"} / "test_013.sql"));
        REQUIRE(files[3] == (std::filesystem::path{"scripts"} / "test_021.sql"));
    }

    SECTION("execute single valid script")
    {
        ScopedDir dir("execute_valid_script");
        std::string test_file("test_001.sql");

        auto file = std::fopen(dir(test_file).c_str(), "w");
        std::fwrite(script_create.data(), sizeof(char), script_create.size(), file);
        std::fclose(file);

        Database db(dir("test.db").c_str());
        DatabaseInitializer initializer(&db);
        auto commands = initializer.readCommands(dir(test_file));
        REQUIRE(commands.size() == 1);

        bool r = initializer.run(dir());
        REQUIRE(r == true);
    }

    SECTION("execute 2 valid script files")
    {
        ScopedDir dir("scripts");
        std::string test_file("test_001.sql");

        auto file = std::fopen(dir(test_file).c_str(), "w");
        std::fwrite(script_create.data(), sizeof(char), script_create.size(), file);
        std::fwrite(script_insert.data(), sizeof(char), script_insert.size(), file);
        std::fclose(file);

        Database db(dir("test.db").c_str());
        DatabaseInitializer initializer(&db);
        auto commands = initializer.readCommands(dir(test_file));
        REQUIRE(commands.size() == 2);

        bool result = initializer.run(dir());
        REQUIRE(result == true);
    }

    SECTION("execute multiple valid script files")
    {
        ScopedDir dir("scripts");
        std::string test_file("test_001.sql");

        auto file = std::fopen(dir(test_file).c_str(), "w");
        std::fwrite(script_create.data(), sizeof(char), script_create.size(), file);
        std::fwrite(script_insert.data(), sizeof(char), script_insert.size(), file);
        std::fwrite(script_insert.data(), sizeof(char), script_insert.size(), file);
        std::fwrite(script_insert.data(), sizeof(char), script_insert.size(), file);
        std::fwrite(script_insert.data(), sizeof(char), script_insert.size(), file);
        std::fwrite(script_insert.data(), sizeof(char), script_insert.size(), file);
        std::fclose(file);

        Database db(dir("test.db").c_str());
        DatabaseInitializer initializer(&db);
        auto commands = initializer.readCommands(dir(test_file));
        REQUIRE(commands.size() == 6);

        bool result = initializer.run(dir());
        REQUIRE(result == true);
    }

    SECTION("execute empty script files")
    {
        ScopedDir dir("scripts");
        std::string test_file("test_001.sql");

        auto file = std::fopen(dir(test_file).c_str(), "w");
        std::fclose(file);

        Database db(dir("test.db").c_str());
        DatabaseInitializer initializer(&db);
        auto commands = initializer.readCommands(dir(test_file));
        REQUIRE(commands.empty());

        bool result = initializer.run(dir());
        REQUIRE(result == true);
    }

    SECTION("execute script file with comment")
    {
        ScopedDir dir("read_script_file_with_comment");
        std::string test_file("test_001.sql");

        auto file = std::fopen(dir(test_file).c_str(), "w");
        std::fwrite(script_create.data(), sizeof(char), script_create.size(), file);
        std::fwrite(script_comment.data(), sizeof(char), script_comment.size(), file);
        std::fclose(file);

        Database db(dir("test.db").c_str());
        DatabaseInitializer initializer(&db);
        auto commands = initializer.readCommands(dir(test_file));
        REQUIRE(commands.size() == 3);

        bool result = initializer.run(dir());
        REQUIRE(result == true);
    }

    SECTION("execute invalid script")
    {
        ScopedDir dir("execute_invalid_script");
        std::string test_file("test_001.sql");

        auto file = std::fopen(dir(test_file).c_str(), "w");
        std::fwrite(script_create.data(), sizeof(char), script_create.size(), file);
        std::fwrite(script_invalid.data(), sizeof(char), script_invalid.size(), file);
        std::fclose(file);

        Database db(dir("test.db").c_str());
        DatabaseInitializer initializer(&db);
        auto commands = initializer.readCommands(dir(test_file));
        REQUIRE(commands.size() == 2);

        bool result = initializer.run(dir());
        REQUIRE(result == false);
    }

    REQUIRE(Database::deinitialize() == true);
}