Skip to content

Add test for resize instruction #273

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

Merged
merged 2 commits into from
Aug 29, 2022
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
1 change: 1 addition & 0 deletions program/rust/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod test_del_product;
mod test_del_publisher;
mod test_init_mapping;
mod test_init_price;
mod test_resize_account;
mod test_set_min_pub;
mod test_sizes;
mod test_upd_aggregate;
Expand Down
26 changes: 25 additions & 1 deletion program/rust/src/tests/pyth_simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use solana_program::instruction::{
};
use solana_program::pubkey::Pubkey;
use solana_program::rent::Rent;
use solana_program::system_instruction;
use solana_program::{
system_instruction,
system_program,
};
use solana_program_test::{
processor,
BanksClient,
Expand Down Expand Up @@ -224,6 +227,27 @@ impl PythSimulator {
.await
}

/// Resize a price account (using the resize_price_account
/// instruction).
pub async fn resize_price_account(
&mut self,
price_keypair: &Keypair,
) -> Result<(), BanksClientError> {
let cmd: CommandHeader = OracleCommand::ResizePriceAccount.into();
let instruction = Instruction::new_with_bytes(
self.program_id,
bytes_of(&cmd),
vec![
AccountMeta::new(self.payer.pubkey(), true),
AccountMeta::new(price_keypair.pubkey(), true),
AccountMeta::new(system_program::id(), false),
],
);

self.process_ix(instruction, &vec![&price_keypair]).await
}


/// Get the account at `key`. Returns `None` if no such account exists.
pub async fn get_account(&mut self, key: Pubkey) -> Option<Account> {
self.banks_client.get_account(key).await.unwrap()
Expand Down
31 changes: 31 additions & 0 deletions program/rust/src/tests/test_resize_account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use solana_sdk::signer::Signer;
use std::mem::size_of;

use crate::c_oracle_header::pc_price_t;
use crate::tests::pyth_simulator::PythSimulator;
use crate::time_machine_types::PriceAccountWrapper;


/// Warning : This test will fail if you run cargo test instead of cargo test-bpf
#[tokio::test]
async fn test_resize_account() {
let mut sim = PythSimulator::new().await;
let mapping_keypair = sim.init_mapping().await.unwrap();
let product1 = sim.add_product(&mapping_keypair).await.unwrap();
let price1 = sim.add_price(&product1, -8).await.unwrap();

// Check size after initialization
let price1_account = sim.get_account(price1.pubkey()).await.unwrap();
assert_eq!(price1_account.data.len(), size_of::<pc_price_t>());

// Run the instruction once
assert!(sim.resize_price_account(&price1).await.is_ok());
// Check new size
let price1_account = sim.get_account(price1.pubkey()).await.unwrap();
assert_eq!(price1_account.data.len(), size_of::<PriceAccountWrapper>());

// Future calls don't change the size
assert!(sim.resize_price_account(&price1).await.is_ok());
let price1_account = sim.get_account(price1.pubkey()).await.unwrap();
assert_eq!(price1_account.data.len(), size_of::<PriceAccountWrapper>());
}