From 1625f773c2c1db1b911c4ab51b9a9e51ef9fc2ee Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Sun, 21 Jan 2018 08:39:50 +0200 Subject: [PATCH] hammond-data: Fix PartialEq implementations, add NewPodcast.insert unit test. --- hammond-data/src/models/new_episode.rs | 12 ++++---- hammond-data/src/models/new_podcast.rs | 42 ++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/hammond-data/src/models/new_episode.rs b/hammond-data/src/models/new_episode.rs index 3caa8e4..7a3f87b 100644 --- a/hammond-data/src/models/new_episode.rs +++ b/hammond-data/src/models/new_episode.rs @@ -95,9 +95,9 @@ impl Index for NewEpisode { impl PartialEq for NewEpisode { fn eq(&self, other: &EpisodeMinimal) -> bool { - (self.title() != other.title()) || (self.uri() != other.uri()) - || (self.duration() != other.duration()) || (self.epoch() != other.epoch()) - || (self.guid() != other.guid()) + (self.title() == other.title()) && (self.uri() == other.uri()) + && (self.duration() == other.duration()) && (self.epoch() == other.epoch()) + && (self.guid() == other.guid()) } } @@ -167,9 +167,9 @@ pub(crate) struct NewEpisodeMinimal { impl PartialEq for NewEpisodeMinimal { fn eq(&self, other: &EpisodeMinimal) -> bool { - (self.title() != other.title()) || (self.uri() != other.uri()) - || (self.duration() != other.duration()) || (self.epoch() != other.epoch()) - || (self.guid() != other.guid()) + (self.title() == other.title()) && (self.uri() == other.uri()) + && (self.duration() == other.duration()) && (self.epoch() == other.epoch()) + && (self.guid() == other.guid()) } } diff --git a/hammond-data/src/models/new_podcast.rs b/hammond-data/src/models/new_podcast.rs index 41196a7..cef2967 100644 --- a/hammond-data/src/models/new_podcast.rs +++ b/hammond-data/src/models/new_podcast.rs @@ -77,9 +77,10 @@ impl Index for NewPodcast { impl PartialEq for NewPodcast { fn eq(&self, other: &Podcast) -> bool { - (self.link() != other.link()) || (self.title() != other.title()) - || (self.image_uri() != other.image_uri()) - || (self.description() != other.description()) + (self.link() == other.link()) && (self.title() == other.title()) + && (self.image_uri() == other.image_uri()) + && (self.description() == other.description()) + && (self.source_id() == other.source_id()) } } @@ -153,6 +154,7 @@ mod tests { use rss::Channel; + use database::truncate_db; use models::NewPodcastBuilder; use std::fs::File; @@ -286,4 +288,38 @@ mod tests { assert_eq!(pd, expected); } + + #[test] + // This maybe could be a doc test on insert. + fn test_new_podcast_insert() { + truncate_db().unwrap(); + let file = File::open("tests/feeds/2018-01-20-Intercepted.xml").unwrap(); + let channel = Channel::read_from(BufReader::new(file)).unwrap(); + + let npd = NewPodcast::new(&channel, 0); + npd.insert().unwrap(); + let pd = dbqueries::get_podcast_from_source_id(0).unwrap(); + + let descr = "The people behind The Intercept’s fearless reporting and incisive \ + commentary—Jeremy Scahill, Glenn Greenwald, Betsy Reed and others—discuss \ + the crucial issues of our time: national security, civil liberties, foreign \ + policy, and criminal justice. Plus interviews with artists, thinkers, and \ + newsmakers who challenge our preconceptions about the world we live in."; + + let expected = NewPodcastBuilder::default() + .title("Intercepted with Jeremy Scahill") + .link("https://theintercept.com/podcasts") + .description(descr) + .image_uri(Some(String::from( + "http://static.megaphone.fm/podcasts/d5735a50-d904-11e6-8532-73c7de466ea6/image/\ + uploads_2F1484252190700-qhn5krasklbce3dh-a797539282700ea0298a3a26f7e49b0b_\ + 2FIntercepted_COVER%2B_281_29.png") + )) + .build() + .unwrap(); + + assert_eq!(npd, pd); + assert_eq!(&expected, &pd); + assert_eq!(&expected, &npd); + } }