use std::{collections::HashMap, mem::MaybeUninit, str::FromStr, sync::Once};
use ascii::AsciiString;
use orchestra_toolkit::*;
use time::OffsetDateTime;
fn setup() -> (Session, Token) {
static mut SINGLETON: MaybeUninit<Session> = MaybeUninit::uninit();
static ONCE: Once = Once::new();
dotenv::dotenv().ok();
let auth_str = std::env::var("AVESTERRA_AUTH").expect("Getting `AVESTERRA_AUTH` from env");
let auth = Token::from_str(&auth_str).expect("Parsing `AVESTERRA_AUTH` found in env");
unsafe {
ONCE.call_once(|| {
let config = SessionConfig::default();
SINGLETON.write(Session::initialize(config).unwrap());
});
return (SINGLETON.assume_init_ref().clone(), auth);
}
}
#[test]
fn retrieve_entity() {
let (session, auth) = setup();
let e = session
.create_general(String255::NULL, auth)
.call()
.unwrap();
session
.set_property(
e,
String255::unchecked("My property key"),
String255::unchecked("My property name"),
Value::Text("My property value".to_owned()),
auth,
)
.with_deferred(true)
.call()
.unwrap();
session
.set_fact(e, Attribute::Id, Value::Integer(42), auth)
.with_deferred(true)
.call()
.unwrap();
session
.set_facet(
e,
Attribute::Date,
String255::unchecked("The facet name"),
Value::Time(OffsetDateTime::from_unix_timestamp(1_546_300_800).unwrap()),
auth,
)
.with_deferred(true)
.call()
.unwrap();
session
.set_factor(
e,
Attribute::City,
String255::unchecked("New York City"),
String255::unchecked("Population"),
Value::Integer(8_623_000),
auth,
)
.with_deferred(true)
.call()
.unwrap();
session
.set_feature(
e,
Attribute::Sku,
String255::unchecked("The feature key"),
String255::unchecked("The feature name"),
Value::String(AsciiString::from_str("The feature value").unwrap()),
auth,
)
.with_deferred(true)
.call()
.unwrap();
session
.set_field(
e,
Attribute::Table,
String255::unchecked("The only field name"),
Value::Text("The only field default value".to_owned()),
auth,
)
.with_deferred(true)
.call()
.unwrap();
session
.set_frame(
e,
Attribute::Table,
String255::unchecked("The frame key"),
String255::unchecked("The only field name"),
Value::Integer(98),
auth,
)
.with_deferred(true)
.call()
.unwrap();
let model = session.retrieve_entity(e, auth).call().unwrap();
assert_eq!(
model,
AvialModel {
name: String255::NULL,
key: String255::NULL,
data: 0,
attributes: vec![],
properties: vec![ModelProperty {
key: String255::unchecked("My property key"),
name: String255::unchecked("My property name"),
value: Value::Text("My property value".to_owned()),
annotations: HashMap::new(),
},],
facts: vec![
ModelFact {
attribute: Attribute::Id,
value: Value::Integer(42),
..Default::default()
},
ModelFact {
attribute: Attribute::Date,
facets: vec![Facet {
name: String255::unchecked("The facet name"),
value: Value::Time(
OffsetDateTime::from_unix_timestamp(1_546_300_800).unwrap()
),
factors: vec![],
}],
..Default::default()
},
ModelFact {
attribute: Attribute::City,
facets: vec![Facet {
name: String255::unchecked("New York City"),
factors: vec![ModelFactor {
key: String255::unchecked("Population"),
value: Value::Integer(8_623_000),
},],
..Default::default()
}],
..Default::default()
},
ModelFact {
attribute: Attribute::Sku,
features: vec![ModelFeature {
key: String255::unchecked("The feature key"),
name: String255::unchecked("The feature name"),
value: Value::String(AsciiString::from_str("The feature value").unwrap()),
}],
..Default::default()
},
ModelFact {
attribute: Attribute::Table,
fields: vec![ModelField {
name: String255::unchecked("The only field name"),
default_value: Value::Text("The only field default value".to_owned()),
}],
frames: vec![ModelFrame {
key: String255::unchecked("The frame key"),
values: vec![Value::Integer(98)],
}],
..Default::default()
}
],
}
)
}