Line data Source code
1 : // Copyright (c) 2019-2025 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/prinbee
4 : // contact@m2osw.com
5 : //
6 : // This program is free software: you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation, either version 3 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 :
19 : // self
20 : //
21 : #include "catch_main.h"
22 :
23 :
24 : // prinbee
25 : //
26 : #include <prinbee/database/context.h>
27 : #include <prinbee/database/row.h>
28 :
29 :
30 : // advgetopt
31 : //
32 : #include <advgetopt/options.h>
33 :
34 :
35 : // C
36 : //
37 : #include <grp.h>
38 : #include <pwd.h>
39 :
40 :
41 : // last include
42 : //
43 : #include <snapdev/poison.h>
44 :
45 :
46 :
47 1 : CATCH_TEST_CASE("Context", "[centext]")
48 : {
49 : // TODO: fix this test at some point
50 :
51 1 : std::cerr << "error: ... test not working at the moment ...\n";
52 :
53 1 : CATCH_REQUIRE(true);
54 :
55 : // CATCH_START_SECTION("context: create a context")
56 : // {
57 : // std::vector<std::string> const simple_context =
58 : // {
59 : // {
60 : // "<!-- name=simple-context -->\n"
61 : // "<context>\n"
62 : // "<table name='foo' sparse='sparse' model='queue' row-key='c2,c1'>\n" // drop="..." temporary="..." secure="...">
63 : // "<block-size>4096</block-size>\n"
64 : // "<description>Create a Context</description>\n"
65 : // "<schema>\n"
66 : // "<column name='c1' type='uint16'>\n" // limited="..." encrypt="..." required="..." blob="...">
67 : // "<description>column 1</description>\n"
68 : // "<external>1Mb</external>\n"
69 : // "<default>55</default>\n"
70 : // "<min-value>0</min-value>\n"
71 : // "<max-value>100</max-value>\n"
72 : // "<min-length>1</min-length>\n"
73 : // "<max-length>10</max-length>\n"
74 : // "<validation>c1 > c2</validation>\n"
75 : // "</column>\n"
76 : // "<column name='c2' type='int16' required='required'>\n" // limited="..." encrypt="..." blob="...">
77 : // "<description>column 2</description>\n"
78 : // "<external>1Mb</external>\n"
79 : // "<default>-37</default>\n"
80 : // "<min-value>-100</min-value>\n"
81 : // "<max-value>100</max-value>\n"
82 : // "<min-length>5</min-length>\n"
83 : // "<max-length>25</max-length>\n"
84 : // "</column>\n"
85 : // "<column name='c3' type='uint64'>\n" // limited="..." encrypt="..." required="..." blob="...">
86 : // "<description>column 3</description>\n"
87 : // "<default>0</default>\n"
88 : // "</column>\n"
89 : // "</schema>\n"
90 : // "<secondary-index name='created_on'>\n"
91 : // "<order>\n"
92 : // "<column-name name='_created_on' direction='desc'/>\n"
93 : // "<column-name name='c2'>c2 * 16 + rand() % 16</column-name>\n"
94 : // "<column-name name='c1' not-null='not-null'/>\n"
95 : // "</order>\n"
96 : // "<filter>c3 > 100</filter>\n"
97 : // "</secondary-index>\n"
98 : // "<secondary-index name='priority'>\n"
99 : // "<order>\n"
100 : // "<column-name name='c3'/>\n"
101 : // "<column-name name='_created_on' direction='desc'>_created_on + c2</column-name>\n"
102 : // "<column-name name='_deleted_on' not-null='null'/>\n"
103 : // "</order>\n"
104 : // "<filter>c3 > 100</filter>\n"
105 : // "</secondary-index>\n"
106 : // "</table>\n"
107 : // "</context>\n"
108 : // }
109 : // };
110 : //
111 : // std::string const created(SNAP_CATCH2_NAMESPACE::setup_context("simple-context", simple_context));
112 : // CATCH_REQUIRE_FALSE(created.empty());
113 : // if(created.empty())
114 : // {
115 : // return;
116 : // }
117 : //
118 : // std::string database_path(created + "/database");
119 : // std::string tables_path(created + "/tables");
120 : //
121 : // // for the tests the default user/group name is the running user
122 : // //
123 : // struct passwd const * user(getpwuid(getuid()));
124 : // struct group const * group(getgrgid(getgid()));
125 : //
126 : // advgetopt::option options[] =
127 : // {
128 : // advgetopt::define_option(
129 : // advgetopt::Name("context")
130 : // , advgetopt::Flags(advgetopt::standalone_all_flags<
131 : // advgetopt::GETOPT_FLAG_GROUP_OPTIONS>())
132 : // , advgetopt::Help("context is mandatory")
133 : // //, advgetopt::DefaultValue(database_path.c_str())
134 : // ),
135 : // advgetopt::define_option(
136 : // advgetopt::Name("user")
137 : // , advgetopt::Flags(advgetopt::standalone_all_flags<
138 : // advgetopt::GETOPT_FLAG_GROUP_OPTIONS>())
139 : // , advgetopt::Help("user name for the database directory")
140 : // , advgetopt::DefaultValue(user->pw_name)
141 : // ),
142 : // advgetopt::define_option(
143 : // advgetopt::Name("group")
144 : // , advgetopt::Flags(advgetopt::standalone_all_flags<
145 : // advgetopt::GETOPT_FLAG_GROUP_OPTIONS>())
146 : // , advgetopt::Help("group name for the database directory")
147 : // , advgetopt::DefaultValue(group->gr_name)
148 : // ),
149 : // advgetopt::define_option(
150 : // advgetopt::Name("table-schema-path")
151 : // , advgetopt::Flags(advgetopt::command_flags<
152 : // advgetopt::GETOPT_FLAG_GROUP_OPTIONS
153 : // , advgetopt::GETOPT_FLAG_REQUIRED
154 : // , advgetopt::GETOPT_FLAG_MULTIPLE>())
155 : // , advgetopt::Help("path to the list of table schemata is mandatory")
156 : // ),
157 : // advgetopt::end_options()
158 : // };
159 : //
160 : // options[0].f_default = database_path.c_str();
161 : //
162 : // // TODO: once we have stdc++20, remove all defaults
163 : //#pragma GCC diagnostic push
164 : //#pragma GCC diagnostic ignored "-Wpedantic"
165 : // advgetopt::options_environment const options_environment =
166 : // {
167 : // .f_project_name = "database",
168 : // .f_group_name = nullptr,
169 : // .f_options = options,
170 : // };
171 : //#pragma GCC diagnostic pop
172 : //
173 : // char const * cargv[] =
174 : // {
175 : // "/usr/bin/xontext",
176 : // "--table-schema-path",
177 : // tables_path.c_str(),
178 : // nullptr
179 : // };
180 : // int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
181 : // char ** argv = const_cast<char **>(cargv);
182 : //
183 : // advgetopt::getopt::pointer_t opt(std::make_shared<advgetopt::getopt>(options_environment, argc, argv));
184 : // prinbee::context::pointer_t context(prinbee::context::create_context(opt));
185 : //
186 : // // make sure to reset before creating a new version otherwise
187 : // // we would have two contexts open simultaneously
188 : // //
189 : // context.reset();
190 : //
191 : // // try again, this time we hit the schema compare functionality
192 : // // (i.e. the file already exists)
193 : // //
194 : // context = prinbee::context::create_context(opt);
195 : //
196 : // prinbee::table::pointer_t table(context->get_table("wrong_name"));
197 : // CATCH_REQUIRE(table == nullptr);
198 : //
199 : // table = context->get_table("foo");
200 : // CATCH_REQUIRE(table != nullptr);
201 : //
202 : // struct row_data_t
203 : // {
204 : // typedef std::vector<row_data_t> vector_t;
205 : //
206 : // std::uint16_t f_c1 = 0;
207 : // std::int16_t f_c2 = 0;
208 : // std::uint64_t f_c3 = 0;
209 : // };
210 : // row_data_t::vector_t row_data;
211 : //
212 : // //for(int count(0); count < 580; ++count)
213 : // //for(int count(0); count < 163; ++count)
214 : // for(int count(0); count < 31; ++count)
215 : // {
216 : //std::cerr << "+++ row count = " << count << "\n";
217 : // prinbee::row::pointer_t row(table->row_new());
218 : //
219 : // prinbee::cell::pointer_t c1(row->get_cell("c1", true));
220 : // std::uint16_t const c1_value(rand() & 0xFFFF);
221 : // c1->set_uint16(c1_value);
222 : //
223 : // prinbee::cell::pointer_t c2(row->get_cell("c2", true));
224 : // std::int16_t const c2_value(rand() & 0xFFFF);
225 : // c2->set_int16(c2_value);
226 : //
227 : // prinbee::cell::pointer_t c3(row->get_cell("c3", true));
228 : // std::uint64_t c3_value(static_cast<std::uint64_t>(rand()) ^ (static_cast<std::uint64_t>(rand()) << 32));
229 : // c3_value &= -256;
230 : // c3_value |= count + 1;
231 : // c3->set_uint64(c3_value);
232 : //
233 : // row_data_t data;
234 : // data.f_c1 = c1_value;
235 : // data.f_c2 = c2_value;
236 : // data.f_c3 = c3_value;
237 : // row_data.push_back(data);
238 : //
239 : //std::cerr << "---------------------- INSERT ROW\n";
240 : // table->row_insert(row);
241 : //
242 : // // now verify that this and all the previous inserts worked
243 : // // and all the data is still accessible
244 : // //
245 : // // the indexes vector is used to search for each row in a
246 : // // random order instead of first to last
247 : // //
248 : // std::vector<int> indexes;
249 : // for(size_t p(0); p < row_data.size(); ++p)
250 : // {
251 : // indexes.push_back(p);
252 : // }
253 : // for(size_t p(0); p < row_data.size(); ++p)
254 : // {
255 : // size_t const q(rand() % row_data.size());
256 : // std::swap(indexes[p], indexes[q]);
257 : // }
258 : //std::cerr << "---------------------- VERIFY " << row_data.size() << " ROWS\n";
259 : // for(size_t p(0); p < row_data.size(); ++p)
260 : // {
261 : // row_data_t & d(row_data[indexes[p]]);
262 : //
263 : // prinbee::conditions cond;
264 : // cond.set_columns({"c1", "c2", "c3"});
265 : // prinbee::row::pointer_t key(table->row_new());
266 : // prinbee::cell::pointer_t c2_key(key->get_cell("c2", true));
267 : // c2_key->set_int16(d.f_c2);
268 : // prinbee::cell::pointer_t c1_key(key->get_cell("c1", true));
269 : // c1_key->set_uint16(d.f_c1);
270 : // cond.set_key("primary", key, prinbee::row::pointer_t());
271 : //
272 : //std::cerr << "---------------------- READ ROW: " << d.f_c2 << ", " << d.f_c1 << "\n";
273 : // prinbee::cursor::pointer_t cursor(table->row_select(cond));
274 : // prinbee::row::pointer_t r(cursor->next_row());
275 : // CATCH_REQUIRE(r != nullptr);
276 : // prinbee::cell::pointer_t c1_data(r->get_cell("c1", false));
277 : // CATCH_REQUIRE(c1_data != nullptr);
278 : // CATCH_REQUIRE(c1_data->get_uint16() == d.f_c1);
279 : // prinbee::cell::pointer_t c2_data(r->get_cell("c2", false));
280 : // CATCH_REQUIRE(c2_data != nullptr);
281 : // CATCH_REQUIRE(c2_data->get_int16() == d.f_c2);
282 : // prinbee::cell::pointer_t c3_data(r->get_cell("c3", false));
283 : // CATCH_REQUIRE(c3_data != nullptr);
284 : // CATCH_REQUIRE(c3_data->get_uint64() == d.f_c3);
285 : //
286 : // // only one primary row with a specific key
287 : // //
288 : //std::cerr << "---------------------- VERIFY UNIQUE ROW\n";
289 : // int const max(rand() % 3 + 1);
290 : // for(int i(0); i < max; ++i)
291 : // {
292 : // CATCH_REQUIRE(cursor->next_row() == nullptr);
293 : // }
294 : // }
295 : // }
296 : //
297 : // context.reset();
298 : // }
299 : // CATCH_END_SECTION()
300 1 : }
301 :
302 :
303 : // vim: ts=4 sw=4 et
|