Add integration tests for the built-in SQL functions (#1893)

This commit is contained in:
Tobie Morgan Hitchcock 2023-04-30 19:34:26 +01:00 committed by GitHub
parent 12bf8dea6b
commit 453d368e69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 3517 additions and 18 deletions

View file

@ -178,6 +178,7 @@ pub fn synchronous(ctx: &Context<'_>, name: &str, args: Vec<Value>) -> Result<Va
"parse::url::query" => parse::url::query,
"parse::url::scheme" => parse::url::scheme,
//
"rand" => rand::rand,
"rand::bool" => rand::bool,
"rand::enum" => rand::r#enum,
"rand::float" => rand::float,
@ -189,7 +190,6 @@ pub fn synchronous(ctx: &Context<'_>, name: &str, args: Vec<Value>) -> Result<Va
"rand::uuid::v4" => rand::uuid::v4,
"rand::uuid::v7" => rand::uuid::v7,
"rand::uuid" => rand::uuid,
"rand" => rand::rand,
//
"session::db" => session::db(ctx),
"session::id" => session::id(ctx),

View file

@ -91,8 +91,8 @@ pub fn minute((val,): (Option<Datetime>,)) -> Result<Value, Error> {
pub fn month((val,): (Option<Datetime>,)) -> Result<Value, Error> {
Ok(match val {
Some(v) => v.minute().into(),
None => Datetime::default().minute().into(),
Some(v) => v.month().into(),
None => Datetime::default().month().into(),
})
}

View file

@ -6,8 +6,13 @@ pub trait Mean {
impl Mean for Vec<Number> {
fn mean(&self) -> Number {
let len = Number::from(self.len());
let sum = self.iter().sum::<Number>();
sum / len
match self.len() {
0 => Number::NAN,
_ => {
let len = Number::from(self.len());
let sum = self.iter().sum::<Number>();
sum / len
}
}
}
}

View file

@ -20,6 +20,7 @@ static SECONDS_PER_DAY: u64 = 24 * SECONDS_PER_HOUR;
static SECONDS_PER_HOUR: u64 = 60 * SECONDS_PER_MINUTE;
static SECONDS_PER_MINUTE: u64 = 60;
static NANOSECONDS_PER_MILLISECOND: u32 = 1000000;
static NANOSECONDS_PER_MICROSECOND: u32 = 1000;
pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Duration";
@ -179,6 +180,9 @@ impl fmt::Display for Duration {
// Calculate the total millseconds
let msec = nano / NANOSECONDS_PER_MILLISECOND;
let nano = nano % NANOSECONDS_PER_MILLISECOND;
// Calculate the total microseconds
let usec = nano / NANOSECONDS_PER_MICROSECOND;
let nano = nano % NANOSECONDS_PER_MICROSECOND;
// Write the different parts
if year > 0 {
write!(f, "{year}y")?;
@ -201,6 +205,9 @@ impl fmt::Display for Duration {
if msec > 0 {
write!(f, "{msec}ms")?;
}
if usec > 0 {
write!(f, "{usec}µs")?;
}
if nano > 0 {
write!(f, "{nano}ns")?;
}

View file

@ -179,6 +179,10 @@ impl Number {
// Simple number detection
// -----------------------------------
pub fn is_nan(&self) -> bool {
matches!(self, Number::Float(v) if v.is_nan())
}
pub fn is_int(&self) -> bool {
matches!(self, Number::Int(_))
}

View file

@ -872,6 +872,11 @@ impl Value {
matches!(self, Value::Number(Number::Decimal(_)))
}
/// Check if this Value is a Number but is a NAN
pub fn is_nan(&self) -> bool {
matches!(self, Value::Number(v) if v.is_nan())
}
/// Check if this Value is a Number and is an integer
pub fn is_integer(&self) -> bool {
matches!(self, Value::Number(v) if v.is_integer())
@ -1195,6 +1200,16 @@ impl Value {
match self {
// Allow any boolean value
Value::Bool(boolean) => Ok(boolean),
// Attempt to convert a string value
Value::Strand(ref v) => match v.parse::<bool>() {
// The string can be represented as a Float
Ok(v) => Ok(v),
// Ths string is not a float
_ => Err(Error::ConvertTo {
from: self,
into: "bool".into(),
}),
},
// Anything else raises an error
_ => Err(Error::ConvertTo {
from: self,

File diff suppressed because it is too large Load diff