-
Notifications
You must be signed in to change notification settings - Fork 243
/
lib.rs
116 lines (96 loc) 路 3.16 KB
/
lib.rs
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
#[macro_use]
extern crate lazy_static;
pub use crate::builder::build_query_plan;
use crate::model::QueryPlan;
use graphql_parser::{parse_query, parse_schema, schema, ParseError};
#[macro_use]
mod macros;
mod builder;
mod consts;
mod context;
mod federation;
mod groups;
mod helpers;
pub mod model;
mod visitors;
#[derive(Debug)]
pub enum QueryPlanError {
FailedParsingSchema(ParseError),
FailedParsingQuery(ParseError),
InvalidQuery(&'static str),
}
pub type Result<T> = std::result::Result<T, QueryPlanError>;
pub struct QueryPlanner<'s> {
schema: schema::Document<'s>,
}
impl<'s> QueryPlanner<'s> {
pub fn new(schema: &'s str) -> QueryPlanner<'s> {
let schema = parse_schema(schema).expect("failed parsing schema");
QueryPlanner { schema }
}
pub fn plan(&self, query: &str) -> Result<QueryPlan> {
let query = parse_query(query).expect("failed parsing query");
build_query_plan(&self.schema, &query)
}
}
#[cfg(test)]
mod tests {
use std::fs::{read_dir, read_to_string};
use gherkin_rust::Feature;
use gherkin_rust::StepType;
use crate::model::QueryPlan;
use crate::QueryPlanner;
use std::path::PathBuf;
macro_rules! get_step {
($scenario:ident, $typ:pat) => {
$scenario
.steps
.iter()
.find(|s| matches!(s.ty, $typ))
.unwrap()
.docstring
.as_ref()
.unwrap()
};
}
static SCHEMA: &str = include_str!("../tests/features/csdl.graphql");
#[test]
fn test_all_feature_files() {
let planner = QueryPlanner::new(SCHEMA);
// If debugging with IJ, use `read_dir("query-planner/tests/features")`
let feature_paths = read_dir(PathBuf::from("tests").join("features"))
.unwrap()
.map(|res| res.map(|e| e.path()).unwrap())
.filter(|e| {
if let Some(d) = e.extension() {
d == "feature"
} else {
false
}
});
for path in feature_paths {
let feature = read_to_string(&path).unwrap();
let feature = if cfg!(windows) {
println!("replacing \\n with \\r\\n in {:?}", &path);
feature.replace("\n", "\r\n")
} else {
feature
};
let feature = match Feature::parse(feature) {
Result::Ok(feature) => feature,
Result::Err(e) => panic!("Unparseable .feature file {:?} -- {}", &path, e),
};
let scenarios = feature
.scenarios
.iter()
.filter(|s| !s.steps.iter().any(|s| matches!(s.ty, StepType::When)));
for scenario in scenarios {
let query: &str = get_step!(scenario, StepType::Given);
let expected_str: &str = get_step!(scenario, StepType::Then);
let expected: QueryPlan = serde_json::from_str(&expected_str).unwrap();
let result = planner.plan(query).unwrap();
assert_eq!(result, expected);
}
}
}
}