libsql 0.9.30

The libSQL database library
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
#![allow(deprecated)]

use futures::{StreamExt, TryStreamExt};
use libsql::{
    named_params, params,
    params::{IntoParams, IntoValue},
    AuthAction, Authorization, Connection, Database, Op, Result, Value,
};
use rand::distributions::Uniform;
use rand::prelude::*;
use std::collections::HashSet;
use std::sync::{Arc, Mutex};

async fn setup() -> Connection {
    let db = Database::open(":memory:").unwrap();
    let conn = db.connect().unwrap();
    let _ = conn
        .execute("CREATE TABLE users (id INTEGER, name TEXT)", ())
        .await;
    conn
}

#[tokio::test]
async fn enable_disable_extension() {
    let db = Database::open(":memory:").unwrap();
    let conn = db.connect().unwrap();
    conn.load_extension_enable().unwrap();
    conn.load_extension_disable().unwrap();
}

#[tokio::test]
async fn add_update_hook() {
    let conn = setup().await;

    #[derive(PartialEq, Debug)]
    struct Data {
        op: Op,
        db: String,
        table: String,
        row_id: i64,
    }

    let d = Arc::new(Mutex::new(None::<Data>));

    let d_clone = d.clone();
    conn.add_update_hook(Box::new(move |op, db, table, row_id| {
        *d_clone.lock().unwrap() = Some(Data {
            op,
            db: db.to_string(),
            table: table.to_string(),
            row_id,
        });
    }))
    .unwrap();

    let _ = conn
        .execute("INSERT INTO users (id, name) VALUES (2, 'Alice')", ())
        .await
        .unwrap();

    assert_eq!(
        *d.lock().unwrap().as_ref().unwrap(),
        Data {
            op: Op::Insert,
            db: "main".to_string(),
            table: "users".to_string(),
            row_id: 1,
        }
    );

    let _ = conn
        .execute("UPDATE users SET name = 'Bob' WHERE id = 2", ())
        .await
        .unwrap();

    assert_eq!(
        *d.lock().unwrap().as_ref().unwrap(),
        Data {
            op: Op::Update,
            db: "main".to_string(),
            table: "users".to_string(),
            row_id: 1,
        }
    );

    let _ = conn
        .execute("DELETE FROM users WHERE id = 2", ())
        .await
        .unwrap();

    assert_eq!(
        *d.lock().unwrap().as_ref().unwrap(),
        Data {
            op: Op::Delete,
            db: "main".to_string(),
            table: "users".to_string(),
            row_id: 1,
        }
    );
}

#[tokio::test]
async fn connection_drops_before_statements() {
    let db = Database::open(":memory:").unwrap();
    let conn = db.connect().unwrap();
    let _stmt = conn.prepare("SELECT 1").await.unwrap();
    drop(conn);
}

#[tokio::test]
async fn file_prefix_open() {
    let tempdir = std::env::temp_dir();

    let path = tempdir.join("prefixeddata.db");

    let path = format!("file:{}", path.display());

    let db = Database::open(path).unwrap();
    let conn = db.connect().unwrap();
    let _stmt = conn.prepare("SELECT 1").await.unwrap();
    drop(conn);
}

#[tokio::test]
async fn connection_query() {
    let conn = setup().await;
    let _ = conn
        .execute("INSERT INTO users (id, name) VALUES (2, 'Alice')", ())
        .await
        .unwrap();
    let mut rows = conn.query("SELECT * FROM users", ()).await.unwrap();
    let row = rows.next().await.unwrap().unwrap();
    assert_eq!(row.get::<i32>(0).unwrap(), 2);
    assert_eq!(row.get::<String>(1).unwrap(), "Alice");
}

