1
use core::fmt::Debug;
2

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

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

            
16
1
    assert_eq!(
17
1
        Test::<String>::collection_name(),
18
1
        bonsaidb::core::schema::CollectionName::private("Name")
19
1
    );
20
1
}
21

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

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

            
34
1
#[test]
35
1
fn views() {
36
1
    #[derive(Clone, 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
    schematic
44
1
        .view::<ShapesByNumberOfSides>()
45
1
        .expect("couldn't find view");
46
1

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

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

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

            
60
1
    impl ViewSchema for ShapesByNumberOfSides {
61
1
        type MappedKey<'doc> = u32;
62
1
        type View = Self;
63
1
    }
64
1

            
65
1
    impl CollectionMapReduce for ShapesByNumberOfSides {
66
1
        fn map<'doc>(
67
            &self,
68
            document: CollectionDocument<Shape>,
69
        ) -> ViewMapResult<'doc, Self::View> {
70
            document
71
                .header
72
                .emit_key_and_value(document.contents.sides, 1)
73
        }
74
1
    }
75
1

            
76
1
    impl DefaultViewSerialization for ShapesByNumberOfSides {}
77
1
}
78

            
79
1
#[test]
80
1
fn serialization() {
81
2
    #[derive(Collection, Clone, Debug, Deserialize, Serialize)]
82
1
    #[collection(
83
1
        name = "Name",
84
1
        authority = "Authority",
85
1
        serialization = transmog_bincode::Bincode
86
1
    )]
87
1
    struct Test;
88
1

            
89
1
    assert_eq!(
90
1
        Test::collection_name(),
91
1
        bonsaidb::core::schema::CollectionName::new("Authority", "Name")
92
1
    );
93

            
94
1
    let _: transmog_bincode::Bincode = Test::format();
95
1
}
96

            
97
1
#[test]
98
1
fn serialization_none() {
99
1
    #[derive(Collection, Debug, Deserialize, Serialize)]
100
1
    #[collection(name = "Name", authority = "Authority", serialization = None)]
101
1
    struct Test;
102
1

            
103
1
    impl DefaultSerialization for Test {}
104
1

            
105
1
    assert_eq!(
106
1
        Test::collection_name(),
107
1
        bonsaidb::core::schema::CollectionName::new("Authority", "Name")
108
1
    );
109
1
}
110

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

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

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

            
136
1
#[test]
137
1
fn encryption_key() {
138
1
    #[derive(Collection, Debug, Deserialize, Serialize)]
139
1
    #[collection(name = "Name")]
140
1
    #[collection(encryption_key = Some(KeyId::Master))]
141
1
    struct Test;
142
1
}
143

            
144
1
#[test]
145
1
fn primary_key() {
146
1
    #[derive(Collection, Debug, Deserialize, Serialize)]
147
1
    #[collection(name = "Name")]
148
1
    #[collection(primary_key = u32)]
149
1
    struct Test;
150
1
}
151

            
152
1
#[test]
153
1
fn primary_key_natural_id() {
154
1
    #[derive(Collection, Debug, Deserialize, Serialize)]
155
1
    #[collection(name = "Name")]
156
1
    #[collection(primary_key = u32, natural_id = Some(1_u32))]
157
1
    struct Test;
158
1
}
159

            
160
1
#[test]
161
1
fn natural_id() {
162
1
    #[derive(Collection, Debug, Deserialize, Serialize)]
163
1
    #[collection(name = "Name")]
164
1
    #[collection(natural_id = Some(self.0 as u64))]
165
1
    struct Test(u8);
166
1
}
167

            
168
1
#[test]
169
1
fn natural_id_attr() {
170
1
    #[derive(Collection, Debug, Deserialize, Serialize)]
171
1
    #[collection(name = "Name")]
172
1
    struct TestTuple(#[natural_id] u8);
173
1

            
174
1
    #[derive(Collection, Debug, Deserialize, Serialize)]
175
1
    #[collection(name = "Name")]
176
1
    struct TestStruct {
177
1
        #[natural_id]
178
1
        named: String,
179
1
    }
180
1
}