~aleteoryx/muditaos

ref: a92f163afccc43e0e7e850ee9ee06ef93a1da0db muditaos/module-db/tests/unittest.cpp -rw-r--r-- 8.4 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
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
// 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 "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 = USER_PATH("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:
    ScopedDir(std::string p) : path(p)
    {
        REQUIRE(std::filesystem::create_directory(path.c_str()));
    }

    ~ScopedDir()
    {
        REQUIRE(std::filesystem::remove(path.c_str()));
    }

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

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

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

    const char *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)"
                                ");";

    const char *script_insert = "insert or ignore into artists(name) VALUES('%q');";

    const char *script_comment = "--insert or ignore into artists(name) VALUES('%q');\n"
                                 "insert or ignore into artists(name) VALUES('%q');\n"
                                 "--insert or ignore into artists(name) VALUES('%q');\n"
                                 "insert or ignore into artists(name) VALUES('%q');\n";

    const char *script_invalid = "insert artists(name) VALUES('%q');";

    SECTION("list files")
    {
        ScopedDir dir(USER_PATH("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_003.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(PATH_SYS "/" PATH_USER "/", "test", "sql");

        REQUIRE(files.size() == 4);
        REQUIRE(files[0] == USER_PATH("test_1.sql"));
        REQUIRE(files[1] == USER_PATH("test_003.sql"));
        REQUIRE(files[2] == USER_PATH("test_011.sql"));
        REQUIRE(files[3] == USER_PATH("test_021.sql"));
    }

    SECTION("read script files")
    {
        ScopedDir dir(USER_PATH("scripts"));

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

        std::fprintf(file, "%s\n", script_create);
        std::fprintf(file, "%s\n", script_insert);

        std::fclose(file);

        Database db(dir("test.db").c_str());
        DatabaseInitializer initializer(&db);
        auto commands = initializer.readCommands(dir("test_1.sql"));

        REQUIRE(commands.size() == 2);
    }

    SECTION("read empty script files")
    {
        ScopedDir dir(USER_PATH("scripts"));

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

        Database db(dir("test.db").c_str());
        DatabaseInitializer initializer(&db);
        auto commands = initializer.readCommands(dir("test_1.sql"));

        REQUIRE(commands.size() == 0);
    }

    SECTION("read script file with comment")
    {
        ScopedDir dir(USER_PATH("scripts"));

        auto file = std::fopen(dir("test_1.sql").c_str(), "w");
        std::fprintf(file, "%s\n", script_comment);
        std::fclose(file);

        Database db(dir("test.db").c_str());
        DatabaseInitializer initializer(&db);
        auto commands = initializer.readCommands(dir("test_1.sql"));

        REQUIRE(commands.size() == 2);
    }

    SECTION("execute valid script")
    {
        ScopedDir dir(USER_PATH("scripts"));

        auto file = std::fopen(dir("test_1.sql").c_str(), "w");
        std::fprintf(file, "%s\n", script_create);
        std::fclose(file);

        Database db(dir("test.db").c_str());
        DatabaseInitializer initializer(&db);
        bool r = initializer.run(dir());

        REQUIRE(r == true);
    }

    SECTION("execute invalid script")
    {
        ScopedDir dir(USER_PATH("scripts"));

        auto file = std::fopen(dir("test_1.sql").c_str(), "w");
        std::fprintf(file, "%s\n", script_invalid);
        std::fclose(file);

        Database db(dir("test.db").c_str());
        DatabaseInitializer initializer(&db);
        bool result = initializer.run(dir());

        REQUIRE(result == false);
    }

    Database::deinitialize();
}