Backup, Synchronize, Versionize
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.2 KiB

// This file is part of bsv.
//
// bsv is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// cdb is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
// more details.
//
// You should have received a copy of the Affero GNU General Public License
// along with cdb. If not, see <https://www.gnu.org/licenses/>.
extern crate tempfile;
extern crate libbsv;
use std::path::{PathBuf};
use std::io::{Cursor, Read};
use std::fs::{create_dir_all, write};
use tempfile::{TempDir};
use libbsv::{
Result,
ObjectId,
OTYPE_BLOB,
simple_db::{
SimpleDb,
}
};
const PAYLOAD: &[u8; 12] = b"Hello World!";
const PAYLOAD_OID: &str = "c3b4032160b015b2261530532a6c49f2bdadbe0687fb1f5a6a32e083";
#[test]
fn simple_db_has_object() {
let temp_dir = TempDir::new().unwrap();
let db = SimpleDb::new(temp_dir.path()).unwrap();
db.setup().unwrap();
let oid = ObjectId::from_str("0001020304050607").unwrap();
assert!(!db.has_object(&oid));
let mut object_path: PathBuf = temp_dir.path().into();
object_path.push("objects");
object_path.push("0001");
create_dir_all(&object_path).unwrap();
object_path.push("020304050607");
write(object_path, "test").unwrap();
assert!(db.has_object(&oid));
}
#[test]
fn simple_db_store_read() -> Result<()> {
let expected_oid = ObjectId::from_str(PAYLOAD_OID)?;
let temp_dir = TempDir::new()?;
let db = SimpleDb::new(temp_dir.path())?;
db.setup()?;
assert!(!db.has_object(&expected_oid));
let oid = db.store_object(OTYPE_BLOB, Cursor::new(PAYLOAD))?;
assert!(db.has_object(&expected_oid));
assert_eq!(oid, expected_oid);
let mut reader = db.read_object(&oid)?;
assert_eq!(reader.object_type(), *OTYPE_BLOB);
assert_eq!(reader.size(), PAYLOAD.len() as u64);
let mut data = vec![];
reader.read_to_end(&mut data)?;
assert_eq!(data, *PAYLOAD);
Ok(())
}