Lines
95.51 %
Functions
24.14 %
Branches
100 %
use core::fmt::Debug;
use bonsaidb::core::document::{CollectionDocument, Emit, KeyId};
use bonsaidb::core::schema::{
Collection, CollectionMapReduce, DefaultSerialization, DefaultViewSerialization, Name,
Qualified, Schematic, SerializedCollection, View, ViewMapResult, ViewSchema,
};
use serde::{Deserialize, Serialize};
#[test]
fn name_only() {
#[derive(Collection, Debug)]
#[collection(name = "Name", core = ::bonsaidb::core)]
struct Test<T: Sync + Send + Debug>(T);
assert_eq!(
Test::<String>::collection_name(),
bonsaidb::core::schema::CollectionName::private("Name")
);
}
fn name_and_authority() {
#[collection(name = "Name", authority = "Authority")]
bonsaidb::core::schema::CollectionName::new("Authority", "Name")
fn views() {
#[derive(Clone, Collection, Debug, Serialize, Deserialize)]
#[collection(name = "Name", authority = "Authority", views = [ShapesByNumberOfSides])]
struct Shape {
pub sides: u32,
let schematic = Schematic::from_schema::<Shape>().unwrap();
schematic
.view::<ShapesByNumberOfSides>()
.expect("couldn't find view");
#[derive(Debug, Clone)]
struct ShapesByNumberOfSides;
impl View for ShapesByNumberOfSides {
type Collection = Shape;
type Key = u32;
type Value = usize;
fn name(&self) -> Name {
Name::new("by-number-of-sides")
impl ViewSchema for ShapesByNumberOfSides {
type MappedKey<'doc> = u32;
type View = Self;
impl CollectionMapReduce for ShapesByNumberOfSides {
fn map<'doc>(
&self,
document: CollectionDocument<Shape>,
) -> ViewMapResult<'doc, Self::View> {
document
.header
.emit_key_and_value(document.contents.sides, 1)
impl DefaultViewSerialization for ShapesByNumberOfSides {}
fn serialization() {
#[derive(Collection, Clone, Debug, Deserialize, Serialize)]
#[collection(
name = "Name",
authority = "Authority",
serialization = transmog_bincode::Bincode
)]
struct Test;
Test::collection_name(),
let _: transmog_bincode::Bincode = Test::format();
fn serialization_none() {
#[derive(Collection, Debug, Deserialize, Serialize)]
#[collection(name = "Name", authority = "Authority", serialization = None)]
impl DefaultSerialization for Test {}
// This could be done through using the macro in the tests for encryption
// Pretty pointless, maybe also error?
fn encryption_optional_no_key() {
#[collection(name = "Name", encryption_optional)]
fn encryption_optional_with_key() {
#[collection(name = "Name")]
#[collection(encryption_optional, encryption_key = Some(KeyId::Master))]
fn encryption_required_with_key() {
#[collection(encryption_required, encryption_key = Some(KeyId::Master))]
fn encryption_key() {
#[collection(encryption_key = Some(KeyId::Master))]
fn primary_key() {
#[collection(primary_key = u32)]
fn primary_key_natural_id() {
#[collection(primary_key = u32, natural_id = Some(1_u32))]
fn natural_id() {
#[collection(natural_id = Some(self.0 as u64))]
struct Test(u8);
fn natural_id_attr() {
struct TestTuple(#[natural_id] u8);
struct TestStruct {
#[natural_id]
named: String,