Skip to content
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
3 changes: 3 additions & 0 deletions src/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ impl Default for SonarQubeReporter {
}
}

#[cfg(test)]
pub mod test_helpers;

pub mod sarif;
pub mod sonarqube;
pub mod text;
12 changes: 1 addition & 11 deletions src/output/sarif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,10 @@ impl Reporter for SarifReporter {
#[cfg(test)]
mod tests {
use super::*;
use crate::output::test_helpers::test_finding;
use crate::rules::{Finding, Severity};
use std::path::PathBuf;

fn test_finding() -> Finding {
Finding {
rule_id: "PGM001".to_string(),
severity: Severity::Critical,
message: "CREATE INDEX on existing table 'orders' should use CONCURRENTLY.".to_string(),
file: PathBuf::from("db/migrations/V042__add_index.sql"),
start_line: 3,
end_line: 3,
}
}

#[test]
fn single_finding_produces_valid_sarif() {
let dir = tempfile::tempdir().expect("tempdir");
Expand Down
12 changes: 1 addition & 11 deletions src/output/sonarqube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,10 @@ impl Reporter for SonarQubeReporter {
#[cfg(test)]
mod tests {
use super::*;
use crate::output::test_helpers::test_finding;
use crate::rules::{Finding, Severity};
use std::path::PathBuf;

fn test_finding() -> Finding {
Finding {
rule_id: "PGM001".to_string(),
severity: Severity::Critical,
message: "CREATE INDEX on existing table 'orders' should use CONCURRENTLY.".to_string(),
file: PathBuf::from("db/migrations/V042__add_index.sql"),
start_line: 3,
end_line: 3,
}
}

#[test]
fn single_finding_produces_valid_json() {
let dir = tempfile::tempdir().expect("tempdir");
Expand Down
16 changes: 16 additions & 0 deletions src/output/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! Shared test helpers for output module tests.

use crate::rules::{Finding, Severity};
use std::path::PathBuf;

/// Create a standard test finding for output format tests.
pub fn test_finding() -> Finding {
Finding {
rule_id: "PGM001".to_string(),
severity: Severity::Critical,
message: "CREATE INDEX on existing table 'orders' should use CONCURRENTLY.".to_string(),
file: PathBuf::from("db/migrations/V042__add_index.sql"),
start_line: 3,
end_line: 3,
}
}
12 changes: 1 addition & 11 deletions src/output/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,10 @@ impl Reporter for TextReporter {
#[cfg(test)]
mod tests {
use super::*;
use crate::output::test_helpers::test_finding;
use crate::rules::{Finding, Severity};
use std::path::PathBuf;

fn test_finding() -> Finding {
Finding {
rule_id: "PGM001".to_string(),
severity: Severity::Critical,
message: "CREATE INDEX on existing table 'orders' should use CONCURRENTLY.".to_string(),
file: PathBuf::from("db/migrations/V042__add_index.sql"),
start_line: 3,
end_line: 3,
}
}

#[test]
fn single_finding_correct_format() {
let dir = tempfile::tempdir().expect("tempdir");
Expand Down
3 changes: 3 additions & 0 deletions src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
//! Each rule implements the `Rule` trait and checks for specific migration safety issues.
//! Rules receive IR nodes and catalog state, returning findings with severity levels.

#[cfg(test)]
pub mod test_helpers;

pub mod pgm001;
pub mod pgm002;
pub mod pgm003;
Expand Down
29 changes: 1 addition & 28 deletions src/rules/pgm001.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,37 +94,10 @@ mod tests {
use crate::catalog::Catalog;
use crate::catalog::builder::CatalogBuilder;
use crate::parser::ir::*;
use crate::rules::test_helpers::*;
use std::collections::HashSet;
use std::path::PathBuf;

fn make_ctx<'a>(
before: &'a Catalog,
after: &'a Catalog,
file: &'a PathBuf,
created: &'a HashSet<String>,
) -> LintContext<'a> {
LintContext {
catalog_before: before,
catalog_after: after,
tables_created_in_change: created,
run_in_transaction: true,
is_down: false,
file,
}
}

fn located(node: IrNode) -> Located<IrNode> {
Located {
node,
span: SourceSpan {
start_line: 1,
end_line: 1,
start_offset: 0,
end_offset: 0,
},
}
}

#[test]
fn test_existing_table_no_concurrent_fires() {
let before = CatalogBuilder::new()
Expand Down
30 changes: 1 addition & 29 deletions src/rules/pgm002.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,40 +88,12 @@ impl Rule for Pgm002 {
#[cfg(test)]
mod tests {
use super::*;
use crate::catalog::Catalog;
use crate::catalog::builder::CatalogBuilder;
use crate::parser::ir::*;
use crate::rules::test_helpers::*;
use std::collections::HashSet;
use std::path::PathBuf;

fn make_ctx<'a>(
before: &'a Catalog,
after: &'a Catalog,
file: &'a PathBuf,
created: &'a HashSet<String>,
) -> LintContext<'a> {
LintContext {
catalog_before: before,
catalog_after: after,
tables_created_in_change: created,
run_in_transaction: true,
is_down: false,
file,
}
}

fn located(node: IrNode) -> Located<IrNode> {
Located {
node,
span: SourceSpan {
start_line: 1,
end_line: 1,
start_offset: 0,
end_offset: 0,
},
}
}

#[test]
fn test_drop_index_no_concurrent_fires() {
let before = CatalogBuilder::new()
Expand Down
29 changes: 1 addition & 28 deletions src/rules/pgm003.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,37 +146,10 @@ mod tests {
use crate::catalog::Catalog;
use crate::catalog::builder::CatalogBuilder;
use crate::parser::ir::*;
use crate::rules::test_helpers::*;
use std::collections::HashSet;
use std::path::PathBuf;

fn make_ctx<'a>(
before: &'a Catalog,
after: &'a Catalog,
file: &'a PathBuf,
created: &'a HashSet<String>,
) -> LintContext<'a> {
LintContext {
catalog_before: before,
catalog_after: after,
tables_created_in_change: created,
run_in_transaction: true,
is_down: false,
file,
}
}

fn located(node: IrNode) -> Located<IrNode> {
Located {
node,
span: SourceSpan {
start_line: 1,
end_line: 1,
start_offset: 0,
end_offset: 0,
},
}
}

#[test]
fn test_fk_no_index_fires() {
let before = CatalogBuilder::new()
Expand Down
29 changes: 1 addition & 28 deletions src/rules/pgm004.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,37 +100,10 @@ mod tests {
use crate::catalog::Catalog;
use crate::catalog::builder::CatalogBuilder;
use crate::parser::ir::*;
use crate::rules::test_helpers::*;
use std::collections::HashSet;
use std::path::PathBuf;

fn make_ctx<'a>(
before: &'a Catalog,
after: &'a Catalog,
file: &'a PathBuf,
created: &'a HashSet<String>,
) -> LintContext<'a> {
LintContext {
catalog_before: before,
catalog_after: after,
tables_created_in_change: created,
run_in_transaction: true,
is_down: false,
file,
}
}

fn located(node: IrNode) -> Located<IrNode> {
Located {
node,
span: SourceSpan {
start_line: 1,
end_line: 1,
start_offset: 0,
end_offset: 0,
},
}
}

#[test]
fn test_no_pk_fires() {
let before = Catalog::new();
Expand Down
29 changes: 1 addition & 28 deletions src/rules/pgm005.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,37 +103,10 @@ mod tests {
use crate::catalog::Catalog;
use crate::catalog::builder::CatalogBuilder;
use crate::parser::ir::*;
use crate::rules::test_helpers::*;
use std::collections::HashSet;
use std::path::PathBuf;

fn make_ctx<'a>(
before: &'a Catalog,
after: &'a Catalog,
file: &'a PathBuf,
created: &'a HashSet<String>,
) -> LintContext<'a> {
LintContext {
catalog_before: before,
catalog_after: after,
tables_created_in_change: created,
run_in_transaction: true,
is_down: false,
file,
}
}

fn located(node: IrNode) -> Located<IrNode> {
Located {
node,
span: SourceSpan {
start_line: 1,
end_line: 1,
start_offset: 0,
end_offset: 0,
},
}
}

#[test]
fn test_unique_not_null_no_pk_fires() {
let before = Catalog::new();
Expand Down
30 changes: 1 addition & 29 deletions src/rules/pgm006.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,38 +89,10 @@ mod tests {
use super::*;
use crate::catalog::Catalog;
use crate::parser::ir::*;
use crate::rules::test_helpers::*;
use std::collections::HashSet;
use std::path::PathBuf;

fn make_ctx_with_txn<'a>(
before: &'a Catalog,
after: &'a Catalog,
file: &'a PathBuf,
created: &'a HashSet<String>,
run_in_transaction: bool,
) -> LintContext<'a> {
LintContext {
catalog_before: before,
catalog_after: after,
tables_created_in_change: created,
run_in_transaction,
is_down: false,
file,
}
}

fn located(node: IrNode) -> Located<IrNode> {
Located {
node,
span: SourceSpan {
start_line: 1,
end_line: 1,
start_offset: 0,
end_offset: 0,
},
}
}

#[test]
fn test_concurrent_in_transaction_fires() {
let before = Catalog::new();
Expand Down
29 changes: 1 addition & 28 deletions src/rules/pgm007.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,37 +194,10 @@ mod tests {
use super::*;
use crate::catalog::Catalog;
use crate::parser::ir::*;
use crate::rules::test_helpers::*;
use std::collections::HashSet;
use std::path::PathBuf;

fn make_ctx<'a>(
before: &'a Catalog,
after: &'a Catalog,
file: &'a PathBuf,
created: &'a HashSet<String>,
) -> LintContext<'a> {
LintContext {
catalog_before: before,
catalog_after: after,
tables_created_in_change: created,
run_in_transaction: true,
is_down: false,
file,
}
}

fn located(node: IrNode) -> Located<IrNode> {
Located {
node,
span: SourceSpan {
start_line: 1,
end_line: 1,
start_offset: 0,
end_offset: 0,
},
}
}

fn col_with_default(name: &str, default: DefaultExpr) -> ColumnDef {
ColumnDef {
name: name.to_string(),
Expand Down
Loading