Skip to main content

max / audiofiles

2.9 KB · 70 lines History Blame Raw
1 //! Tests for the storage-accounting queries.
2 //!
3 //! Extracted from the former `db.rs` inline test module.
4
5 use crate::db::*;
6
7 /// The need blob sync computes is a union over synced VFSes, not a sum.
8 ///
9 /// The distinction is the whole reason `synced_storage_stats` exists
10 /// separately from summing `vfs_storage_stats`: a sample placed in two
11 /// synced VFSes uploads once, because blobs are content-addressed. Summing
12 /// would report 300 here and propose a cap for storage nobody needs.
13 #[test]
14 fn synced_storage_counts_a_shared_sample_once() {
15 let db = Database::open_in_memory().unwrap();
16 db.conn()
17 .execute_batch(
18 "INSERT INTO samples
19 (hash, original_name, file_extension, file_size, import_date, last_modified)
20 VALUES ('shared', 's.wav', 'wav', 100, 0, 0),
21 ('only_a', 'a.wav', 'wav', 50, 0, 0),
22 ('unsynced', 'u.wav', 'wav', 999, 0, 0);
23 INSERT INTO vfs (id, name, created_at, modified_at, sync_files)
24 VALUES (1, 'A', 0, 0, 1), (2, 'B', 0, 0, 1), (3, 'Off', 0, 0, 0);
25 INSERT INTO vfs_nodes (vfs_id, parent_id, name, node_type, sample_hash, created_at)
26 VALUES (1, NULL, 's.wav', 'sample', 'shared', 0),
27 (2, NULL, 's.wav', 'sample', 'shared', 0),
28 (1, NULL, 'a.wav', 'sample', 'only_a', 0),
29 (3, NULL, 'u.wav', 'sample', 'unsynced', 0);",
30 )
31 .unwrap();
32
33 let (count, bytes) = db.synced_storage_stats().unwrap();
34 assert_eq!(
35 count, 2,
36 "the shared sample counts once, the unsynced not at all"
37 );
38 assert_eq!(bytes, 150, "100 + 50; summing the two VFSes would say 250");
39
40 // The per-VFS figures are what a naive sum would have used.
41 assert_eq!(db.vfs_storage_stats(1).unwrap(), (2, 150));
42 assert_eq!(db.vfs_storage_stats(2).unwrap(), (1, 100));
43 }
44
45 /// Nothing set to sync means nothing would upload, and the honest answer is
46 /// zero rather than the whole library. A default vault has `sync_files = 0`
47 /// on every VFS, so this is the state a new user is actually in.
48 #[test]
49 fn synced_storage_is_zero_when_no_vfs_syncs_files() {
50 let db = Database::open_in_memory().unwrap();
51 db.conn()
52 .execute_batch(
53 "INSERT INTO samples
54 (hash, original_name, file_extension, file_size, import_date, last_modified)
55 VALUES ('a', 'a.wav', 'wav', 100, 0, 0);
56 INSERT INTO vfs (id, name, created_at, modified_at, sync_files)
57 VALUES (1, 'A', 0, 0, 0);
58 INSERT INTO vfs_nodes (vfs_id, parent_id, name, node_type, sample_hash, created_at)
59 VALUES (1, NULL, 'a.wav', 'sample', 'a', 0);",
60 )
61 .unwrap();
62
63 assert_eq!(db.synced_storage_stats().unwrap(), (0, 0));
64 assert_eq!(
65 db.storage_stats().unwrap(),
66 (1, 100),
67 "the library is not empty"
68 );
69 }
70