NewEpisode: Add index method unit test.

This commit is contained in:
Jordan Petridis 2018-01-24 12:11:49 +02:00
parent 9274aba849
commit d5a7fa9de8
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
2 changed files with 41 additions and 4 deletions

View File

@ -76,7 +76,7 @@ impl Update for NewEpisode {
} }
impl Index for NewEpisode { impl Index for NewEpisode {
// FIXME: Add test // Does not update the episode description if it's the only thing that has changed.
fn index(&self) -> Result<()> { fn index(&self) -> Result<()> {
let exists = dbqueries::episode_exists(self.title(), self.podcast_id())?; let exists = dbqueries::episode_exists(self.title(), self.podcast_id())?;
@ -121,6 +121,8 @@ impl NewEpisode {
#[allow(dead_code)] #[allow(dead_code)]
// FIXME: Add test // FIXME: Add test
// FIXME: Rename to "to_episode", since it can be expensive
// and change it ot &self
pub(crate) fn into_episode(self) -> Result<Episode> { pub(crate) fn into_episode(self) -> Result<Episode> {
self.index()?; self.index()?;
dbqueries::get_episode_from_pk(&self.title, self.podcast_id) dbqueries::get_episode_from_pk(&self.title, self.podcast_id)
@ -275,6 +277,7 @@ impl NewEpisodeMinimal {
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use database::truncate_db;
use dbqueries; use dbqueries;
use models::*; use models::*;
use models::new_episode::{NewEpisodeMinimal, NewEpisodeMinimalBuilder}; use models::new_episode::{NewEpisodeMinimal, NewEpisodeMinimalBuilder};
@ -363,7 +366,7 @@ mod tests {
.unwrap() .unwrap()
}; };
static ref UPDATED_DESC_INTERCEPTED_1: NewEpisode = { static ref UPDATED_DURATION_INTERCEPTED_1: NewEpisode = {
NewEpisodeBuilder::default() NewEpisodeBuilder::default()
.title("The Super Bowl of Racism") .title("The Super Bowl of Racism")
.uri(Some(String::from( .uri(Some(String::from(
@ -373,7 +376,7 @@ mod tests {
.guid(Some(String::from("7df4070a-9832-11e7-adac-cb37b05d5e24"))) .guid(Some(String::from("7df4070a-9832-11e7-adac-cb37b05d5e24")))
.length(Some(66738886)) .length(Some(66738886))
.epoch(1505296800) .epoch(1505296800)
.duration(Some(4171)) .duration(Some(424242))
.podcast_id(42) .podcast_id(42)
.build() .build()
.unwrap() .unwrap()
@ -510,6 +513,8 @@ mod tests {
#[test] #[test]
fn test_minimal_into_new_episode() { fn test_minimal_into_new_episode() {
truncate_db().unwrap();
let file = File::open("tests/feeds/2018-01-20-Intercepted.xml").unwrap(); let file = File::open("tests/feeds/2018-01-20-Intercepted.xml").unwrap();
let channel = Channel::read_from(BufReader::new(file)).unwrap(); let channel = Channel::read_from(BufReader::new(file)).unwrap();
@ -528,6 +533,8 @@ mod tests {
#[test] #[test]
fn test_new_episode_insert() { fn test_new_episode_insert() {
truncate_db().unwrap();
let file = File::open("tests/feeds/2018-01-20-Intercepted.xml").unwrap(); let file = File::open("tests/feeds/2018-01-20-Intercepted.xml").unwrap();
let channel = Channel::read_from(BufReader::new(file)).unwrap(); let channel = Channel::read_from(BufReader::new(file)).unwrap();
@ -552,9 +559,10 @@ mod tests {
#[test] #[test]
fn test_new_episode_update() { fn test_new_episode_update() {
truncate_db().unwrap();
let old = EXPECTED_INTERCEPTED_1.clone().into_episode().unwrap(); let old = EXPECTED_INTERCEPTED_1.clone().into_episode().unwrap();
let updated = &*UPDATED_DESC_INTERCEPTED_1; let updated = &*UPDATED_DURATION_INTERCEPTED_1;
updated.update(old.rowid()).unwrap(); updated.update(old.rowid()).unwrap();
let mut new = dbqueries::get_episode_from_pk(old.title(), old.podcast_id()).unwrap(); let mut new = dbqueries::get_episode_from_pk(old.title(), old.podcast_id()).unwrap();
@ -572,4 +580,31 @@ mod tests {
let new2 = dbqueries::get_episode_from_pk(old.title(), old.podcast_id()).unwrap(); let new2 = dbqueries::get_episode_from_pk(old.title(), old.podcast_id()).unwrap();
assert_eq!(true, new2.archive()); assert_eq!(true, new2.archive());
} }
#[test]
fn test_new_episode_index() {
truncate_db().unwrap();
let expected = &*EXPECTED_INTERCEPTED_1;
// First insert
assert!(expected.index().is_ok());
// Second identical, This should take the early return path
assert!(expected.index().is_ok());
// Get the episode
let old = dbqueries::get_episode_from_pk(expected.title(), expected.podcast_id()).unwrap();
// Assert that NewPodcast is equal to the Indexed one
assert_eq!(*expected, old);
let updated = &*UPDATED_DURATION_INTERCEPTED_1;
// Update the podcast
assert!(updated.index().is_ok());
// Get the new Podcast
let new = dbqueries::get_episode_from_pk(expected.title(), expected.podcast_id()).unwrap();
// Assert it's diff from the old one.
assert_ne!(new, old);
assert_eq!(*updated, new);
assert_eq!(new.rowid(), old.rowid());
assert_eq!(new.podcast_id(), old.podcast_id());
}
} }

View File

@ -388,6 +388,8 @@ mod tests {
let new = dbqueries::get_podcast_from_source_id(42).unwrap(); let new = dbqueries::get_podcast_from_source_id(42).unwrap();
// Assert it's diff from the old one. // Assert it's diff from the old one.
assert_ne!(new, old); assert_ne!(new, old);
assert_eq!(new.id(), old.id());
assert_eq!(new.source_id(), old.source_id());
} }
#[test] #[test]