Skip to content

Commit

Permalink
full test coverage of public profiles zome functions, except for thos…
Browse files Browse the repository at this point in the history
…e calling hdk_crud
  • Loading branch information
weswalla committed Sep 14, 2021
1 parent 4d522ca commit 3daec5b
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 27 deletions.
132 changes: 106 additions & 26 deletions dna/tests/src/profiles/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub mod tests {
use hdk_crud::{ActionType, WrappedAgentPubKey, WrappedHeaderHash};
use holochain_types::prelude::ValidateDataFixturator;
use profiles::profile::{
agent_signal_entry_type, inner_create_whoami, AgentSignal, Profile, SignalData, Status,
WireEntry,
agent_signal_entry_type, create_imported_profile , inner_create_whoami,
inner_update_whoami, AgentSignal, Profile, SignalData, Status, WireEntry,
};
use projects::project::edge::validate::*;
use projects::project::error::Error;
Expand All @@ -21,19 +21,10 @@ pub mod tests {
let mut mock_hdk = MockHdkT::new();
let mock_hdk_ref = &mut mock_hdk;

let profile = Profile {
first_name: "".to_string(),
last_name: "".to_string(),
handle: "".to_string(),
status: Status::Online,
avatar_url: "".to_string(),
address: fixt!(WrappedAgentPubKey),
is_imported: false,
};

let wire_entry = generate_wire_entry();
let profile = wire_entry.clone().entry;
let profile_entry = EntryWithDefId::try_from(profile.clone()).unwrap();

let profile_header_hash = fixt!(HeaderHash);
let profile_header_hash = wire_entry.clone().address.0;
mock_create(mock_hdk_ref, profile_entry, Ok(profile_header_hash.clone()));

let profile_hash = fixt!(EntryHash);
Expand Down Expand Up @@ -73,10 +64,7 @@ pub mod tests {
create_link_input,
Ok(link_header_hash.clone()),
);
let wire_entry = WireEntry {
entry: profile.clone(),
address: WrappedHeaderHash(profile_header_hash),
};

let signal = AgentSignal {
entry_type: agent_signal_entry_type(),
action: ActionType::Create,
Expand All @@ -99,13 +87,105 @@ pub mod tests {
assert_matches!(result, Ok(wire_entry));
}
#[test]
fn test_create_imported_profile() {}
#[test]
fn test_update_whoami() {}
#[test]
fn test_whoami() {}
#[test]
fn test_fetch_agents() {}
fn test_create_imported_profile() {
let mut mock_hdk = MockHdkT::new();
let mock_hdk_ref = &mut mock_hdk;

let wire_entry = generate_wire_entry();
let profile = wire_entry.clone().entry;
let profile_entry = EntryWithDefId::try_from(profile.clone()).unwrap();
let profile_header_hash = wire_entry.clone().address.0;

mock_create(mock_hdk_ref, profile_entry, Ok(profile_header_hash.clone()));

let profile_hash = fixt!(EntryHash);
mock_hash_entry(
mock_hdk_ref,
Entry::try_from(profile.clone()).unwrap(),
Ok(profile_hash.clone()),
);

let agent_path = Path::from("agents");
let agent_path_hash = fixt!(EntryHash);
mock_hash_entry(
mock_hdk_ref,
Entry::try_from(agent_path).unwrap(),
Ok(agent_path_hash.clone()),
);

let create_link_input = CreateLinkInput::new(
agent_path_hash.clone(),
profile_hash.clone(),
LinkTag::from(()),
);
let link_header_hash = fixt!(HeaderHash);
mock_create_link(
mock_hdk_ref,
create_link_input,
Ok(link_header_hash.clone()),
);

set_hdk(mock_hdk);
let result = create_imported_profile(profile);
assert_matches!(result, Ok(wire_entry));
}
#[test]
fn test_fetch_agent_address() {}
fn test_update_whoami() {
let mut mock_hdk = MockHdkT::new();
let mock_hdk_ref = &mut mock_hdk;

let wire_entry = generate_wire_entry();
let profile_address = wire_entry.address.0.clone();
let profile = wire_entry.entry.clone();
let update_input =
UpdateInput::new(profile_address, EntryWithDefId::try_from(profile).unwrap());
let update_header_hash = fixt!(HeaderHash);
mock_update(mock_hdk_ref, update_input, Ok(update_header_hash));

let signal = AgentSignal {
entry_type: agent_signal_entry_type(),
action: ActionType::Update,
data: SignalData::Update(wire_entry.clone()),
};

let payload = ExternIO::encode(signal).unwrap();
let agents = vec![];
let remote_signal = RemoteSignal {
signal: ExternIO::encode(payload).unwrap(),
agents,
};
mock_remote_signal(mock_hdk_ref, remote_signal, Ok(()));

set_hdk(mock_hdk);
fn get_peers() -> ExternResult<Vec<AgentPubKey>> {
Ok(vec![])
}
let result = inner_update_whoami(wire_entry, get_peers);
assert_matches!(result, Ok(wire_entry));
}
// #[test]
// fn test_whoami() {}
// #[test]
// fn test_fetch_agents() {}

/// generate an arbitrary `WireEntry` for unit testing
fn generate_wire_entry() -> WireEntry {
let profile = Profile {
first_name: "".to_string(),
last_name: "".to_string(),
handle: "".to_string(),
status: Status::Online,
avatar_url: "".to_string(),
address: fixt!(WrappedAgentPubKey),
is_imported: false,
};

let profile_header_hash = fixt!(HeaderHash);

let wire_entry = WireEntry {
entry: profile,
address: WrappedHeaderHash(profile_header_hash),
};
wire_entry
}
}
8 changes: 8 additions & 0 deletions dna/tests/src/test_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ pub fn mock_remote_signal(mock_hdk: &mut MockHdkT, input: RemoteSignal, output:
.with(mockall::predicate::eq(input))
.times(1)
.return_const(output);
}

pub fn mock_update(mock_hdk: &mut MockHdkT, expected_input: UpdateInput, expected_output: ExternResult<HeaderHash>) {
mock_hdk
.expect_update()
.with(mockall::predicate::eq(expected_input))
.times(1)
.return_const(expected_output);
}
6 changes: 5 additions & 1 deletion dna/zomes/profiles/src/profile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ pub fn agent_signal_entry_type() -> String {

#[hdk_extern]
pub fn update_whoami(update: WireEntry) -> ExternResult<WireEntry> {
inner_update_whoami(update, get_peers)
}

pub fn inner_update_whoami(update: WireEntry, get_peers_to_signal: fn() -> ExternResult<Vec<AgentPubKey>>) -> ExternResult<WireEntry> {
update_entry(update.address.0.clone(), &update.entry)?;
// // send update to peers
// we don't want to cause real failure for inability to send to peers
Expand All @@ -185,7 +189,7 @@ pub fn update_whoami(update: WireEntry) -> ExternResult<WireEntry> {
action: ActionType::Update,
data: SignalData::Update(update.clone()),
};
let _ = send_agent_signal(signal, get_peers);
let _ = send_agent_signal(signal, get_peers_to_signal);
Ok(update)
}

Expand Down

0 comments on commit 3daec5b

Please sign in to comment.