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

Add tests to preserve API compatibility #124

Merged
merged 2 commits into from
Dec 10, 2023
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
128 changes: 1 addition & 127 deletions rusqlite_migration/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,130 +236,4 @@ impl From<HookError> for Error {
pub type HookResult<E = HookError> = std::result::Result<(), E>;

#[cfg(test)]
mod tests {
use std::num::NonZeroUsize;

use super::*;

// We should be able to convert rusqlite errors transparently
#[test]
fn test_rusqlite_error_conversion() {
assert!(matches!(
Error::from(rusqlite::Error::MultipleStatement),
Error::RusqliteError { query: _, err: _ }
));

let hook_error = HookError::from(rusqlite::Error::MultipleStatement);
assert!(matches!(&hook_error, &HookError::RusqliteError(_)));
assert!(matches!(
Error::from(hook_error),
Error::RusqliteError { query: _, err: _ },
));
}

// Check that Unrecognized errors correctly implement PartialEq, namely that
// > a != b if and only if !(a == b).
// from https://doc.rust-lang.org/std/cmp/trait.PartialEq.html
#[test]
fn test_unrecognized_errors() {
let u1 = Error::Unrecognized(Box::new(Error::Hook(String::new())));
let u2 = Error::Unrecognized(Box::new(Error::Hook(String::new())));
let u3 = Error::Unrecognized(Box::new(Error::Hook("1".to_owned())));
let u4 = Error::Unrecognized(Box::new(Error::Hook(String::new())));
let u5 = Error::FileLoad("1".to_owned());
let u6 = Error::Unrecognized(Box::new(Error::Hook(String::new())));

for (e1, e2) in &[(u1, u2), (u3, u4), (u5, u6)] {
assert!(e1 != e2);
assert!(!(e1 == e2));
}
}

#[test]
// Errors on specified schema versions should be equal if and only if all versions are
// equal
fn test_specified_schema_version_error() {
assert_eq!(
Error::SpecifiedSchemaVersion(SchemaVersionError::TargetVersionOutOfRange {
specified: SchemaVersion::Outside(NonZeroUsize::new(10).unwrap()),
highest: SchemaVersion::Inside(NonZeroUsize::new(4).unwrap()),
}),
Error::SpecifiedSchemaVersion(SchemaVersionError::TargetVersionOutOfRange {
specified: SchemaVersion::Outside(NonZeroUsize::new(10).unwrap()),
highest: SchemaVersion::Inside(NonZeroUsize::new(4).unwrap()),
}),
);
assert_ne!(
Error::SpecifiedSchemaVersion(SchemaVersionError::TargetVersionOutOfRange {
specified: SchemaVersion::Outside(NonZeroUsize::new(9).unwrap()),
highest: SchemaVersion::Inside(NonZeroUsize::new(4).unwrap()),
}),
Error::SpecifiedSchemaVersion(SchemaVersionError::TargetVersionOutOfRange {
specified: SchemaVersion::Outside(NonZeroUsize::new(10).unwrap()),
highest: SchemaVersion::Inside(NonZeroUsize::new(4).unwrap()),
}),
);
}

// Two errors with different queries or errors should be considered different
#[test]
fn test_rusqlite_error_query() {
assert_ne!(
Error::RusqliteError {
query: "SELECTTT".to_owned(),
err: rusqlite::Error::InvalidQuery
},
Error::RusqliteError {
query: "SSSELECT".to_owned(),
err: rusqlite::Error::InvalidQuery
}
);
assert_ne!(
Error::RusqliteError {
query: "SELECT".to_owned(),
err: rusqlite::Error::MultipleStatement
},
Error::RusqliteError {
query: "SELECT".to_owned(),
err: rusqlite::Error::InvalidQuery
}
)
}

// Two errors with different file load errors should be considered different
#[test]
fn test_rusqlite_error_file_load() {
assert_ne!(
Error::FileLoad("s1".to_owned()),
Error::FileLoad("s2".to_owned())
)
}

// Two errors with different foreign key checks should be considered different
#[test]
fn test_rusqlite_error_fkc() {
assert_ne!(
Error::ForeignKeyCheck(vec![ForeignKeyCheckError {
table: "t1".to_owned(),
rowid: 1,
parent: "t2".to_owned(),
fkid: 3
}]),
Error::ForeignKeyCheck(vec![ForeignKeyCheckError {
table: "t1".to_owned(),
rowid: 3,
parent: "t2".to_owned(),
fkid: 3
}]),
)
}

// Hook error conversion preserves the message
#[test]
fn test_hook_conversion_msg() {
let msg = String::from("some error encountered in the hook");
let hook_error = HookError::Hook(msg.clone());

assert_eq!(Error::from(hook_error), Error::Hook(msg))
}
}
mod tests;
125 changes: 125 additions & 0 deletions rusqlite_migration/src/errors/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use std::num::NonZeroUsize;

use super::*;

// We should be able to convert rusqlite errors transparently
#[test]
fn test_rusqlite_error_conversion() {
assert!(matches!(
Error::from(rusqlite::Error::MultipleStatement),
Error::RusqliteError { query: _, err: _ }
));

let hook_error = HookError::from(rusqlite::Error::MultipleStatement);
assert!(matches!(&hook_error, &HookError::RusqliteError(_)));
assert!(matches!(
Error::from(hook_error),
Error::RusqliteError { query: _, err: _ },
));
}

// Check that Unrecognized errors correctly implement PartialEq, namely that
// > a != b if and only if !(a == b).
// from https://doc.rust-lang.org/std/cmp/trait.PartialEq.html
#[test]
fn test_unrecognized_errors() {
let u1 = Error::Unrecognized(Box::new(Error::Hook(String::new())));
let u2 = Error::Unrecognized(Box::new(Error::Hook(String::new())));
let u3 = Error::Unrecognized(Box::new(Error::Hook("1".to_owned())));
let u4 = Error::Unrecognized(Box::new(Error::Hook(String::new())));
let u5 = Error::FileLoad("1".to_owned());
let u6 = Error::Unrecognized(Box::new(Error::Hook(String::new())));

for (e1, e2) in &[(u1, u2), (u3, u4), (u5, u6)] {
assert!(e1 != e2);
assert!(!(e1 == e2));
}
}

#[test]
// Errors on specified schema versions should be equal if and only if all versions are
// equal
fn test_specified_schema_version_error() {
assert_eq!(
Error::SpecifiedSchemaVersion(SchemaVersionError::TargetVersionOutOfRange {
specified: SchemaVersion::Outside(NonZeroUsize::new(10).unwrap()),
highest: SchemaVersion::Inside(NonZeroUsize::new(4).unwrap()),
}),
Error::SpecifiedSchemaVersion(SchemaVersionError::TargetVersionOutOfRange {
specified: SchemaVersion::Outside(NonZeroUsize::new(10).unwrap()),
highest: SchemaVersion::Inside(NonZeroUsize::new(4).unwrap()),
}),
);
assert_ne!(
Error::SpecifiedSchemaVersion(SchemaVersionError::TargetVersionOutOfRange {
specified: SchemaVersion::Outside(NonZeroUsize::new(9).unwrap()),
highest: SchemaVersion::Inside(NonZeroUsize::new(4).unwrap()),
}),
Error::SpecifiedSchemaVersion(SchemaVersionError::TargetVersionOutOfRange {
specified: SchemaVersion::Outside(NonZeroUsize::new(10).unwrap()),
highest: SchemaVersion::Inside(NonZeroUsize::new(4).unwrap()),
}),
);
}

// Two errors with different queries or errors should be considered different
#[test]
fn test_rusqlite_error_query() {
assert_ne!(
Error::RusqliteError {
query: "SELECTTT".to_owned(),
err: rusqlite::Error::InvalidQuery
},
Error::RusqliteError {
query: "SSSELECT".to_owned(),
err: rusqlite::Error::InvalidQuery
}
);
assert_ne!(
Error::RusqliteError {
query: "SELECT".to_owned(),
err: rusqlite::Error::MultipleStatement
},
Error::RusqliteError {
query: "SELECT".to_owned(),
err: rusqlite::Error::InvalidQuery
}
)
}

// Two errors with different file load errors should be considered different
#[test]
fn test_rusqlite_error_file_load() {
assert_ne!(
Error::FileLoad("s1".to_owned()),
Error::FileLoad("s2".to_owned())
)
}

// Two errors with different foreign key checks should be considered different
#[test]
fn test_rusqlite_error_fkc() {
assert_ne!(
Error::ForeignKeyCheck(vec![ForeignKeyCheckError {
table: "t1".to_owned(),
rowid: 1,
parent: "t2".to_owned(),
fkid: 3
}]),
Error::ForeignKeyCheck(vec![ForeignKeyCheckError {
table: "t1".to_owned(),
rowid: 3,
parent: "t2".to_owned(),
fkid: 3
}]),
)
}

// Hook error conversion preserves the message
#[test]
fn test_hook_conversion_msg() {
let msg = String::from("some error encountered in the hook");
let hook_error = HookError::Hook(msg.clone());

assert_eq!(Error::from(hook_error), Error::Hook(msg))
}
14 changes: 14 additions & 0 deletions rusqlite_migration/src/tests/asynch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,17 @@ async fn test_from_iter() {
let migrations = AsyncMigrations::from_iter(vec![m_valid0(), m_valid10()]);
assert_eq!(Ok(()), migrations.validate().await);
}

#[tokio::test]
async fn test_tokio_rusqlite_conversion() {
use tokio_rusqlite::Error as TError;

insta::assert_debug_snapshot!(
"convert_connection_closed_error",
crate::Error::from(TError::ConnectionClosed)
);
insta::assert_debug_snapshot!(
"convert_rusqlite_error",
crate::Error::from(TError::Rusqlite(rusqlite::Error::InvalidQuery))
);
}
54 changes: 54 additions & 0 deletions rusqlite_migration/src/tests/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,57 @@ pub fn m_invalid_fk() -> M<'static> {
)
.foreign_key_check()
}

