Skip to content

Commit 56d422d

Browse files
authored
Add test for resize instruction (#273)
* Add test for resize instruction * fixup! Add test for resize instruction
1 parent eab08d4 commit 56d422d

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

program/rust/src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod test_del_product;
88
mod test_del_publisher;
99
mod test_init_mapping;
1010
mod test_init_price;
11+
mod test_resize_account;
1112
mod test_set_min_pub;
1213
mod test_sizes;
1314
mod test_upd_aggregate;

program/rust/src/tests/pyth_simulator.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use solana_program::instruction::{
1111
};
1212
use solana_program::pubkey::Pubkey;
1313
use solana_program::rent::Rent;
14-
use solana_program::system_instruction;
14+
use solana_program::{
15+
system_instruction,
16+
system_program,
17+
};
1518
use solana_program_test::{
1619
processor,
1720
BanksClient,
@@ -224,6 +227,27 @@ impl PythSimulator {
224227
.await
225228
}
226229

230+
/// Resize a price account (using the resize_price_account
231+
/// instruction).
232+
pub async fn resize_price_account(
233+
&mut self,
234+
price_keypair: &Keypair,
235+
) -> Result<(), BanksClientError> {
236+
let cmd: CommandHeader = OracleCommand::ResizePriceAccount.into();
237+
let instruction = Instruction::new_with_bytes(
238+
self.program_id,
239+
bytes_of(&cmd),
240+
vec![
241+
AccountMeta::new(self.payer.pubkey(), true),
242+
AccountMeta::new(price_keypair.pubkey(), true),
243+
AccountMeta::new(system_program::id(), false),
244+
],
245+
);
246+
247+
self.process_ix(instruction, &vec![&price_keypair]).await
248+
}
249+
250+
227251
/// Get the account at `key`. Returns `None` if no such account exists.
228252
pub async fn get_account(&mut self, key: Pubkey) -> Option<Account> {
229253
self.banks_client.get_account(key).await.unwrap()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use solana_sdk::signer::Signer;
2+
use std::mem::size_of;
3+
4+
use crate::c_oracle_header::pc_price_t;
5+
use crate::tests::pyth_simulator::PythSimulator;
6+
use crate::time_machine_types::PriceAccountWrapper;
7+
8+
9+
/// Warning : This test will fail if you run cargo test instead of cargo test-bpf
10+
#[tokio::test]
11+
async fn test_resize_account() {
12+
let mut sim = PythSimulator::new().await;
13+
let mapping_keypair = sim.init_mapping().await.unwrap();
14+
let product1 = sim.add_product(&mapping_keypair).await.unwrap();
15+
let price1 = sim.add_price(&product1, -8).await.unwrap();
16+
17+
// Check size after initialization
18+
let price1_account = sim.get_account(price1.pubkey()).await.unwrap();
19+
assert_eq!(price1_account.data.len(), size_of::<pc_price_t>());
20+
21+
// Run the instruction once
22+
assert!(sim.resize_price_account(&price1).await.is_ok());
23+
// Check new size
24+
let price1_account = sim.get_account(price1.pubkey()).await.unwrap();
25+
assert_eq!(price1_account.data.len(), size_of::<PriceAccountWrapper>());
26+
27+
// Future calls don't change the size
28+
assert!(sim.resize_price_account(&price1).await.is_ok());
29+
let price1_account = sim.get_account(price1.pubkey()).await.unwrap();
30+
assert_eq!(price1_account.data.len(), size_of::<PriceAccountWrapper>());
31+
}

0 commit comments

Comments
 (0)