Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

87 load schema as holons #91

Merged
merged 14 commits into from
Jun 19, 2024
Merged
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ Cargo.lock
.idea
/package-lock.json
settings.json
/tests/node_modules/
/tests/node_modules/
3 changes: 2 additions & 1 deletion crates/coordinator/dances/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ holochain_integrity_types = { workspace = true }
thiserror = { workspace = true, features = [] }
#holochain = "0.1.7"

holons_integrity = { workspace = true }
#holons_integrity = { workspace = true }
serde = { workspace = true }
derive-new = { workspace = true }



shared_types_holon = { workspace = true }
holons = {workspace = true}
descriptors = {workspace = true}

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]

Expand Down
1 change: 1 addition & 0 deletions crates/coordinator/dances/src/dance_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub enum ResponseBody {
Holons(Vec<Holon>), // will be replaced by SmartCollection once supported
// SmartCollection(SmartCollection),
Index(StagedIndex),
HolonReference(HolonReference),
}

impl From<HolonError> for ResponseStatusCode {
Expand Down
11 changes: 7 additions & 4 deletions crates/coordinator/dances/src/dancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ use holons::holon_error::HolonError;
use shared_types_holon::MapString;

use crate::dance_response::{DanceResponse, ResponseBody, ResponseStatusCode};
use crate::descriptors_dance_adapter::load_core_schema_dance;

use crate::holon_dance_adapter::{
abandon_staged_changes_dance, add_related_holons_dance, commit_dance, get_all_holons_dance,
get_holon_by_id_dance, stage_new_holon_dance, with_properties_dance,
};

use crate::staging_area::StagingArea;

/// The Dancer handles dance() requests on the uniform API and dispatches the Rust function
Expand Down Expand Up @@ -104,15 +107,15 @@ impl Dancer {
dispatch_table.insert("stage_new_holon", stage_new_holon_dance as DanceFunction);
dispatch_table.insert("commit", commit_dance as DanceFunction);
dispatch_table.insert("with_properties", with_properties_dance as DanceFunction);
dispatch_table.insert(
"add_related_holons",
add_related_holons_dance as DanceFunction,
);

dispatch_table.insert(
"abandon_staged_changes_foo",
abandon_staged_changes_dance as DanceFunction,
);

dispatch_table.insert("add_related_holons", add_related_holons_dance as DanceFunction);
dispatch_table.insert("load_core_schema", load_core_schema_dance as DanceFunction);

// Add more functions as needed

Dancer { dispatch_table }
Expand Down
79 changes: 79 additions & 0 deletions crates/coordinator/dances/src/descriptors_dance_adapter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//! This file defines the DancesAdaptors offered by the descriptors zome.
//! TODO: Move these adaptors to their own zome
//!
//! For each Dance, this file defines:
//! - a `build_` function as a helper function for creating DanceRequests for that Dance from
//! native parameters.
//!- a function that performs the dance
//!
//!
//! As a dance adaptor, this function wraps (and insulates) Dancer from native functionality
//! and insulates the native function from any dependency on Dances. In general, this means:
//! 1. Extracting any required input parameters from the DanceRequest's request_body
//! 2. Invoking the native function
//! 3. Creating a DanceResponse based on the results returned by the native function. This includes,
//! mapping any errors into an appropriate ResponseStatus and returning results in the body.


use std::borrow::Borrow;
use std::rc::Rc;

use hdk::prelude::*;
use holons::commit_manager::CommitRequestStatus::*;
use holons::commit_manager::{CommitManager, StagedIndex};
use holons::context::HolonsContext;
use holons::holon::Holon;
use holons::holon_error::HolonError;
use holons::holon_reference::HolonReference;
use descriptors::loader::load_core_schema;
use holons::relationship::RelationshipName;
use shared_types_holon::{MapString, MapInteger, PropertyMap};
use shared_types_holon::HolonId;



use crate::dance_request::{DanceRequest, DanceType,RequestBody};
use crate::dance_response::ResponseBody;
use crate::staging_area::StagingArea;

/// *DanceRequest:*
/// - dance_name: "load_core_schema"
/// - dance_type: Standalone
/// - request_body: None
///
/// *ResponseBody:*
/// - Holon -- the created Schema Holon
///
pub fn load_core_schema_dance(context: &HolonsContext, request: DanceRequest) -> Result<ResponseBody, HolonError> {
debug!("Entered load_core_schema_dance");

// Match the dance_type
match request.dance_type {
DanceType::Standalone=> {
// Call the native load_core_schema function
let result = load_core_schema(context);
match result {
Ok(commit_response)=> {
match commit_response.status {
Complete => Ok(ResponseBody::None),
Incomplete => {
Err(HolonError::CommitFailure("Incomplete commit".to_string()))
}
}

}
Err(e) => Err(e),
}
}
_ => Err(HolonError::InvalidParameter("Expected Standalone DanceType, didn't get one".to_string())),
}
}


pub fn build_load_core_schema_dance_request(
staging_area: StagingArea,
)->Result<DanceRequest, HolonError> {
let body = RequestBody::new();
Ok(DanceRequest::new(MapString("load_core_schema".to_string()), DanceType::Standalone,body, staging_area))
}

1 change: 1 addition & 0 deletions crates/coordinator/dances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod dancer;
pub mod staging_area;
pub mod holon_dance_adapter;
pub mod dance_request;
pub mod descriptors_dance_adapter;


// use hdk::prelude::*;
Expand Down
7 changes: 7 additions & 0 deletions crates/coordinator/dances/tests/dance_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use holons::holon_api::*;
use holons::holon_error::HolonError;
use dances::staging_area::StagingArea;
use shared_test::dance_fixtures::*;
use shared_test::descriptor_dance_fixtures::*;
use shared_test::test_data_types::{DancesTestCase};
use shared_test::*;
use shared_types_holon::holon_node::{HolonNode, PropertyMap, PropertyName};
Expand All @@ -51,6 +52,7 @@ use crate::shared_test::test_add_related_holon::execute_add_related_holons;
use crate::shared_test::test_commit::execute_commit;
use crate::shared_test::test_data_types::{DanceTestState, DanceTestStep};
use crate::shared_test::test_ensure_database_count::execute_ensure_database_count;
use crate::shared_test::test_load_core_schema::execute_load_new_schema;
use crate::shared_test::test_match_db_content::execute_match_db_content;
use crate::shared_test::test_stage_new_holon::execute_stage_new_holon;
use crate::shared_test::test_with_properties_command::execute_with_properties;
Expand All @@ -72,9 +74,12 @@ use crate::shared_test::test_with_properties_command::execute_with_properties;
/// set WASM_LOG to enable guest-side (i.e., zome code) tracing
///
#[rstest]

#[case::simple_undescribed_create_holon_test(simple_create_test_fixture())]
#[case::simple_add_related_holon_test(simple_add_related_holons_fixture())]
#[case::simple_abandon_staged_changes_test(simple_abandon_staged_changes_fixture())]
#[case::load_core_schema(load_core_schema_test_fixture())]

#[tokio::test(flavor = "multi_thread")]
async fn rstest_dance_tests(#[case] input: Result<DancesTestCase, HolonError>) {
// Setup
Expand Down Expand Up @@ -109,6 +114,8 @@ async fn rstest_dance_tests(#[case] input: Result<DancesTestCase, HolonError>) {
DanceTestStep::WithProperties(staged_index, properties, expected_response) => execute_with_properties(&conductor, &cell, &mut test_state, staged_index, properties, expected_response).await,
DanceTestStep::MatchSavedContent => execute_match_db_content(&conductor, &cell, &mut test_state,).await,
DanceTestStep::AbandonStagedChanges(staged_index, expected_response) => execute_abandon_staged_changes(&conductor, &cell, &mut test_state, staged_index, expected_response).await,
DanceTestStep::LoadCoreSchema => execute_load_new_schema(&conductor, &cell, &mut test_state).await,

}
}
info!("-------------- END OF {name} TEST CASE ------------------");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Simple Create Test Fixture
//
// This file is used to creates a TestCase that exercises the following steps:
// - Ensure database is empty
// - stage a new holon
// - update the staged holon's properties
// - commit the holon
// - get the holon
// - delete holon
// - ensure database is empty
//
//

#![allow(dead_code)]

use core::panic;
use holons::helpers::*;
use holons::holon::Holon;
use holons::holon_api::*;
use rstest::*;
use shared_types_holon::value_types::BaseValue;
use std::collections::btree_map::BTreeMap;
use dances::dance_response::ResponseStatusCode;

use holons::commit_manager::{CommitManager, StagedIndex};
use holons::context::HolonsContext;


use crate::shared_test::test_data_types::DancesTestCase;

// use hdk::prelude::*;

// use crate::shared_test::fixture_helpers::{derive_label, derive_type_description, derive_type_name};
// use crate::shared_test::property_descriptor_data_creators::{
// create_example_property_descriptors, create_example_updates_for_property_descriptors,
// };

use holons::holon_error::HolonError;
use holons::holon_reference::HolonReference;
use holons::relationship::RelationshipName;
use holons::staged_reference::StagedReference;

use shared_types_holon::{MapBoolean, MapInteger, MapString, PropertyMap, PropertyName, PropertyValue};


/// This function creates a set of simple (undescribed) holons
///
#[fixture]
pub fn load_core_schema_test_fixture() -> Result<DancesTestCase, HolonError> {
let mut test_case = DancesTestCase::new(
"Load the MAP Core (L0) Schema Testcase".to_string(),
"Bulk of work is on the guest-side load_core_schema function".to_string(),

);

//let mut expected_holons = Vec::new();

test_case.add_ensure_database_count_step(MapInteger(0))?;

test_case.add_load_core_schema()?;
//expected_holons.push(book_holon.clone());

//test_case.add_ensure_database_count_step(MapInteger(1))?;

// test_case.holons = expected_holons;

// let mut book_holon = Holon::new();
// book_holon
// .with_property_value(
// PropertyName(MapString("title".to_string())),
// BaseValue::StringValue(MapString("Emerging World: The Evolution of Consciousness and the Future of Humanity".to_string())))
// .with_property_value(
// PropertyName(MapString("description".to_string())),
// BaseValue::StringValue(MapString("Why is there so much chaos and suffering in the world today? Are we sliding towards dystopia and perhaps extinction, or is there hope for a better future?".to_string())))
// ;
// test_case.add_create_step(book_holon)?;

// debug!("expected holons: {:?}", expected_holons);

Ok(test_case.clone())
}
2 changes: 2 additions & 0 deletions crates/coordinator/dances/tests/shared_test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub mod test_ensure_database_count;
pub mod test_match_db_content;
pub mod test_stage_new_holon;
pub mod test_with_properties_command;
pub mod descriptor_dance_fixtures;
pub mod test_load_core_schema;

use hdk::prelude::*;
use holochain::sweettest::{SweetAgents, SweetCell, SweetConductor, SweetDnaFile};
Expand Down
15 changes: 14 additions & 1 deletion crates/coordinator/dances/tests/shared_test/test_data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ pub enum DanceTestStep {
), // Adds relationship between two Holons
EnsureDatabaseCount(MapInteger), // Ensures the expected number of holons exist in the DB
StageHolon(Holon), // Associated data is expected Holon, it could be an empty Holon (i.e., with no internal state)

Commit, // Attempts to commit
WithProperties(StagedIndex, PropertyMap, ResponseStatusCode), // Update properties for Holon at StagedIndex with PropertyMap
MatchSavedContent, // Ensures data committed to persistent store (DHT) matches expected
AbandonStagedChanges(StagedIndex, ResponseStatusCode), // Marks a staged Holon as 'abandoned'
LoadCoreSchema,

}

impl fmt::Display for DanceTestStep {
Expand All @@ -41,7 +44,7 @@ impl fmt::Display for DanceTestStep {
holons_to_add,
expected_response,
) => {
write!(f, "AddRelatedHolons to Holon at ({:#?}) for relationship: {:#?}, added_count: {:#?}, expecting: {:#?}", index, relationship_name, holons_to_add.len(), expected_response )
write!(f, "AddRelatedHolons to Holon at ({:#?}) for relationship: {:#?}, added_count: {:#?}, expecting: {:#?}", index, relationship_name, holons_to_add.len(), expected_response)
}
DanceTestStep::EnsureDatabaseCount(count) => {
write!(f, "EnsureDatabaseCount = {}", count.0)
Expand All @@ -62,13 +65,17 @@ impl fmt::Display for DanceTestStep {
DanceTestStep::MatchSavedContent => {
write!(f, "MatchSavedContent")
}

DanceTestStep::AbandonStagedChanges(index, expected_response) => {
write!(
f,
"Marking Holon at ({:#?}) as Abandoned, expecting ({:#?})",
index, expected_response
)
}
DanceTestStep::LoadCoreSchema => {
write!(f, "LoadCoreSchema")
}
}
}
}
Expand Down Expand Up @@ -153,4 +160,10 @@ impl DancesTestCase {
));
Ok(())
}
pub fn add_load_core_schema(&mut self) -> Result<(), HolonError> {
self.steps.push_back(DanceTestStep::LoadCoreSchema);
Ok(())
}


}
Loading
Loading