Unverified Commit bc39d1a1 authored by Vasili Novikov's avatar Vasili Novikov
Browse files

Implement property search tests

parent a5f1fee9
Showing with 132 additions and 23 deletions
+132 -23
......@@ -464,14 +464,15 @@ fn add_sql_param(query: &mut String, column: &str, operation: &Comparison) {
}
#[cfg(test)]
mod tests {
pub mod tests {
use super::super::database_migrate_refinery;
use super::super::error::Result;
use super::*;
use chrono::Utc;
use rusqlite::Connection;
use std::ops::Not;
fn new_conn() -> Connection {
pub fn new_conn() -> Connection {
let mut conn = rusqlite::Connection::open_in_memory().unwrap();
database_migrate_refinery::embedded::migrations::runner()
.run(&mut conn)
......@@ -479,7 +480,7 @@ mod tests {
conn
}
fn random_id() -> String {
pub fn random_id() -> String {
rand::random::<i64>().to_string()
}
......@@ -722,4 +723,38 @@ mod tests {
);
Ok(())
}
#[test]
fn test_property_checks() -> Result<()> {
let mut conn = new_conn();
let tx = conn.transaction()?;
let date = Utc::now().timestamp_millis();
// let item = insert_item_base(&tx, "one", "Person", date, date, date, false)?;
let item: Rowid = insert_item_base(
&tx,
&random_id(),
"ItemPropertySchema",
date,
date,
date,
false,
)?;
insert_string(&tx, item, "itemType", "Person")?;
insert_string(&tx, item, "propertyName", "age")?;
insert_string(&tx, item, "valueType", "integer")?;
assert!(check_string_exists(&tx, item, "itemType", "Person")?);
assert!(check_string_exists(&tx, item, "itemType", "Person2")?.not());
// The property should have a String value,
// so normally this would be a schema check error.
// However, database_api is is the lowest layer and it's unaware of schemas.
// The result is a successful check with the result "no, such integer value is not found")
assert!(check_integer_exists(&tx, item, "itemType", 1)?.not());
assert!(check_real_exists(&tx, item, "itemType", 1.)?.not());
Ok(())
}
}
......@@ -276,3 +276,95 @@ fn add_item_base_properties(props: &mut Map<String, Value>, item: ItemBase) {
);
props.insert("deleted".to_string(), item.deleted.into());
}
#[cfg(test)]
mod tests {
use super::*;
use crate::database_api::tests::new_conn;
use crate::database_api::tests::random_id;
use serde_json::json;
use chrono::Utc;
use std::ops::Not;
#[test]
fn test_one_property() -> Result<()> {
let mut conn = new_conn();
let tx = conn.transaction()?;
let mut schema = database_api::get_schema(&tx).unwrap();
schema.property_types.insert("age".to_string(), SchemaPropertyType::Integer);
schema.property_types.insert("strength".to_string(), SchemaPropertyType::Real);
schema.property_types.insert("myDescription".to_string(), SchemaPropertyType::Text);
let date = Utc::now().timestamp_millis();
let item: Rowid = database_api::insert_item_base(&tx, &random_id(), "Person", date, date, date, false)?;
assert!(check_item_has_property(&tx, &schema, item, "age", &json!(20))?.not());
insert_property(&tx, &schema, item, "age", &json!(20))?;
assert!(check_item_has_property(&tx, &schema, item, "age", &json!(20))?);
assert!(check_item_has_property(&tx, &schema, item, "age", &json!(99))?.not());
// Checking non-existing property should yield an error, not a successful "no" response
assert!(check_item_has_property(&tx, &schema, item, "antiAge", &json!(99)).is_err());
insert_property(&tx, &schema, item, "strength", &json!(13.5))?;
assert!(check_item_has_property(&tx, &schema, item, "strength", &json!(13.5))?);
insert_property(&tx, &schema, item, "myDescription", &json!("Wow such person"))?;
assert!(check_item_has_property(&tx, &schema, item, "myDescription", &json!("Wow such person"))?);
Ok(())
}
#[test]
fn test_all_properties() -> Result<()> {
let mut conn = new_conn();
let tx = conn.transaction()?;
let mut schema = database_api::get_schema(&tx).unwrap();
schema.property_types.insert("age".to_string(), SchemaPropertyType::Integer);
schema.property_types.insert("strength".to_string(), SchemaPropertyType::Real);
schema.property_types.insert("myDescription".to_string(), SchemaPropertyType::Text);
let date = Utc::now().timestamp_millis();
let item: Rowid = database_api::insert_item_base(&tx, &random_id(), "Person", date, date, date, false)?;
insert_property(&tx, &schema, item, "age", &json!(20))?;
insert_property(&tx, &schema, item, "strength", &json!(13.5))?;
insert_property(&tx, &schema, item, "myDescription", &json!("Wow such person"))?;
{
let mut props = HashMap::new();
props.insert("age".to_string(), json!(20));
props.insert("strength".to_string(), json!(13.5));
props.insert("myDescription".to_string(), json!("Wow such person"));
assert!(check_item_has_all_properties(&tx, &schema, item, &props)?);
}
{
let mut props = HashMap::new();
props.insert("age".to_string(), json!(20));
assert!(check_item_has_all_properties(&tx, &schema, item, &props)?);
}
{
let mut props = HashMap::new();
props.insert("age".to_string(), json!(99999999));
props.insert("strength".to_string(), json!(13.5));
assert!(check_item_has_all_properties(&tx, &schema, item, &props)?.not());
}
{
let mut props = HashMap::new();
props.insert("antiAge".to_string(), json!(-200000000));
assert!(check_item_has_all_properties(&tx, &schema, item, &props).is_err());
}
{
let props = HashMap::new();
assert!(check_item_has_all_properties(&tx, &schema, item, &props)?);
}
Ok(())
}
}
......@@ -191,11 +191,10 @@ mod tests {
use super::upload_file;
use crate::command_line_interface;
use crate::database_api;
use crate::database_migrate_refinery;
use crate::database_api::tests::new_conn;
use crate::error::Result;
use crate::internal_api;
use crate::plugin_auth_crypto::DatabaseKey;
use rusqlite::Connection;
use serde_json::json;
#[test]
......@@ -224,12 +223,4 @@ mod tests {
std::fs::remove_dir_all(owner_dir).ok();
Ok(())
}
fn new_conn() -> Connection {
let mut conn = rusqlite::Connection::open_in_memory().unwrap();
database_migrate_refinery::embedded::migrations::runner()
.run(&mut conn)
.expect("Failed to run refinery migrations");
conn
}
}
......@@ -328,25 +328,16 @@ mod tests {
use crate::api_model::CreateItem;
use crate::command_line_interface;
use crate::database_api;
use crate::database_migrate_refinery;
use crate::error::Result;
use crate::internal_api;
use crate::internal_api::*;
use crate::plugin_auth_crypto::DatabaseKey;
use crate::schema::Schema;
use rusqlite::Connection;
use crate::database_api::tests::new_conn;
use serde_json::json;
use std::collections::HashMap;
use warp::hyper::StatusCode;
fn new_conn() -> Connection {
let mut conn = rusqlite::Connection::open_in_memory().unwrap();
database_migrate_refinery::embedded::migrations::runner()
.run(&mut conn)
.expect("Failed to run refinery migrations");
conn
}
#[test]
fn test_schema_checking() -> Result<()> {
let mut conn = new_conn();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment