Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LKozlowski committed Nov 28, 2024
1 parent 416ac23 commit f5a36f4
Show file tree
Hide file tree
Showing 5 changed files with 1,394 additions and 56 deletions.
76 changes: 76 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,79 @@ fn as_felt(bytes: &[u8]) -> Result<Felt> {
let felt = Felt::try_new(&hex)?;
Ok(felt)
}


mod tests {
use super::*;

#[test]
fn test_as_felt() {
let result = as_felt(&[]);
assert!(result.is_err());
assert!(
as_felt(&[
0x0,
0x0,
]).is_err()
);
assert_eq!(
as_felt(&[
0x1,
]).unwrap().as_ref(),
Felt::try_new("0x1").unwrap().as_ref(),
);
}

#[test]
fn test_equality_for_state() {
let s1 = State {
block_number: 1,
block_hash: Felt::try_new("0x0").unwrap(),
root: Felt::try_new("0x0").unwrap(),
};
let s2 = State {
block_number: 1,
block_hash: Felt::try_new("0x0").unwrap(),
root: Felt::try_new("0x0").unwrap(),
};

assert!(s1 == s2);

let s1 = State {
block_number: 1,
block_hash: Felt::try_new("0x0").unwrap(),
root: Felt::try_new("0x0").unwrap(),
};
let s2 = State {
block_number: 2,
block_hash: Felt::try_new("0x0").unwrap(),
root: Felt::try_new("0x0").unwrap(),
};
assert!(s1 != s2);

let s1 = State {
block_number: 1,
block_hash: Felt::try_new("0x1").unwrap(),
root: Felt::try_new("0x0").unwrap(),
};
let s2 = State {
block_number: 1,
block_hash: Felt::try_new("0x0").unwrap(),
root: Felt::try_new("0x0").unwrap(),
};
assert!(s1 != s2);

let s1 = State {
block_number: 1,
block_hash: Felt::try_new("0x0").unwrap(),
root: Felt::try_new("0x0").unwrap(),
};
let s2 = State {
block_number: 1,
block_hash: Felt::try_new("0x0").unwrap(),
root: Felt::try_new("0x1").unwrap(),
};
assert!(s1 != s2);
}

}
115 changes: 115 additions & 0 deletions src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,118 @@ async fn sleep(delay: std::time::Duration) {
gloo_timers::future::TimeoutFuture::new(millis).await;
}
}


#[cfg(test)]
mod tests {
use std::time::Duration;

use super::*;
#[test]
fn test_get_core_contract_address() {
assert_eq!(
get_core_contract_address(&Network::MAINNET).unwrap(),
Address::from_str(MAINNET_CC_ADDRESS).unwrap(),
);

assert_eq!(
get_core_contract_address(&Network::SEPOLIA).unwrap(),
Address::from_str(SEPOLIA_CC_ADDRESS).unwrap(),
);

assert_eq!(
get_core_contract_address(&Network::GOERLI).unwrap_err().to_string(),
"unsupported network: GOERLI".to_string(),
);

assert_eq!(
get_core_contract_address(&Network::HOLESKY).unwrap_err().to_string(),
"unsupported network: HOLESKY".to_string(),
);
}

#[test]
fn test_get_consensus_rpc() {
assert_eq!(
get_consensus_rpc(&Network::MAINNET).unwrap(),
MAINNET_CONSENSUS_RPC,
);

assert_eq!(
get_consensus_rpc(&Network::SEPOLIA).unwrap(),
SEPOLIA_CONSENSUS_RPC,
);

assert_eq!(
get_consensus_rpc(&Network::GOERLI).unwrap_err().to_string(),
"unsupported network: GOERLI".to_string(),
);

assert_eq!(
get_consensus_rpc(&Network::HOLESKY).unwrap_err().to_string(),
"unsupported network: HOLESKY".to_string(),
);
}

#[test]
fn test_get_fallback_address() {
assert_eq!(
get_fallback_address(&Network::MAINNET).unwrap(),
MAINNET_FALLBACK_RPC,
);

assert_eq!(
get_fallback_address(&Network::SEPOLIA).unwrap(),
SEPOLIA_FALLBACK_RPC,
);

assert_eq!(
get_fallback_address(&Network::GOERLI).unwrap_err().to_string(),
"unsupported network: GOERLI".to_string(),
);

assert_eq!(
get_fallback_address(&Network::HOLESKY).unwrap_err().to_string(),
"unsupported network: HOLESKY".to_string(),
);
}

#[tokio::test]
async fn test_get_checkpoint() {
assert_eq!(
get_checkpoint(&Network::GOERLI).await.unwrap_err().to_string(),
"unsupported network: GOERLI".to_string(),
);

assert_eq!(
get_checkpoint(&Network::HOLESKY).await.unwrap_err().to_string(),
"unsupported network: HOLESKY".to_string(),
);

// TODO: it could utilise mock server
get_checkpoint(&Network::MAINNET).await.unwrap();
get_checkpoint(&Network::SEPOLIA).await.unwrap();
}

#[tokio::test]
async fn test_sleep() {
let start = std::time::Instant::now();
sleep(Duration::from_millis(200)).await;
assert!(start.elapsed().as_millis() >= 200);
}

#[tokio::test]
async fn test_get_client() {
let rpc = "https://rpc/v2";
let data_dir = "tmp";

assert_eq!(
get_client(&rpc, Network::GOERLI, data_dir).await.err().unwrap().to_string(),
"consensus rpc url",
);

// sucesfully build client

Check warning on line 350 in src/eth.rs

View workflow job for this annotation

GitHub Actions / typos

"sucesfully" should be "successfully".
get_client(&rpc, Network::SEPOLIA, data_dir).await.unwrap();

}
}
Loading

0 comments on commit f5a36f4

Please sign in to comment.