Skip to content

Commit

Permalink
test: add test to preserve API compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
cljoly committed Dec 10, 2023
1 parent 884cb8b commit fa93707
Show file tree
Hide file tree
Showing 27 changed files with 233 additions and 53 deletions.
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))
);
}
39 changes: 39 additions & 0 deletions rusqlite_migration/src/tests/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,42 @@ pub fn m_invalid_fk() -> M<'static> {
)
.foreign_key_check()
}

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

vec![
RusqliteError {
query: "SELECT * FROM table42;".to_owned(),
err: rusqlite::Error::InvalidQuery,
},
#[cfg(feature = "async-tokio-rusqlite")]
ConnectionClosed,
SpecifiedSchemaVersion(SchemaVersionError::TargetVersionOutOfRange {
specified: SchemaVersion::NoneSet,
highest: SchemaVersion::NoneSet,
}),
MigrationDefinition(MigrationDefinitionError::NoMigrationsDefined),
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("in hook".to_owned()),
FileLoad("file causing problem".to_owned()),
Unrecognized(Box::new(Hook("unknown".to_owned()))),
]
}
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: RusqliteError { query: "SELECT * FROM table42;", err: InvalidQuery }
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: SpecifiedSchemaVersion(TargetVersionOutOfRange { specified: NoneSet, highest: NoneSet })
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: rusqlite_migration/src/tests/synch.rs
expression: "Error::MigrationDefinition(MigrationDefinitionError::NoMigrationsDefined)"
expression: e
---
rusqlite_migrate error: MigrationDefinition(NoMigrationsDefined)
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")
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: Unrecognized(Hook("unknown"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: rusqlite_migration/src/tests/synch.rs
expression: e.source()
---
Some(
InvalidQuery,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: rusqlite_migration/src/tests/synch.rs
expression: e.source()
---
None
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: rusqlite_migration/src/tests/synch.rs
expression: e.source()
---
Some(
TargetVersionOutOfRange {
specified: NoneSet,
highest: NoneSet,
},
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: rusqlite_migration/src/tests/synch.rs
expression: e.source()
---
Some(
NoMigrationsDefined,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: rusqlite_migration/src/tests/synch.rs
expression: e.source()
---
Some(
ForeignKeyCheckError {
table: "t1",
rowid: 1,
parent: "t2",
fkid: 2,
},
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: rusqlite_migration/src/tests/synch.rs
expression: e.source()
---
None
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: rusqlite_migration/src/tests/synch.rs
expression: e.source()
---
None
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: rusqlite_migration/src/tests/synch.rs
expression: e.source()
---
Some(
Hook(
"unknown",
),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: rusqlite_migration/src/tests/synch.rs
expression: e
---
Err(
RusqliteError {
query: "CREATE TABLE m1(a, b); CREATE TABLE m2(a, b, c);",
err: SqliteFailure(
Error {
code: ReadOnly,
extended_code: 8,
},
Some(
"attempt to write a readonly database",
),
),
},
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: rusqlite_migration/src/tests/synch.rs
expression: e
---
Err(
RusqliteError {
query: "PRAGMA user_version = 1; -- Approximate query",
err: SqliteFailure(
Error {
code: ReadOnly,
extended_code: 8,
},
Some(
"attempt to write a readonly database",
),
),
},
)
67 changes: 35 additions & 32 deletions rusqlite_migration/src/tests/synch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{iter::FromIterator, num::NonZeroUsize};

use rusqlite::{Connection, Transaction};
use rusqlite::{Connection, OpenFlags, Transaction};

use crate::{
tests::helpers::{
Expand All @@ -10,7 +10,7 @@ use crate::{
SchemaVersionError, M,
};

use super::helpers::{m_invalid0, m_invalid1, m_valid20, m_valid21};
use super::helpers::{all_errors, m_invalid0, m_invalid1, m_valid20, m_valid21};

#[test]
fn empty_migrations_test() {
Expand Down Expand Up @@ -148,38 +148,19 @@ fn test_migration_definition_error_display() {

#[test]
fn test_error_display() {
insta::assert_display_snapshot!(Error::SpecifiedSchemaVersion(
SchemaVersionError::TargetVersionOutOfRange {
specified: SchemaVersion::NoneSet,
highest: SchemaVersion::NoneSet,
}
));

insta::assert_display_snapshot!(Error::Hook(String::new()));

insta::assert_display_snapshot!(Error::ForeignKeyCheck(vec![
ForeignKeyCheckError {
table: String::new(),
rowid: 1,
parent: String::new(),
fkid: 2,
},
ForeignKeyCheckError {
table: String::new(),
rowid: 2,
parent: String::new(),
fkid: 3,
},
]));
for (i, e) in all_errors().into_iter().enumerate() {
insta::assert_display_snapshot!(format!("error_display_number_{i}"), e);
}
}

insta::assert_display_snapshot!(Error::MigrationDefinition(
MigrationDefinitionError::NoMigrationsDefined
));
#[test]
fn test_error_source() {
use std::error::Error;

insta::assert_display_snapshot!(Error::RusqliteError {
query: String::new(),
err: rusqlite::Error::InvalidQuery,
});
for (i, e) in all_errors().into_iter().enumerate() {
// For API stability reasons (if that changes, we must change the major version)
insta::assert_debug_snapshot!(format!("error_source_number_{i}"), e.source());
}
}

#[test]
Expand Down Expand Up @@ -371,6 +352,18 @@ fn all_valid_test() {
insta::assert_debug_snapshot!(migrations)
}

// When the DB encounters an error, it is surfaced
#[test]
fn test_read_only_db_all_valid() {
let mut conn = Connection::open_in_memory_with_flags(OpenFlags::SQLITE_OPEN_READ_ONLY).unwrap();
let migrations = Migrations::new(all_valid());

let e = migrations.to_latest(&mut conn);

assert!(e.is_err());
insta::assert_debug_snapshot!(e)
}

// If we encounter a database with a migration number higher than the number of defined migration,
// we should return an error, not panic.
// See https://github.com/cljoly/rusqlite_migration/issues/17
Expand Down Expand Up @@ -527,3 +520,13 @@ fn test_from_iter() {
let migrations = Migrations::from_iter(vec![m_valid0(), m_valid10()]);
assert_eq!(Ok(()), migrations.validate());
}

#[test]
fn test_user_version_error() {
// This will cause error because the DB is read only
let conn = Connection::open_in_memory_with_flags(OpenFlags::SQLITE_OPEN_READ_ONLY).unwrap();
let e = crate::set_user_version(&conn, 1);

assert!(e.is_err(), "{:?}", e);
insta::assert_debug_snapshot!(e)
}

0 comments on commit fa93707

Please sign in to comment.