//! Tests for the storage-accounting queries. //! //! Extracted from the former `db.rs` inline test module. use crate::db::*; /// The need blob sync computes is a union over synced VFSes, not a sum. /// /// The distinction is the whole reason `synced_storage_stats` exists /// separately from summing `vfs_storage_stats`: a sample placed in two /// synced VFSes uploads once, because blobs are content-addressed. Summing /// would report 300 here and propose a cap for storage nobody needs. #[test] fn synced_storage_counts_a_shared_sample_once() { let db = Database::open_in_memory().unwrap(); db.conn() .execute_batch( "INSERT INTO samples (hash, original_name, file_extension, file_size, import_date, last_modified) VALUES ('shared', 's.wav', 'wav', 100, 0, 0), ('only_a', 'a.wav', 'wav', 50, 0, 0), ('unsynced', 'u.wav', 'wav', 999, 0, 0); INSERT INTO vfs (id, name, created_at, modified_at, sync_files) VALUES (1, 'A', 0, 0, 1), (2, 'B', 0, 0, 1), (3, 'Off', 0, 0, 0); INSERT INTO vfs_nodes (vfs_id, parent_id, name, node_type, sample_hash, created_at) VALUES (1, NULL, 's.wav', 'sample', 'shared', 0), (2, NULL, 's.wav', 'sample', 'shared', 0), (1, NULL, 'a.wav', 'sample', 'only_a', 0), (3, NULL, 'u.wav', 'sample', 'unsynced', 0);", ) .unwrap(); let (count, bytes) = db.synced_storage_stats().unwrap(); assert_eq!( count, 2, "the shared sample counts once, the unsynced not at all" ); assert_eq!(bytes, 150, "100 + 50; summing the two VFSes would say 250"); // The per-VFS figures are what a naive sum would have used. assert_eq!(db.vfs_storage_stats(1).unwrap(), (2, 150)); assert_eq!(db.vfs_storage_stats(2).unwrap(), (1, 100)); } /// Nothing set to sync means nothing would upload, and the honest answer is /// zero rather than the whole library. A default vault has `sync_files = 0` /// on every VFS, so this is the state a new user is actually in. #[test] fn synced_storage_is_zero_when_no_vfs_syncs_files() { let db = Database::open_in_memory().unwrap(); db.conn() .execute_batch( "INSERT INTO samples (hash, original_name, file_extension, file_size, import_date, last_modified) VALUES ('a', 'a.wav', 'wav', 100, 0, 0); INSERT INTO vfs (id, name, created_at, modified_at, sync_files) VALUES (1, 'A', 0, 0, 0); INSERT INTO vfs_nodes (vfs_id, parent_id, name, node_type, sample_hash, created_at) VALUES (1, NULL, 'a.wav', 'sample', 'a', 0);", ) .unwrap(); assert_eq!(db.synced_storage_stats().unwrap(), (0, 0)); assert_eq!( db.storage_stats().unwrap(), (1, 100), "the library is not empty" ); }