#[tokio::test]
async fn connection_execute_transactional_batch_success() {
    let conn = setup().await;

    conn.execute_transactional_batch(
        "CREATE TABLE foo(x INTEGER);
         CREATE TABLE bar(y TEXT);",
    )
    .await
    .unwrap();

    let mut rows = conn
        .query(
            "SELECT
                name
            FROM
                sqlite_schema
            WHERE
                type ='table' AND
                name NOT LIKE 'sqlite_%';",
            (),
        )
        .await
        .unwrap();

    let row = rows.next().await.unwrap().unwrap();
    assert_eq!(row.get::<String>(0).unwrap(), "users");

    let row = rows.next().await.unwrap().unwrap();
    assert_eq!(row.get::<String>(0).unwrap(), "foo");

    let row = rows.next().await.unwrap().unwrap();
    assert_eq!(row.get::<String>(0).unwrap(), "bar");

    assert!(rows.next().await.unwrap().is_none());
}

#[tokio::test]
async fn connection_execute_transactional_batch_fail() {
    let conn = setup().await;

    let res = conn
        .execute_transactional_batch(
            "CREATE TABLE unexpected_foo(x INTEGER);
            CREATE TABLE sqlite_schema(y TEXT);
         CREATE TABLE unexpected_bar(y TEXT);",
        )
        .await;
    assert!(res.is_err());

    let mut rows = conn
        .query(
            "SELECT
                name
            FROM
                sqlite_schema
            WHERE
                type ='table' AND
                name NOT LIKE 'sqlite_%';",
            (),
        )
        .await
        .unwrap();

    let row = rows.next().await.unwrap().unwrap();
    assert_eq!(row.get::<String>(0).unwrap(), "users");

    assert!(rows.next().await.unwrap().is_none());
}

#[tokio::test]
async fn connection_execute_transactional_batch_transaction_fail() {
    let conn = setup().await;

    let res = conn
        .execute_transactional_batch(
            "BEGIN;
        CREATE TABLE unexpected_foo(x INTEGER);
        COMMIT;
        CREATE TABLE sqlite_schema(y TEXT);
        CREATE TABLE unexpected_bar(y TEXT);",
        )
        .await;
    assert!(res.is_err());

    let mut rows = conn
        .query(
            "SELECT
                name
            FROM
                sqlite_schema
            WHERE
                type ='table' AND
                name NOT LIKE 'sqlite_%';",
            (),
        )
        .await
        .unwrap();

    let row = rows.next().await.unwrap().unwrap();
    assert_eq!(row.get::<String>(0).unwrap(), "users");

    assert!(rows.next().await.unwrap().is_none());
}

#[tokio::test]
async fn connection_execute_transactional_batch_transaction_incorrect() {
    let conn = setup().await;

    let res = conn
        .execute_transactional_batch(
            "COMMIT;
        CREATE TABLE unexpected_foo(x INTEGER);
        CREATE TABLE sqlite_schema(y TEXT);
        CREATE TABLE unexpected_bar(y TEXT);",
        )
        .await;
    assert!(res.is_err());

    let mut rows = conn
        .query(
            "SELECT
                name
            FROM
                sqlite_schema
            WHERE
                type ='table' AND
                name NOT LIKE 'sqlite_%';",
            (),
        )
        .await
        .unwrap();

    let row = rows.next().await.unwrap().unwrap();
    assert_eq!(row.get::<String>(0).unwrap(), "users");

    assert!(rows.next().await.unwrap().is_none());
}

#[tokio::test]
async fn connection_execute_batch() {
    let conn = setup().await;

    conn.execute_batch(
        "BEGIN;
         CREATE TABLE foo(x INTEGER);
         CREATE TABLE bar(y TEXT);
         COMMIT;",
    )
    .await
    .unwrap();

    let mut rows = conn
        .query(
            "SELECT 
                name
            FROM 
                sqlite_schema
            WHERE 
                type ='table' AND 
                name NOT LIKE 'sqlite_%';",
            (),
        )
        .await
        .unwrap();

    let row = rows.next().await.unwrap().unwrap();
    assert_eq!(row.get::<String>(0).unwrap(), "users");

    let row = rows.next().await.unwrap().unwrap();
    assert_eq!(row.get::<String>(0).unwrap(), "foo");

    let row = rows.next().await.unwrap().unwrap();
    assert_eq!(row.get::<String>(0).unwrap(), "bar");
}

