Skip to content

Commit

Permalink
Add left join test
Browse files Browse the repository at this point in the history
  • Loading branch information
chaseWillden committed Oct 7, 2024
1 parent 00f3f0f commit 83d3eb6
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 101 deletions.
Binary file modified njord/db/select_join.db
Binary file not shown.
17 changes: 13 additions & 4 deletions njord/tests/sqlite/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
mod open_test;
mod insert_test;
mod update_test;
mod delete_test;
mod insert_test;
mod open_test;
mod select_joins_test;
mod select_test;
mod update_test;

use njord::keys::{AutoIncrementPrimaryKey, PrimaryKey};
use njord::table::Table;
Expand Down Expand Up @@ -44,4 +45,12 @@ pub struct Product {
pub stock_quantity: usize,
pub category: Category, // one-to-one relationship
pub discount: f64,
}
}

#[derive(Table)]
#[table_name = "users"]
pub struct UsersWithJoin {
username: String,
price: f64,
name: String,
}
81 changes: 81 additions & 0 deletions njord/tests/sqlite/select_joins_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use njord::column::Column;
use njord::condition::Condition;
use njord::sqlite;
use njord::util::JoinType;
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);

// Assume we have pre-inserted some data into the users and products tables
let columns = vec![
Column::Text("users.username".to_string()),
Column::Text("products.name".to_string()),
Column::Text("products.price".to_string()),
];

// Assuming a hypothetical join condition: users.id = products.user_id
let join_condition = Condition::Eq("users.id".to_string(), "products.user_id".to_string());
match conn {
Ok(c) => {
let result = sqlite::select(&c, columns)
.from(UsersWithJoin::default())
.join(
JoinType::Inner,
Arc::new(Product::default()),
join_condition,
)
.build();
match result {
Ok(r) => {
// Check the number of results and assert against expected values
assert!(!r.is_empty(), "Expected results, but got none.");
// Further assertions on expected data can be made here based on inserted data
}
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);

// Assume we have pre-inserted some data into the users and products tables
let columns = vec![
Column::Text("users.username".to_string()),
Column::Text("products.name".to_string()),
Column::Text("products.price".to_string()),
];

// Assuming a hypothetical join condition: users.id = products.user_id
let join_condition = Condition::Eq("users.id".to_string(), "products.user_id".to_string());
match conn {
Ok(c) => {
let result = sqlite::select(&c, columns)
.from(UsersWithJoin::default())
.join(JoinType::Left, Arc::new(Product::default()), join_condition)
.build();
match result {
Ok(r) => {
// Check the number of results and assert against expected values
assert!(!r.is_empty(), "Expected results, but got none.");
assert_eq!(r.len(), 2, "Expected 2 results from the LEFT JOIN query.");
// Further assertions on expected data can be made here based on inserted data
}
Err(e) => panic!("Failed to SELECT with JOIN: {:?}", e),
};
}
Err(e) => panic!("Failed to SELECT: {:?}", e),
}
}
99 changes: 2 additions & 97 deletions njord/tests/sqlite/select_test.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,11 @@
use njord::column::Column;
use njord::condition::Condition;
use njord::keys::{AutoIncrementPrimaryKey, PrimaryKey};
use njord::keys::AutoIncrementPrimaryKey;
use njord::sqlite;
use njord::table::Table;
use njord::util::JoinType;
use njord_derive::Table;
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;

#[derive(Table, Clone)]
#[table_name = "users"]
pub struct User {
id: AutoIncrementPrimaryKey<usize>,
username: String,
email: String,
address: String,
}

#[derive(Table)]
#[table_name = "users"]
pub struct UserWithSubQuery {
id: AutoIncrementPrimaryKey<usize>,
username: String,
email: String,
address: String,
additional_address: String,
}

#[derive(Table)]
#[table_name = "users"]
pub struct UsersWithJoin {
username: String,
price: f64,
name: String,
}

#[derive(Table)]
#[table_name = "categories"]
pub struct Category {
id: PrimaryKey<usize>,
name: String,
}

#[derive(Table)]
#[table_name = "products"]
pub struct Product {
id: PrimaryKey<usize>,
name: String,
description: String,
price: f64,
stock_quantity: usize,
category: Category, // one-to-one relationship
discount: f64,
}

// #[derive(Table)]
// #[table_name = "orders"]
// pub struct Order {
// id: usize,
// user: User, // one-to-one relationship
// products: Vec<Product>, // one-to-many relationship - populates from based on junction table (gets from macro attribute "table_name" and combines them for example, orders_products)
// total_cost: f64,
// }
use crate::{User, UserWithSubQuery};

#[test]
fn open_db() {
Expand Down Expand Up @@ -546,41 +489,3 @@ fn select_in() {
Err(e) => panic!("Failed to SELECT: {:?}", e),
};
}

#[test]
fn select_join() {
let db_relative_path = "./db/select_join.db";
let db_path = Path::new(&db_relative_path);
let conn = sqlite::open(db_path);

// Assume we have pre-inserted some data into the users and products tables
let columns = vec![
Column::Text("users.username".to_string()),
Column::Text("products.name".to_string()),
Column::Text("products.price".to_string()),
];

// Assuming a hypothetical join condition: users.id = products.user_id
let join_condition = Condition::Eq("users.id".to_string(), "products.user_id".to_string());
match conn {
Ok(c) => {
let result = sqlite::select(&c, columns)
.from(UsersWithJoin::default())
.join(
JoinType::Inner,
Arc::new(Product::default()),
join_condition,
)
.build();
match result {
Ok(r) => {
// Check the number of results and assert against expected values
assert!(!r.is_empty(), "Expected results, but got none.");
// Further assertions on expected data can be made here based on inserted data
}
Err(e) => panic!("Failed to SELECT with JOIN: {:?}", e),
};
}
Err(e) => panic!("Failed to SELECT: {:?}", e),
}
}

0 comments on commit 83d3eb6

Please sign in to comment.