stoolap 0.4.0

High-performance embedded SQL database with MVCC, time-travel queries, and full ACID compliance
Documentation
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// Copyright 2025 Stoolap Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! DML Function Verification Tests
//!
//! Tests to verify that functions work correctly in INSERT statements

use stoolap::Database;

/// Test UPPER function works correctly in INSERT
#[test]
fn test_dml_upper_function() {
    let db = Database::open("memory://dml_upper").expect("Failed to create database");

    db.execute(
        "CREATE TABLE verification_test (
            id INTEGER PRIMARY KEY,
            text_result TEXT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO verification_test (id, text_result) VALUES (1, UPPER('hello world'))",
        (),
    )
    .expect("Failed to insert with UPPER");

    let result: String = db
        .query_one("SELECT text_result FROM verification_test WHERE id = 1", ())
        .expect("Failed to query");

    assert_eq!(result, "HELLO WORLD", "Expected 'HELLO WORLD'");
}

/// Test CONCAT function works correctly in INSERT
#[test]
fn test_dml_concat_function() {
    let db = Database::open("memory://dml_concat").expect("Failed to create database");

    db.execute(
        "CREATE TABLE verification_test (
            id INTEGER PRIMARY KEY,
            text_result TEXT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO verification_test (id, text_result) VALUES (1, CONCAT('Hello', ' ', 'World', '!'))", ())
    .expect("Failed to insert with CONCAT");

    let result: String = db
        .query_one("SELECT text_result FROM verification_test WHERE id = 1", ())
        .expect("Failed to query");

    assert_eq!(result, "Hello World!", "Expected 'Hello World!'");
}

/// Test arithmetic expressions work correctly in INSERT
#[test]
fn test_dml_arithmetic() {
    let db = Database::open("memory://dml_arith").expect("Failed to create database");

    db.execute(
        "CREATE TABLE verification_test (
            id INTEGER PRIMARY KEY,
            number_result FLOAT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO verification_test (id, number_result) VALUES (1, 10.0 + 5.0 * 2.0)",
        (),
    )
    .expect("Failed to insert with arithmetic");

    let result: f64 = db
        .query_one(
            "SELECT number_result FROM verification_test WHERE id = 1",
            (),
        )
        .expect("Failed to query");

    // Order of operations: 10 + (5 * 2) = 20
    assert!(
        (result - 20.0).abs() < 0.01,
        "Expected 20.0, got {}",
        result
    );
}

/// Test nested functions work correctly in INSERT
#[test]
fn test_dml_nested_functions() {
    let db = Database::open("memory://dml_nested").expect("Failed to create database");

    db.execute(
        "CREATE TABLE verification_test (
            id INTEGER PRIMARY KEY,
            text_result TEXT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO verification_test (id, text_result) VALUES (1, UPPER(CONCAT('nested', ' test')))", ())
    .expect("Failed to insert with nested functions");

    let result: String = db
        .query_one("SELECT text_result FROM verification_test WHERE id = 1", ())
        .expect("Failed to query");

    assert_eq!(result, "NESTED TEST", "Expected 'NESTED TEST'");
}

/// Test CAST function works correctly in INSERT
#[test]
fn test_dml_cast_function() {
    let db = Database::open("memory://dml_cast").expect("Failed to create database");

    db.execute(
        "CREATE TABLE verification_test (
            id INTEGER PRIMARY KEY,
            text_result TEXT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO verification_test (id, text_result) VALUES (1, CAST(12345 AS TEXT))",
        (),
    )
    .expect("Failed to insert with CAST");

    let result: String = db
        .query_one("SELECT text_result FROM verification_test WHERE id = 1", ())
        .expect("Failed to query");

    assert_eq!(result, "12345", "Expected '12345'");
}

/// Test CASE expression works correctly in INSERT
#[test]
fn test_dml_case_expression() {
    let db = Database::open("memory://dml_case").expect("Failed to create database");

    db.execute(
        "CREATE TABLE verification_test (
            id INTEGER PRIMARY KEY,
            text_result TEXT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO verification_test (id, text_result) VALUES (1, CASE WHEN 5 > 3 THEN 'greater' ELSE 'less' END)", ())
    .expect("Failed to insert with CASE");

    let result: String = db
        .query_one("SELECT text_result FROM verification_test WHERE id = 1", ())
        .expect("Failed to query");

    assert_eq!(result, "greater", "Expected 'greater'");
}

/// Test NOW function works correctly in INSERT
#[test]
fn test_dml_now_function() {
    let db = Database::open("memory://dml_now").expect("Failed to create database");

    db.execute(
        "CREATE TABLE verification_test (
            id INTEGER PRIMARY KEY,
            timestamp_result TIMESTAMP
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO verification_test (id, timestamp_result) VALUES (1, NOW())",
        (),
    )
    .expect("Failed to insert with NOW");

    let result: String = db
        .query_one(
            "SELECT timestamp_result FROM verification_test WHERE id = 1",
            (),
        )
        .expect("Failed to query");

    // Just verify we got a non-empty timestamp
    assert!(
        !result.is_empty(),
        "Expected non-empty timestamp from NOW()"
    );
}

/// Test complex expression with multiple functions in INSERT
#[test]
fn test_dml_complex_expression() {
    let db = Database::open("memory://dml_complex").expect("Failed to create database");

    db.execute(
        "CREATE TABLE verification_test (
            id INTEGER PRIMARY KEY,
            text_result TEXT
        )",
        (),
    )
    .expect("Failed to create table");

    // This tests: UPPER(CONCAT('Result: ', CAST((3 + 4) * 2 AS TEXT)))
    // (3 + 4) * 2 = 14
    db.execute(
        "INSERT INTO verification_test (id, text_result) VALUES (1, UPPER(CONCAT('Result: ', CAST((3 + 4) * 2 AS TEXT))))", ())
    .expect("Failed to insert with complex expression");

    let result: String = db
        .query_one("SELECT text_result FROM verification_test WHERE id = 1", ())
        .expect("Failed to query");

    // The result format might vary - it could be "RESULT: 14" or "RESULT: 14.000000"
    assert!(
        result.starts_with("RESULT: 14"),
        "Expected result starting with 'RESULT: 14', got '{}'",
        result
    );
}

/// Test that INSERT and SELECT produce consistent results for same expression
#[test]
fn test_dml_select_consistency() {
    let db = Database::open("memory://dml_consist").expect("Failed to create database");

    db.execute(
        "CREATE TABLE comparison_test (
            id INTEGER PRIMARY KEY,
            insert_result TEXT
        )",
        (),
    )
    .expect("Failed to create table");

    // Test UPPER function consistency
    db.execute(
        "INSERT INTO comparison_test (id, insert_result) VALUES (1, UPPER('hello'))",
        (),
    )
    .expect("Failed to insert");

    let insert_result: String = db
        .query_one("SELECT insert_result FROM comparison_test WHERE id = 1", ())
        .expect("Failed to query insert result");

    let select_result: String = db
        .query_one("SELECT UPPER('hello')", ())
        .expect("Failed to query SELECT result");

    assert_eq!(
        insert_result, select_result,
        "INSERT and SELECT should produce same result for UPPER"
    );

    // Test CONCAT function consistency
    db.execute(
        "INSERT INTO comparison_test (id, insert_result) VALUES (2, CONCAT('a', 'b', 'c'))",
        (),
    )
    .expect("Failed to insert");

    let insert_result: String = db
        .query_one("SELECT insert_result FROM comparison_test WHERE id = 2", ())
        .expect("Failed to query insert result");

    let select_result: String = db
        .query_one("SELECT CONCAT('a', 'b', 'c')", ())
        .expect("Failed to query SELECT result");

    assert_eq!(
        insert_result, select_result,
        "INSERT and SELECT should produce same result for CONCAT"
    );
}

/// Test that invalid function name returns error
#[test]
fn test_dml_invalid_function() {
    let db = Database::open("memory://dml_invalid").expect("Failed to create database");

    db.execute(
        "CREATE TABLE error_test (
            id INTEGER PRIMARY KEY,
            result TEXT
        )",
        (),
    )
    .expect("Failed to create table");

    let result = db.execute(
        "INSERT INTO error_test (id, result) VALUES (1, INVALID_FUNCTION('test'))",
        (),
    );

    assert!(result.is_err(), "Expected error for non-existent function");
}