#[tokio::test]
async fn connection_execute_batch_inserts() {
    let conn = setup().await;

    conn.execute("CREATE TABLE foo(x INTEGER)", ())
        .await
        .unwrap();

    conn.execute_batch(
        "BEGIN;
        INSERT INTO foo VALUES (1);
        INSERT INTO foo VALUES (2);
        INSERT INTO foo VALUES (3);
        COMMIT;
        ",
    )
    .await
    .unwrap();

    let mut rows = conn.query("SELECT count(*) FROM foo", ()).await.unwrap();

    let count = rows.next().await.unwrap().unwrap().get::<u64>(0).unwrap();
    assert_eq!(count, 3);
}

#[tokio::test]
async fn connection_execute_batch_inserts_returning() {
    let conn = setup().await;

    conn.execute("CREATE TABLE foo(x INTEGER)", ())
        .await
        .unwrap();

    let mut batch_rows = conn
        .execute_batch(
            "BEGIN;
            INSERT INTO foo VALUES (1) RETURNING *;
            INSERT INTO foo VALUES (2) RETURNING *;
            INSERT INTO foo VALUES (3) RETURNING *;
            COMMIT;
            ",
        )
        .await
        .unwrap();

    assert!(batch_rows.next_stmt_row().unwrap().is_none());

    let mut rows = batch_rows.next_stmt_row().unwrap().unwrap();
    assert_eq!(
        rows.next().await.unwrap().unwrap().get::<u64>(0).unwrap(),
        1
    );
    let mut rows = batch_rows.next_stmt_row().unwrap().unwrap();
    assert_eq!(
        rows.next().await.unwrap().unwrap().get::<u64>(0).unwrap(),
        2
    );

    let mut rows = batch_rows.next_stmt_row().unwrap().unwrap();
    assert_eq!(
        rows.next().await.unwrap().unwrap().get::<u64>(0).unwrap(),
        3
    );

    assert!(batch_rows.next_stmt_row().unwrap().is_none());
}

#[tokio::test]
async fn connection_execute_batch_newline() {
    // This test checks that we handle a null raw
    // stament in execute_batch. What happens when there
    // are no more queries but the sql string is not empty?
    // Well sqlite returns a null statment from prepare_v2
    // so this test checks that we check if the statement is
    // null before we try to step it!
    let conn = setup().await;

    conn.execute_batch(
        "
        create table foo (x INT);
        ",
    )
    .await
    .unwrap();
}

#[tokio::test]
async fn prepare_invalid_sql() {
    let conn = setup().await;
    let result = conn.prepare("SYNTAX ERROR").await;
    assert!(result.is_err());
    let actual = result.err().unwrap();
    match actual {
        libsql::Error::SqliteFailure(code, msg) => {
            assert_eq!(code, 1);
            assert_eq!(msg, "near \"SYNTAX\": syntax error".to_string());
        }
        _ => panic!("Expected SqliteFailure"),
    }
}

#[tokio::test]
async fn statement_query() {
    let conn = setup().await;
    let _ = conn
        .execute("INSERT INTO users (id, name) VALUES (2, 'Alice')", ())
        .await
        .unwrap();

    let params = [2];

    let mut stmt = conn
        .prepare("SELECT * FROM users WHERE id = ?1")
        .await
        .unwrap();

    let mut rows = stmt.query(&params).await.unwrap();
    let row = rows.next().await.unwrap().unwrap();

    assert_eq!(row.get::<i32>(0).unwrap(), 2);
    assert_eq!(row.get::<String>(1).unwrap(), "Alice");

    stmt.reset();

    let row = stmt.query_row(&params).await.unwrap();

    assert_eq!(row.get::<i32>(0).unwrap(), 2);
    assert_eq!(row.get::<String>(1).unwrap(), "Alice");

    stmt.reset();

    let rows = stmt.query(&params).await.unwrap();
    let mut names = rows
        .into_stream()
        .boxed()
        .map_ok(|r| r.get::<String>(1).unwrap());

    let name = names.next().await.unwrap().unwrap();

    assert_eq!(name, "Alice");
}

