Skip to content

Commit

Permalink
refactor: replace tests json files with instances
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeletstarkware committed Apr 3, 2024
1 parent 2e9a1d0 commit d233110
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 187 deletions.
105 changes: 86 additions & 19 deletions crates/gateway/src/gateway_test.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,93 @@
use crate::gateway::add_transaction;
use axum::Json;
use axum::{body::HttpBody, response::IntoResponse};
use rstest::rstest;
use starknet_api::external_transaction::ExternalTransaction;
use std::fs::File;
use std::io::BufReader;

// TODO(Ayelet): Replace the use of the JSON files with generated instances, then serialize these
// into JSON for testing.
#[rstest]
#[case("./src/json_files_for_testing/declare_v3.json", "DECLARE")]
#[case(
"./src/json_files_for_testing/deploy_account_v3.json",
"DEPLOY_ACCOUNT"
)]
#[case("./src/json_files_for_testing/invoke_v3.json", "INVOKE")]
use rstest::fixture;
use starknet_api::external_transaction::DataAvailabilityMode::L1;
use starknet_api::external_transaction::{
ExternalDeclareTransaction, ExternalDeclareTransactionV3, ExternalDeployAccountTransaction,
ExternalDeployAccountTransactionV3, ExternalInvokeTransaction, ExternalInvokeTransactionV3,
ExternalTransaction,
};

// TODO(Ayelet): Change to non-default values.
#[fixture]
fn create_external_declare_transaction_v3() -> ExternalTransaction {
let declare_transaction = ExternalDeclareTransactionV3 {
contract_class: Default::default(),
resource_bounds: Default::default(),
tip: Default::default(),
signature: Default::default(),
nonce: Default::default(),
compiled_class_hash: Default::default(),
sender_address: Default::default(),
nonce_data_availability_mode: L1,
fee_data_availability_mode: L1,
paymaster_data: Default::default(),
account_deployment_data: Default::default(),
version: Default::default(),
r#type: Default::default(),
};

ExternalTransaction::Declare(ExternalDeclareTransaction::V3(declare_transaction))
}

// TODO(Ayelet): Change to non-default values.
#[fixture]
fn create_external_deploy_account_transaction_v3() -> ExternalTransaction {
let deploy_account_transaction = ExternalDeployAccountTransactionV3 {
resource_bounds: Default::default(),
tip: Default::default(),
contract_address_salt: Default::default(),
class_hash: Default::default(),
constructor_calldata: Default::default(),
nonce: Default::default(),
signature: Default::default(),
nonce_data_availability_mode: L1,
fee_data_availability_mode: L1,
paymaster_data: Default::default(),
version: Default::default(),
r#type: Default::default(),
};

ExternalTransaction::DeployAccount(ExternalDeployAccountTransaction::V3(
deploy_account_transaction,
))
}

// TODO(Ayelet): Change to non-default values.
#[fixture]
fn create_external_invoke_transaction_v3() -> ExternalTransaction {
let invoke_transaction = ExternalInvokeTransactionV3 {
resource_bounds: Default::default(),
tip: Default::default(),
calldata: Default::default(),
sender_address: Default::default(),
nonce: Default::default(),
signature: Default::default(),
nonce_data_availability_mode: L1,
fee_data_availability_mode: L1,
paymaster_data: Default::default(),
account_deployment_data: Default::default(),
version: Default::default(),
r#type: Default::default(),
};

ExternalTransaction::Invoke(ExternalInvokeTransaction::V3(invoke_transaction))
}

#[rstest::rstest]
#[case("DECLARE", create_external_declare_transaction_v3())]
#[case("DEPLOY_ACCOUNT", create_external_deploy_account_transaction_v3())]
#[case("INVOKE", create_external_invoke_transaction_v3())]
#[tokio::test]
async fn test_add_transaction(#[case] json_file_path: &str, #[case] expected_response: &str) {
let file = File::open(json_file_path).unwrap();
let reader = BufReader::new(file);
let transaction: ExternalTransaction = serde_json::from_reader(reader).unwrap();
let response = add_transaction(transaction.into()).await.into_response();
async fn test_add_transaction(
#[case] expected_response: &str,
#[case] transaction_instance: ExternalTransaction,
) {
let transaction_json: Json<ExternalTransaction> = Json(transaction_instance);
let response = add_transaction(transaction_json).await.into_response();
let response_bytes = response.into_body().collect().await.unwrap().to_bytes();

assert_eq!(
&String::from_utf8(response_bytes.to_vec()).unwrap(),
expected_response
Expand Down
94 changes: 0 additions & 94 deletions crates/gateway/src/json_files_for_testing/declare_v3.json

This file was deleted.

37 changes: 0 additions & 37 deletions crates/gateway/src/json_files_for_testing/deploy_account_v3.json

This file was deleted.

37 changes: 0 additions & 37 deletions crates/gateway/src/json_files_for_testing/invoke_v3.json

This file was deleted.

0 comments on commit d233110

Please sign in to comment.