This commit is contained in:
Mateusz Gruszczyński
2026-08-30 13:39:29 +02:00
parent 3e950ab5fa
commit 5c05eddb8f
83 changed files with 10130 additions and 9954 deletions
+70
View File
@@ -0,0 +1,70 @@
#[cfg(test)]
mod tests {
use super::*;
use crate::models::{ApiTokenInfo, Device, HaReading, Reading};
#[test]
fn history_compaction_keeps_one_sample_per_old_bucket() {
let dir = tempfile::tempdir().unwrap();
let db = Db::open(&dir.path().join("compact.db")).unwrap();
let device = Device::simulated_default();
db.save_device(&device).unwrap();
let seconds = (Utc::now().timestamp() - 2 * 86_400) / 600 * 600;
let base = DateTime::<Utc>::from_timestamp(seconds, 0).unwrap();
for offset in [10_i64, 20_i64] {
db.add_reading(&Reading {
id: 0, device_id: device.id.clone(), timestamp: base + Duration::seconds(offset),
indoor_temperature: Some(22.0), outdoor_temperature: None, target_temperature: 23.0,
power: true, source: "gree".into(),
}).unwrap();
}
assert_eq!(db.history_counts().unwrap().0, 2);
assert_eq!(db.compact_history(30).unwrap(), 1);
assert_eq!(db.history_counts().unwrap().0, 1);
}
#[test]
fn sqlite_round_trip() {
let dir = tempfile::tempdir().unwrap();
let db = Db::open(&dir.path().join("test.db")).unwrap();
let device = Device::simulated_default();
db.save_device(&device).unwrap();
let loaded = db.get_device(&device.id).unwrap().unwrap();
assert_eq!(loaded.mac, device.mac);
assert_eq!(db.list_devices().unwrap().len(), 1);
db.log_event("info", "test", "ok", &serde_json::json!({"a":1})).unwrap();
assert_eq!(db.list_events(10).unwrap().len(), 1);
{
let conn = db.lock().unwrap();
conn.execute(queries::INSERT_EVENT, rusqlite::params![(Utc::now() - Duration::days(40)).to_rfc3339(), "info", "old", "old", "{}"] ).unwrap();
}
assert_eq!(db.prune_events(30).unwrap(), 1);
assert_eq!(db.list_events(10).unwrap().len(), 1);
let access_token = ApiTokenInfo {
id: "token-1".into(),
name: "Home Assistant".into(),
token_prefix: "gree_controller_test...".into(),
created_at: Utc::now(),
};
db.save_api_token(&access_token, "test-hash").unwrap();
assert!(db.api_token_exists("test-hash").unwrap());
assert_eq!(db.list_api_tokens().unwrap().len(), 1);
assert!(db.delete_api_token(&access_token.id).unwrap());
assert!(!db.api_token_exists("test-hash").unwrap());
let now = Utc::now();
db.add_reading(&Reading {
id: 0, device_id: device.id.clone(), timestamp: now.clone(), indoor_temperature: Some(22.5),
outdoor_temperature: Some(31.0), target_temperature: 23.0, power: true, source: "gree".into(),
}).unwrap();
assert_eq!(db.list_device_history(Some(&device.id), now.clone() - Duration::minutes(1), 30, 100).unwrap().len(), 1);
db.add_ha_reading_if_due(&HaReading {
id: 0, entity_id: "sensor.room".into(), zone_id: Some("zone-room".into()), kind: "room".into(),
timestamp: now.clone(), temperature: 22.1,
}, 15).unwrap();
assert_eq!(db.list_ha_history(Some("sensor.room"), now.clone() - Duration::minutes(1), 30, 100).unwrap().len(), 1);
assert_eq!(db.history_counts().unwrap(), (1, 0, 1));
}
}