2017-02-08 22:04:32 +00:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
2018-08-08 17:36:41 +00:00
|
|
|
extern crate core_traits;
|
2020-01-14 15:46:21 +00:00
|
|
|
extern crate edn;
|
2017-02-08 22:04:32 +00:00
|
|
|
extern crate mentat_db;
|
|
|
|
extern crate ordered_float;
|
|
|
|
extern crate rusqlite;
|
|
|
|
|
|
|
|
use ordered_float::OrderedFloat;
|
Use Rc for TypedValue, Variable, and query Ident keywords. (#395) r=nalexander
Part 1, core: use Rc for String and Keyword.
Part 2, query: use Rc for Variable.
Part 3, sql: use Rc for args in SQLiteQueryBuilder.
Part 4, query-algebrizer: use Rc.
Part 5, db: use Rc.
Part 6, query-parser: use Rc.
Part 7, query-projector: use Rc.
Part 8, query-translator: use Rc.
Part 9, top level: use Rc.
Part 10: intern Ident and IdentOrKeyword.
2017-03-29 20:18:17 +00:00
|
|
|
|
2017-02-08 22:04:32 +00:00
|
|
|
use edn::symbols;
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use core_traits::{TypedValue, ValueType};
|
Use Rc for TypedValue, Variable, and query Ident keywords. (#395) r=nalexander
Part 1, core: use Rc for String and Keyword.
Part 2, query: use Rc for Variable.
Part 3, sql: use Rc for args in SQLiteQueryBuilder.
Part 4, query-algebrizer: use Rc.
Part 5, db: use Rc.
Part 6, query-parser: use Rc.
Part 7, query-projector: use Rc.
Part 8, query-translator: use Rc.
Part 9, top level: use Rc.
Part 10: intern Ident and IdentOrKeyword.
2017-03-29 20:18:17 +00:00
|
|
|
use mentat_db::db::TypedSQLValue;
|
|
|
|
|
2017-02-08 22:04:32 +00:00
|
|
|
// It's not possible to test to_sql_value_pair since rusqlite::ToSqlOutput doesn't implement
|
|
|
|
// PartialEq.
|
|
|
|
#[test]
|
|
|
|
fn test_from_sql_value_pair() {
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
TypedValue::from_sql_value_pair(rusqlite::types::Value::Integer(1234), 0).unwrap(),
|
|
|
|
TypedValue::Ref(1234)
|
|
|
|
);
|
2017-02-08 22:04:32 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
TypedValue::from_sql_value_pair(rusqlite::types::Value::Integer(0), 1).unwrap(),
|
|
|
|
TypedValue::Boolean(false)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
TypedValue::from_sql_value_pair(rusqlite::types::Value::Integer(1), 1).unwrap(),
|
|
|
|
TypedValue::Boolean(true)
|
|
|
|
);
|
2017-02-08 22:04:32 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
TypedValue::from_sql_value_pair(rusqlite::types::Value::Integer(0), 5).unwrap(),
|
|
|
|
TypedValue::Long(0)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
TypedValue::from_sql_value_pair(rusqlite::types::Value::Integer(1234), 5).unwrap(),
|
|
|
|
TypedValue::Long(1234)
|
|
|
|
);
|
2017-02-08 22:04:32 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
TypedValue::from_sql_value_pair(rusqlite::types::Value::Real(0.0), 5).unwrap(),
|
|
|
|
TypedValue::Double(OrderedFloat(0.0))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
TypedValue::from_sql_value_pair(rusqlite::types::Value::Real(0.5), 5).unwrap(),
|
|
|
|
TypedValue::Double(OrderedFloat(0.5))
|
|
|
|
);
|
2017-02-08 22:04:32 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
TypedValue::from_sql_value_pair(rusqlite::types::Value::Text(":db/keyword".into()), 10)
|
|
|
|
.unwrap(),
|
|
|
|
TypedValue::typed_string(":db/keyword")
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
TypedValue::from_sql_value_pair(rusqlite::types::Value::Text(":db/keyword".into()), 13)
|
|
|
|
.unwrap(),
|
|
|
|
TypedValue::typed_ns_keyword("db", "keyword")
|
|
|
|
);
|
2021-08-23 21:25:10 +00:00
|
|
|
assert_eq!(
|
2021-08-24 01:31:50 +00:00
|
|
|
TypedValue::from_sql_value_pair(rusqlite::types::Value::Blob(vec![1, 2, 3, 42]), 15)
|
2021-08-23 21:25:10 +00:00
|
|
|
.unwrap(),
|
2021-08-24 01:31:50 +00:00
|
|
|
TypedValue::Bytes((vec![1, 2, 3, 42]).into())
|
2021-08-23 21:25:10 +00:00
|
|
|
);
|
2017-02-08 22:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_edn_value_pair() {
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
TypedValue::Ref(1234).to_edn_value_pair(),
|
|
|
|
(edn::Value::Integer(1234), ValueType::Ref)
|
|
|
|
);
|
2017-02-08 22:04:32 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
TypedValue::Boolean(false).to_edn_value_pair(),
|
|
|
|
(edn::Value::Boolean(false), ValueType::Boolean)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
TypedValue::Boolean(true).to_edn_value_pair(),
|
|
|
|
(edn::Value::Boolean(true), ValueType::Boolean)
|
|
|
|
);
|
2017-02-08 22:04:32 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
TypedValue::Long(0).to_edn_value_pair(),
|
|
|
|
(edn::Value::Integer(0), ValueType::Long)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
TypedValue::Long(1234).to_edn_value_pair(),
|
|
|
|
(edn::Value::Integer(1234), ValueType::Long)
|
|
|
|
);
|
2017-02-08 22:04:32 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
TypedValue::Double(OrderedFloat(0.0)).to_edn_value_pair(),
|
|
|
|
(edn::Value::Float(OrderedFloat(0.0)), ValueType::Double)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
TypedValue::Double(OrderedFloat(0.5)).to_edn_value_pair(),
|
|
|
|
(edn::Value::Float(OrderedFloat(0.5)), ValueType::Double)
|
|
|
|
);
|
2017-02-08 22:04:32 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
TypedValue::typed_string(":db/keyword").to_edn_value_pair(),
|
|
|
|
(edn::Value::Text(":db/keyword".into()), ValueType::String)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
TypedValue::typed_ns_keyword("db", "keyword").to_edn_value_pair(),
|
|
|
|
(
|
|
|
|
edn::Value::Keyword(symbols::Keyword::namespaced("db", "keyword")),
|
|
|
|
ValueType::Keyword
|
|
|
|
)
|
|
|
|
);
|
2017-02-08 22:04:32 +00:00
|
|
|
}
|