Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clone edge ids with block group clone #27

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
40 changes: 38 additions & 2 deletions src/models/block_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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",
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/models/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading