Lines
95.83 %
Functions
28.1 %
Branches
100 %
use core::fmt::Debug;
use bonsaidb::core::{
document::{CollectionDocument, Emit, KeyId},
schema::{
Collection, CollectionViewSchema, DefaultSerialization, DefaultViewSerialization, Name,
Schematic, SerializedCollection, View, ViewMapResult,
},
};
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();
assert!(schematic.view::<ShapesByNumberOfSides>().is_some());
#[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 CollectionViewSchema for ShapesByNumberOfSides {
type View = Self;
fn map(&self, document: CollectionDocument<Shape>) -> ViewMapResult<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 = |_:&Self| Some(1_u32))]
fn natural_id() {
#[collection( natural_id = |_:&Self| Some(1_u64))]