Skip to content

Commit

Permalink
add tests, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ddboline committed Aug 19, 2024
1 parent 1a0a46e commit 4de18f8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
8 changes: 6 additions & 2 deletions movie_collection_http/src/movie_queue_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,12 @@ impl ImdbRatingsUpdateRequest {
for show in self.shows {
let show: ImdbRatings = show.into();
match ImdbRatings::get_show_by_link(show.link.as_ref(), pool).await? {
Some(_) => show.update_show(pool).await?,
None => show.insert_show(pool).await?,
Some(_) => {
show.update_show(pool).await?;
}
None => {
show.insert_show(pool).await?;
}
}
}
Ok(())
Expand Down
54 changes: 45 additions & 9 deletions movie_collection_lib/src/imdb_episodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use uuid::Uuid;

use crate::pgpool::PgPool;

#[derive(Clone, Serialize, Deserialize, FromSqlRow, PartialEq, Eq)]
#[derive(Clone, Serialize, Deserialize, FromSqlRow, PartialEq, Eq, Debug)]
pub struct ImdbEpisodes {
pub show: StackString,
pub title: StackString,
Expand Down Expand Up @@ -90,8 +90,7 @@ impl ImdbEpisodes {
pub async fn from_index(idx: Uuid, pool: &PgPool) -> Result<Option<Self>, Error> {
let query = query!(
r#"
SELECT a.show, b.title, a.season, a.episode, a.airdate,
a.rating, a.eptitle, a.epurl, a.id
SELECT a.*, b.*
FROM imdb_episodes a
JOIN imdb_ratings b ON a.show = b.show
WHERE a.id = $id
Expand All @@ -110,8 +109,7 @@ impl ImdbEpisodes {
) -> Result<impl Stream<Item = Result<Self, PqError>>, Error> {
let query = query!(
r#"
SELECT a.show, b.title, a.season, a.episode, a.airdate,
a.rating, a.eptitle, a.epurl, a.id
SELECT a.show, b.*
FROM imdb_episodes a
JOIN imdb_ratings b ON a.show = b.show
WHERE a.last_modified >= $timestamp
Expand Down Expand Up @@ -145,8 +143,7 @@ impl ImdbEpisodes {
let query = query_dyn!(
&format_sstr!(
r#"
SELECT a.show, b.title, a.season, a.episode, a.airdate,
a.rating, a.eptitle, a.epurl, a.id
SELECT a.*, b.title
FROM imdb_episodes a
JOIN imdb_ratings b ON a.show = b.show
WHERE {constraints}
Expand All @@ -166,8 +163,7 @@ impl ImdbEpisodes {
) -> Result<impl Stream<Item = Result<Self, PqError>>, Error> {
let query = query!(
r#"
SELECT ie.show, ir.title, ie.season, ie.episode, ie.airdate,
ie.rating, ie.eptitle, ie.epurl, ie.id
SELECT ie.*, ir.title
FROM plex_event pe
JOIN plex_filename pf ON pf.metadata_key = pe.metadata_key
JOIN movie_collection mc ON mc.idx = pf.collection_id
Expand Down Expand Up @@ -301,3 +297,43 @@ impl ImdbSeason {
query.fetch_streaming(&conn).await.map_err(Into::into)
}
}

#[cfg(test)]
mod tests {
use anyhow::Error;
use futures::TryStreamExt;

use crate::{
config::Config, imdb_episodes::{ImdbEpisodes, ImdbSeason}, imdb_ratings::ImdbRatings, pgpool::PgPool
};

#[tokio::test]
#[ignore]
async fn test_imdb_episodes() -> Result<(), Error> {
let config = Config::with_config()?;
let pool = PgPool::new(&config.pgurl)?;

let episodes: Vec<_> =
ImdbEpisodes::get_episodes_by_show_season_episode("the_sopranos", None, None, &pool)
.await?
.try_collect()
.await?;
assert_eq!(episodes.len(), 86);
let episode = episodes.first().unwrap();
println!("{episode:?}");
assert_eq!(episode.season, 1);
assert_eq!(episode.episode, 1);
let index = episode.get_index(&pool).await?.unwrap();
assert_eq!(index, episode.id);
let episode0 = ImdbEpisodes::from_index(episode.id, &pool).await?.unwrap();
assert_eq!(episode.id, episode0.id);
let seasons: Vec<_> = ImdbSeason::get_seasons("the_sopranos", &pool)
.await?
.try_collect()
.await?;
assert_eq!(seasons.len(), 6);
let show = ImdbRatings::get_show_by_link("the_sopranos", &pool).await?.unwrap();
assert_eq!(show.show, episode.show);
Ok(())
}
}
8 changes: 4 additions & 4 deletions movie_collection_lib/src/imdb_ratings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl fmt::Display for ImdbRatings {
impl ImdbRatings {
/// # Errors
/// Returns error if db query fails
pub async fn insert_show(&self, pool: &PgPool) -> Result<(), Error> {
pub async fn insert_show(&self, pool: &PgPool) -> Result<u64, Error> {
let source = self.source.as_ref().map(StackString::from_display);
let query = query!(
r#"
Expand All @@ -63,12 +63,12 @@ impl ImdbRatings {
);
debug!("{:?}", self);
let conn = pool.get().await?;
query.execute(&conn).await.map(|_| ()).map_err(Into::into)
query.execute(&conn).await.map_err(Into::into)
}

/// # Errors
/// Returns error if db query fails
pub async fn update_show(&self, pool: &PgPool) -> Result<(), Error> {
pub async fn update_show(&self, pool: &PgPool) -> Result<u64, Error> {
let mut bindings = Vec::new();
let mut updates: SmallVec<[&'static str; 6]> = smallvec!["last_modified=now()"];
if let Some(title) = &self.title {
Expand Down Expand Up @@ -100,7 +100,7 @@ impl ImdbRatings {
);
let query = query_dyn!(&query, show = self.show, ..bindings)?;
let conn = pool.get().await?;
query.execute(&conn).await.map(|_| ()).map_err(Into::into)
query.execute(&conn).await.map_err(Into::into)
}

/// # Errors
Expand Down

0 comments on commit 4de18f8

Please sign in to comment.