use stoolap::Database;
#[test]
fn test_auto_increment_pk_table() {
let db = Database::open("memory://auto_inc_pk").expect("Failed to create database");
db.execute(
"CREATE TABLE products (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
name TEXT NOT NULL,
price FLOAT NOT NULL
)",
(),
)
.expect("Failed to create table");
db.execute(
"INSERT INTO products (id, name, price) VALUES (1, 'Product A', 10.99)",
(),
)
.expect("Failed to insert");
db.execute(
"INSERT INTO products (id, name, price) VALUES (2, 'Product B', 20.50)",
(),
)
.expect("Failed to insert");
db.execute(
"INSERT INTO products (id, name, price) VALUES (3, 'Product C', 30.75)",
(),
)
.expect("Failed to insert");
db.execute(
"INSERT INTO products (name, price) VALUES ('Product D', 40.25)",
(),
)
.expect("Failed to insert without ID");
let max_id: i64 = db
.query_one("SELECT MAX(id) FROM products", ())
.expect("Failed to query max ID");
assert_eq!(max_id, 4, "Expected max ID to be 4");
db.execute(
"INSERT INTO products (id, name, price) VALUES (10, 'Product E', 50.0)",
(),
)
.expect("Failed to insert with higher ID");
db.execute(
"INSERT INTO products (name, price) VALUES ('Product F', 60.50)",
(),
)
.expect("Failed to insert second record without ID");
let max_id: i64 = db
.query_one("SELECT MAX(id) FROM products", ())
.expect("Failed to query max ID");
assert_eq!(max_id, 11, "Expected max ID to be 11");
}
#[test]
fn test_auto_increment_non_sequential() {
let db = Database::open("memory://auto_inc_nonseq").expect("Failed to create database");
db.execute(
"CREATE TABLE items (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
name TEXT
)",
(),
)
.expect("Failed to create table");
db.execute("INSERT INTO items (id, name) VALUES (100, 'First')", ())
.unwrap();
db.execute("INSERT INTO items (name) VALUES ('Second')", ())
.unwrap();
let max_id: i64 = db
.query_one("SELECT MAX(id) FROM items", ())
.expect("Failed to query");
assert_eq!(max_id, 101);
db.execute("INSERT INTO items (id, name) VALUES (50, 'Third')", ())
.unwrap();
db.execute("INSERT INTO items (name) VALUES ('Fourth')", ())
.unwrap();
let max_id: i64 = db
.query_one("SELECT MAX(id) FROM items", ())
.expect("Failed to query");
assert_eq!(max_id, 102);
let count: i64 = db
.query_one("SELECT COUNT(*) FROM items", ())
.expect("Failed to count");
assert_eq!(count, 4);
}
#[test]
fn test_table_without_pk() {
let db = Database::open("memory://no_pk").expect("Failed to create database");
db.execute(
"CREATE TABLE events (
event_type TEXT NOT NULL,
description TEXT
)",
(),
)
.expect("Failed to create table");
db.execute(
"INSERT INTO events (event_type, description) VALUES ('START', 'System started')",
(),
)
.expect("Failed to insert");
db.execute(
"INSERT INTO events (event_type, description) VALUES ('LOG', 'Operation logged')",
(),
)
.expect("Failed to insert");
db.execute(
"INSERT INTO events (event_type, description) VALUES ('ALERT', 'Alert triggered')",
(),
)
.expect("Failed to insert");
let count: i64 = db
.query_one("SELECT COUNT(*) FROM events", ())
.expect("Failed to count");
assert_eq!(count, 3);
db.execute(
"INSERT INTO events (event_type, description) VALUES ('LOG', 'New operation logged')",
(),
)
.expect("Failed to insert fourth record");
let count: i64 = db
.query_one("SELECT COUNT(*) FROM events", ())
.expect("Failed to count");
assert_eq!(count, 4);
}
#[test]
fn test_multiple_auto_increment_inserts() {
let db = Database::open("memory://multi_auto_inc").expect("Failed to create database");
db.execute(
"CREATE TABLE users (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
username TEXT NOT NULL
)",
(),
)
.expect("Failed to create table");
for i in 1..=10 {
db.execute(
&format!("INSERT INTO users (username) VALUES ('user{}')", i),
(),
)
.expect("Failed to insert");
}
let result = db
.query("SELECT id, username FROM users ORDER BY id", ())
.expect("Failed to query");
let mut expected_id = 1;
for row in result {
let row = row.expect("Failed to get row");
let id: i64 = row.get(0).unwrap();
let username: String = row.get(1).unwrap();
assert_eq!(id, expected_id, "Expected sequential ID");
assert_eq!(username, format!("user{}", expected_id));
expected_id += 1;
}
assert_eq!(expected_id, 11, "Expected 10 records");
}
#[test]
fn test_auto_increment_with_gaps() {
let db = Database::open("memory://auto_inc_gaps").expect("Failed to create database");
db.execute(
"CREATE TABLE orders (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
product TEXT
)",
(),
)
.expect("Failed to create table");
db.execute("INSERT INTO orders (id, product) VALUES (1, 'A')", ())
.unwrap();
db.execute("INSERT INTO orders (id, product) VALUES (5, 'B')", ())
.unwrap();
db.execute("INSERT INTO orders (id, product) VALUES (10, 'C')", ())
.unwrap();
db.execute("INSERT INTO orders (product) VALUES ('D')", ())
.unwrap();
let max_id: i64 = db
.query_one("SELECT MAX(id) FROM orders", ())
.expect("Failed to query");
assert_eq!(max_id, 11);
db.execute("INSERT INTO orders (id, product) VALUES (3, 'E')", ())
.unwrap();
db.execute("INSERT INTO orders (id, product) VALUES (7, 'F')", ())
.unwrap();
let count: i64 = db
.query_one("SELECT COUNT(*) FROM orders", ())
.expect("Failed to count");
assert_eq!(count, 6);
db.execute("INSERT INTO orders (product) VALUES ('G')", ())
.unwrap();
let max_id: i64 = db
.query_one("SELECT MAX(id) FROM orders", ())
.expect("Failed to query");
assert_eq!(max_id, 12);
}