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
1 change: 1 addition & 0 deletions .github/workflows/freebsd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ jobs:
. $HOME/.cargo/env
rustup target add i686-unknown-freebsd
cargo build --all --all-targets --verbose --target i686-unknown-freebsd
cargo test --features test --all --all-targets --verbose
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ endif

#----------------------------------------------------------------------------------------------

test: pytest
test: cargo_test pytest

pytest:
$(SHOW)MODULE=$(abspath $(TARGET)) $(realpath ./tests/pytest/tests.sh)
Expand Down
64 changes: 64 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,67 @@ impl From<Error> for redis_module::RedisError {
redis_module::RedisError::String(e.msg)
}
}

#[cfg(test)]
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;

#[test]
fn test_from_string() {
let err: Error = String::from("error from String").into();
assert_eq!(err.msg, "error from String");
}

#[test]
fn test_from_generic_error() {
let err: Error = redis_module::error::GenericError::new("error from GenericError").into();
assert_eq!(err.msg, "Store error: error from GenericError");
}

#[test]
fn test_from_utf8_error() {
let err: Error = String::from_utf8(vec![128, 0, 0]).unwrap_err().into();
assert_eq!(err.msg, "invalid utf-8 sequence of 1 bytes from index 0");
}

#[test]
fn test_from_parse_int_error() {
let err: Error = i32::from_str_radix("a12", 10).unwrap_err().into();
assert_eq!(err.msg, "invalid digit found in string");
}

#[test]
fn test_from_redis_error() {
let err: Error = redis_module::error::Error::generic("error from Generic").into();
assert_eq!(err.msg, "Store error: error from Generic");

let utf8_err: redis_module::error::Error =
String::from_utf8(vec![128, 0, 0]).unwrap_err().into();
let err: Error = utf8_err.into();
assert_eq!(err.msg, "invalid utf-8 sequence of 1 bytes from index 0");

let parse_int_error: redis_module::error::Error =
i32::from_str_radix("a12", 10).unwrap_err().into();
let err: Error = parse_int_error.into();
assert_eq!(err.msg, "invalid digit found in string");
}

// #[test]
// fn test_from_serde_json_error() {
// let err : Error = serde_json::Error "error from str".into();
// assert_eq!(err.msg, "error from str");
// }

#[test]
fn test_from_jsonpath_error() {
let err: Error = JsonPathError::Path("JsonPathError".to_string()).into();
assert_eq!(err.msg, "JSON Path error: path error: \\nJsonPathError\\n");
}

#[test]
fn test_from_str() {
let err: Error = "error from str".into();
assert_eq!(err.msg, "error from str");
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding

Suggested change
}
#[test]
fn test_from_error_to_redis_error() {
let err: Error = String::from("error from String").into();
let redis_err: RedisError = err.into();
if let RedisError::String(s) = redis_err {
assert_eq!(s, "error from String");
}
}
#[test]
fn test_from_redis_error_to_error() {
let redis_err = RedisError::String(String::from("redis error from String"));
let err: Error = redis_err.into();
assert_eq!(err.msg, "ERR redis error from String");
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oshadmi I don't get the suggestion, it seems like you suggest to delete on }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gkorland Suggested adding 2 tests at the end
(the closing bracket } is removed, but also added with 2 additional tests)