|
| 1 | +#[cfg(test)] |
| 2 | +mod tests { |
| 3 | + use crate::data_acces::transaction::TransactionVinDb; |
| 4 | + use crate::data_acces::transaction_vout::TransactionVoutDb; |
| 5 | + use crate::database::db_manger::{ColumnFamilyOperations, RocksDB}; |
| 6 | + use crate::model::transaction::{Transaction, TransactionBlock}; |
| 7 | + use crate::model::transaction_vout::{TransactionVout, TransactionVoutScript}; |
| 8 | + use tempfile::tempdir; |
| 9 | + use tokio::task; |
| 10 | + |
| 11 | + fn setup_test_db() -> TransactionVinDb { |
| 12 | + let temp_dir = tempdir().unwrap(); |
| 13 | + let db = RocksDB::new(temp_dir.path().to_str().unwrap()).unwrap(); |
| 14 | + TransactionVinDb { db } |
| 15 | + } |
| 16 | + |
| 17 | + // Sample transaction for testing |
| 18 | + fn sample_transaction(id: &str) -> Transaction { |
| 19 | + Transaction { |
| 20 | + id: id.to_string(), |
| 21 | + order: 1, |
| 22 | + block: TransactionBlock { |
| 23 | + hash: "sample_hash".to_string(), |
| 24 | + height: 123, |
| 25 | + time: 1000, |
| 26 | + median_time: 1000, |
| 27 | + }, |
| 28 | + txid: id.to_string(), |
| 29 | + hash: "sample_transaction_hash".to_string(), |
| 30 | + version: 1, |
| 31 | + size: 200, |
| 32 | + v_size: 200, |
| 33 | + weight: 300, |
| 34 | + total_vout_value: "1000".to_string(), |
| 35 | + lock_time: 0, |
| 36 | + vin_count: 2, |
| 37 | + vout_count: 2, |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + fn sample_transaction_Vout() { |
| 42 | + // Instantiate a sample TransactionVoutScript |
| 43 | + let sample_script = TransactionVoutScript { |
| 44 | + hex: "76a91488ac".to_string(), // Sample hex string |
| 45 | + r#type: "pubkeyhash".to_string(), // Sample type |
| 46 | + }; |
| 47 | + |
| 48 | + // Instantiate a sample TransactionVout |
| 49 | + let sample_vout = TransactionVout { |
| 50 | + id: "vout1".to_string(), // Sample ID |
| 51 | + txid: "tx12345".to_string(), // Sample transaction ID |
| 52 | + n: 0, // Sample index |
| 53 | + value: "0.0001".to_string(), // Sample value in BTC or similar currency |
| 54 | + token_id: 123, // Sample token ID |
| 55 | + script: sample_script, // Use the sample script created above |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + #[tokio::test] |
| 60 | + async fn test_store_transaction() { |
| 61 | + let txn_vin_db = setup_test_db(); |
| 62 | + |
| 63 | + let test_transaction = sample_transaction("tx1"); |
| 64 | + |
| 65 | + let result = txn_vin_db.store(test_transaction).await; |
| 66 | + assert!(result.is_ok()); |
| 67 | + } |
| 68 | + |
| 69 | + #[tokio::test] |
| 70 | + async fn test_store_get_transaction() { |
| 71 | + let txn_vin_db = setup_test_db(); |
| 72 | + |
| 73 | + let test_transaction = sample_transaction("tx1"); |
| 74 | + let trx_id = test_transaction.id.clone(); |
| 75 | + |
| 76 | + let result = txn_vin_db.store(test_transaction.clone()).await; |
| 77 | + assert!(result.is_ok()); |
| 78 | + |
| 79 | + let result = txn_vin_db.get(trx_id).await.unwrap().unwrap(); |
| 80 | + assert_eq!(test_transaction, result); |
| 81 | + } |
| 82 | + |
| 83 | + #[tokio::test] |
| 84 | + async fn test_store_get_delete_transaction() { |
| 85 | + let txn_vin_db = setup_test_db(); |
| 86 | + |
| 87 | + let test_transaction = sample_transaction("tx1"); |
| 88 | + let trx_id = test_transaction.id.clone(); |
| 89 | + |
| 90 | + let result = txn_vin_db.store(test_transaction.clone()).await; |
| 91 | + assert!(result.is_ok()); |
| 92 | + |
| 93 | + let result = txn_vin_db.get(trx_id.clone()).await.unwrap().unwrap(); |
| 94 | + assert_eq!(test_transaction, result); |
| 95 | + |
| 96 | + let result = txn_vin_db.delete(trx_id).await; |
| 97 | + assert!(result.is_ok()); |
| 98 | + } |
| 99 | +} |
0 commit comments