Skip to content

Commit

Permalink
feat(dispatcher_with_tests): Added registry tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akhercha committed Sep 16, 2024
1 parent 4ad2aee commit e07d7ee
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cairo/crates/pragma_feeds_registry/src/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub mod PragmaFeedRegistry {
}
let feed_id = self.feed_ids.read(i);
all_feeds.append(feed_id);
i += 1;
};
all_feeds
}
Expand Down Expand Up @@ -175,6 +176,7 @@ pub mod PragmaFeedRegistry {
feed_id_index = Option::Some(i);
break;
}
i += 1;
};

feed_id_index
Expand Down
3 changes: 2 additions & 1 deletion cairo/crates/pragma_feeds_registry/tests/lib.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

#[cfg(test)]
pub mod test_contract;
83 changes: 83 additions & 0 deletions cairo/crates/pragma_feeds_registry/tests/test_contract.cairo
Original file line number Diff line number Diff line change
@@ -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<felt252> = 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);
}

0 comments on commit e07d7ee

Please sign in to comment.