From 0e16f0acb00da6f6a6710f0fadaef641b63ed960 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Wed, 24 Jan 2018 14:31:33 +0200 Subject: [PATCH] NewEpisode: rename into_episode -> to_episode, change its signature and add unit test. --- hammond-data/src/models/new_episode.rs | 39 ++++++++++++++++++++++---- hammond-data/src/utils.rs | 4 +-- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/hammond-data/src/models/new_episode.rs b/hammond-data/src/models/new_episode.rs index 3cc7b96..444698e 100644 --- a/hammond-data/src/models/new_episode.rs +++ b/hammond-data/src/models/new_episode.rs @@ -120,10 +120,7 @@ impl NewEpisode { } #[allow(dead_code)] - // 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 { + pub(crate) fn to_episode(&self) -> Result { self.index()?; dbqueries::get_episode_from_pk(&self.title, self.podcast_id) } @@ -290,6 +287,7 @@ mod tests { // TODO: Add tests for other feeds too. // Especially if you find an *intresting* generated feed. + // Known prebuilt expected objects. lazy_static! { static ref EXPECTED_MINIMAL_INTERCEPTED_1: NewEpisodeMinimal = { NewEpisodeMinimalBuilder::default() @@ -560,7 +558,7 @@ mod tests { #[test] fn test_new_episode_update() { truncate_db().unwrap(); - let old = EXPECTED_INTERCEPTED_1.clone().into_episode().unwrap(); + let old = EXPECTED_INTERCEPTED_1.clone().to_episode().unwrap(); let updated = &*UPDATED_DURATION_INTERCEPTED_1; updated.update(old.rowid()).unwrap(); @@ -607,4 +605,35 @@ mod tests { assert_eq!(new.rowid(), old.rowid()); assert_eq!(new.podcast_id(), old.podcast_id()); } + + #[test] + fn test_new_episode_to_episode() { + let expected = &*EXPECTED_INTERCEPTED_1; + let updated = &*UPDATED_DURATION_INTERCEPTED_1; + + // Assert insert() produces the same result that you would get with to_podcast() + truncate_db().unwrap(); + expected.insert().unwrap(); + let old = dbqueries::get_episode_from_pk(expected.title(), expected.podcast_id()).unwrap(); + let ep = expected.to_episode().unwrap(); + assert_eq!(old, ep); + + // Same as above, diff order + truncate_db().unwrap(); + let ep = expected.to_episode().unwrap(); + // This should error as a unique constrain violation + assert!(expected.insert().is_err()); + let mut old = + dbqueries::get_episode_from_pk(expected.title(), expected.podcast_id()).unwrap(); + assert_eq!(old, ep); + + old.set_archive(true); + old.save().unwrap(); + + // Assert that it does not mess with user preferences + let ep = updated.to_episode().unwrap(); + let old = dbqueries::get_episode_from_pk(expected.title(), expected.podcast_id()).unwrap(); + assert_eq!(old, ep); + assert_eq!(old.archive(), true); + } } diff --git a/hammond-data/src/utils.rs b/hammond-data/src/utils.rs index 80d2b00..18a38b5 100644 --- a/hammond-data/src/utils.rs +++ b/hammond-data/src/utils.rs @@ -199,7 +199,7 @@ mod tests { .podcast_id(0) .build() .unwrap() - .into_episode() + .to_episode() .unwrap(); let n2 = NewEpisodeBuilder::default() @@ -207,7 +207,7 @@ mod tests { .podcast_id(1) .build() .unwrap() - .into_episode() + .to_episode() .unwrap(); let mut ep1 = dbqueries::get_episode_from_pk(n1.title(), n1.podcast_id()).unwrap();