Skip to content

Commit

Permalink
[EASY] Iterator for OnchainOrderPlacementError (#2576)
Browse files Browse the repository at this point in the history
# Description
Inspired by error #2574, now
the unit test will fail if we forget to add the new variant to database.

## How to test
Existing tests
  • Loading branch information
sunce86 authored Mar 29, 2024
1 parent 7ac776f commit 51cea79
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const_format = "0.2"
futures = { workspace = true }
hex = { workspace = true }
sqlx = { workspace = true }
strum = { workspace = true }

[dev-dependencies]
hex-literal = { workspace = true }
Expand Down
23 changes: 3 additions & 20 deletions crates/database/src/onchain_broadcasted_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use {
sqlx::{Executor, PgConnection},
};

#[derive(Clone, Debug, Eq, PartialEq, sqlx::Type)]
#[derive(Clone, Debug, Eq, PartialEq, sqlx::Type, strum::EnumIter)]
#[sqlx(type_name = "OnchainOrderPlacementError", rename_all = "snake_case")]
pub enum OnchainOrderPlacementError {
QuoteNotFound,
Expand Down Expand Up @@ -34,23 +34,6 @@ impl OnchainOrderPlacementError {
}
}

#[cfg(test)]
impl OnchainOrderPlacementError {
pub fn into_iter() -> std::array::IntoIter<OnchainOrderPlacementError, 8> {
const ERRORS: [OnchainOrderPlacementError; 8] = [
OnchainOrderPlacementError::QuoteNotFound,
OnchainOrderPlacementError::InvalidQuote,
OnchainOrderPlacementError::PreValidationError,
OnchainOrderPlacementError::DisabledOrderClass,
OnchainOrderPlacementError::ValidToTooFarInFuture,
OnchainOrderPlacementError::InvalidOrderData,
OnchainOrderPlacementError::InsufficientFee,
OnchainOrderPlacementError::Other,
];
ERRORS.into_iter()
}
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct OnchainOrderPlacement {
pub order_uid: OrderUid,
Expand Down Expand Up @@ -133,7 +116,7 @@ pub async fn read_order(

#[cfg(test)]
mod tests {
use {super::*, crate::byte_array::ByteArray, sqlx::Connection};
use {super::*, crate::byte_array::ByteArray, sqlx::Connection, strum::IntoEnumIterator};

#[tokio::test]
#[ignore]
Expand Down Expand Up @@ -164,7 +147,7 @@ mod tests {
let mut db = PgConnection::connect("postgresql://").await.unwrap();
let mut db = db.begin().await.unwrap();
round_trip_for_error(&mut db, None).await;
for error in OnchainOrderPlacementError::into_iter() {
for error in OnchainOrderPlacementError::iter() {
crate::clear_DANGER_(&mut db).await.unwrap();
round_trip_for_error(&mut db, Some(error)).await;
}
Expand Down

0 comments on commit 51cea79

Please sign in to comment.