mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-27 03:51:38 +08:00
More extensive unit tests for Blob.
This commit is contained in:
parent
a43da3ef73
commit
c15a8dba79
70
src/blob.rs
70
src/blob.rs
@ -206,36 +206,78 @@ impl<'conn> Drop for Blob<'conn> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use std::io::{Read, Write, Seek, SeekFrom};
|
use std::io::{BufReader, BufRead, Read, Write, Seek, SeekFrom};
|
||||||
use {Connection, DatabaseName};
|
use {Connection, DatabaseName, Result};
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[cfg_attr(rustfmt, rustfmt_skip)]
|
#[cfg_attr(rustfmt, rustfmt_skip)]
|
||||||
fn test_blob() {
|
fn db_with_test_blob() -> Result<(Connection, i64)> {
|
||||||
let db = Connection::open_in_memory().unwrap();
|
let db = try!(Connection::open_in_memory());
|
||||||
let sql = "BEGIN;
|
let sql = "BEGIN;
|
||||||
CREATE TABLE test (content BLOB);
|
CREATE TABLE test (content BLOB);
|
||||||
INSERT INTO test VALUES (ZEROBLOB(10));
|
INSERT INTO test VALUES (ZEROBLOB(10));
|
||||||
END;";
|
END;";
|
||||||
db.execute_batch(sql).unwrap();
|
try!(db.execute_batch(sql));
|
||||||
let rowid = db.last_insert_rowid();
|
let rowid = db.last_insert_rowid();
|
||||||
|
Ok((db, rowid))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_blob() {
|
||||||
|
let (db, rowid) = db_with_test_blob().unwrap();
|
||||||
|
|
||||||
let mut blob = db.blob_open(DatabaseName::Main, "test", "content", rowid, false).unwrap();
|
let mut blob = db.blob_open(DatabaseName::Main, "test", "content", rowid, false).unwrap();
|
||||||
blob.write(b"Clob").unwrap();
|
assert_eq!(4, blob.write(b"Clob").unwrap());
|
||||||
let err = blob.write(b"5678901");
|
assert!(blob.write(b"5678901").is_err()); // cannot write past 10
|
||||||
// writeln!(io::stderr(), "{:?}", err);
|
assert_eq!(4, blob.write(b"5678").unwrap());
|
||||||
assert!(err.is_err());
|
|
||||||
|
|
||||||
assert!(blob.reopen(rowid).is_ok());
|
blob.reopen(rowid).unwrap();
|
||||||
assert!(blob.close().is_ok());
|
blob.close().unwrap();
|
||||||
|
|
||||||
blob = db.blob_open(DatabaseName::Main, "test", "content", rowid, true).unwrap();
|
blob = db.blob_open(DatabaseName::Main, "test", "content", rowid, true).unwrap();
|
||||||
let mut bytes = [0u8; 5];
|
let mut bytes = [0u8; 5];
|
||||||
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
|
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
|
||||||
|
assert_eq!(&bytes, b"Clob5");
|
||||||
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
|
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
|
||||||
|
assert_eq!(&bytes, b"678\0\0");
|
||||||
assert_eq!(0, blob.read(&mut bytes[..]).unwrap());
|
assert_eq!(0, blob.read(&mut bytes[..]).unwrap());
|
||||||
|
|
||||||
assert!(blob.reopen(rowid).is_ok());
|
blob.seek(SeekFrom::Start(2)).unwrap();
|
||||||
blob.seek(SeekFrom::Start(0));
|
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
|
||||||
|
assert_eq!(&bytes, b"ob567");
|
||||||
|
|
||||||
|
blob.seek(SeekFrom::Current(-6)).unwrap();
|
||||||
|
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
|
||||||
|
assert_eq!(&bytes, b"lob56");
|
||||||
|
|
||||||
|
blob.seek(SeekFrom::End(-6)).unwrap();
|
||||||
|
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
|
||||||
|
assert_eq!(&bytes, b"5678\0");
|
||||||
|
|
||||||
|
blob.reopen(rowid).unwrap();
|
||||||
|
assert_eq!(5, blob.read(&mut bytes[..]).unwrap());
|
||||||
|
assert_eq!(&bytes, b"Clob5");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_blob_in_bufreader() {
|
||||||
|
let (db, rowid) = db_with_test_blob().unwrap();
|
||||||
|
|
||||||
|
let mut blob = db.blob_open(DatabaseName::Main, "test", "content", rowid, false).unwrap();
|
||||||
|
assert_eq!(8, blob.write(b"one\ntwo\n").unwrap());
|
||||||
|
|
||||||
|
blob.reopen(rowid).unwrap();
|
||||||
|
let mut reader = BufReader::new(blob);
|
||||||
|
|
||||||
|
let mut line = String::new();
|
||||||
|
assert_eq!(4, reader.read_line(&mut line).unwrap());
|
||||||
|
assert_eq!("one\n", line);
|
||||||
|
|
||||||
|
line.truncate(0);
|
||||||
|
assert_eq!(4, reader.read_line(&mut line).unwrap());
|
||||||
|
assert_eq!("two\n", line);
|
||||||
|
|
||||||
|
line.truncate(0);
|
||||||
|
assert_eq!(2, reader.read_line(&mut line).unwrap());
|
||||||
|
assert_eq!("\0\0", line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user