1
1
use core::fmt::Debug;
2

            
3
use bonsaidb::core::{
4
    document::{CollectionDocument, KeyId},
5
    schema::{
6
        Collection, CollectionViewSchema, DefaultSerialization, DefaultViewSerialization, Name,
7
        Schematic, SerializedCollection, View, ViewMapResult,
8
    },
9
};
10
use serde::{Deserialize, Serialize};
11

            
12
1
#[test]
13
1
fn name_only() {
14
1
    #[derive(Collection, Debug)]
15
1
    #[collection(name = "Name", core = ::bonsaidb::core)]
16
1
    struct Test<T: Sync + Send + Debug>(T);
17
1

            
18
1
    assert_eq!(
19
1
        Test::<String>::collection_name(),
20
1
        bonsaidb::core::schema::CollectionName::private("Name")
21
1
    );
22
1
}
23
1
#[test]
24
1
fn name_and_authority() {
25
1
    #[derive(Collection, Debug)]
26
1
    #[collection(name = "Name", authority = "Authority")]
27
1
    struct Test<T: Sync + Send + Debug>(T);
28
1

            
29
1
    assert_eq!(
30
1
        Test::<String>::collection_name(),
31
1
        bonsaidb::core::schema::CollectionName::new("Authority", "Name")
32
1
    );
33
1
}
34
1
#[test]
35
1
fn views() {
36
1
    #[derive(Collection, Debug, Serialize, Deserialize)]
37
1
    #[collection(name = "Name", authority = "Authority", views = [ShapesByNumberOfSides])]
38
1
    struct Shape {
39
1
        pub sides: u32,
40
1
    }
41
1

            
42
1
    let schematic = Schematic::from_schema::<Shape>().unwrap();
43
1
    assert!(schematic.view::<ShapesByNumberOfSides>().is_some());
44

            
45
1
    #[derive(Debug, Clone)]
46
    struct ShapesByNumberOfSides;
47

            
48
    impl View for ShapesByNumberOfSides {
49
        type Collection = Shape;
50
        type Key = u32;
51
        type Value = usize;
52

            
53
1
        fn name(&self) -> Name {
54
1
            Name::new("by-number-of-sides")
55
1
        }
56
    }
57

            
58
    impl CollectionViewSchema for ShapesByNumberOfSides {
59
        type View = Self;
60

            
61
        fn map(&self, document: CollectionDocument<Shape>) -> ViewMapResult<Self::View> {
62
            Ok(document.emit_key_and_value(document.contents.sides, 1))
63
        }
64
    }
65

            
66
    impl DefaultViewSerialization for ShapesByNumberOfSides {}
67
1
}
68

            
69
1
#[test]
70
1
fn serialization() {
71
2
    #[derive(Collection, Debug, Deserialize, Serialize)]
72
1
    #[collection(
73
1
        name = "Name",
74
1
        authority = "Authority",
75
1
        serialization = transmog_bincode::Bincode
76
1
    )]
77
1
    struct Test;
78
1

            
79
1
    assert_eq!(
80
1
        Test::collection_name(),
81
1
        bonsaidb::core::schema::CollectionName::new("Authority", "Name")
82
1
    );
83

            
84
1
    let _: transmog_bincode::Bincode = Test::format();
85
1
}
86

            
87
1
#[test]
88
1
fn serialization_none() {
89
1
    #[derive(Collection, Debug, Deserialize, Serialize)]
90
1
    #[collection(name = "Name", authority = "Authority", serialization = None)]
91
1
    struct Test;
92
1

            
93
1
    impl DefaultSerialization for Test {}
94
1

            
95
1
    assert_eq!(
96
1
        Test::collection_name(),
97
1
        bonsaidb::core::schema::CollectionName::new("Authority", "Name")
98
1
    );
99
1
}
100

            
101
// TODO actually test the functionality
102
// This could be done through using the macro in the tests for encryption
103
1
#[test]
104
// Pretty pointless, maybe also error?
105
1
fn encryption_optional_no_key() {
106
1
    #[derive(Collection, Debug, Deserialize, Serialize)]
107
1
    #[collection(name = "Name", encryption_optional)]
108
1
    struct Test;
109
1
}
110

            
111
1
#[test]
112
1
fn encryption_optional_with_key() {
113
1
    #[derive(Collection, Debug, Deserialize, Serialize)]
114
1
    #[collection(name = "Name")]
115
1
    #[collection(encryption_optional, encryption_key = Some(KeyId::Master))]
116
1
    struct Test;
117
1
}
118

            
119
1
#[test]
120
1
fn encryption_required_with_key() {
121
1
    #[derive(Collection, Debug, Deserialize, Serialize)]
122
1
    #[collection(name = "Name")]
123
1
    #[collection(encryption_required, encryption_key = Some(KeyId::Master))]
124
1
    struct Test;
125
1
}
126

            
127
1
#[test]
128
1
fn encryption_key() {
129
1
    #[derive(Collection, Debug, Deserialize, Serialize)]
130
1
    #[collection(name = "Name")]
131
1
    #[collection(encryption_key = Some(KeyId::Master))]
132
1
    struct Test;
133
1
}