#[tokio::test]
async fn prepare_and_query() {
    let conn = setup().await;
    check_insert(
        &conn,
        "INSERT INTO users (id, name) VALUES (2, 'Alice')",
        (),
    )
    .await;
    check_insert(
        &conn,
        "INSERT INTO users (id, name) VALUES (?1, ?2)",
        params![2u64, "Alice".to_string()],
    )
    .await;
    check_insert(
        &conn,
        "INSERT INTO users (id, name) VALUES (?1, ?2)",
        params![2, "Alice"],
    )
    .await;
}

#[tokio::test]
async fn prepare_and_query_named_params() {
    let conn = setup().await;

    conn.query("SELECT 1", named_params![]).await.unwrap();
    conn.query("SELECT 1", params![]).await.unwrap();
    conn.query("SELECT 1", ()).await.unwrap();

    check_insert(
        &conn,
        "INSERT INTO users (id, name) VALUES (:a, :b)",
        ((":a", 2), (":b", "Alice")),
    )
    .await;

    check_insert(
        &conn,
        "INSERT INTO users (id, name) VALUES (:a, :b)",
        named_params! {
            ":a": 2,
            ":b": "Alice"
        },
    )
    .await;

    check_insert(
        &conn,
        "INSERT INTO users (id, name) VALUES (:a, :b)",
        named_params! {
            ":a": 2,
            ":b": "Alice",
        },
    )
    .await;

    check_insert(
        &conn,
        "INSERT INTO users (id, name) VALUES (@a, @b)",
        named_params! {
            "@a": 2,
            "@b": "Alice",
        },
    )
    .await;

    check_insert(
        &conn,
        "INSERT INTO users (id, name) VALUES ($a, $b)",
        named_params! {
            "$a": 2,
            "$b": "Alice",
        },
    )
    .await;

    check_insert(
        &conn,
        "INSERT INTO users (id, name) VALUES ($a, $b)",
        named_params! {
            "$a": 2,
            "$b": "Alice",
        },
    )
    .await;
}

#[tokio::test]
async fn prepare_and_dont_query() {
    // TODO: how can we check that we've cleaned up the statement?

    let conn = setup().await;

    conn.prepare("INSERT INTO users (id, name) VALUES (?1, ?2)")
        .await
        .unwrap();

    // Drop the connection explicitly here to show that we want to drop
    // it while the above statment has not been queryd.
    drop(conn);
}

async fn check_insert(conn: &Connection, sql: &str, params: impl IntoParams) {
    let _ = conn.execute(sql, params).await.unwrap();
    let mut rows = conn.query("SELECT * FROM users", ()).await.unwrap();
    let row = rows.next().await.unwrap().unwrap();
    // Use two since if you forget to insert an id it will automatically
    // be set to 1 which defeats the purpose of checking it here.
    assert_eq!(row.get::<i32>(0).unwrap(), 2);
    assert_eq!(row.get::<String>(1).unwrap(), "Alice");
}

#[tokio::test]
async fn nulls() {
    let conn = setup().await;
    let _ = conn
        .execute("INSERT INTO users (id, name) VALUES (NULL, NULL)", ())
        .await
        .unwrap();
    let mut rows = conn.query("SELECT * FROM users", ()).await.unwrap();
    let row = rows.next().await.unwrap().unwrap();
    assert!(row.get::<i32>(0).is_err());
    assert!(row.get::<String>(1).is_err());
}

