memory.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //! In-memory database that is provided by the `cdk-sqlite` crate, mainly for testing purposes.
  2. use std::collections::HashMap;
  3. use cdk_common::database::{
  4. self, MintDatabase, MintKeysDatabase, MintProofsDatabase, MintQuotesDatabase,
  5. };
  6. use cdk_common::mint::{self, MintKeySetInfo, MintQuote};
  7. use cdk_common::nuts::{CurrencyUnit, Id, Proofs};
  8. use cdk_common::MintInfo;
  9. use super::MintSqliteDatabase;
  10. /// Creates a new in-memory [`MintSqliteDatabase`] instance
  11. pub async fn empty() -> Result<MintSqliteDatabase, database::Error> {
  12. #[cfg(not(feature = "sqlcipher"))]
  13. let db = MintSqliteDatabase::new(":memory:").await?;
  14. #[cfg(feature = "sqlcipher")]
  15. let db = MintSqliteDatabase::new(":memory:", "memory".to_string()).await?;
  16. Ok(db)
  17. }
  18. /// Creates a new in-memory [`MintSqliteDatabase`] instance with the given state
  19. #[allow(clippy::too_many_arguments)]
  20. pub async fn new_with_state(
  21. active_keysets: HashMap<CurrencyUnit, Id>,
  22. keysets: Vec<MintKeySetInfo>,
  23. mint_quotes: Vec<MintQuote>,
  24. melt_quotes: Vec<mint::MeltQuote>,
  25. pending_proofs: Proofs,
  26. spent_proofs: Proofs,
  27. mint_info: MintInfo,
  28. ) -> Result<MintSqliteDatabase, database::Error> {
  29. let db = empty().await?;
  30. for active_keyset in active_keysets {
  31. db.set_active_keyset(active_keyset.0, active_keyset.1)
  32. .await?;
  33. }
  34. for keyset in keysets {
  35. db.add_keyset_info(keyset).await?;
  36. }
  37. for quote in mint_quotes {
  38. db.add_mint_quote(quote).await?;
  39. }
  40. for quote in melt_quotes {
  41. db.add_melt_quote(quote).await?;
  42. }
  43. db.add_proofs(pending_proofs, None).await?;
  44. db.add_proofs(spent_proofs, None).await?;
  45. db.set_mint_info(mint_info).await?;
  46. Ok(db)
  47. }