use crate::core::document::document::Document;
use crate::core::document::field::Field;
use crate::core::document::field_type::FieldType;
use crate::core::document::stored_field::StoredField;
use crate::core::index::BytesRefValue;
use crate::core::index::index_reader::IndexReader;
use crate::test_framework::core::util::lucene_test_case::{new_directory_shared, random};
use crate::core::index::stored_fields::StoredFields;
use crate::core::util::error::lucene_error::Result;
use crate::test_framework::core::index::random_index_writer::RandomIndexWriter;
#[allow(dead_code)] pub struct TestBinaryDocument;
#[test]
fn test_binary_field_in_index() -> Result<()> {
let binary_val_stored = "this text will be stored as a byte array in the index";
let _binary_val_compressed =
"this text will be also stored and compressed as a byte array in the index";
let mut ft = FieldType::new();
ft.set_stored(true)?;
let binary_fld_stored =
StoredField::from_binary("binaryStored", binary_val_stored.as_bytes().to_vec())?;
let string_fld_stored = Field::from_string("stringStored", binary_val_stored, ft)?;
let mut doc = Document::new();
doc.add(binary_fld_stored);
doc.add(string_fld_stored);
assert_eq!(2, doc.get_fields().len());
let mut random = random();
let dir = new_directory_shared(&mut random)?;
let writer = RandomIndexWriter::new(&mut random, dir.clone())?;
writer.add_document(&mut random, doc)?;
let reader = writer.get_reader(&mut random)?;
let doc_from_reader = reader.stored_fields()?.document(0)?;
assert!(!doc_from_reader.get_fields().is_empty());
let bytes = doc_from_reader.get_binary_value("binaryStored")?;
assert!(bytes.is_some());
let binary_fld_stored_test = bytes.unwrap().as_bytes_ref().utf8_to_string()?.to_owned();
assert_eq!(binary_fld_stored_test, binary_val_stored);
let string_fld_stored_test = doc_from_reader.get("stringStored")?;
assert!(string_fld_stored_test.is_some());
assert_eq!(string_fld_stored_test.unwrap().as_ref(), binary_val_stored);
writer.close(&mut random)?;
Ok(())
}