use cucumber::{given, then, when, World as _};
use genius_core_client::{
client::{Client, Protocol},
query::QueryEntitiesReturn,
types::{entity::HSMLEntity, error::HstpError},
};
use std::env;
use tokio::runtime::Runtime;
mod prepare_tests;
#[derive(cucumber::World, Debug, Default)]
struct QueryWorld {
client: Option<Client>,
result: Option<serde_json::value::Value>,
upsert_result: Option<HSMLEntity>,
}
#[given("a genius core client")]
async fn given_fetch(w: &mut QueryWorld) {
let genius_db_url = env::var("GENIUS_DB_URL").unwrap();
let genius_db_port = env::var("GENIUS_DB_PORT").unwrap();
let token = env::var("GENIUS_JWT").unwrap();
let rt = Runtime::new().unwrap();
let client_result = rt.block_on(Client::new_with_oauth2_token(
Protocol::HTTPS,
genius_db_url,
genius_db_port,
token,
None,
));
w.client = Some(client_result.unwrap());
}
#[when("I issue a fetch Query")]
async fn when_fetch(w: &mut QueryWorld) {
let query = "#person NEAR steve IN name_space TOPK 1";
let client = w.client.as_mut().unwrap();
let result: Result<serde_json::value::Value, HstpError> = client.query(query).await;
w.result = result.ok();
}
#[then("I should get back a list of entities")]
async fn then_fetch(w: &mut QueryWorld) {
let result = w.result.as_ref().unwrap();
let query_entities_return: Result<QueryEntitiesReturn, _> =
serde_json::from_value(result.clone());
match query_entities_return {
Ok(result) => assert!(result.entities.len() > 0),
Err(_) => panic!("Failed to parse result into QueryEntitiesReturn"),
}
}
#[given("another genius core client")]
async fn given_upsert(w: &mut QueryWorld) {
let genius_db_url = env::var("GENIUS_DB_URL").unwrap();
let genius_db_port = env::var("GENIUS_DB_PORT").unwrap();
let token = env::var("GENIUS_JWT").unwrap();
let rt = Runtime::new().unwrap();
let client_result = rt.block_on(Client::new_with_oauth2_token(
Protocol::HTTPS,
genius_db_url,
genius_db_port,
token,
None,
));
w.client = Some(client_result.unwrap());
}
#[when("I issue an HSQL Upsert Query")]
async fn when_upsert(w: &mut QueryWorld) {
let query = "%upsert
[
{
\"schema\": [\"person\", \"entity\"],
\"swid\": \"swid:entity:steven000000000000000\",
\"name\": \"Steven\",
\"description\": \"steven\",
\"firstName\": \"steven\",
\"lastName\": \"\",
\"email\": \"steven@steve.com\",
\"phone\": \"555-556-2014 x9858\"
}
] on collide FAIL";
let client = w.client.as_mut().unwrap();
let result: Result<serde_json::value::Value, HstpError> = client.query(query).await;
w.result = result.ok();
}
#[then("I should receive an upserted entity")]
async fn then_upsert(w: &mut QueryWorld) {
let result = w.upsert_result.as_ref().unwrap();
let query_entities_return = &result;
assert!(query_entities_return.swid == "swid:entity:steven000000000000000");
}
#[tokio::main]
async fn main() {
QueryWorld::cucumber()
.run("tests/features/query.feature")
.await;
}