pub fn all_errors() -> Vec<(&'static str, crate::Error)> {
use crate::Error::*;
use crate::ForeignKeyCheckError;
use crate::MigrationDefinitionError;
use crate::SchemaVersion;
use crate::SchemaVersionError;

vec![
(
"rusqlite_error",
RusqliteError {
query: "SELECT * FROM table42;".to_owned(),
err: rusqlite::Error::InvalidQuery,
},
),
(
"specified_schema_version",
SpecifiedSchemaVersion(SchemaVersionError::TargetVersionOutOfRange {
specified: SchemaVersion::NoneSet,
highest: SchemaVersion::NoneSet,
}),
),
(
"migration_definition",
MigrationDefinition(MigrationDefinitionError::NoMigrationsDefined),
),
(
"foreign_key_check",
ForeignKeyCheck(vec![
ForeignKeyCheckError {
table: "t1".to_owned(),
rowid: 1,
parent: "t2".to_owned(),
fkid: 2,
},
ForeignKeyCheckError {
table: "t3".to_owned(),
rowid: 2,
parent: "t4".to_owned(),
fkid: 3,
},
]),
),
("hook", Hook("in hook".to_owned())),
("file_load", FileLoad("file causing problem".to_owned())),
(
"unrecognized",
Unrecognized(Box::new(Hook("unknown".to_owned()))),
),
#[cfg(feature = "async-tokio-rusqlite")]
("connection_closed", ConnectionClosed),
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: rusqlite_migration/src/tests/asynch.rs
expression: "crate::Error::from(TError::ConnectionClosed)"
---
ConnectionClosed
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: rusqlite_migration/src/tests/asynch.rs
expression: "crate::Error::from(TError::Rusqlite(rusqlite::Error::InvalidQuery))"
---
RusqliteError {
query: "",
err: InvalidQuery,
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: rusqlite_migration/src/tests/synch.rs
expression: e
---
rusqlite_migrate error: ConnectionClosed
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: rusqlite_migration/src/tests/synch.rs
expression: e
---
rusqlite_migrate error: FileLoad("file causing problem")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: rusqlite_migration/src/tests/synch.rs
expression: e
---
rusqlite_migrate error: ForeignKeyCheck([ForeignKeyCheckError { table: "t1", rowid: 1, parent: "t2", fkid: 2 }, ForeignKeyCheckError { table: "t3", rowid: 2, parent: "t4", fkid: 3 }])
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: rusqlite_migration/src/tests/synch.rs
expression: e
---
rusqlite_migrate error: Hook("in hook")
Loading
Loading