/// Test LOWER function in INSERT
#[test]
fn test_dml_lower_function() {
    let db = Database::open("memory://dml_lower").expect("Failed to create database");

    db.execute(
        "CREATE TABLE verification_test (
            id INTEGER PRIMARY KEY,
            text_result TEXT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO verification_test (id, text_result) VALUES (1, LOWER('HELLO WORLD'))",
        (),
    )
    .expect("Failed to insert with LOWER");

    let result: String = db
        .query_one("SELECT text_result FROM verification_test WHERE id = 1", ())
        .expect("Failed to query");

    assert_eq!(result, "hello world", "Expected 'hello world'");
}

/// Test LENGTH function in INSERT
#[test]
fn test_dml_length_function() {
    let db = Database::open("memory://dml_length").expect("Failed to create database");

    db.execute(
        "CREATE TABLE verification_test (
            id INTEGER PRIMARY KEY,
            int_result INTEGER
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO verification_test (id, int_result) VALUES (1, LENGTH('hello'))",
        (),
    )
    .expect("Failed to insert with LENGTH");

    let result: i64 = db
        .query_one("SELECT int_result FROM verification_test WHERE id = 1", ())
        .expect("Failed to query");

    assert_eq!(result, 5, "Expected length 5");
}

/// Test COALESCE function in INSERT
#[test]
fn test_dml_coalesce_function() {
    let db = Database::open("memory://dml_coalesce").expect("Failed to create database");

    db.execute(
        "CREATE TABLE verification_test (
            id INTEGER PRIMARY KEY,
            text_result TEXT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO verification_test (id, text_result) VALUES (1, COALESCE(NULL, 'default'))",
        (),
    )
    .expect("Failed to insert with COALESCE");

    let result: String = db
        .query_one("SELECT text_result FROM verification_test WHERE id = 1", ())
        .expect("Failed to query");

    assert_eq!(result, "default", "Expected 'default'");
}

