use stoolap::Database;
fn setup_products_table(db: &Database) {
db.execute(
"CREATE TABLE test_products (
id INTEGER,
name TEXT,
category TEXT,
price FLOAT,
in_stock BOOLEAN
)",
(),
)
.expect("Failed to create table");
db.execute(
"INSERT INTO test_products VALUES (1, 'Laptop', 'Electronics', 1200.00, true)",
(),
)
.expect("Failed to insert");
db.execute(
"INSERT INTO test_products VALUES (2, 'Smartphone', 'Electronics', 800.00, true)",
(),
)
.expect("Failed to insert");
db.execute(
"INSERT INTO test_products VALUES (3, 'Headphones', 'Electronics', 150.00, true)",
(),
)
.expect("Failed to insert");
db.execute(
"INSERT INTO test_products VALUES (4, 'T-shirt', 'Clothing', 25.00, true)",
(),
)
.expect("Failed to insert");
db.execute(
"INSERT INTO test_products VALUES (5, 'Jeans', 'Clothing', 50.00, false)",
(),
)
.expect("Failed to insert");
db.execute(
"INSERT INTO test_products VALUES (6, 'Sneakers', 'Footwear', 80.00, true)",
(),
)
.expect("Failed to insert");
db.execute(
"INSERT INTO test_products VALUES (7, 'Boots', 'Footwear', 120.00, false)",
(),
)
.expect("Failed to insert");
db.execute(
"INSERT INTO test_products VALUES (8, 'Desk', 'Furniture', 250.00, true)",
(),
)
.expect("Failed to insert");
db.execute(
"INSERT INTO test_products VALUES (9, 'Chair', 'Furniture', 150.00, true)",
(),
)
.expect("Failed to insert");
}
#[test]
fn test_not_with_inequality() {
let db = Database::open("memory://not_inequality").expect("Failed to create database");
setup_products_table(&db);
let result = db
.query(
"SELECT * FROM test_products WHERE category != 'Electronics'",
(),
)
.expect("Failed to query");
let mut count = 0;
for row in result {
let row = row.expect("Failed to get row");
let category: String = row.get(2).unwrap();
assert_ne!(category, "Electronics", "Should not include Electronics");
count += 1;
}
assert_eq!(count, 6, "Expected 6 non-Electronics products");
}
#[test]
fn test_not_as_prefix() {
let db = Database::open("memory://not_prefix").expect("Failed to create database");
setup_products_table(&db);
let result = db
.query(
"SELECT * FROM test_products WHERE NOT category = 'Electronics'",
(),
)
.expect("Failed to query");
let mut count = 0;
for row in result {
let row = row.expect("Failed to get row");
let category: String = row.get(2).unwrap();
assert_ne!(category, "Electronics", "Should not include Electronics");
count += 1;
}
assert_eq!(count, 6, "Expected 6 non-Electronics products");
}
#[test]
fn test_not_in() {
let db = Database::open("memory://not_in").expect("Failed to create database");
setup_products_table(&db);
let result = db
.query(
"SELECT * FROM test_products WHERE category NOT IN ('Electronics', 'Clothing')",
(),
)
.expect("Failed to query");
let mut count = 0;
for row in result {
let row = row.expect("Failed to get row");
let category: String = row.get(2).unwrap();
assert!(
category != "Electronics" && category != "Clothing",
"Should not include Electronics or Clothing, got {}",
category
);
count += 1;
}
assert_eq!(count, 4, "Expected 4 products (Footwear and Furniture)");
}
#[test]
fn test_not_like() {
let db = Database::open("memory://not_like").expect("Failed to create database");
setup_products_table(&db);
let result = db
.query("SELECT * FROM test_products WHERE name NOT LIKE '%e%'", ())
.expect("Failed to query");
let mut count = 0;
for row in result {
let row = row.expect("Failed to get row");
let name: String = row.get(1).unwrap();
let name_lower = name.to_lowercase();
assert!(
!name_lower.contains('e'),
"Name should not contain 'e': {}",
name
);
count += 1;
}
assert!(count >= 0, "Count should be non-negative: {}", count);
}
#[test]
fn test_not_between() {
let db = Database::open("memory://not_between").expect("Failed to create database");
setup_products_table(&db);
let result = db
.query(
"SELECT * FROM test_products WHERE price NOT BETWEEN 50 AND 200",
(),
)
.expect("Failed to query");
let mut count = 0;
for row in result {
let row = row.expect("Failed to get row");
let price: f64 = row.get(3).unwrap();
assert!(
!(50.0..=200.0).contains(&price),
"Price should be < 50 or > 200: {}",
price
);
count += 1;
}
assert_eq!(count, 4, "Expected 4 products outside 50-200 range");
}
#[test]
fn test_not_is_null() {
let db = Database::open("memory://not_is_null").expect("Failed to create database");
db.execute(
"CREATE TABLE nullable_test (
id INTEGER,
name TEXT,
value INTEGER
)",
(),
)
.expect("Failed to create table");
db.execute("INSERT INTO nullable_test VALUES (1, 'A', 10)", ())
.expect("Failed to insert");
db.execute("INSERT INTO nullable_test VALUES (2, 'B', NULL)", ())
.expect("Failed to insert");
db.execute("INSERT INTO nullable_test VALUES (3, 'C', 30)", ())
.expect("Failed to insert");
db.execute("INSERT INTO nullable_test VALUES (4, 'D', NULL)", ())
.expect("Failed to insert");
let result = db
.query("SELECT * FROM nullable_test WHERE value IS NOT NULL", ())
.expect("Failed to query");
let mut count = 0;
for row in result {
let row = row.expect("Failed to get row");
assert!(!row.is_null(2), "Value should not be NULL");
count += 1;
}
assert_eq!(count, 2, "Expected 2 rows with non-NULL value");
}
#[test]
fn test_not_boolean() {
let db = Database::open("memory://not_boolean").expect("Failed to create database");
setup_products_table(&db);
let result = db
.query("SELECT * FROM test_products WHERE NOT in_stock", ())
.expect("Failed to query");
let mut count = 0;
for row in result {
let row = row.expect("Failed to get row");
let in_stock: bool = row.get(4).unwrap();
assert!(!in_stock, "in_stock should be false");
count += 1;
}
assert_eq!(count, 2, "Expected 2 products not in stock");
}
#[test]
fn test_double_not() {
let db = Database::open("memory://double_not").expect("Failed to create database");
setup_products_table(&db);
let result = db
.query(
"SELECT * FROM test_products WHERE NOT NOT category = 'Electronics'",
(),
)
.expect("Failed to query");
let mut count = 0;
for row in result {
let row = row.expect("Failed to get row");
let category: String = row.get(2).unwrap();
assert_eq!(category, "Electronics", "Should only include Electronics");
count += 1;
}
assert_eq!(count, 3, "Expected 3 Electronics products");
}