Skip to content

Commit

Permalink
Extract test helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
czocher authored and cljoly committed Oct 25, 2023
1 parent ce4ce1c commit 1b0405f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 124 deletions.
70 changes: 7 additions & 63 deletions rusqlite_migration/src/tests/asynch.rs
Original file line number Diff line number Diff line change
@@ -1,70 +1,14 @@
use std::iter::FromIterator;

use crate::{AsyncMigrations, Error, MigrationDefinitionError, M};
use crate::{
tests::helpers::{
all_valid, m_invalid0, m_invalid1, m_invalid_fk, m_valid0, m_valid10, m_valid11, m_valid20,
m_valid21, m_valid_fk,
},
AsyncMigrations, Error, MigrationDefinitionError,
};
use tokio_rusqlite::Connection as AsyncConnection;

fn m_valid0() -> M<'static> {
M::up("CREATE TABLE m1(a, b); CREATE TABLE m2(a, b, c);")
}
fn m_valid10() -> M<'static> {
M::up("CREATE TABLE t1(a, b);")
}
fn m_valid11() -> M<'static> {
M::up("ALTER TABLE t1 RENAME COLUMN b TO c;")
}
fn m_valid20() -> M<'static> {
M::up("CREATE TABLE t2(b);")
}
fn m_valid21() -> M<'static> {
M::up("ALTER TABLE t2 ADD COLUMN a;")
}

fn m_valid_fk() -> M<'static> {
M::up(
"CREATE TABLE fk1(a PRIMARY KEY); \
CREATE TABLE fk2( \
a, \
FOREIGN KEY(a) REFERENCES fk1(a) \
); \
INSERT INTO fk1 (a) VALUES ('foo'); \
INSERT INTO fk2 (a) VALUES ('foo'); \
",
)
.foreign_key_check()
}

// All valid Ms in the right order
fn all_valid() -> Vec<M<'static>> {
vec![
m_valid0(),
m_valid10(),
m_valid11(),
m_valid20(),
m_valid21(),
m_valid_fk(),
]
}

fn m_invalid0() -> M<'static> {
M::up("CREATE TABLE table3()")
}
fn m_invalid1() -> M<'static> {
M::up("something invalid")
}

fn m_invalid_fk() -> M<'static> {
M::up(
"CREATE TABLE fk1(a PRIMARY KEY); \
CREATE TABLE fk2( \
a, \
FOREIGN KEY(a) REFERENCES fk1(a) \
); \
INSERT INTO fk2 (a) VALUES ('foo'); \
",
)
.foreign_key_check()
}

#[tokio::test]
async fn invalid_migration_statement_test() {
for m in &[m_invalid0(), m_invalid1(), m_valid11(), m_valid21()] {
Expand Down
63 changes: 63 additions & 0 deletions rusqlite_migration/src/tests/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use crate::M;

pub fn m_valid0() -> M<'static> {
M::up("CREATE TABLE m1(a, b); CREATE TABLE m2(a, b, c);")
}
pub fn m_valid10() -> M<'static> {
M::up("CREATE TABLE t1(a, b);")
}
pub fn m_valid11() -> M<'static> {
M::up("ALTER TABLE t1 RENAME COLUMN b TO c;")
}
pub fn m_valid20() -> M<'static> {
M::up("CREATE TABLE t2(b);")
}
pub fn m_valid21() -> M<'static> {
M::up("ALTER TABLE t2 ADD COLUMN a;")
}

pub fn m_valid_fk() -> M<'static> {
M::up(
"CREATE TABLE fk1(a PRIMARY KEY); \
CREATE TABLE fk2( \
a, \
FOREIGN KEY(a) REFERENCES fk1(a) \
); \
INSERT INTO fk1 (a) VALUES ('foo'); \
INSERT INTO fk2 (a) VALUES ('foo'); \
",
)
.foreign_key_check()
}

// All valid Ms in the right order
pub fn all_valid() -> Vec<M<'static>> {
vec![
m_valid0(),
m_valid10(),
m_valid11(),
m_valid20(),
m_valid21(),
m_valid_fk(),
]
}

pub fn m_invalid0() -> M<'static> {
M::up("CREATE TABLE table3()")
}
pub fn m_invalid1() -> M<'static> {
M::up("something invalid")
}

pub fn m_invalid_fk() -> M<'static> {
M::up(
"CREATE TABLE fk1(a PRIMARY KEY); \
CREATE TABLE fk2( \
a, \
FOREIGN KEY(a) REFERENCES fk1(a) \
); \
INSERT INTO fk2 (a) VALUES ('foo'); \
",
)
.foreign_key_check()
}
1 change: 1 addition & 0 deletions rusqlite_migration/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod asynch;
mod builder;
mod helpers;
mod synch;
63 changes: 2 additions & 61 deletions rusqlite_migration/src/tests/synch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,12 @@ use std::{iter::FromIterator, num::NonZeroUsize};
use rusqlite::{Connection, Transaction};

use crate::{
tests::helpers::{all_valid, m_invalid_fk, m_valid0, m_valid10, m_valid11, m_valid_fk},
user_version, Error, ForeignKeyCheckError, MigrationDefinitionError, Migrations, SchemaVersion,
SchemaVersionError, M,
};

fn m_valid0() -> M<'static> {
M::up("CREATE TABLE m1(a, b); CREATE TABLE m2(a, b, c);")
}
fn m_valid10() -> M<'static> {
M::up("CREATE TABLE t1(a, b);")
}
fn m_valid11() -> M<'static> {
M::up("ALTER TABLE t1 RENAME COLUMN b TO c;")
}
fn m_valid20() -> M<'static> {
M::up("CREATE TABLE t2(b);")
}
fn m_valid21() -> M<'static> {
M::up("ALTER TABLE t2 ADD COLUMN a;")
}

fn m_valid_fk() -> M<'static> {
M::up(
"CREATE TABLE fk1(a PRIMARY KEY); \
CREATE TABLE fk2( \
a, \
FOREIGN KEY(a) REFERENCES fk1(a) \
); \
INSERT INTO fk1 (a) VALUES ('foo'); \
INSERT INTO fk2 (a) VALUES ('foo'); \
",
)
.foreign_key_check()
}

// All valid Ms in the right order
fn all_valid() -> Vec<M<'static>> {
vec![
m_valid0(),
m_valid10(),
m_valid11(),
m_valid20(),
m_valid21(),
m_valid_fk(),
]
}

fn m_invalid0() -> M<'static> {
M::up("CREATE TABLE table3()")
}
fn m_invalid1() -> M<'static> {
M::up("something invalid")
}

fn m_invalid_fk() -> M<'static> {
M::up(
"CREATE TABLE fk1(a PRIMARY KEY); \
CREATE TABLE fk2( \
a, \
FOREIGN KEY(a) REFERENCES fk1(a) \
); \
INSERT INTO fk2 (a) VALUES ('foo'); \
",
)
.foreign_key_check()
}
use super::helpers::{m_invalid0, m_invalid1, m_valid20, m_valid21};

#[test]
fn empty_migrations_test() {
Expand Down

0 comments on commit 1b0405f

Please sign in to comment.