Skip to content

Commit

Permalink
test: move TransactionTestData to test_util (#2112)
Browse files Browse the repository at this point in the history
  • Loading branch information
guy-starkware authored Nov 25, 2024
1 parent 1e38b56 commit 31fa7b7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
18 changes: 17 additions & 1 deletion crates/starknet_api/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use std::fs::read_to_string;
use std::path::{Path, PathBuf};

use infra_utils::path::cargo_manifest_dir;
use serde::{Deserialize, Serialize};
use starknet_types_core::felt::Felt;

use crate::core::{ContractAddress, Nonce};
use crate::block::BlockNumber;
use crate::core::{ChainId, ContractAddress, Nonce};
use crate::transaction::{Transaction, TransactionHash};

pub mod declare;
pub mod deploy_account;
Expand All @@ -27,6 +30,19 @@ pub fn read_json_file<P: AsRef<Path>>(path_in_resource_dir: P) -> serde_json::Va
serde_json::from_str(&json_str).unwrap()
}

#[derive(Deserialize, Serialize, Debug)]
/// A struct used for reading the transaction test data (e.g., for transaction hash tests).
pub struct TransactionTestData {
/// The actual transaction.
pub transaction: Transaction,
/// The expected transaction hash.
pub transaction_hash: TransactionHash,
/// An optional transaction hash to query.
pub only_query_transaction_hash: Option<TransactionHash>,
pub chain_id: ChainId,
pub block_number: BlockNumber,
}

#[derive(Debug, Default)]
pub struct NonceManager {
next_nonce: HashMap<ContractAddress, Felt>,
Expand Down
20 changes: 4 additions & 16 deletions crates/starknet_api/src/transaction_hash_test.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use pretty_assertions::assert_eq;
use serde::{Deserialize, Serialize};
use sha3::{Digest, Keccak256};
use starknet_types_core::felt::Felt;

use super::{get_transaction_hash, validate_transaction_hash, CONSTRUCTOR_ENTRY_POINT_SELECTOR};
use crate::block::BlockNumber;
use crate::core::ChainId;
use crate::test_utils::read_json_file;
use crate::transaction::{Transaction, TransactionHash, TransactionOptions};
use crate::test_utils::{read_json_file, TransactionTestData};
use crate::transaction::{Transaction, TransactionOptions};

#[test]
fn test_constructor_selector() {
Expand All @@ -19,18 +16,9 @@ fn test_constructor_selector() {
assert_eq!(constructor_felt, CONSTRUCTOR_ENTRY_POINT_SELECTOR);
}

#[derive(Deserialize, Serialize)]
struct TransactionTestData {
transaction: Transaction,
transaction_hash: TransactionHash,
only_query_transaction_hash: Option<TransactionHash>,
chain_id: ChainId,
block_number: BlockNumber,
}

#[test]
fn test_transaction_hash() {
// The details were taken from Starknet Mainnet. You can found the transactions by hash in:
// The details were taken from Starknet Mainnet. You can find the transactions by hash in:
// https://alpha-mainnet.starknet.io/feeder_gateway/get_transaction?transactionHash=<transaction_hash>
let transactions_test_data_vec: Vec<TransactionTestData> =
serde_json::from_value(read_json_file("transaction_hash.json")).unwrap();
Expand Down Expand Up @@ -64,7 +52,7 @@ fn test_transaction_hash() {

#[test]
fn test_deprecated_transaction_hash() {
// The details were taken from Starknet Mainnet. You can found the transactions by hash in:
// The details were taken from Starknet Mainnet. You can find the transactions by hash in:
// https://alpha-mainnet.starknet.io/feeder_gateway/get_transaction?transactionHash=<transaction_hash>
let transaction_test_data_vec: Vec<TransactionTestData> =
serde_json::from_value(read_json_file("deprecated_transaction_hash.json")).unwrap();
Expand Down

0 comments on commit 31fa7b7

Please sign in to comment.