use njord::condition::Condition;
use njord::sqlite;
use njord::util::JoinType;
use njord::{column::Column, condition::Value};
use std::path::Path;
use std::sync::Arc;
use crate::{Product, UsersWithJoin};
#[test]
fn select_inner_join() {
let db_relative_path = "./db/select_join.db";
let db_path = Path::new(&db_relative_path);
let conn = sqlite::open(db_path);
let columns = vec![
Column::Text("users.username".to_string()),
Column::Text("products.name".to_string()),
Column::Text("products.price".to_string()),
];
let join_condition = Condition::Eq(
"users.id".to_string(),
Value::Literal("products.user_id".to_string()),
);
match conn {
Ok(ref c) => {
let result = sqlite::select(columns)
.from(UsersWithJoin::default())
.join(
JoinType::Inner,
Arc::new(Product::default()),
join_condition,
)
.build(c);
match result {
Ok(r) => {
assert!(!r.is_empty(), "Expected results, but got none.");
}
Err(e) => panic!("Failed to SELECT with JOIN: {:?}", e),
};
}
Err(e) => panic!("Failed to SELECT: {:?}", e),
}
}
#[test]
fn select_left_join() {
let db_relative_path = "./db/select_join.db";
let db_path = Path::new(&db_relative_path);
let conn = sqlite::open(db_path);
let columns = vec![
Column::Text("users.username".to_string()),
Column::Text("products.name".to_string()),
Column::Text("products.price".to_string()),
];
let join_condition = Condition::Eq(
"users.id".to_string(),
Value::Literal("products.user_id".to_string()),
);
match conn {
Ok(ref c) => {
let result = sqlite::select(columns)
.from(UsersWithJoin::default())
.join(JoinType::Left, Arc::new(Product::default()), join_condition)
.build(c);
match result {
Ok(r) => {
assert!(!r.is_empty(), "Expected results, but got none.");
assert_eq!(r.len(), 2, "Expected 2 results from the LEFT JOIN query.");
}
Err(e) => panic!("Failed to SELECT with JOIN: {:?}", e),
};
}
Err(e) => panic!("Failed to SELECT: {:?}", e),
}
}