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)]
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use core_traits::{Attribute, Entid, ValueType};
|
2018-08-08 17:35:06 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use mentat_core::Schema;
|
2018-01-29 22:29:16 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use edn::query::Keyword;
|
2018-01-29 22:29:16 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use query_algebrizer_traits::errors::AlgebrizerError;
|
2018-08-08 18:56:27 +00:00
|
|
|
|
|
|
|
use mentat_query_algebrizer::{
|
2020-01-14 15:46:21 +00:00
|
|
|
algebrize, algebrize_with_inputs, parse_find_string, ConjoiningClauses, Known, QueryInputs,
|
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());
|
2020-08-06 03:03:58 +00:00
|
|
|
schema.ident_map.insert(i, e);
|
2018-01-29 22:29:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
2020-01-14 15:46:21 +00:00
|
|
|
counter: 65,
|
2018-01-29 22:29:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
pub fn define_simple_attr<T>(
|
|
|
|
self,
|
|
|
|
keyword_ns: T,
|
|
|
|
keyword_name: T,
|
|
|
|
value_type: ValueType,
|
|
|
|
multival: bool,
|
|
|
|
) -> Self
|
|
|
|
where
|
|
|
|
T: AsRef<str>,
|
2018-01-29 22:29:16 +00:00
|
|
|
{
|
2020-01-14 15:46:21 +00:00
|
|
|
self.define_attr(
|
|
|
|
Keyword::namespaced(keyword_ns, keyword_name),
|
|
|
|
Attribute {
|
|
|
|
value_type,
|
|
|
|
multival,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
2018-01-29 22:29:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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");
|
2020-01-14 15:46: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");
|
2020-01-14 15:46:21 +00:00
|
|
|
algebrize_with_inputs(known, parsed, 0, inputs)
|
|
|
|
.expect("algebrizing to have succeeded")
|
|
|
|
.cc
|
2018-05-09 15:24:12 +00:00
|
|
|
}
|