#[tokio::test]
async fn blob() {
    let conn = setup().await;
    let _ = conn
        .execute("CREATE TABLE bbb (id INTEGER PRIMARY KEY, data BLOB)", ())
        .await
        .unwrap();

    let bytes = vec![2u8; 64];
    let _ = conn
        .execute("INSERT INTO bbb (data) VALUES (?1)", [bytes.clone()])
        .await
        .unwrap();

    let mut rows = conn.query("SELECT * FROM bbb", ()).await.unwrap();
    let row = rows.next().await.unwrap().unwrap();

    let out = row.get::<Vec<u8>>(1).unwrap();
    assert_eq!(&out, &bytes);

    let empty: Vec<u8> = vec![];
    let mut rows = conn
        .query(
            "INSERT INTO bbb (data) VALUES (?1) RETURNING *",
            [Value::Blob(empty.clone())],
        )
        .await
        .unwrap();
    let row = rows.next().await.unwrap().unwrap();
    assert_eq!(row.get::<Vec<u8>>(1).unwrap(), empty);
}

#[tokio::test]
async fn transaction() {
    let conn = setup().await;
    conn.execute("INSERT INTO users (id, name) VALUES (2, 'Alice')", ())
        .await
        .unwrap();
    let tx = conn.transaction().await.unwrap();
    tx.execute("INSERT INTO users (id, name) VALUES (3, 'Bob')", ())
        .await
        .unwrap();
    tx.rollback().await.unwrap();
    let mut rows = conn.query("SELECT * FROM users", ()).await.unwrap();
    let row = rows.next().await.unwrap().unwrap();
    assert_eq!(row.get::<i32>(0).unwrap(), 2);
    assert_eq!(row.get::<String>(1).unwrap(), "Alice");
    assert!(rows.next().await.unwrap().is_none());
}

#[tokio::test]
async fn custom_params() {
    let conn = setup().await;

    enum MyValue {
        Text(String),
        Int(i64),
    }

    impl IntoValue for MyValue {
        fn into_value(self) -> libsql::Result<Value> {
            match self {
                MyValue::Text(s) => Ok(Value::Text(s)),
                MyValue::Int(i) => Ok(Value::Integer(i)),
            }
        }
    }

    let params = vec![MyValue::Int(2), MyValue::Text("Alice".into())];

    conn.execute(
        "INSERT INTO users (id, name) VALUES (?1, ?2)",
        libsql::params_from_iter(params),
    )
    .await
    .unwrap();
}

#[tokio::test]
async fn debug_print_row() {
    let db = Database::open(":memory:").unwrap();
    let conn = db.connect().unwrap();
    let _ = conn
        .execute(
            "CREATE TABLE users (id INTEGER, name TEXT, score REAL, data BLOB, age INTEGER)",
            (),
        )
        .await;
    conn.execute("INSERT INTO users (id, name, score, data, age) VALUES (123, 'potato', 3.14, X'deadbeef', NULL)", ())
    .await
    .unwrap();

    let stmt = conn.prepare("SELECT * FROM users").await.unwrap();
    let mut rows = stmt.query(()).await.unwrap();
    assert_eq!(
        format!("{:?}", rows.next().await.unwrap().unwrap()),
        "{Some(\"id\"): (Integer, 123), Some(\"name\"): (Text, \"potato\"), Some(\"score\"): (Real, 3.14), Some(\"data\"): (Blob, 4), Some(\"age\"): (Null, ())}"
    );
}

#[tokio::test]
async fn fts5_invalid_tokenizer() {
    let db = Database::open(":memory:").unwrap();
    let conn = db.connect().unwrap();
    assert!(conn
        .execute(
            "CREATE VIRTUAL TABLE t USING fts5(s, tokenize='trigram case_sensitive ')",
            (),
        )
        .await
        .is_err());
    assert!(conn
        .execute(
            "CREATE VIRTUAL TABLE t USING fts5(s, tokenize='trigram remove_diacritics ')",
            (),
        )
        .await
        .is_err());
}