/// Test ABS function in INSERT
#[test]
fn test_dml_abs_function() {
    let db = Database::open("memory://dml_abs").expect("Failed to create database");

    db.execute(
        "CREATE TABLE verification_test (
            id INTEGER PRIMARY KEY,
            int_result INTEGER
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO verification_test (id, int_result) VALUES (1, ABS(-42))",
        (),
    )
    .expect("Failed to insert with ABS");

    let result: i64 = db
        .query_one("SELECT int_result FROM verification_test WHERE id = 1", ())
        .expect("Failed to query");

    assert_eq!(result, 42, "Expected 42");
}

/// Test ROUND function in INSERT
#[test]
fn test_dml_round_function() {
    let db = Database::open("memory://dml_round").expect("Failed to create database");

    db.execute(
        "CREATE TABLE verification_test (
            id INTEGER PRIMARY KEY,
            float_result FLOAT
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO verification_test (id, float_result) VALUES (1, ROUND(3.54159, 2))",
        (),
    )
    .expect("Failed to insert with ROUND");

    let result: f64 = db
        .query_one(
            "SELECT float_result FROM verification_test WHERE id = 1",
            (),
        )
        .expect("Failed to query");

    assert!(
        (result - 3.54).abs() < 0.01,
        "Expected 3.54, got {}",
        result
    );
}

/// Test multiple functions in a single INSERT
#[test]
fn test_dml_multiple_functions_single_insert() {
    let db = Database::open("memory://dml_multi").expect("Failed to create database");

    db.execute(
        "CREATE TABLE verification_test (
            id INTEGER PRIMARY KEY,
            col1 TEXT,
            col2 TEXT,
            col3 INTEGER
        )",
        (),
    )
    .expect("Failed to create table");

    db.execute(
        "INSERT INTO verification_test (id, col1, col2, col3) VALUES (1, UPPER('hello'), LOWER('WORLD'), ABS(-100))", ())
    .expect("Failed to insert with multiple functions");

    let result = db
        .query(
            "SELECT col1, col2, col3 FROM verification_test WHERE id = 1",
            (),
        )
        .expect("Failed to query");

    for row in result {
        let row = row.expect("Failed to get row");
        let col1: String = row.get(0).unwrap();
        let col2: String = row.get(1).unwrap();
        let col3: i64 = row.get(2).unwrap();

        assert_eq!(col1, "HELLO");
        assert_eq!(col2, "world");
        assert_eq!(col3, 100);
    }
}