Skip to content

Commit a58b53f

Browse files
committed
chore: improve sqlx maintenance path
1 parent 75bc048 commit a58b53f

1 file changed

Lines changed: 126 additions & 0 deletions

File tree

sqlx-core/src/error.rs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,129 @@ macro_rules! err_protocol {
351351
)
352352
};
353353
}
354+
355+
#[cfg(test)]
356+
mod tests {
357+
use super::*;
358+
359+
#[test]
360+
fn test_display_wrapper_variants() {
361+
let io = || std::io::Error::new(std::io::ErrorKind::Other, "oops");
362+
363+
assert_eq!(
364+
Error::Configuration(Box::new(io())).to_string(),
365+
"error with configuration: oops"
366+
);
367+
assert_eq!(
368+
Error::Io(io()).to_string(),
369+
"error communicating with database: oops"
370+
);
371+
assert_eq!(
372+
Error::Tls(Box::new(io())).to_string(),
373+
"error occurred while attempting to establish a TLS connection: oops"
374+
);
375+
assert_eq!(
376+
Error::Protocol("bad".into()).to_string(),
377+
"encountered unexpected or invalid data: bad"
378+
);
379+
assert_eq!(
380+
Error::ColumnDecode {
381+
index: "1".into(),
382+
source: Box::new(io()),
383+
}
384+
.to_string(),
385+
"error occurred while decoding column 1: oops"
386+
);
387+
assert_eq!(
388+
Error::Encode(Box::new(io())).to_string(),
389+
"error occurred while encoding a value: oops"
390+
);
391+
assert_eq!(
392+
Error::Decode(Box::new(io())).to_string(),
393+
"error occurred while decoding: oops"
394+
);
395+
assert_eq!(
396+
Error::AnyDriverError(Box::new(io())).to_string(),
397+
"error in Any driver mapping: oops"
398+
);
399+
}
400+
401+
#[test]
402+
fn test_display_message_variants() {
403+
assert_eq!(
404+
Error::InvalidArgument("missing host".into()).to_string(),
405+
"missing host"
406+
);
407+
assert_eq!(
408+
Error::TypeNotFound {
409+
type_name: "custom".into(),
410+
}
411+
.to_string(),
412+
"type named custom not found"
413+
);
414+
assert_eq!(
415+
Error::ColumnIndexOutOfBounds { index: 5, len: 3 }.to_string(),
416+
"column index out of bounds: the len is 3, but the index is 5"
417+
);
418+
assert_eq!(
419+
Error::ColumnNotFound("foo".into()).to_string(),
420+
"no column found for name: foo"
421+
);
422+
}
423+
424+
#[test]
425+
fn test_display_unit_variants() {
426+
assert_eq!(
427+
Error::RowNotFound.to_string(),
428+
"no rows returned by a query that expected to return at least one row"
429+
);
430+
assert_eq!(
431+
Error::PoolTimedOut.to_string(),
432+
"pool timed out while waiting for an open connection"
433+
);
434+
assert_eq!(
435+
Error::PoolClosed.to_string(),
436+
"attempted to acquire a connection on a closed pool"
437+
);
438+
assert_eq!(
439+
Error::WorkerCrashed.to_string(),
440+
"attempted to communicate with a crashed background worker"
441+
);
442+
assert_eq!(
443+
Error::InvalidSavePointStatement.to_string(),
444+
"attempted to call begin_with at non-zero transaction depth"
445+
);
446+
assert_eq!(
447+
Error::BeginFailed.to_string(),
448+
"got unexpected connection status after attempting to begin transaction"
449+
);
450+
}
451+
452+
#[test]
453+
fn test_debug_contains_variant_name() {
454+
let io = || std::io::Error::new(std::io::ErrorKind::Other, "oops");
455+
456+
assert!(format!("{:?}", Error::Configuration(Box::new(io()))).contains("Configuration"));
457+
assert!(format!("{:?}", Error::InvalidArgument("x".into())).contains("InvalidArgument"));
458+
assert!(format!("{:?}", Error::Io(io())).contains("Io"));
459+
assert!(format!("{:?}", Error::Protocol("x".into())).contains("Protocol"));
460+
assert!(format!("{:?}", Error::RowNotFound).contains("RowNotFound"));
461+
assert!(
462+
format!("{:?}", Error::TypeNotFound { type_name: "x".into() }).contains("TypeNotFound")
463+
);
464+
assert!(
465+
format!("{:?}", Error::ColumnIndexOutOfBounds { index: 0, len: 0 })
466+
.contains("ColumnIndexOutOfBounds")
467+
);
468+
assert!(
469+
format!("{:?}", Error::ColumnNotFound("x".into())).contains("ColumnNotFound")
470+
);
471+
assert!(format!("{:?}", Error::PoolTimedOut).contains("PoolTimedOut"));
472+
assert!(format!("{:?}", Error::PoolClosed).contains("PoolClosed"));
473+
assert!(format!("{:?}", Error::WorkerCrashed).contains("WorkerCrashed"));
474+
assert!(
475+
format!("{:?}", Error::InvalidSavePointStatement).contains("InvalidSavePointStatement")
476+
);
477+
assert!(format!("{:?}", Error::BeginFailed).contains("BeginFailed"));
478+
}
479+
}

0 commit comments

Comments
 (0)