#[cfg(feature = "serde")]
#[tokio::test]
async fn deserialize_row() {
    let db = Database::open(":memory:").unwrap();
    let conn = db.connect().unwrap();
    let _ = conn
        .execute(
            "CREATE TABLE users (id INTEGER, name TEXT, score REAL, data BLOB, age INTEGER, status TEXT, wrapper TEXT)",
            (),
        )
        .await;
    conn.execute("INSERT INTO users (id, name, score, data, age, status, wrapper) VALUES (123, 'potato', 42.0, X'deadbeef', NULL, 'Draft', 'Published')", ())
    .await
    .unwrap();

    use serde::Deserialize;

    #[derive(Deserialize, Debug)]
    struct Data {
        id: i64,
        name: String,
        score: f64,
        data: Vec<u8>,
        age: Option<i64>,
        none: Option<()>,
        status: Status,
        wrapper: Wrapper,
    }

    #[derive(Deserialize, Debug, PartialEq)]
    enum Status {
        Draft,
        Published,
    }

    #[derive(Deserialize, Debug, PartialEq)]
    #[serde(transparent)]
    struct Wrapper(Status);

    let row = conn
        .query("SELECT * FROM users", ())
        .await
        .unwrap()
        .next()
        .await
        .unwrap()
        .unwrap();
    let data: Data = libsql::de::from_row(&row).unwrap();
    assert_eq!(data.id, 123);
    assert_eq!(data.name, "potato".to_string());
    assert_eq!(data.score, 42.0);
    assert_eq!(data.data, vec![0xde, 0xad, 0xbe, 0xef]);
    assert_eq!(data.age, None);
    assert_eq!(data.none, None);
    assert_eq!(data.status, Status::Draft);
    assert_eq!(data.wrapper, Wrapper(Status::Published));
}

#[tokio::test]
#[ignore]
// fuzz test can be run explicitly with following command:
// cargo test vector_fuzz_test -- --nocapture --include-ignored
async fn vector_fuzz_test() {
    let mut global_rng = rand::thread_rng();
    for attempt in 0..10000 {
        let seed = global_rng.next_u64();

        let mut rng =
            rand::rngs::StdRng::from_seed(unsafe { std::mem::transmute([seed, seed, seed, seed]) });
        let db = Database::open(":memory:").unwrap();
        let conn = db.connect().unwrap();
        let dim = rng.gen_range(1..=1536);
        let operations = rng.gen_range(1..128);
        println!(
            "============== ATTEMPT {} (seed {}u64, dim {}, operations {}) ================",
            attempt, seed, dim, operations
        );

        let _ = conn
            .execute(
                &format!(
                    "CREATE TABLE users (id INTEGER PRIMARY KEY, v FLOAT32({}) )",
                    dim
                ),
                (),
            )
            .await;
        // println!("CREATE TABLE users (id INTEGER PRIMARY KEY, v FLOAT32({}) );", dim);
        let _ = conn
            .execute(
                "CREATE INDEX users_idx ON users ( libsql_vector_idx(v) );",
                (),
            )
            .await;
        // println!("CREATE INDEX users_idx ON users ( libsql_vector_idx(v) );");

        let mut next_id = 1;
        let mut alive = HashSet::new();
        let uniform = Uniform::new(-1.0, 1.0);
        for _ in 0..operations {
            let operation = rng.gen_range(0..4);
            let vector: Vec<f32> = (0..dim).map(|_| rng.sample(uniform)).collect();
            let vector_str = format!(
                "[{}]",
                vector
                    .iter()
                    .map(|x| format!("{}", x))
                    .collect::<Vec<String>>()
                    .join(",")
            );
            if operation == 0 {
                // println!("INSERT INTO users VALUES ({}, vector('{}') );", next_id, vector_str);
                conn.execute(
                    "INSERT INTO users VALUES (?, vector(?) )",
                    libsql::params![next_id, vector_str],
                )
                .await
                .unwrap();
                alive.insert(next_id);
                next_id += 1;
            } else if operation == 1 {
                let id = rng.gen_range(0..next_id);
                // println!("DELETE FROM users WHERE id = {};", id);
                conn.execute("DELETE FROM users WHERE id = ?", libsql::params![id])
                    .await
                    .unwrap();
                alive.remove(&id);
            } else if operation == 2 && !alive.is_empty() {
                let id = alive.iter().collect::<Vec<_>>()[rng.gen_range(0..alive.len())];
                // println!("UPDATE users SET v = vector('{}') WHERE id = {};", vector_str, id);
                conn.execute(
                    "UPDATE users SET v = vector(?) WHERE id = ?",
                    libsql::params![vector_str, id],
                )
                .await
                .unwrap();
            } else if operation == 3 {
                let k = rng.gen_range(1..200);
                // println!("SELECT * FROM vector_top_k('users_idx', '{}', {});", vector_str, k);
                let result = conn
                    .query(
                        "SELECT * FROM vector_top_k('users_idx', ?, ?)",
                        libsql::params![vector_str, k],
                    )
                    .await
                    .unwrap();
                let count = result.into_stream().count().await;
                assert!(count <= alive.len());
                if alive.len() > 0 {
                    assert!(count > 0);
                }
            }
        }
        let _ = conn.execute("REINDEX users;", ()).await.unwrap();
    }
}

