diff --git a/src/models.rs b/src/models.rs index 3b348c3..e1fc17c 100644 --- a/src/models.rs +++ b/src/models.rs @@ -56,16 +56,20 @@ pub struct Sample { } impl Sample { - pub fn create(conn: &Connection, name: &String) -> Sample { + pub fn create(conn: &Connection, name: &str) -> Sample { let mut stmt = conn .prepare("INSERT INTO sample (name) VALUES (?1)") .unwrap(); match stmt.execute((name,)) { - Ok(_) => Sample { name: name.clone() }, + Ok(_) => Sample { + name: name.to_string(), + }, Err(rusqlite::Error::SqliteFailure(err, details)) => { if err.code == rusqlite::ErrorCode::ConstraintViolation { println!("{err:?} {details:?}"); - Sample { name: name.clone() } + Sample { + name: name.to_string(), + } } else { panic!("something bad happened querying the database") } diff --git a/src/models/block_group.rs b/src/models/block_group.rs index a23cba6..5f0900a 100644 --- a/src/models/block_group.rs +++ b/src/models/block_group.rs @@ -149,6 +149,12 @@ impl BlockGroup { vec![SQLValue::from(source_block_group_id)], ); + let edge_ids = BlockGroupEdge::edges_for_block_group(conn, source_block_group_id) + .iter() + .map(|edge| edge.id) + .collect(); + BlockGroupEdge::bulk_create(conn, target_block_group_id, edge_ids); + for path in existing_paths { let edge_ids = PathEdge::edges_for(conn, path.id) .into_iter() @@ -162,7 +168,7 @@ impl BlockGroup { conn: &Connection, collection_name: &str, sample_name: &str, - group_name: String, + group_name: &str, ) -> i32 { let mut bg_id : i32 = match conn.query_row( "select id from block_group where collection_name = ?1 AND sample_name = ?2 AND name = ?3", @@ -527,7 +533,7 @@ impl BlockGroup { mod tests { use super::*; use crate::migrations::run_migrations; - use crate::models::Collection; + use crate::models::{Collection, Sample}; fn get_connection() -> Connection { let mut conn = Connection::open_in_memory() @@ -624,6 +630,36 @@ mod tests { (block_group.id, path) } + #[test] + fn test_blockgroup_create() { + let conn = &get_connection(); + Collection::create(conn, "test"); + let bg1 = BlockGroup::create(conn, "test", None, "hg19"); + assert_eq!(bg1.collection_name, "test"); + assert_eq!(bg1.name, "hg19"); + Sample::create(conn, "sample"); + let bg2 = BlockGroup::create(conn, "test", Some("sample"), "hg19"); + assert_eq!(bg2.collection_name, "test"); + assert_eq!(bg2.name, "hg19"); + assert_eq!(bg2.sample_name, Some("sample".to_string())); + assert_ne!(bg1.id, bg2.id); + } + + #[test] + fn test_blockgroup_clone() { + let conn = &get_connection(); + Collection::create(conn, "test"); + let bg1 = BlockGroup::create(conn, "test", None, "hg19"); + assert_eq!(bg1.collection_name, "test"); + assert_eq!(bg1.name, "hg19"); + Sample::create(conn, "sample"); + let bg2 = BlockGroup::get_or_create_sample_block_group(conn, "test", "sample", "hg19"); + assert_eq!( + BlockGroupEdge::edges_for_block_group(conn, bg1.id), + BlockGroupEdge::edges_for_block_group(conn, bg2) + ); + } + #[test] fn insert_and_deletion_new_get_all() { let conn = get_connection(); diff --git a/src/models/edge.rs b/src/models/edge.rs index 3443dfd..98b8245 100644 --- a/src/models/edge.rs +++ b/src/models/edge.rs @@ -3,7 +3,7 @@ use rusqlite::{params_from_iter, Connection}; use std::collections::HashSet; use std::hash::RandomState; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Eq, PartialEq)] pub struct Edge { pub id: i32, pub source_hash: String,