Don’t treat NONE and NULL as the same

This commit is contained in:
Tobie Morgan Hitchcock 2023-01-06 10:26:25 +00:00
parent 953a2008e2
commit 372cd65969
2 changed files with 67 additions and 4 deletions

View file

@ -602,11 +602,11 @@ impl Value {
// ----------------------------------- // -----------------------------------
pub fn is_none(&self) -> bool { pub fn is_none(&self) -> bool {
matches!(self, Value::None | Value::Null) matches!(self, Value::None)
} }
pub fn is_null(&self) -> bool { pub fn is_null(&self) -> bool {
matches!(self, Value::None | Value::Null) matches!(self, Value::Null)
} }
pub fn is_some(&self) -> bool { pub fn is_some(&self) -> bool {
@ -1002,6 +1002,7 @@ impl Value {
true => self, true => self,
_ => Value::None, _ => Value::None,
}, },
Kind::Option(v) => todo!(),
} }
} }
@ -1530,14 +1531,14 @@ mod tests {
#[test] #[test]
fn check_none() { fn check_none() {
assert_eq!(true, Value::None.is_none()); assert_eq!(true, Value::None.is_none());
assert_eq!(true, Value::Null.is_none()); assert_eq!(false, Value::Null.is_none());
assert_eq!(false, Value::from(1).is_none()); assert_eq!(false, Value::from(1).is_none());
} }
#[test] #[test]
fn check_null() { fn check_null() {
assert_eq!(true, Value::None.is_null());
assert_eq!(true, Value::Null.is_null()); assert_eq!(true, Value::Null.is_null());
assert_eq!(false, Value::None.is_null());
assert_eq!(false, Value::from(1).is_null()); assert_eq!(false, Value::from(1).is_null());
} }

62
lib/tests/compare.rs Normal file
View file

@ -0,0 +1,62 @@
mod parse;
use surrealdb::dbs::Session;
use surrealdb::err::Error;
use surrealdb::kvs::Datastore;
use surrealdb::sql::Value;
#[tokio::test]
async fn compare_empty() -> Result<(), Error> {
let sql = r#"
RETURN NONE = NONE;
RETURN NULL = NULL;
RETURN NONE = NULL;
RETURN [] = [];
RETURN {} = {};
RETURN [] = {};
RETURN 0 = 0;
RETURN 0 = 0.0;
RETURN 0 = 0.1;
"#;
let dbs = Datastore::new("memory").await?;
let ses = Session::for_kv().with_ns("test").with_db("test");
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
assert_eq!(res.len(), 9);
//
let tmp = res.remove(0).result?;
let val = Value::True;
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::True;
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::False;
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::True;
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::True;
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::False;
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::True;
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::True;
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::False;
assert_eq!(tmp, val);
//
Ok(())
}