#[tokio::test]
async fn test_deny_authorizer() {
    let db = Database::open(":memory:").unwrap();
    let conn = db.connect().unwrap();
    conn.execute("CREATE TABLE users (id INTEGER, name TEXT)", ())
        .await
        .unwrap();
    conn.authorizer(Some(Arc::new(|ctx| {
        assert_eq!(
            ctx.action,
            AuthAction::Insert {
                table_name: "users"
            }
        );
        assert_eq!(ctx.database_name, Some("main"));
        assert_eq!(ctx.accessor, None);
        Authorization::Deny
    })))
    .unwrap();
    let res = conn
        .execute("INSERT INTO users (id, name) VALUES (1, 'Alice')", ())
        .await;
    assert_sqlite_error(res, libsql::ffi::SQLITE_AUTH);
    conn.authorizer(None).unwrap();
    conn.execute("INSERT INTO users (id, name) VALUES (1, 'Alice')", ())
        .await
        .unwrap();
}

#[tokio::test]
async fn test_ignore_authorizer() {
    let db = Database::open(":memory:").unwrap();
    let conn = db.connect().unwrap();
    conn.execute("CREATE TABLE users (id INTEGER, name TEXT)", ())
        .await
        .unwrap();
    conn.authorizer(Some(Arc::new(|ctx| {
        assert_eq!(
            ctx.action,
            AuthAction::Insert {
                table_name: "users"
            }
        );
        assert_eq!(ctx.database_name, Some("main"));
        assert_eq!(ctx.accessor, None);
        Authorization::Ignore
    })))
    .unwrap();
    conn.execute("INSERT INTO users (id, name) VALUES (1, 'Alice')", ())
        .await
        .unwrap();
    conn.authorizer(None).unwrap();
    let rows = conn.query("SELECT * FROM users", ()).await.unwrap();
    // There should be no rows
    assert_eq!(rows.into_stream().count().await, 0);
}

fn assert_sqlite_error<T>(res: Result<T>, code: i32) {
    match res {
        Ok(_) => panic!("Expected error, got Ok"),
        Err(e) => {
            if let libsql::Error::SqliteFailure(c, _) = e {
                assert!(c == code, "Expected error code {}, got {}", code, c);
            } else {
                panic!("Expected SqliteFailure, got {:?}", e);
            }
        }
    }
}