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

Leverage get_connection test helper #33

Merged
merged 1 commit into from
Sep 5, 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
13 changes: 3 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::str;
pub mod graph;
pub mod migrations;
pub mod models;
mod test_helpers;
pub mod test_helpers;

use crate::migrations::run_migrations;
use noodles::vcf::variant::record::samples::series::value::genotype::Phasing;
Expand Down Expand Up @@ -81,14 +81,7 @@ pub fn get_overlap(a: i32, b: i32, x: i32, y: i32) -> (bool, bool, bool) {
#[cfg(test)]
mod tests {
use super::*;
use crate::migrations::run_migrations;

fn get_connection() -> Connection {
let mut conn = Connection::open_in_memory()
.unwrap_or_else(|_| panic!("Error opening in memory test db"));
run_migrations(&mut conn);
conn
}
use crate::test_helpers::get_connection;

#[test]
fn it_hashes() {
Expand All @@ -100,7 +93,7 @@ mod tests {

#[test]
fn it_queries() {
let conn = get_connection();
let conn = get_connection(None);
let sequence_count: i32 = conn
.query_row(
"SELECT count(*) from sequence where hash = 'foo'",
Expand Down
19 changes: 1 addition & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,24 +441,7 @@ mod tests {
use std::fs;
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
use gen::migrations::run_migrations;

fn get_connection<'a>(db_path: impl Into<Option<&'a str>>) -> Connection {
let path: Option<&str> = db_path.into();
let mut conn;
if let Some(v) = path {
if fs::metadata(v).is_ok() {
fs::remove_file(v).unwrap();
}
conn = Connection::open(v).unwrap_or_else(|_| panic!("Error connecting to {}", v));
} else {
conn = Connection::open_in_memory()
.unwrap_or_else(|_| panic!("Error opening in memory test db"));
}
rusqlite::vtab::array::load_module(&conn).unwrap();
run_migrations(&mut conn);
conn
}
use gen::test_helpers::get_connection;

#[test]
fn test_add_fasta() {
Expand Down
35 changes: 14 additions & 21 deletions src/models/block_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,15 +563,8 @@ impl BlockGroup {
#[cfg(test)]
mod tests {
use super::*;
use crate::migrations::run_migrations;
use crate::models::{Collection, Sample};

fn get_connection() -> Connection {
let mut conn = Connection::open_in_memory()
.unwrap_or_else(|_| panic!("Error opening in memory test db"));
run_migrations(&mut conn);
conn
}
use crate::test_helpers::get_connection;

fn setup_block_group(conn: &Connection) -> (i32, Path) {
let a_seq = Sequence::new()
Expand Down Expand Up @@ -663,7 +656,7 @@ mod tests {

#[test]
fn test_blockgroup_create() {
let conn = &get_connection();
let conn = &get_connection(None);
Collection::create(conn, "test");
let bg1 = BlockGroup::create(conn, "test", None, "hg19");
assert_eq!(bg1.collection_name, "test");
Expand All @@ -678,7 +671,7 @@ mod tests {

#[test]
fn test_blockgroup_clone() {
let conn = &get_connection();
let conn = &get_connection(None);
Collection::create(conn, "test");
let bg1 = BlockGroup::create(conn, "test", None, "hg19");
assert_eq!(bg1.collection_name, "test");
Expand All @@ -693,7 +686,7 @@ mod tests {

#[test]
fn insert_and_deletion_get_all() {
let conn = get_connection();
let conn = get_connection(None);
let (block_group_id, path) = setup_block_group(&conn);
let insert_sequence = Sequence::new()
.sequence_type("DNA")
Expand Down Expand Up @@ -771,7 +764,7 @@ mod tests {

#[test]
fn simple_insert_get_all() {
let conn = get_connection();
let conn = get_connection(None);
let (block_group_id, path) = setup_block_group(&conn);
let insert_sequence = Sequence::new()
.sequence_type("DNA")
Expand Down Expand Up @@ -811,7 +804,7 @@ mod tests {

#[test]
fn insert_on_block_boundary_middle() {
let conn = get_connection();
let conn = get_connection(None);
let (block_group_id, path) = setup_block_group(&conn);
let insert_sequence = Sequence::new()
.sequence_type("DNA")
Expand Down Expand Up @@ -851,7 +844,7 @@ mod tests {

#[test]
fn insert_within_block() {
let conn = get_connection();
let conn = get_connection(None);
let (block_group_id, path) = setup_block_group(&conn);
let insert_sequence = Sequence::new()
.sequence_type("DNA")
Expand Down Expand Up @@ -891,7 +884,7 @@ mod tests {

#[test]
fn insert_on_block_boundary_start() {
let conn = get_connection();
let conn = get_connection(None);
let (block_group_id, path) = setup_block_group(&conn);
let insert_sequence = Sequence::new()
.sequence_type("DNA")
Expand Down Expand Up @@ -931,7 +924,7 @@ mod tests {

#[test]
fn insert_on_block_boundary_end() {
let conn = get_connection();
let conn = get_connection(None);
let (block_group_id, path) = setup_block_group(&conn);
let insert_sequence = Sequence::new()
.sequence_type("DNA")
Expand Down Expand Up @@ -971,7 +964,7 @@ mod tests {

#[test]
fn insert_across_entire_block_boundary() {
let conn = get_connection();
let conn = get_connection(None);
let (block_group_id, path) = setup_block_group(&conn);
let insert_sequence = Sequence::new()
.sequence_type("DNA")
Expand Down Expand Up @@ -1011,7 +1004,7 @@ mod tests {

#[test]
fn insert_across_two_blocks() {
let conn = get_connection();
let conn = get_connection(None);
let (block_group_id, path) = setup_block_group(&conn);
let insert_sequence = Sequence::new()
.sequence_type("DNA")
Expand Down Expand Up @@ -1051,7 +1044,7 @@ mod tests {

#[test]
fn insert_spanning_blocks() {
let conn = get_connection();
let conn = get_connection(None);
let (block_group_id, path) = setup_block_group(&conn);
let insert_sequence = Sequence::new()
.sequence_type("DNA")
Expand Down Expand Up @@ -1091,7 +1084,7 @@ mod tests {

#[test]
fn simple_deletion() {
let conn = get_connection();
let conn = get_connection(None);
let (block_group_id, path) = setup_block_group(&conn);
let deletion_sequence = Sequence::new()
.sequence_type("DNA")
Expand Down Expand Up @@ -1133,7 +1126,7 @@ mod tests {

#[test]
fn doesnt_apply_same_insert_twice() {
let conn = get_connection();
let conn = get_connection(None);
let (block_group_id, path) = setup_block_group(&conn);
let insert_sequence = Sequence::new()
.sequence_type("DNA")
Expand Down
14 changes: 3 additions & 11 deletions src/models/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,12 @@ mod tests {
use super::*;
use std::collections::HashMap;

use crate::migrations::run_migrations;
use crate::models::{sequence::Sequence, Collection};

fn get_connection() -> Connection {
let mut conn = Connection::open_in_memory()
.unwrap_or_else(|_| panic!("Error opening in memory test db"));
rusqlite::vtab::array::load_module(&conn).unwrap();
run_migrations(&mut conn);
conn
}
use crate::test_helpers::get_connection;

#[test]
fn test_bulk_create() {
let conn = &mut get_connection();
let conn = &mut get_connection(None);
Collection::create(conn, "test collection");
let sequence1 = Sequence::new()
.sequence_type("DNA")
Expand Down Expand Up @@ -315,7 +307,7 @@ mod tests {

#[test]
fn test_bulk_create_with_existing_edge() {
let conn = &mut get_connection();
let conn = &mut get_connection(None);
Collection::create(conn, "test collection");
let sequence1 = Sequence::new()
.sequence_type("DNA")
Expand Down
16 changes: 4 additions & 12 deletions src/models/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,20 +224,12 @@ mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;

use crate::migrations::run_migrations;
use crate::models::{block_group::BlockGroup, sequence::NewSequence, Collection};

fn get_connection() -> Connection {
let mut conn = Connection::open_in_memory()
.unwrap_or_else(|_| panic!("Error opening in memory test db"));
rusqlite::vtab::array::load_module(&conn).unwrap();
run_migrations(&mut conn);
conn
}
use crate::test_helpers::get_connection;

#[test]
fn test_gets_sequence() {
let conn = &mut get_connection();
let conn = &mut get_connection(None);
Collection::create(conn, "test collection");
let block_group = BlockGroup::create(conn, "test collection", None, "test block group");
let sequence1 = Sequence::new()
Expand Down Expand Up @@ -323,7 +315,7 @@ mod tests {

#[test]
fn test_gets_sequence_with_rc() {
let conn = &mut get_connection();
let conn = &mut get_connection(None);
Collection::create(conn, "test collection");
let block_group = BlockGroup::create(conn, "test collection", None, "test block group");
let sequence1 = Sequence::new()
Expand Down Expand Up @@ -416,7 +408,7 @@ mod tests {

#[test]
fn test_intervaltree() {
let conn = &mut get_connection();
let conn = &mut get_connection(None);
Collection::create(conn, "test collection");
let block_group = BlockGroup::create(conn, "test collection", None, "test block group");
let sequence1 = Sequence::new()
Expand Down
18 changes: 5 additions & 13 deletions src/models/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,7 @@ mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;

use crate::migrations::run_migrations;

fn get_connection() -> Connection {
let mut conn = Connection::open_in_memory()
.unwrap_or_else(|_| panic!("Error opening in memory test db"));
rusqlite::vtab::array::load_module(&conn).unwrap();
run_migrations(&mut conn);
conn
}
use crate::test_helpers::get_connection;

#[test]
fn test_builder() {
Expand All @@ -330,7 +322,7 @@ mod tests {

#[test]
fn test_create_sequence_in_db() {
let conn = &mut get_connection();
let conn = &mut get_connection(None);
let sequence = Sequence::new()
.sequence_type("DNA")
.sequence("AACCTT")
Expand All @@ -342,7 +334,7 @@ mod tests {

#[test]
fn test_create_sequence_on_disk() {
let conn = &mut get_connection();
let conn = &mut get_connection(None);
let sequence = Sequence::new()
.sequence_type("DNA")
.name("chr1")
Expand All @@ -359,7 +351,7 @@ mod tests {

#[test]
fn test_get_sequence() {
let conn = &mut get_connection();
let conn = &mut get_connection(None);
let mut fasta_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
fasta_path.push("fixtures/simple.fa");
let sequence = Sequence::new()
Expand All @@ -381,7 +373,7 @@ mod tests {

#[test]
fn test_get_sequence_from_disk() {
let conn = &mut get_connection();
let conn = &mut get_connection(None);
let mut fasta_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
fasta_path.push("fixtures/simple.fa");
let seq = Sequence::new()
Expand Down
Loading