1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
mod ent;
mod filter;

pub use ent::*;
pub use filter::*;

#[cfg(feature = "macros")]
pub use entity_async_graphql_macros::*;

#[cfg(test)]
mod tests {
    use super::*;

    use async_graphql::{value, Context, EmptyMutation, EmptySubscription, Object, Schema};
    use entity::{Database, DatabaseRc, Edge, Field, Id, UntypedEnt, Value};
    use entity_inmemory::InmemoryDatabase;
    use entity_sled::SledDatabase;
    use std::collections::HashMap;

    macro_rules! impl_tests {
        ($db_type:ty, $new_db:expr) => {
            /// Creates a new database with some test entries used throughout
            ///
            /// IDs: 1-3 ~ are type1 with no fields or edges
            /// IDs: 4-6 ~ are type2 with value fields and no edges
            /// IDs: 7-9 ~ are type3 with collection fields and no edges
            /// IDs: 10-12 ~ are type4 with edges to 1-9 and no fields
            fn new_test_database() -> $db_type {
                let db = $new_db;

                // 1-3 have no fields or edges
                let _ = db
                    .insert(Box::from(UntypedEnt::from_collections(1, vec![], vec![])))
                    .unwrap();
                let _ = db
                    .insert(Box::from(UntypedEnt::from_collections(2, vec![], vec![])))
                    .unwrap();
                let _ = db
                    .insert(Box::from(UntypedEnt::from_collections(3, vec![], vec![])))
                    .unwrap();

                // 4-6 have value fields only
                let _ = db
                    .insert(Box::from(UntypedEnt::from_collections(
                        4,
                        vec![Field::new("a", 1), Field::new("b", 2)],
                        vec![],
                    )))
                    .unwrap();
                let _ = db
                    .insert(Box::from(UntypedEnt::from_collections(
                        5,
                        vec![Field::new("a", 3), Field::new("b", 4)],
                        vec![],
                    )))
                    .unwrap();
                let _ = db
                    .insert(Box::from(UntypedEnt::from_collections(
                        6,
                        vec![Field::new("a", 5), Field::new("b", 6)],
                        vec![],
                    )))
                    .unwrap();

                // 7-9 have collection fields only
                let _ = db
                    .insert(Box::from(UntypedEnt::from_collections(
                        7,
                        vec![Field::new(
                            "f",
                            Value::from(
                                vec![(String::from("a"), 3), (String::from("b"), 5)]
                                    .into_iter()
                                    .collect::<HashMap<String, u8>>(),
                            ),
                        )],
                        vec![],
                    )))
                    .unwrap();
                let _ = db
                    .insert(Box::from(UntypedEnt::from_collections(
                        8,
                        vec![Field::new("f", vec![1, 2])],
                        vec![],
                    )))
                    .unwrap();
                let _ = db
                    .insert(Box::from(UntypedEnt::from_collections(
                        9,
                        vec![Field::new(
                            "f",
                            Value::from(
                                vec![
                                    (String::from("a"), Value::from(vec![1, 2])),
                                    (String::from("b"), Value::from(vec![3, 4])),
                                ]
                                .into_iter()
                                .collect::<HashMap<String, Value>>(),
                            ),
                        )],
                        vec![],
                    )))
                    .unwrap();

                // 10-12 have edges only
                let _ = db
                    .insert(Box::from(UntypedEnt::from_collections(
                        10,
                        vec![],
                        vec![
                            Edge::new("a", 1),
                            Edge::new("b", vec![3, 4, 5]),
                            Edge::new("c", None),
                        ],
                    )))
                    .unwrap();
                let _ = db
                    .insert(Box::from(UntypedEnt::from_collections(
                        11,
                        vec![],
                        vec![Edge::new("a", 2), Edge::new("b", vec![1, 2, 3, 4, 5, 6])],
                    )))
                    .unwrap();
                let _ = db
                    .insert(Box::from(UntypedEnt::from_collections(
                        12,
                        vec![],
                        vec![
                            Edge::new("a", 3),
                            Edge::new("b", vec![]),
                            Edge::new("c", Some(8)),
                        ],
                    )))
                    .unwrap();

                db
            }

            struct TestQuery;

            #[Object]
            impl TestQuery {
                async fn ent<'ctx>(
                    &self,
                    ctx: &'ctx Context<'_>,
                    id: Option<Id>,
                    filter: Option<GqlEntFilter>,
                ) -> async_graphql::Result<Vec<GqlDynEnt>> {
                    let db = ctx.data::<DatabaseRc>()?;

                    if let Some(id) = id {
                        db.get_all(vec![id]).map(|x| x.into_iter().map(GqlDynEnt::from).collect())
                            .map_err(|x| async_graphql::Error::new(x.to_string()))
                    } else if let Some(filter) = filter {
                        db.find_all(filter.into()).map(|x| x.into_iter().map(GqlDynEnt::from).collect())
                            .map_err(|x| async_graphql::Error::new(x.to_string()))
                    } else {
                        Err(async_graphql::Error::new("Must provide one argument"))
                    }
                }
            }

            #[test]
            fn supports_ent_trait_object_as_output_object() {
                let schema = Schema::build(TestQuery, EmptyMutation, EmptySubscription)
                    .data(DatabaseRc::new(Box::new(new_test_database())))
                    .finish();
                let input = r#"
                    { 
                        ent(id: 1) { 
                            id 
                        } 
                    }
                "#;
                let response = futures::executor::block_on(schema.execute(input.trim()));
                assert_eq!(
                    response.data,
                    value!({
                        "ent": [
                            { "id": 1 },
                        ],
                    })
                );
            }

            #[test]
            fn supports_filtering() {
                let schema = Schema::build(TestQuery, EmptyMutation, EmptySubscription)
                    .data(DatabaseRc::new(Box::new(new_test_database())))
                    .finish();
                let input = r#"
                    { 
                        ent(filter: { id: { equals: 1 } }) { 
                            id 
                        } 
                    }
                "#;
                let response = futures::executor::block_on(schema.execute(input.trim()));
                assert_eq!(
                    response.data,
                    value!({
                        "ent": [
                            { "id": 1 },
                        ],
                    })
                );
            }
        };
    }

    mod inmemory {
        use super::*;

        impl_tests!(InmemoryDatabase, InmemoryDatabase::default());
    }

    mod sled {
        use super::*;

        impl_tests!(
            SledDatabase,
            SledDatabase::new(::sled::Config::new().temporary(true).open().unwrap())
        );
    }
}