feat(gpodder_sqlite): switch to on-disk sqlite for tests and benches

main
Jef Roosens 2025-03-20 19:19:54 +01:00
parent d329a0e61c
commit 5f57d85584
No known key found for this signature in database
GPG Key ID: 21FD3D77D56BAF49
5 changed files with 51 additions and 12 deletions

View File

@ -1,8 +1,17 @@
use std::path::PathBuf;
use gpodder::{AuthStore, User}; use gpodder::{AuthStore, User};
use gpodder_sqlite::SqliteRepository; use gpodder_sqlite::SqliteRepository;
use rand::{distributions::Alphanumeric, Rng};
pub fn setup() -> (SqliteRepository, Vec<User>) { pub fn setup() -> (PathBuf, SqliteRepository, Vec<User>) {
let store = SqliteRepository::in_memory().unwrap(); let fname: String = rand::thread_rng()
.sample_iter(Alphanumeric)
.take(10)
.map(char::from)
.collect();
let path = std::env::temp_dir().join(fname);
let store = SqliteRepository::from_path(&path).unwrap();
let mut users = Vec::new(); let mut users = Vec::new();
for i in 0..1000 { for i in 0..1000 {
@ -12,5 +21,9 @@ pub fn setup() -> (SqliteRepository, Vec<User>) {
users.push(store.insert_user(&username, &password_hash).unwrap()); users.push(store.insert_user(&username, &password_hash).unwrap());
} }
(store, users) (path, store, users)
}
pub fn teardown(path: PathBuf) {
let _ = std::fs::remove_file(path);
} }

View File

@ -4,7 +4,7 @@ use criterion::{criterion_group, criterion_main, Criterion};
use gpodder::{DevicePatch, DeviceRepository}; use gpodder::{DevicePatch, DeviceRepository};
pub fn bench_devices_for_user(c: &mut Criterion) { pub fn bench_devices_for_user(c: &mut Criterion) {
let (store, users) = common::setup(); let (path, store, users) = common::setup();
for i in 0..100000 { for i in 0..100000 {
store store
@ -22,6 +22,8 @@ pub fn bench_devices_for_user(c: &mut Criterion) {
c.bench_function("devices for user", |b| { c.bench_function("devices for user", |b| {
b.iter(|| store.devices_for_user(&users[0]).unwrap()) b.iter(|| store.devices_for_user(&users[0]).unwrap())
}); });
common::teardown(path);
} }
criterion_group!(devices, bench_devices_for_user); criterion_group!(devices, bench_devices_for_user);

View File

@ -28,7 +28,7 @@ fn test_create_user() {
#[test] #[test]
fn test_create_session() { fn test_create_session() {
let (store, users) = common::setup(); let (path, store, users) = common::setup();
let session = store.get_session(123).expect("operation shouldn't fail"); let session = store.get_session(123).expect("operation shouldn't fail");
assert_eq!(session, None); assert_eq!(session, None);
@ -46,11 +46,13 @@ fn test_create_session() {
let session = store.get_session(123).expect("operation shouldn't fail"); let session = store.get_session(123).expect("operation shouldn't fail");
assert_eq!(session, Some(new_session)); assert_eq!(session, Some(new_session));
common::teardown(path);
} }
#[test] #[test]
fn test_remove_session() { fn test_remove_session() {
let (store, users) = common::setup(); let (path, store, users) = common::setup();
let new_session = Session { let new_session = Session {
id: 123, id: 123,
@ -74,11 +76,13 @@ fn test_remove_session() {
.get_session(123) .get_session(123)
.expect("get session shouldn't fail") .expect("get session shouldn't fail")
.is_none()); .is_none());
common::teardown(path);
} }
#[test] #[test]
fn test_refresh_session() { fn test_refresh_session() {
let (store, users) = common::setup(); let (path, store, users) = common::setup();
let mut new_session = Session { let mut new_session = Session {
id: 123, id: 123,
@ -101,11 +105,13 @@ fn test_refresh_session() {
store.get_session(123).expect("get session shouldn't fail"), store.get_session(123).expect("get session shouldn't fail"),
Some(new_session) Some(new_session)
); );
common::teardown(path);
} }
#[test] #[test]
fn test_remove_old_sessions() { fn test_remove_old_sessions() {
let (store, users) = common::setup(); let (path, store, users) = common::setup();
let now = chrono::Utc::now().trunc_subsecs(0); let now = chrono::Utc::now().trunc_subsecs(0);
let timestamps = [ let timestamps = [
@ -141,4 +147,6 @@ fn test_remove_old_sessions() {
user: users[0].clone(), user: users[0].clone(),
}) })
); );
common::teardown(path);
} }

View File

@ -1,8 +1,18 @@
use gpodder::{AuthStore, User}; use gpodder::{AuthStore, User};
use gpodder_sqlite::SqliteRepository; use gpodder_sqlite::SqliteRepository;
pub fn setup() -> (SqliteRepository, Vec<User>) { use rand::{distributions::Alphanumeric, Rng};
let store = SqliteRepository::in_memory().unwrap();
use std::path::PathBuf;
pub fn setup() -> (PathBuf, SqliteRepository, Vec<User>) {
let fname: String = rand::thread_rng()
.sample_iter(Alphanumeric)
.take(10)
.map(char::from)
.collect();
let path = std::env::temp_dir().join(fname);
let store = SqliteRepository::from_path(&path).unwrap();
let mut users = Vec::new(); let mut users = Vec::new();
for i in 0..4 { for i in 0..4 {
@ -12,5 +22,9 @@ pub fn setup() -> (SqliteRepository, Vec<User>) {
users.push(store.insert_user(&username, &password_hash).unwrap()); users.push(store.insert_user(&username, &password_hash).unwrap());
} }
(store, users) (path, store, users)
}
pub fn teardown(path: PathBuf) {
let _ = std::fs::remove_file(path);
} }

View File

@ -4,7 +4,7 @@ use gpodder::{DevicePatch, DeviceRepository, DeviceType};
#[test] #[test]
fn test_insert_devices() { fn test_insert_devices() {
let (store, users) = common::setup(); let (path, store, users) = common::setup();
store.devices_for_user(&users[0]).unwrap(); store.devices_for_user(&users[0]).unwrap();
@ -38,4 +38,6 @@ fn test_insert_devices() {
assert_eq!(devices[0].caption, "caption1"); assert_eq!(devices[0].caption, "caption1");
assert_eq!(devices[0].r#type, DeviceType::Other); assert_eq!(devices[0].r#type, DeviceType::Other);
common::teardown(path);
} }