-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switch from diesel to rusqlite, add initial report builder
- Loading branch information
1 parent
0c6fe12
commit 9ca25cd
Showing
7 changed files
with
477 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,25 @@ | ||
use std::path::PathBuf; | ||
|
||
use diesel::{sqlite::SqliteConnection, Connection}; | ||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; | ||
|
||
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations"); | ||
|
||
pub mod models; | ||
pub mod schema; | ||
|
||
pub struct Report { | ||
pub filename: PathBuf, | ||
pub conn: SqliteConnection, | ||
} | ||
|
||
impl Report { | ||
pub fn new(filename: PathBuf) -> Report { | ||
// TODO: handle errors/results properly | ||
let mut conn = SqliteConnection::establish(filename.to_str().unwrap()) | ||
.ok() | ||
.unwrap(); | ||
conn.run_pending_migrations(MIGRATIONS).ok(); | ||
|
||
Report { filename, conn } | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use diesel::{QueryDsl, RunQueryDsl, SelectableHelper}; | ||
use tempfile::TempDir; | ||
|
||
use super::*; | ||
|
||
struct Ctx { | ||
temp_dir: TempDir, | ||
} | ||
|
||
fn setup() -> Ctx { | ||
Ctx { | ||
temp_dir: TempDir::new().ok().unwrap(), | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_new_report() { | ||
let ctx = setup(); | ||
let db_file = ctx.temp_dir.path().join("db.sqlite"); | ||
assert!(!db_file.exists()); | ||
|
||
let mut report = Report::new(db_file); | ||
assert!(!report.conn.has_pending_migration(MIGRATIONS).ok().unwrap()); | ||
} | ||
mod sqlite_report; | ||
use rusqlite::Result; | ||
pub use sqlite_report::*; | ||
|
||
#[test] | ||
fn test_existing_report() { | ||
let ctx = setup(); | ||
let db_file = ctx.temp_dir.path().join("db.sqlite"); | ||
assert!(!db_file.exists()); | ||
pub trait Report {} | ||
|
||
let mut new_report = Report::new(db_file.clone()); | ||
pub trait ReportBuilder<R: Report> { | ||
fn insert_file(&mut self, file: models::SourceFile) -> Result<models::SourceFile>; | ||
fn insert_context(&mut self, context: models::Context) -> Result<models::Context>; | ||
|
||
let mock_context = models::Context { | ||
id: 0, | ||
context_type: schema::ContextType::TestCase, | ||
name: "mock_context".to_string(), | ||
}; | ||
diesel::insert_into(schema::context::table) | ||
.values(&mock_context) | ||
.execute(&mut new_report.conn) | ||
.expect("failed to add mock context"); | ||
fn insert_line( | ||
&mut self, | ||
line: models::LineStatus, | ||
context: &models::Context, | ||
) -> Result<models::LineStatus>; | ||
fn insert_branch( | ||
&mut self, | ||
branch: models::BranchStatus, | ||
context: &models::Context, | ||
) -> Result<models::BranchStatus>; | ||
|
||
let mut existing_report = Report::new(db_file.clone()); | ||
let contexts = schema::context::table | ||
.select(models::Context::as_select()) | ||
.load(&mut existing_report.conn) | ||
.expect("error loading contexts"); | ||
assert!(contexts.len() == 1); | ||
} | ||
fn build(self) -> R; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,80 @@ | ||
use diesel::{Associations, Identifiable, Insertable, Queryable, Selectable}; | ||
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef}; | ||
use strum_macros::{Display, EnumString}; | ||
|
||
use crate::report::schema::*; | ||
#[derive(Copy, Clone, Debug, PartialEq)] | ||
pub enum CoverageStatus { | ||
Hit = 1, | ||
Miss, | ||
Partial, | ||
} | ||
|
||
impl ToSql for CoverageStatus { | ||
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> { | ||
Ok((*self as i32).into()) | ||
} | ||
} | ||
|
||
impl FromSql for CoverageStatus { | ||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> { | ||
let variant = match value.as_i64()? { | ||
1 => CoverageStatus::Hit, | ||
2 => CoverageStatus::Miss, | ||
3 => CoverageStatus::Partial, | ||
_ => panic!("Uh oh"), | ||
}; | ||
Ok(variant) | ||
} | ||
} | ||
|
||
#[derive(EnumString, Display, Debug, PartialEq)] | ||
pub enum ContextType { | ||
TestCase, | ||
Upload, | ||
} | ||
|
||
impl ToSql for ContextType { | ||
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> { | ||
Ok(self.to_string().into()) | ||
} | ||
} | ||
|
||
impl FromSql for ContextType { | ||
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> { | ||
value | ||
.as_str()? | ||
.parse() | ||
.map_err(|e| FromSqlError::Other(Box::new(e))) | ||
} | ||
} | ||
|
||
#[derive(Queryable, Identifiable, Selectable, Insertable, Debug, PartialEq)] | ||
#[diesel(table_name = source_file)] | ||
pub struct SourceFile { | ||
pub id: i32, | ||
pub id: Option<i32>, | ||
pub path: String, | ||
} | ||
|
||
#[derive(Queryable, Identifiable, Selectable, Associations, Insertable, Debug, PartialEq)] | ||
#[diesel(belongs_to(SourceFile))] | ||
#[diesel(table_name = line_status)] | ||
pub struct LineStatus { | ||
pub id: i32, | ||
pub id: Option<i32>, | ||
pub source_file_id: i32, | ||
pub line_no: i32, | ||
pub coverage_status: CoverageStatus, | ||
} | ||
|
||
#[derive(Queryable, Identifiable, Selectable, Associations, Insertable, Debug, PartialEq)] | ||
#[diesel(belongs_to(SourceFile))] | ||
#[diesel(table_name = branch_status)] | ||
pub struct BranchStatus { | ||
pub id: i32, | ||
pub id: Option<i32>, | ||
pub source_file_id: i32, | ||
pub start_line_no: i32, | ||
pub end_line_no: i32, | ||
pub coverage_status: CoverageStatus, | ||
} | ||
|
||
#[derive(Queryable, Identifiable, Selectable, Associations, Insertable, Debug, PartialEq)] | ||
#[diesel(belongs_to(Context))] | ||
#[diesel(belongs_to(LineStatus, foreign_key = line_id))] | ||
#[diesel(belongs_to(BranchStatus, foreign_key = branch_id))] | ||
#[diesel(table_name = context_assoc)] | ||
#[diesel(primary_key(context_id, line_id, branch_id))] | ||
pub struct ContextAssoc { | ||
pub context_id: i32, | ||
pub line_id: Option<i32>, | ||
pub branch_id: Option<i32>, | ||
} | ||
|
||
#[derive(Queryable, Identifiable, Selectable, Insertable, Debug, PartialEq)] | ||
#[diesel(table_name = context)] | ||
pub struct Context { | ||
pub id: i32, | ||
pub id: Option<i32>, | ||
pub context_type: ContextType, | ||
pub name: String, | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.