From e07d7eebc1005c141142fe163516e49a730b5f00 Mon Sep 17 00:00:00 2001 From: akhercha Date: Mon, 16 Sep 2024 09:02:58 +0200 Subject: [PATCH] feat(dispatcher_with_tests): Added registry tests --- .../pragma_feeds_registry/src/contract.cairo | 2 + .../pragma_feeds_registry/tests/lib.cairo | 3 +- .../tests/test_contract.cairo | 83 +++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 cairo/crates/pragma_feeds_registry/tests/test_contract.cairo diff --git a/cairo/crates/pragma_feeds_registry/src/contract.cairo b/cairo/crates/pragma_feeds_registry/src/contract.cairo index b5327507..3319c9fa 100644 --- a/cairo/crates/pragma_feeds_registry/src/contract.cairo +++ b/cairo/crates/pragma_feeds_registry/src/contract.cairo @@ -133,6 +133,7 @@ pub mod PragmaFeedRegistry { } let feed_id = self.feed_ids.read(i); all_feeds.append(feed_id); + i += 1; }; all_feeds } @@ -175,6 +176,7 @@ pub mod PragmaFeedRegistry { feed_id_index = Option::Some(i); break; } + i += 1; }; feed_id_index diff --git a/cairo/crates/pragma_feeds_registry/tests/lib.cairo b/cairo/crates/pragma_feeds_registry/tests/lib.cairo index 8b137891..aaef0003 100644 --- a/cairo/crates/pragma_feeds_registry/tests/lib.cairo +++ b/cairo/crates/pragma_feeds_registry/tests/lib.cairo @@ -1 +1,2 @@ - +#[cfg(test)] +pub mod test_contract; diff --git a/cairo/crates/pragma_feeds_registry/tests/test_contract.cairo b/cairo/crates/pragma_feeds_registry/tests/test_contract.cairo new file mode 100644 index 00000000..d546b999 --- /dev/null +++ b/cairo/crates/pragma_feeds_registry/tests/test_contract.cairo @@ -0,0 +1,83 @@ +use pragma_feed_types::FeedId; +use pragma_feeds_registry::{IPragmaFeedRegistryDispatcher, IPragmaFeedRegistryDispatcherTrait}; +use snforge_std::{declare, ContractClassTrait, start_prank, stop_prank, CheatTarget}; +use starknet::{ContractAddress, contract_address_const}; + +/// Returns the mock owner +fn owner() -> ContractAddress { + contract_address_const::<'new_owner'>() +} + +/// Deploys the Pragma Feeds Registry contract and returns: +/// * the deployed contract address +/// * the registry dispatcher +fn deploy_pragma_registry() -> (ContractAddress, IPragmaFeedRegistryDispatcher) { + // Deploy contract + let contract = declare("PragmaFeedRegistry").unwrap(); + let (contract_address, _) = contract.deploy(@array![owner().into()]).unwrap(); + let dispatcher = IPragmaFeedRegistryDispatcher { contract_address }; + start_prank(CheatTarget::One(contract_address), owner()); + (contract_address, dispatcher) +} + +#[test] +fn test_add_feed() { + let (_, registry) = deploy_pragma_registry(); + + let feed_id: FeedId = 0x456; + + registry.add_feed(feed_id); + + assert!(registry.feed_exists(feed_id), "Feed should exist"); +} + +#[test] +#[should_panic(expected: ('Feed ID already registed',))] +fn test_add_duplicate_feed() { + let (_, registry) = deploy_pragma_registry(); + + let feed_id: FeedId = 0x456; + registry.add_feed(feed_id); + registry.add_feed(feed_id); +} + +#[test] +fn test_remove_feed() { + let (_, registry) = deploy_pragma_registry(); + + let feed_id: FeedId = 0x456; + registry.add_feed(feed_id); + registry.remove_feed(feed_id); + + assert!(!registry.feed_exists(feed_id), "Feed should not exist"); +} + +#[test] +fn test_get_all_feeds() { + let (_, registry) = deploy_pragma_registry(); + + let expected_feeds: Array = array![0x123, 0x456, 0x789]; + + let mut feeds_to_add = expected_feeds.span(); + loop { + match feeds_to_add.pop_front() { + Option::Some(v) => registry.add_feed(*v), + Option::None(()) => { break; }, + } + }; + + let out_feeds = registry.get_all_feeds(); + assert_eq!(out_feeds, expected_feeds, "Should return all added feeds"); +} + +#[test] +#[should_panic(expected: ('Caller is not the owner',))] +fn test_add_feed_not_owner() { + let (registry_address, registry) = deploy_pragma_registry(); + + stop_prank(CheatTarget::One(registry_address)); + let non_owner = 0x789.try_into().unwrap(); + start_prank(CheatTarget::One(registry_address), non_owner); + + registry.add_feed(0x123); +}