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

            
3
use bonsaidb::core::{
4
    document::{CollectionDocument, Emit, 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

            
24
1
#[test]
25
1
fn name_and_authority() {
26
1
    #[derive(Collection, Debug)]
27
1
    #[collection(name = "Name", authority = "Authority")]
28
1
    struct Test<T: Sync + Send + Debug>(T);
29
1

            
30
1
    assert_eq!(
31
1
        Test::<String>::collection_name(),
32
1
        bonsaidb::core::schema::CollectionName::new("Authority", "Name")
33
1
    );
34
1
}
35

            
36
1
#[test]
37
1
fn views() {
38
1
    #[derive(Clone, Collection, Debug, Serialize, Deserialize)]
39
1
    #[collection(name = "Name", authority = "Authority", views = [ShapesByNumberOfSides])]
40
1
    struct Shape {
41
1
        pub sides: u32,
42
1
    }
43
1

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

            
47
1
    #[derive(Debug, Clone)]
48
    struct ShapesByNumberOfSides;
49

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

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

            
60
    impl CollectionViewSchema for ShapesByNumberOfSides {
61
        type View = Self;
62

            
63
        fn map(&self, document: CollectionDocument<Shape>) -> ViewMapResult<Self::View> {
64
            document
65
                .header
66
                .emit_key_and_value(document.contents.sides, 1)
67
        }
68
    }
69

            
70
    impl DefaultViewSerialization for ShapesByNumberOfSides {}
71
1
}
72

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

            
83
1
    assert_eq!(
84
1
        Test::collection_name(),
85
1
        bonsaidb::core::schema::CollectionName::new("Authority", "Name")
86
1
    );
87

            
88
1
    let _: transmog_bincode::Bincode = Test::format();
89
1
}
90

            
91
1
#[test]
92
1
fn serialization_none() {
93
1
    #[derive(Collection, Debug, Deserialize, Serialize)]
94
1
    #[collection(name = "Name", authority = "Authority", serialization = None)]
95
1
    struct Test;
96
1

            
97
1
    impl DefaultSerialization for Test {}
98
1

            
99
1
    assert_eq!(
100
1
        Test::collection_name(),
101
1
        bonsaidb::core::schema::CollectionName::new("Authority", "Name")
102
1
    );
103
1
}
104

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

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

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

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

            
138
1
#[test]
139
1
fn primary_key() {
140
1
    #[derive(Collection, Debug, Deserialize, Serialize)]
141
1
    #[collection(name = "Name")]
142
1
    #[collection(primary_key = u32)]
143
1
    struct Test;
144
1
}
145

            
146
1
#[test]
147
1
fn primary_key_natural_id() {
148
1
    #[derive(Collection, Debug, Deserialize, Serialize)]
149
1
    #[collection(name = "Name")]
150
1
    #[collection(primary_key = u32, natural_id = |_:&Self| Some(1_u32))]
151
1
    struct Test;
152
1
}
153

            
154
1
#[test]
155
1
fn natural_id() {
156
1
    #[derive(Collection, Debug, Deserialize, Serialize)]
157
1
    #[collection(name = "Name")]
158
1
    #[collection( natural_id = |_:&Self| Some(1_u64))]
159
1
    struct Test;
160
1
}