This commit is contained in:
gwenn
2018-10-28 08:51:02 +01:00
parent 03561e36fb
commit 1598d4bc30
16 changed files with 177 additions and 111 deletions

View File

@@ -236,7 +236,8 @@ mod test {
let v4: DateTime<Utc> = db
.query_row("SELECT '2016-02-23 23:56:04.789+00:00'", NO_PARAMS, |r| {
r.get(0)
}).unwrap();
})
.unwrap();
assert_eq!(utc, v4);
}

View File

@@ -28,8 +28,9 @@ impl fmt::Display for FromSqlError {
FromSqlError::InvalidType => write!(f, "Invalid type"),
FromSqlError::OutOfRange(i) => write!(f, "Value {} out of range", i),
#[cfg(feature = "i128_blob")]
FromSqlError::InvalidI128Size(s) =>
write!(f, "Cannot read 128bit value out of {} byte blob", s),
FromSqlError::InvalidI128Size(s) => {
write!(f, "Cannot read 128bit value out of {} byte blob", s)
}
FromSqlError::Other(ref err) => err.fmt(f),
}
}
@@ -41,8 +42,7 @@ impl Error for FromSqlError {
FromSqlError::InvalidType => "invalid type",
FromSqlError::OutOfRange(_) => "value out of range",
#[cfg(feature = "i128_blob")]
FromSqlError::InvalidI128Size(_) =>
"unexpected blob size for 128bit value",
FromSqlError::InvalidI128Size(_) => "unexpected blob size for 128bit value",
FromSqlError::Other(ref err) => err.description(),
}
}

View File

@@ -46,7 +46,8 @@
//! }
//! }
//!
//! # // Prevent this doc test from being wrapped in a `fn main()` so that it will compile.
//! # // Prevent this doc test from being wrapped in a `fn main()` so that it
//! # // will compile.
//! # fn main() {}
//! ```
//!
@@ -239,7 +240,8 @@ mod test {
db.execute(
"INSERT INTO foo(b, t, i, f) VALUES (X'0102', 'text', 1, 1.5)",
NO_PARAMS,
).unwrap();
)
.unwrap();
let mut stmt = db.prepare("SELECT b, t, i, f, n FROM foo").unwrap();
let mut rows = stmt.query(NO_PARAMS).unwrap();
@@ -354,7 +356,8 @@ mod test {
db.execute(
"INSERT INTO foo(b, t, i, f) VALUES (X'0102', 'text', 1, 1.5)",
NO_PARAMS,
).unwrap();
)
.unwrap();
let mut stmt = db.prepare("SELECT b, t, i, f, n FROM foo").unwrap();
let mut rows = stmt.query(NO_PARAMS).unwrap();

View File

@@ -20,7 +20,8 @@ impl FromSql for Value {
ValueRef::Text(s) => serde_json::from_str(s),
ValueRef::Blob(b) => serde_json::from_slice(b),
_ => return Err(FromSqlError::InvalidType),
}.map_err(|err| FromSqlError::Other(Box::new(err)))
}
.map_err(|err| FromSqlError::Other(Box::new(err)))
}
}
@@ -46,7 +47,8 @@ mod test {
db.execute(
"INSERT INTO foo (t, b) VALUES (?, ?)",
&[&data as &ToSql, &json.as_bytes()],
).unwrap();
)
.unwrap();
let t: serde_json::Value = db
.query_row("SELECT t FROM foo", NO_PARAMS, |r| r.get(0))

View File

@@ -25,7 +25,8 @@ impl FromSql for time::Timespec {
time::strptime(s, SQLITE_DATETIME_FMT_LEGACY)
.or_else(|_| Err(FromSqlError::Other(Box::new(err))))
})
}).map(|tm| tm.to_timespec())
})
.map(|tm| tm.to_timespec())
}
}

View File

@@ -207,34 +207,45 @@ mod test {
#[cfg(feature = "i128_blob")]
#[test]
fn test_i128() {
use {Connection, NO_PARAMS};
use std::i128;
use {Connection, NO_PARAMS};
let db = Connection::open_in_memory().unwrap();
db.execute_batch("CREATE TABLE foo (i128 BLOB, desc TEXT)").unwrap();
db.execute("
db.execute_batch("CREATE TABLE foo (i128 BLOB, desc TEXT)")
.unwrap();
db.execute(
"
INSERT INTO foo(i128, desc) VALUES
(?, 'zero'),
(?, 'neg one'), (?, 'neg two'),
(?, 'pos one'), (?, 'pos two'),
(?, 'min'), (?, 'max')",
&[0i128, -1i128, -2i128, 1i128, 2i128, i128::MIN, i128::MAX]
).unwrap();
&[0i128, -1i128, -2i128, 1i128, 2i128, i128::MIN, i128::MAX],
)
.unwrap();
let mut stmt = db.prepare("SELECT i128, desc FROM foo ORDER BY i128 ASC").unwrap();
let mut stmt = db
.prepare("SELECT i128, desc FROM foo ORDER BY i128 ASC")
.unwrap();
let res = stmt.query_map(
NO_PARAMS,
|row| (row.get::<_, i128>(0), row.get::<_, String>(1))
).unwrap().collect::<Result<Vec<_>, _>>().unwrap();
let res = stmt
.query_map(NO_PARAMS, |row| {
(row.get::<_, i128>(0), row.get::<_, String>(1))
})
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(res, &[
(i128::MIN, "min".to_owned()),
(-2, "neg two".to_owned()),
(-1, "neg one".to_owned()),
(0, "zero".to_owned()),
(1, "pos one".to_owned()),
(2, "pos two".to_owned()),
(i128::MAX, "max".to_owned()),
]);
assert_eq!(
res,
&[
(i128::MIN, "min".to_owned()),
(-2, "neg two".to_owned()),
(-1, "neg one".to_owned()),
(0, "zero".to_owned()),
(1, "pos one".to_owned()),
(2, "pos two".to_owned()),
(i128::MAX, "max".to_owned()),
]
);
}
}