feat(gpodder_sqlite): switch to on-disk sqlite for tests and benches
parent
d329a0e61c
commit
5f57d85584
|
@ -1,8 +1,17 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use gpodder::{AuthStore, User};
|
||||
use gpodder_sqlite::SqliteRepository;
|
||||
use rand::{distributions::Alphanumeric, Rng};
|
||||
|
||||
pub fn setup() -> (SqliteRepository, Vec<User>) {
|
||||
let store = SqliteRepository::in_memory().unwrap();
|
||||
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();
|
||||
|
||||
for i in 0..1000 {
|
||||
|
@ -12,5 +21,9 @@ pub fn setup() -> (SqliteRepository, Vec<User>) {
|
|||
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);
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ use criterion::{criterion_group, criterion_main, Criterion};
|
|||
use gpodder::{DevicePatch, DeviceRepository};
|
||||
|
||||
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 {
|
||||
store
|
||||
|
@ -22,6 +22,8 @@ pub fn bench_devices_for_user(c: &mut Criterion) {
|
|||
c.bench_function("devices for user", |b| {
|
||||
b.iter(|| store.devices_for_user(&users[0]).unwrap())
|
||||
});
|
||||
|
||||
common::teardown(path);
|
||||
}
|
||||
|
||||
criterion_group!(devices, bench_devices_for_user);
|
||||
|
|
|
@ -28,7 +28,7 @@ fn test_create_user() {
|
|||
|
||||
#[test]
|
||||
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");
|
||||
assert_eq!(session, None);
|
||||
|
@ -46,11 +46,13 @@ fn test_create_session() {
|
|||
|
||||
let session = store.get_session(123).expect("operation shouldn't fail");
|
||||
assert_eq!(session, Some(new_session));
|
||||
|
||||
common::teardown(path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_remove_session() {
|
||||
let (store, users) = common::setup();
|
||||
let (path, store, users) = common::setup();
|
||||
|
||||
let new_session = Session {
|
||||
id: 123,
|
||||
|
@ -74,11 +76,13 @@ fn test_remove_session() {
|
|||
.get_session(123)
|
||||
.expect("get session shouldn't fail")
|
||||
.is_none());
|
||||
|
||||
common::teardown(path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_refresh_session() {
|
||||
let (store, users) = common::setup();
|
||||
let (path, store, users) = common::setup();
|
||||
|
||||
let mut new_session = Session {
|
||||
id: 123,
|
||||
|
@ -101,11 +105,13 @@ fn test_refresh_session() {
|
|||
store.get_session(123).expect("get session shouldn't fail"),
|
||||
Some(new_session)
|
||||
);
|
||||
|
||||
common::teardown(path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
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 timestamps = [
|
||||
|
@ -141,4 +147,6 @@ fn test_remove_old_sessions() {
|
|||
user: users[0].clone(),
|
||||
})
|
||||
);
|
||||
|
||||
common::teardown(path);
|
||||
}
|
||||
|
|
|
@ -1,8 +1,18 @@
|
|||
use gpodder::{AuthStore, User};
|
||||
use gpodder_sqlite::SqliteRepository;
|
||||
|
||||
pub fn setup() -> (SqliteRepository, Vec<User>) {
|
||||
let store = SqliteRepository::in_memory().unwrap();
|
||||
use rand::{distributions::Alphanumeric, Rng};
|
||||
|
||||
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();
|
||||
|
||||
for i in 0..4 {
|
||||
|
@ -12,5 +22,9 @@ pub fn setup() -> (SqliteRepository, Vec<User>) {
|
|||
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);
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ use gpodder::{DevicePatch, DeviceRepository, DeviceType};
|
|||
|
||||
#[test]
|
||||
fn test_insert_devices() {
|
||||
let (store, users) = common::setup();
|
||||
let (path, store, users) = common::setup();
|
||||
|
||||
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].r#type, DeviceType::Other);
|
||||
|
||||
common::teardown(path);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue