Skip to main content

max / audiofiles

2.0 KB · 64 lines History Blame Raw
1 //! Shared test utilities: helpers to insert fake sample rows for use in unit tests.
2
3 use crate::db::Database;
4 use crate::error::unix_now;
5 use crate::id_types::{NodeId, VfsId};
6
7 /// Insert a fake sample row for testing (no actual file on disk).
8 pub fn insert_fake_sample(db: &Database, hash: &str) {
9 let now = unix_now();
10 db.conn()
11 .execute(
12 "INSERT OR IGNORE INTO samples (hash, original_name, file_extension, file_size, import_date, last_modified)
13 VALUES (?1, ?2, 'wav', 100, ?3, ?4)",
14 rusqlite::params![hash, format!("{hash}.wav"), now, now],
15 )
16 .unwrap();
17 }
18
19 /// Insert a fake sample with analysis data and a VFS link in one call.
20 ///
21 /// Combines `insert_fake_sample`, `save_analysis`, and `create_sample_link` for
22 /// tests that need samples with BPM/key/duration/classification populated.
23 #[allow(clippy::too_many_arguments)]
24 pub fn insert_sample_with_analysis(
25 db: &Database,
26 hash: &str,
27 name: &str,
28 vfs_id: VfsId,
29 bpm: Option<f64>,
30 key: Option<&str>,
31 duration: Option<f64>,
32 classification: Option<&str>,
33 ) -> NodeId {
34 insert_fake_sample(db, hash);
35
36 let result = crate::analysis::AnalysisResult {
37 hash: hash.to_string(),
38 duration: duration.unwrap_or(1.0),
39 sample_rate: 44100,
40 channels: 1,
41 peak_db: Some(-3.0),
42 rms_db: None,
43 lufs: None,
44 bpm,
45 musical_key: key.map(String::from),
46 is_loop: None,
47 spectral_centroid: None,
48 spectral_flatness: None,
49 spectral_rolloff: None,
50 zero_crossing_rate: None,
51 onset_strength: None,
52 classification: classification.map(|c| c.parse().unwrap()),
53 fingerprint: None,
54 spectral_bandwidth: None,
55 centroid_variance: None,
56 crest_factor: None,
57 attack_time: None,
58 classification_confidence: None,
59 };
60 crate::analysis::save_analysis(db, &result).unwrap();
61
62 crate::vfs::create_sample_link(db, vfs_id, None, name, hash).unwrap()
63 }
64