feat(gpodder_sqlite): set up testing
parent
b44a47fefd
commit
705b347775
|
@ -1,12 +1,13 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct User {
|
||||
pub id: i64,
|
||||
pub username: String,
|
||||
pub password_hash: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum DeviceType {
|
||||
Desktop,
|
||||
Laptop,
|
||||
|
@ -15,6 +16,7 @@ pub enum DeviceType {
|
|||
Other,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Device {
|
||||
pub id: String,
|
||||
pub caption: String,
|
||||
|
@ -22,11 +24,13 @@ pub struct Device {
|
|||
pub subscriptions: i64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct DevicePatch {
|
||||
pub caption: Option<String>,
|
||||
pub r#type: Option<DeviceType>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum EpisodeActionType {
|
||||
Download,
|
||||
Play {
|
||||
|
@ -38,6 +42,7 @@ pub enum EpisodeActionType {
|
|||
New,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct EpisodeAction {
|
||||
pub podcast: String,
|
||||
pub episode: String,
|
||||
|
@ -47,12 +52,14 @@ pub struct EpisodeAction {
|
|||
pub action: EpisodeActionType,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Session {
|
||||
pub id: i64,
|
||||
pub last_seen: DateTime<Utc>,
|
||||
pub user: User,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Subscription {
|
||||
pub url: String,
|
||||
pub time_changed: DateTime<Utc>,
|
||||
|
|
|
@ -94,3 +94,16 @@ pub fn initialize_db(path: impl AsRef<Path>, run_migrations: bool) -> Result<DbP
|
|||
|
||||
Ok(pool)
|
||||
}
|
||||
|
||||
pub fn initialize_db_in_memory(run_migrations: bool) -> Result<DbPool, DbError> {
|
||||
let manager = ConnectionManager::<SqliteConnection>::new(":memory:");
|
||||
let pool = Pool::builder()
|
||||
.connection_customizer(Box::new(AddQueryDebugLogs))
|
||||
.build(manager)?;
|
||||
|
||||
if run_migrations {
|
||||
pool.get()?.run_pending_migrations(MIGRATIONS).unwrap();
|
||||
}
|
||||
|
||||
Ok(pool)
|
||||
}
|
||||
|
|
|
@ -24,4 +24,10 @@ impl SqliteRepository {
|
|||
|
||||
Ok(Self { pool })
|
||||
}
|
||||
|
||||
pub fn in_memory() -> Result<Self, gpodder::AuthErr> {
|
||||
let pool = super::initialize_db_in_memory(true)?;
|
||||
|
||||
Ok(Self { pool })
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
mod common;
|
||||
|
||||
use gpodder::{AuthStore, Session};
|
||||
use gpodder_sqlite::SqliteRepository;
|
||||
|
||||
#[test]
|
||||
fn test_create_user() {
|
||||
let store = SqliteRepository::in_memory().unwrap();
|
||||
|
||||
let user = store.get_user("test1");
|
||||
assert!(user.is_ok());
|
||||
assert_eq!(user.unwrap(), None);
|
||||
|
||||
let new_user = store.insert_user("test1", "dummyhash");
|
||||
|
||||
assert!(new_user.is_ok());
|
||||
|
||||
let new_user = new_user.unwrap();
|
||||
assert_eq!(new_user.username, "test1");
|
||||
assert_eq!(new_user.password_hash, "dummyhash");
|
||||
|
||||
let user = store.get_user("test1");
|
||||
|
||||
assert!(user.is_ok());
|
||||
assert_eq!(user.unwrap(), Some(new_user));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_session() {
|
||||
let (store, users) = common::setup();
|
||||
|
||||
let session = store.get_session(123).expect("operation shouldn't fail");
|
||||
assert_eq!(session, None);
|
||||
|
||||
let new_session = Session {
|
||||
id: 123,
|
||||
last_seen: chrono::Utc::now(),
|
||||
user: users[0].clone(),
|
||||
};
|
||||
|
||||
store
|
||||
.insert_session(&new_session)
|
||||
.expect("insert session shouldn't fail");
|
||||
|
||||
let session = store.get_session(123).expect("operation shouldn't fail");
|
||||
assert_eq!(session, Some(new_session));
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
use gpodder::{AuthStore, User};
|
||||
use gpodder_sqlite::SqliteRepository;
|
||||
|
||||
pub fn setup() -> (SqliteRepository, Vec<User>) {
|
||||
let store = SqliteRepository::in_memory().unwrap();
|
||||
let mut users = Vec::new();
|
||||
|
||||
for i in 0..4 {
|
||||
let username = format!("test{}", i + 1);
|
||||
let password_hash = format!("dummyhash{}", i + 1);
|
||||
|
||||
users.push(store.insert_user(&username, &password_hash).unwrap());
|
||||
}
|
||||
|
||||
(store, users)
|
||||
}
|
Loading…
Reference in New Issue