|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use value::Value; |
| 4 | +use ast::InputValue; |
| 5 | +use schema::model::RootNode; |
| 6 | + |
| 7 | +struct TestType; |
| 8 | + |
| 9 | +graphql_object!(TestType: () |&self| { |
| 10 | + field a() -> &str { |
| 11 | + "a" |
| 12 | + } |
| 13 | + |
| 14 | + field b() -> &str { |
| 15 | + "b" |
| 16 | + } |
| 17 | +}); |
| 18 | + |
| 19 | +fn run_variable_query<F>(query: &str, vars: HashMap<String, InputValue>, f: F) |
| 20 | + where F: Fn(&HashMap<String, Value>) -> () |
| 21 | +{ |
| 22 | + let schema = RootNode::new(TestType, ()); |
| 23 | + |
| 24 | + let (result, errs) = ::execute(query, None, &schema, &vars, &()) |
| 25 | + .expect("Execution failed"); |
| 26 | + |
| 27 | + assert_eq!(errs, []); |
| 28 | + |
| 29 | + println!("Result: {:?}", result); |
| 30 | + |
| 31 | + let obj = result.as_object_value().expect("Result is not an object"); |
| 32 | + |
| 33 | + f(obj); |
| 34 | +} |
| 35 | + |
| 36 | +fn run_query<F>(query: &str, f: F) |
| 37 | + where F: Fn(&HashMap<String, Value>) -> () |
| 38 | +{ |
| 39 | + run_variable_query(query, HashMap::new(), f); |
| 40 | +} |
| 41 | + |
| 42 | +#[test] |
| 43 | +fn scalar_include_true() { |
| 44 | + run_query( |
| 45 | + "{ a, b @include(if: true) }", |
| 46 | + |result| { |
| 47 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 48 | + assert_eq!(result.get("b"), Some(&Value::string("b"))); |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +#[test] |
| 53 | +fn scalar_include_false() { |
| 54 | + run_query( |
| 55 | + "{ a, b @include(if: false) }", |
| 56 | + |result| { |
| 57 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 58 | + assert_eq!(result.get("b"), None); |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +#[test] |
| 63 | +fn scalar_skip_false() { |
| 64 | + run_query( |
| 65 | + "{ a, b @skip(if: false) }", |
| 66 | + |result| { |
| 67 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 68 | + assert_eq!(result.get("b"), Some(&Value::string("b"))); |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +fn scalar_skip_true() { |
| 74 | + run_query( |
| 75 | + "{ a, b @skip(if: true) }", |
| 76 | + |result| { |
| 77 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 78 | + assert_eq!(result.get("b"), None); |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +#[test] |
| 86 | +fn fragment_spread_include_true() { |
| 87 | + run_query( |
| 88 | + "{ a, ...Frag @include(if: true) } fragment Frag on TestType { b }", |
| 89 | + |result| { |
| 90 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 91 | + assert_eq!(result.get("b"), Some(&Value::string("b"))); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +#[test] |
| 96 | +fn fragment_spread_include_false() { |
| 97 | + run_query( |
| 98 | + "{ a, ...Frag @include(if: false) } fragment Frag on TestType { b }", |
| 99 | + |result| { |
| 100 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 101 | + assert_eq!(result.get("b"), None); |
| 102 | + }); |
| 103 | +} |
| 104 | + |
| 105 | +#[test] |
| 106 | +fn fragment_spread_skip_false() { |
| 107 | + run_query( |
| 108 | + "{ a, ...Frag @skip(if: false) } fragment Frag on TestType { b }", |
| 109 | + |result| { |
| 110 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 111 | + assert_eq!(result.get("b"), Some(&Value::string("b"))); |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +#[test] |
| 116 | +fn fragment_spread_skip_true() { |
| 117 | + run_query( |
| 118 | + "{ a, ...Frag @skip(if: true) } fragment Frag on TestType { b }", |
| 119 | + |result| { |
| 120 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 121 | + assert_eq!(result.get("b"), None); |
| 122 | + }); |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +#[test] |
| 128 | +fn inline_fragment_include_true() { |
| 129 | + run_query( |
| 130 | + "{ a, ... on TestType @include(if: true) { b } }", |
| 131 | + |result| { |
| 132 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 133 | + assert_eq!(result.get("b"), Some(&Value::string("b"))); |
| 134 | + }); |
| 135 | +} |
| 136 | + |
| 137 | +#[test] |
| 138 | +fn inline_fragment_include_false() { |
| 139 | + run_query( |
| 140 | + "{ a, ... on TestType @include(if: false) { b } }", |
| 141 | + |result| { |
| 142 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 143 | + assert_eq!(result.get("b"), None); |
| 144 | + }); |
| 145 | +} |
| 146 | + |
| 147 | +#[test] |
| 148 | +fn inline_fragment_skip_false() { |
| 149 | + run_query( |
| 150 | + "{ a, ... on TestType @skip(if: false) { b } }", |
| 151 | + |result| { |
| 152 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 153 | + assert_eq!(result.get("b"), Some(&Value::string("b"))); |
| 154 | + }); |
| 155 | +} |
| 156 | + |
| 157 | +#[test] |
| 158 | +fn inline_fragment_skip_true() { |
| 159 | + run_query( |
| 160 | + "{ a, ... on TestType @skip(if: true) { b } }", |
| 161 | + |result| { |
| 162 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 163 | + assert_eq!(result.get("b"), None); |
| 164 | + }); |
| 165 | +} |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | +#[test] |
| 171 | +fn anonymous_inline_fragment_include_true() { |
| 172 | + run_query( |
| 173 | + "{ a, ... @include(if: true) { b } }", |
| 174 | + |result| { |
| 175 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 176 | + assert_eq!(result.get("b"), Some(&Value::string("b"))); |
| 177 | + }); |
| 178 | +} |
| 179 | + |
| 180 | +#[test] |
| 181 | +fn anonymous_inline_fragment_include_false() { |
| 182 | + run_query( |
| 183 | + "{ a, ... @include(if: false) { b } }", |
| 184 | + |result| { |
| 185 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 186 | + assert_eq!(result.get("b"), None); |
| 187 | + }); |
| 188 | +} |
| 189 | + |
| 190 | +#[test] |
| 191 | +fn anonymous_inline_fragment_skip_false() { |
| 192 | + run_query( |
| 193 | + "{ a, ... @skip(if: false) { b } }", |
| 194 | + |result| { |
| 195 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 196 | + assert_eq!(result.get("b"), Some(&Value::string("b"))); |
| 197 | + }); |
| 198 | +} |
| 199 | + |
| 200 | +#[test] |
| 201 | +fn anonymous_inline_fragment_skip_true() { |
| 202 | + run_query( |
| 203 | + "{ a, ... @skip(if: true) { b } }", |
| 204 | + |result| { |
| 205 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 206 | + assert_eq!(result.get("b"), None); |
| 207 | + }); |
| 208 | +} |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +#[test] |
| 214 | +fn scalar_include_true_skip_true() { |
| 215 | + run_query( |
| 216 | + "{ a, b @include(if: true) @skip(if: true) }", |
| 217 | + |result| { |
| 218 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 219 | + assert_eq!(result.get("b"), None); |
| 220 | + }); |
| 221 | +} |
| 222 | + |
| 223 | +#[test] |
| 224 | +fn scalar_include_true_skip_false() { |
| 225 | + run_query( |
| 226 | + "{ a, b @include(if: true) @skip(if: false) }", |
| 227 | + |result| { |
| 228 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 229 | + assert_eq!(result.get("b"), Some(&Value::string("b"))); |
| 230 | + }); |
| 231 | +} |
| 232 | + |
| 233 | +#[test] |
| 234 | +fn scalar_include_false_skip_true() { |
| 235 | + run_query( |
| 236 | + "{ a, b @include(if: false) @skip(if: true) }", |
| 237 | + |result| { |
| 238 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 239 | + assert_eq!(result.get("b"), None); |
| 240 | + }); |
| 241 | +} |
| 242 | + |
| 243 | +#[test] |
| 244 | +fn scalar_include_false_skip_false() { |
| 245 | + run_query( |
| 246 | + "{ a, b @include(if: false) @skip(if: false) }", |
| 247 | + |result| { |
| 248 | + assert_eq!(result.get("a"), Some(&Value::string("a"))); |
| 249 | + assert_eq!(result.get("b"), None); |
| 250 | + }); |
| 251 | +} |
0 commit comments