2018-01-29 22:29:16 +00:00
|
|
|
// Copyright 2018 Mozilla
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
|
|
// this file except in compliance with the License. You may obtain a copy of the
|
|
|
|
// License at http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed
|
|
|
|
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
|
|
// specific language governing permissions and limitations under the License.
|
|
|
|
|
|
|
|
// This is required to prevent warnings about unused functions in this file just
|
|
|
|
// because it's unused in a single file (tests that don't use every function in
|
|
|
|
// this module will get warnings otherwise).
|
|
|
|
#![allow(dead_code)]
|
|
|
|
|
2018-08-08 17:35:06 +00:00
|
|
|
use core_traits::{
|
|
|
|
Entid,
|
2018-08-08 17:36:41 +00:00
|
|
|
ValueType,
|
2018-08-08 17:35:06 +00:00
|
|
|
};
|
|
|
|
|
2018-01-29 22:29:16 +00:00
|
|
|
use mentat_core::{
|
|
|
|
Attribute,
|
|
|
|
Schema,
|
|
|
|
};
|
|
|
|
|
2018-08-08 18:23:07 +00:00
|
|
|
use edn::query::{
|
2018-05-11 16:52:17 +00:00
|
|
|
Keyword,
|
2018-01-29 22:29:16 +00:00
|
|
|
};
|
|
|
|
|
2018-08-08 18:56:27 +00:00
|
|
|
use query_algebrizer_traits::errors::{
|
2018-06-27 18:53:35 +00:00
|
|
|
AlgebrizerError,
|
2018-08-08 18:56:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use mentat_query_algebrizer::{
|
2018-01-29 22:29:16 +00:00
|
|
|
ConjoiningClauses,
|
2018-02-14 00:51:21 +00:00
|
|
|
Known,
|
2018-01-29 22:29:16 +00:00
|
|
|
QueryInputs,
|
2018-02-14 00:51:21 +00:00
|
|
|
algebrize,
|
|
|
|
algebrize_with_inputs,
|
2018-05-31 21:10:49 +00:00
|
|
|
parse_find_string,
|
2018-01-29 22:29:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Common utility functions used in multiple test files.
|
|
|
|
|
|
|
|
// These are helpers that tests use to build Schema instances.
|
2018-05-11 16:52:17 +00:00
|
|
|
pub fn associate_ident(schema: &mut Schema, i: Keyword, e: Entid) {
|
2018-01-29 22:29:16 +00:00
|
|
|
schema.entid_map.insert(e, i.clone());
|
|
|
|
schema.ident_map.insert(i.clone(), e);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_attribute(schema: &mut Schema, e: Entid, a: Attribute) {
|
|
|
|
schema.attribute_map.insert(e, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SchemaBuilder {
|
|
|
|
pub schema: Schema,
|
|
|
|
pub counter: Entid,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SchemaBuilder {
|
|
|
|
pub fn new() -> SchemaBuilder {
|
|
|
|
SchemaBuilder {
|
|
|
|
schema: Schema::default(),
|
|
|
|
counter: 65
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 16:52:17 +00:00
|
|
|
pub fn define_attr(mut self, kw: Keyword, attr: Attribute) -> Self {
|
2018-01-29 22:29:16 +00:00
|
|
|
associate_ident(&mut self.schema, kw, self.counter);
|
|
|
|
add_attribute(&mut self.schema, self.counter, attr);
|
|
|
|
self.counter += 1;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn define_simple_attr<T>(self,
|
|
|
|
keyword_ns: T,
|
|
|
|
keyword_name: T,
|
|
|
|
value_type: ValueType,
|
|
|
|
multival: bool) -> Self
|
2018-05-11 16:52:17 +00:00
|
|
|
where T: AsRef<str>
|
2018-01-29 22:29:16 +00:00
|
|
|
{
|
2018-05-11 16:52:17 +00:00
|
|
|
self.define_attr(Keyword::namespaced(keyword_ns, keyword_name), Attribute {
|
2018-01-29 22:29:16 +00:00
|
|
|
value_type,
|
|
|
|
multival,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-27 18:53:35 +00:00
|
|
|
pub fn bails(known: Known, input: &str) -> AlgebrizerError {
|
2018-01-29 22:29:16 +00:00
|
|
|
let parsed = parse_find_string(input).expect("query input to have parsed");
|
2018-02-14 00:51:21 +00:00
|
|
|
algebrize(known, parsed).expect_err("algebrize to have failed")
|
2018-01-29 22:29:16 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 18:53:35 +00:00
|
|
|
pub fn bails_with_inputs(known: Known, input: &str, inputs: QueryInputs) -> AlgebrizerError {
|
2018-01-29 22:29:16 +00:00
|
|
|
let parsed = parse_find_string(input).expect("query input to have parsed");
|
2018-02-14 00:51:21 +00:00
|
|
|
algebrize_with_inputs(known, parsed, 0, inputs).expect_err("algebrize to have failed")
|
2018-01-29 22:29:16 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 00:51:21 +00:00
|
|
|
pub fn alg(known: Known, input: &str) -> ConjoiningClauses {
|
2018-01-29 22:29:16 +00:00
|
|
|
let parsed = parse_find_string(input).expect("query input to have parsed");
|
2018-02-14 00:51:21 +00:00
|
|
|
algebrize(known, parsed).expect("algebrizing to have succeeded").cc
|
2018-01-29 22:29:16 +00:00
|
|
|
}
|
2018-05-09 15:24:12 +00:00
|
|
|
|
|
|
|
pub fn alg_with_inputs(known: Known, input: &str, inputs: QueryInputs) -> ConjoiningClauses {
|
|
|
|
let parsed = parse_find_string(input).expect("query input to have parsed");
|
|
|
|
algebrize_with_inputs(known, parsed, 0, inputs).expect("algebrizing to have succeeded").cc
|
|
|
|
}
|