Просмотр исходного кода

test: add PaymentMethod parsing and db read/write tests

vnprc 2 месяцев назад
Родитель
Сommit
714022fe14
2 измененных файлов с 108 добавлено и 0 удалено
  1. 53 0
      crates/cashu/src/nuts/nut00/mod.rs
  2. 55 0
      crates/cdk-sqlite/src/wallet/mod.rs

+ 53 - 0
crates/cashu/src/nuts/nut00/mod.rs

@@ -985,4 +985,57 @@ mod tests {
         let deserialized: CurrencyUnit = serde_json::from_str(&serialized).unwrap();
         assert_eq!(unit, deserialized)
     }
+
+    #[test]
+    fn test_payment_method_parsing() {
+        // Test standard variants
+        assert_eq!(
+            PaymentMethod::from_str("bolt11").unwrap(),
+            PaymentMethod::Bolt11
+        );
+        assert_eq!(
+            PaymentMethod::from_str("BOLT11").unwrap(),
+            PaymentMethod::Bolt11
+        );
+        assert_eq!(
+            PaymentMethod::from_str("Bolt11").unwrap(),
+            PaymentMethod::Bolt11
+        );
+
+        assert_eq!(
+            PaymentMethod::from_str("bolt12").unwrap(),
+            PaymentMethod::Bolt12
+        );
+        assert_eq!(
+            PaymentMethod::from_str("BOLT12").unwrap(),
+            PaymentMethod::Bolt12
+        );
+        assert_eq!(
+            PaymentMethod::from_str("Bolt12").unwrap(),
+            PaymentMethod::Bolt12
+        );
+
+        // Test custom variants
+        assert_eq!(
+            PaymentMethod::from_str("custom").unwrap(),
+            PaymentMethod::Custom("custom".to_string())
+        );
+        assert_eq!(
+            PaymentMethod::from_str("CUSTOM").unwrap(),
+            PaymentMethod::Custom("custom".to_string())
+        );
+
+        // Test serialization/deserialization consistency
+        let methods = vec![
+            PaymentMethod::Bolt11,
+            PaymentMethod::Bolt12,
+            PaymentMethod::Custom("test".to_string()),
+        ];
+
+        for method in methods {
+            let serialized = serde_json::to_string(&method).unwrap();
+            let deserialized: PaymentMethod = serde_json::from_str(&serialized).unwrap();
+            assert_eq!(method, deserialized);
+        }
+    }
 }

+ 55 - 0
crates/cdk-sqlite/src/wallet/mod.rs

@@ -306,4 +306,59 @@ mod tests {
         assert_eq!(retrieved_dleq.s.to_string(), s.to_string());
         assert_eq!(retrieved_dleq.r.to_string(), r.to_string());
     }
+
+    #[tokio::test]
+    async fn test_mint_quote_payment_method_read_and_write() {
+        use cdk_common::mint_url::MintUrl;
+        use cdk_common::nuts::{CurrencyUnit, MintQuoteState, PaymentMethod};
+        use cdk_common::wallet::MintQuote;
+        use cdk_common::Amount;
+
+        // Create a temporary database
+        let path = std::env::temp_dir().to_path_buf().join(format!(
+            "cdk-test-migration-{}.sqlite",
+            uuid::Uuid::new_v4()
+        ));
+
+        #[cfg(feature = "sqlcipher")]
+        let db = WalletSqliteDatabase::new((path, "password".to_string()))
+            .await
+            .unwrap();
+
+        #[cfg(not(feature = "sqlcipher"))]
+        let db = WalletSqliteDatabase::new(path).await.unwrap();
+
+        // Test PaymentMethod variants
+        let mint_url = MintUrl::from_str("https://example.com").unwrap();
+        let payment_methods = vec![
+            PaymentMethod::Bolt11,
+            PaymentMethod::Bolt12,
+            PaymentMethod::Custom("custom".to_string()),
+        ];
+
+        for (i, payment_method) in payment_methods.iter().enumerate() {
+            let quote = MintQuote {
+                id: format!("test_quote_{}", i),
+                mint_url: mint_url.clone(),
+                amount: Some(Amount::from(100)),
+                unit: CurrencyUnit::Sat,
+                request: "test_request".to_string(),
+                state: MintQuoteState::Unpaid,
+                expiry: 1000000000,
+                secret_key: None,
+                payment_method: payment_method.clone(),
+                amount_issued: Amount::from(0),
+                amount_paid: Amount::from(0),
+            };
+
+            // Store the quote
+            db.add_mint_quote(quote.clone()).await.unwrap();
+
+            // Retrieve and verify
+            let retrieved = db.get_mint_quote(&quote.id).await.unwrap().unwrap();
+            assert_eq!(retrieved.payment_method, *payment_method);
+            assert_eq!(retrieved.amount_issued, Amount::from(0));
+            assert_eq!(retrieved.amount_paid, Amount::from(0));
+        }
+    }
 }