From 7868b8400676e8c053aa107ac6993356e3a28c1f Mon Sep 17 00:00:00 2001 From: nninkoviSQA Date: Tue, 26 Apr 2022 13:28:05 +0200 Subject: [PATCH 1/2] Fixed diff not loading. --- sputnikdao-factory2/src/lib.rs | 206 +++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) diff --git a/sputnikdao-factory2/src/lib.rs b/sputnikdao-factory2/src/lib.rs index b1f028e1f..01fac7af2 100644 --- a/sputnikdao-factory2/src/lib.rs +++ b/sputnikdao-factory2/src/lib.rs @@ -517,4 +517,210 @@ mod tests { assert_eq!(factory.get_owner(), bob()) } + + // ################################## // + // # Factory View Function Tests # // + // ################################## // + + // get_dao_list tests + #[test] + fn test_returns_empty_array_for_new_factory() { + let mut context = VMContextBuilder::new(); + testing_env!(context + .current_account_id(bob()) + .predecessor_account_id(bob()) + .attached_deposit(to_yocto("6")) + .build()); + let factory = SputnikDAOFactory::new(); + + assert_eq!(factory.get_dao_list(), []); + } + + #[test] + fn test_returns_full_list_of_daos() { + let mut context = VMContextBuilder::new(); + testing_env!(context + .current_account_id(bob()) + .predecessor_account_id(bob()) + .attached_deposit(to_yocto("6")) + .build()); + let mut factory = SputnikDAOFactory::new(); + + factory.daos.insert(&bob()); + + factory.daos.insert(&alice()); + + let mut full_list_of_daos = Vec::new(); + + full_list_of_daos.push(bob()); + + full_list_of_daos.push(alice()); + + assert_eq!(factory.get_dao_list(), full_list_of_daos); + } + + //get_number_daos test + #[test] + fn test_returns_integer_of_daos() { + let mut context = VMContextBuilder::new(); + testing_env!(context + .current_account_id(bob()) + .predecessor_account_id(bob()) + .attached_deposit(to_yocto("6")) + .build()); + let mut factory = SputnikDAOFactory::new(); + + factory.daos.insert(&bob()); + + factory.daos.insert(&alice()); + + assert_eq!(factory.get_number_daos(), 2); + } + + //get_daos test + #[test] + fn test_returns_a_list_of_daos_matching_the_specified() { + let mut context = VMContextBuilder::new(); + testing_env!(context + .current_account_id(bob()) + .predecessor_account_id(bob()) + .attached_deposit(to_yocto("6")) + .build()); + let mut factory = SputnikDAOFactory::new(); + factory.daos.insert(&bob()); + factory.daos.insert(&alice()); + let mut list_of_daos = Vec::new(); + list_of_daos.push(bob()); + list_of_daos.push(alice()); + assert_eq!(factory.get_daos(0, 2), list_of_daos); + } + + //get_owner tests + #[test] + fn test_returns_a_string_representing_the_account_that_owns_the_factory() { + let mut context = VMContextBuilder::new(); + testing_env!(context + .current_account_id(bob()) + .predecessor_account_id(bob()) + .attached_deposit(to_yocto("6")) + .build()); + let factory = SputnikDAOFactory::new(); + + assert_eq!(factory.get_owner(), bob()); + } + + #[test] + #[should_panic] + fn test_fails_if_storage_is_corrupted_or_no_owner() { + let mut context = VMContextBuilder::new(); + testing_env!(context + .current_account_id(bob()) + .predecessor_account_id(bob()) + .attached_deposit(to_yocto("6")) + .build()); + let factory = SputnikDAOFactory::new(); + + assert_eq!(factory.get_owner().to_string(), ""); + } + + // get_default_code_hash tests + #[test] + fn test_returns_the_default_code_hash_for_a_new_dao() { + let mut context = VMContextBuilder::new(); + testing_env!(context + .current_account_id(bob()) + .predecessor_account_id(bob()) + .attached_deposit(to_yocto("6")) + .build()); + let mut factory = SputnikDAOFactory::new(); + + factory.create(bob(), "{}".as_bytes().to_vec().into()); + + let hash = [ + 9, 47, 14, 126, 93, 206, 56, 168, 143, 142, 82, 99, 62, 169, 170, 85, 9, 87, 50, 25, + 233, 191, 251, 21, 172, 100, 42, 69, 56, 190, 91, 168, + ]; + + assert_eq!( + factory.get_default_code_hash(), + Base58CryptoHash::from(hash) + ); + } + + #[test] + fn test_returns_the_default_code_hash_that_has_been_updated_after_new_code_blob_in_factory() { + let mut context = VMContextBuilder::new(); + testing_env!(context + .current_account_id(bob()) + .predecessor_account_id(bob()) + .attached_deposit(to_yocto("6")) + .build()); + let mut factory = SputnikDAOFactory::new(); + + factory.create(bob(), "{}".as_bytes().to_vec().into()); + + let code_hash = [ + 9, 47, 14, 126, 93, 206, 56, 168, 143, 142, 82, 99, 62, 169, 170, 85, 9, 87, 50, 25, + 233, 191, 251, 21, 172, 100, 42, 69, 56, 190, 91, 200, + ]; + + let code_hash: CryptoHash = code_hash.into(); + + env::storage_write(DEFAULT_CODE_HASH_KEY, &code_hash); + + assert_eq!( + factory.get_default_code_hash(), + Base58CryptoHash::from(code_hash) + ); + } + + // get_default_version test + #[test] + fn test_returns_the_default_metadata_version() { + let mut context = VMContextBuilder::new(); + testing_env!(context + .current_account_id(bob()) + .predecessor_account_id(bob()) + .attached_deposit(to_yocto("6")) + .build()); + let mut factory = SputnikDAOFactory::new(); + + factory.create(bob(), "{}".as_bytes().to_vec().into()); + + assert_eq!(factory.get_default_version(), DAO_CONTRACT_INITIAL_VERSION); + } + + #[test] + fn test_returns_an_entire_code_blob_based_on_given_code_hash() { + let mut context = VMContextBuilder::new(); + testing_env!(context + .current_account_id(bob()) + .predecessor_account_id(bob()) + .attached_deposit(to_yocto("6")) + .build()); + let factory = SputnikDAOFactory::new(); + + let code_hash: CryptoHash = factory.get_default_code_hash().into(); + + let code = env::storage_read(&code_hash).unwrap(); + + assert_ne!(code, [0u8; 0]) + } + + #[test] + #[allow(unused_variables)] + fn test_returns_no_value_if_code_doesnt_exist() { + let mut context = VMContextBuilder::new(); + testing_env!(context + .current_account_id(bob()) + .predecessor_account_id(bob()) + .attached_deposit(to_yocto("6")) + .build()); + + let factory = SputnikDAOFactory::new(); + + let code = env::storage_read(&[0u8; 32]).unwrap_or_default(); + + assert_eq!(code, [0u8; 0]) + } } From 285ccd8f70959ae34feda34f6f0d9e056e38af2f Mon Sep 17 00:00:00 2001 From: nninkoviSQA Date: Wed, 27 Apr 2022 09:08:58 +0200 Subject: [PATCH 2/2] Reformated tests and added comments. --- sputnikdao-factory2/src/lib.rs | 140 ++++++++++++++++++++------------- 1 file changed, 87 insertions(+), 53 deletions(-) diff --git a/sputnikdao-factory2/src/lib.rs b/sputnikdao-factory2/src/lib.rs index 01fac7af2..be6cf06d8 100644 --- a/sputnikdao-factory2/src/lib.rs +++ b/sputnikdao-factory2/src/lib.rs @@ -522,7 +522,6 @@ mod tests { // # Factory View Function Tests # // // ################################## // - // get_dao_list tests #[test] fn test_returns_empty_array_for_new_factory() { let mut context = VMContextBuilder::new(); @@ -546,22 +545,30 @@ mod tests { .build()); let mut factory = SputnikDAOFactory::new(); - factory.daos.insert(&bob()); - - factory.daos.insert(&alice()); - - let mut full_list_of_daos = Vec::new(); + factory.daos.insert(&AccountId::new_unchecked( + "bob.sputnik-dao.near".to_string(), + )); - full_list_of_daos.push(bob()); + factory.daos.insert(&AccountId::new_unchecked( + "alice.sputnik-dao.near".to_string(), + )); - full_list_of_daos.push(alice()); + factory.daos.insert(&AccountId::new_unchecked( + "mike.sputnik-dao.near".to_string(), + )); - assert_eq!(factory.get_dao_list(), full_list_of_daos); + assert_eq!( + factory.get_dao_list(), + [ + "bob.sputnik-dao.near".to_string().parse().unwrap(), + "alice.sputnik-dao.near".to_string().parse().unwrap(), + "mike.sputnik-dao.near".to_string().parse().unwrap() + ] + ); } - //get_number_daos test #[test] - fn test_returns_integer_of_daos() { + fn test_returns_an_integer_of_the_total_amount_of_daos_known_to_factory() { let mut context = VMContextBuilder::new(); testing_env!(context .current_account_id(bob()) @@ -570,16 +577,25 @@ mod tests { .build()); let mut factory = SputnikDAOFactory::new(); - factory.daos.insert(&bob()); + factory.daos.insert(&AccountId::new_unchecked( + "bob.sputnik-dao.near".to_string(), + )); + + factory.daos.insert(&AccountId::new_unchecked( + "alice.sputnik-dao.near".to_string(), + )); - factory.daos.insert(&alice()); + factory.daos.insert(&AccountId::new_unchecked( + "mike.sputnik-dao.near".to_string(), + )); - assert_eq!(factory.get_number_daos(), 2); + assert_eq!(factory.get_number_daos(), 3); } - //get_daos test + /* + get_daos test (Needs Impl)???? #[test] - fn test_returns_a_list_of_daos_matching_the_specified() { + fn test_returns_default_list_of_daos_with_max_len_of_100_&_offset_of_0() { let mut context = VMContextBuilder::new(); testing_env!(context .current_account_id(bob()) @@ -587,15 +603,17 @@ mod tests { .attached_deposit(to_yocto("6")) .build()); let mut factory = SputnikDAOFactory::new(); - factory.daos.insert(&bob()); - factory.daos.insert(&alice()); - let mut list_of_daos = Vec::new(); - list_of_daos.push(bob()); - list_of_daos.push(alice()); - assert_eq!(factory.get_daos(0, 2), list_of_daos); } - //get_owner tests + fn test_returns_list_of_daos_matching_specified_from_index_and_limit() { + pass + } + + fn test_capable_of_returning_non_zero_indexed_list() { + pass + } + */ + #[test] fn test_returns_a_string_representing_the_account_that_owns_the_factory() { let mut context = VMContextBuilder::new(); @@ -606,7 +624,7 @@ mod tests { .build()); let factory = SputnikDAOFactory::new(); - assert_eq!(factory.get_owner(), bob()); + assert_eq!(factory.get_owner(), "bob.near".to_string().parse().unwrap()); } #[test] @@ -623,7 +641,6 @@ mod tests { assert_eq!(factory.get_owner().to_string(), ""); } - // get_default_code_hash tests #[test] fn test_returns_the_default_code_hash_for_a_new_dao() { let mut context = VMContextBuilder::new(); @@ -636,17 +653,19 @@ mod tests { factory.create(bob(), "{}".as_bytes().to_vec().into()); - let hash = [ + factory.set_owner("bob.sputnik-dao.near".to_string().parse().unwrap()); + + let code_hash = [ 9, 47, 14, 126, 93, 206, 56, 168, 143, 142, 82, 99, 62, 169, 170, 85, 9, 87, 50, 25, 233, 191, 251, 21, 172, 100, 42, 69, 56, 190, 91, 168, - ]; + ] + .into(); - assert_eq!( - factory.get_default_code_hash(), - Base58CryptoHash::from(hash) - ); + assert_eq!(factory.get_default_code_hash(), code_hash); } + /* + Not sure how to implement this #[test] fn test_returns_the_default_code_hash_that_has_been_updated_after_new_code_blob_in_factory() { let mut context = VMContextBuilder::new(); @@ -659,33 +678,35 @@ mod tests { factory.create(bob(), "{}".as_bytes().to_vec().into()); - let code_hash = [ - 9, 47, 14, 126, 93, 206, 56, 168, 143, 142, 82, 99, 62, 169, 170, 85, 9, 87, 50, 25, - 233, 191, 251, 21, 172, 100, 42, 69, 56, 190, 91, 200, - ]; + factory.daos.insert(&bob()); - let code_hash: CryptoHash = code_hash.into(); + factory.set_owner("bob.sputnik-dao.near".to_string().parse().unwrap()); - env::storage_write(DEFAULT_CODE_HASH_KEY, &code_hash); + let initial_code_hash = factory.get_default_code_hash(); - assert_eq!( - factory.get_default_code_hash(), - Base58CryptoHash::from(code_hash) - ); + let new_code_hash: Base58CryptoHash = [ + 9, 47, 14, 126, 93, 200, 46, 168, 143, 142, 82, 99, 62, 169, 170, 85, 9, 87, 50, 25, + 233, 191, 251, 21, 172, 100, 42, 69, 56, 180, 90, 200, + ] + .into(); + + factory.update(factory.get_owner(), new_code_hash); PANICS WITH A + MESSAGE[panicked at 'Must be updated by the factory owner or the DAO + itself] + + assert_ne!(factory.get_default_code_hash(), initial_code_hash); } + */ - // get_default_version test #[test] - fn test_returns_the_default_metadata_version() { + fn test_returns_the_default_metadata_version_for_a_new_dao() { let mut context = VMContextBuilder::new(); testing_env!(context .current_account_id(bob()) .predecessor_account_id(bob()) .attached_deposit(to_yocto("6")) .build()); - let mut factory = SputnikDAOFactory::new(); - - factory.create(bob(), "{}".as_bytes().to_vec().into()); + let factory = SputnikDAOFactory::new(); assert_eq!(factory.get_default_version(), DAO_CONTRACT_INITIAL_VERSION); } @@ -702,25 +723,38 @@ mod tests { let code_hash: CryptoHash = factory.get_default_code_hash().into(); - let code = env::storage_read(&code_hash).unwrap(); + let blob_code = env::storage_read(&code_hash).unwrap(); - assert_ne!(code, [0u8; 0]) + assert_ne!(blob_code, [0u8; 0]) + // For some reason this is the only way that get_code() doesn't + // return empty; () } #[test] - #[allow(unused_variables)] - fn test_returns_no_value_if_code_doesnt_exist() { + #[should_panic] + fn test_returns_no_value_if_code_hash_doesnt_exist() { let mut context = VMContextBuilder::new(); testing_env!(context .current_account_id(bob()) .predecessor_account_id(bob()) .attached_deposit(to_yocto("6")) .build()); + let mut factory = SputnikDAOFactory::new(); - let factory = SputnikDAOFactory::new(); + factory.create(bob(), "{}".as_bytes().to_vec().into()); - let code = env::storage_read(&[0u8; 32]).unwrap_or_default(); + factory.daos.insert(&bob()); - assert_eq!(code, [0u8; 0]) + let fake_code_hash: Base58CryptoHash = [0u8; 32].into(); + + factory.get_code(fake_code_hash); + } + + /* + Not sure which args to pass + #[test] + fn test_returns_contract_metadata() { + Pass } + */ }