Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ app.log

# Binaries / Temp
test_str_cmp
.code-graph/
38 changes: 19 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions teaql-core/src/entity_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ mod tests {
}

impl TeaqlEntity for DummyEntity {
const ENTITY_NAME: &'static str = "Dummy";
fn entity_descriptor() -> EntityDescriptor {
EntityDescriptor::new("Dummy")
.property(PropertyDescriptor::new("id", DataType::I64).id())
Expand Down
4 changes: 3 additions & 1 deletion teaql-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,10 @@ mod tests {
}

impl TeaqlEntity for SafeExpressionEntity {
const ENTITY_NAME: &'static str = "SafeExpression";
fn entity_descriptor() -> EntityDescriptor {
EntityDescriptor::new("SafeExpressionEntity")
EntityDescriptor::new("SafeExpression")
.property(PropertyDescriptor::new("id", DataType::I64).id())
}
}

Expand Down
30 changes: 30 additions & 0 deletions teaql-core/src/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,33 @@ impl RecoverCommand {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_insert_command_builder() {
let cmd = InsertCommand::new("User").value("name", "Alice");
assert_eq!(cmd.entity, "User");
assert_eq!(cmd.values.get("name").unwrap(), &Value::Text("Alice".to_owned()));
}

#[test]
fn test_update_command_builder() {
let cmd = UpdateCommand::new("User", 1).expected_version(5).value("name", "Bob");
assert_eq!(cmd.entity, "User");
assert_eq!(cmd.id, Value::I64(1));
assert_eq!(cmd.expected_version, Some(5));
assert_eq!(cmd.values.get("name").unwrap(), &Value::Text("Bob".to_owned()));
}

#[test]
fn test_delete_command_builder() {
let cmd = DeleteCommand::new("User", 1).expected_version(5).hard_delete();
assert_eq!(cmd.entity, "User");
assert_eq!(cmd.id, Value::I64(1));
assert_eq!(cmd.expected_version, Some(5));
assert_eq!(cmd.soft_delete, false);
}
}
18 changes: 18 additions & 0 deletions teaql-core/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,21 @@ impl TraceNode {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_trace_node_new() {
let node = TraceNode::new("User", Some(123), "created user");
assert_eq!(node.entity_type, "User");
assert_eq!(node.entity_id, Some(123));
assert_eq!(node.comment, "created user");

let node2 = TraceNode::new("System", None, "system startup");
assert_eq!(node2.entity_type, "System");
assert_eq!(node2.entity_id, None);
assert_eq!(node2.comment, "system startup");
}
}
19 changes: 15 additions & 4 deletions teaql-data-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,21 @@ mod tests {
let insert_empty = InsertCommand {
entity: "User".to_string(),
values: Record::new(),
trace_chain: vec![],
trace_chain: Vec::new(),
};
let req_empty = MutationRequest::Insert(insert_empty);
assert_eq!(req_empty.trace_chain().len(), 0);
assert_eq!(req_empty.comment(), None);
let req_insert_empty = MutationRequest::Insert(insert_empty);
assert_eq!(req_insert_empty.comment(), None);
}

#[test]
fn test_data_service_capabilities_default() {
let caps = DataServiceCapabilities::default();
assert!(!caps.query);
assert!(!caps.mutation);
assert!(!caps.transaction);
assert!(!caps.schema);
assert!(!caps.id_generation);
assert!(!caps.batch_mutation);
assert!(!caps.returning);
}
}
61 changes: 61 additions & 0 deletions teaql-macros/src/derive_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ pub fn expand_teaql_entity(input: DeriveInput) -> proc_macro2::TokenStream {
});
}

let tracks_original_version = has_root_field && id_impl.is_some() && version_impl.is_some();

let identifiable_impl_tokens = id_impl.map(|id_value| {
quote! {
impl ::teaql_core::IdentifiableEntity for #struct_name {
Expand Down Expand Up @@ -293,6 +295,20 @@ pub fn expand_teaql_entity(input: DeriveInput) -> proc_macro2::TokenStream {
Default::default()
};

let set_original_version_impl = if tracks_original_version {
quote! {
entity.root.set_original_version(
::teaql_runtime::EntityKey::new(
#entity_name,
::teaql_core::IdentifiableEntity::id_value(&entity),
),
::teaql_core::VersionedEntity::version(&entity),
);
}
} else {
Default::default()
};

let root_methods_impl = if has_root_field {
{
quote! {
Expand Down Expand Up @@ -346,6 +362,7 @@ pub fn expand_teaql_entity(input: DeriveInput) -> proc_macro2::TokenStream {
};
#set_load_state_impl
#set_original_record_impl
#set_original_version_impl
Ok(entity)
}

Expand Down Expand Up @@ -380,6 +397,50 @@ pub fn expand_teaql_entity(input: DeriveInput) -> proc_macro2::TokenStream {
}
}

#[cfg(test)]
mod tests {
use super::*;
use syn::parse_quote;

#[test]
fn loaded_versioned_entity_records_its_occ_baseline() {
let input: DeriveInput = parse_quote! {
#[teaql(entity = "Task", table = "task_data")]
struct Task {
#[teaql(id)]
id: u64,
name: String,
#[teaql(version)]
version: i64,
#[teaql(skip)]
root: teaql_runtime::EntityRoot,
}
};

let expanded = expand_teaql_entity(input).to_string();
assert!(expanded.contains("set_original_record"));
assert!(expanded.contains("set_original_version"));
assert!(expanded.contains("IdentifiableEntity :: id_value"));
assert!(expanded.contains("VersionedEntity :: version"));
}

#[test]
fn entity_without_version_does_not_record_occ_baseline() {
let input: DeriveInput = parse_quote! {
#[teaql(entity = "Event", table = "event_data")]
struct Event {
#[teaql(id)]
id: u64,
#[teaql(skip)]
root: teaql_runtime::EntityRoot,
}
};

let expanded = expand_teaql_entity(input).to_string();
assert!(!expanded.contains("set_original_version"));
}
}

pub fn expand_teaql_reverse_relations(input: DeriveInput) -> proc_macro2::TokenStream {
let struct_name = input.ident;
let fields = match input.data {
Expand Down
Loading
Loading