From c3e8e0b3cac6e0c9a3a37b33ca88a028c3421622 Mon Sep 17 00:00:00 2001 From: Andy Wortman Date: Tue, 12 Dec 2017 00:14:28 -0800 Subject: tweak tests for tweet retrieval, add display_id tests tweet retrieval was brittle, would fail if UTC calendar day != Local TZ calendar day display_id tests expose the bug in tweet ids being Bare id rather than Today id due to timezone mismatch in looking up tweets --- src/tw/mod.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) (limited to 'src/tw') diff --git a/src/tw/mod.rs b/src/tw/mod.rs index 0a073c9..0e5ec37 100644 --- a/src/tw/mod.rs +++ b/src/tw/mod.rs @@ -230,8 +230,48 @@ mod tests { } #[test] - fn test_tweet_retrieval() { + fn test_display_id() { + /* + * ... I think this test only works if Local is -0800 or further from UTC. + * I'm not even sure how to make any of this work in a TZ-invariant way. + */ + // THIS NOW ONLY WORKS FOR DAYS OTHER THAN THE LAST OF A MONTH. FML let today = Local::now(); + let pst_1630_but_utc: DateTime = DateTime::parse_from_rfc3339( + &format!("{:04}-{:02}-{:02}T00:30:00Z", today.year(), today.month(), today.day() + 1)).unwrap().with_timezone(&Utc); + let local_pst_1630: DateTime = DateTime::parse_from_rfc3339( + &format!("{:04}-{:02}-{:02}T16:30:00-08:00", today.year(), today.month(), today.day())).unwrap().with_timezone(&Local); + let tweet = Tweet { + id: "manual_tweet_1".to_owned(), + author_id: "you, dummy".to_owned(), + text: "test tweet please ignore".to_owned(), + created_at: "1234 not real".to_owned(), + recieved_at: pst_1630_but_utc, + urls: HashMap::new(), + quoted_tweet_id: None, + rt_tweet: None, + reply_to_tweet: None, + internal_id: 0 + }; + + let mut tweeter = TwitterCache::new(); + + tweeter.number_and_insert_tweet(tweet.clone()); + + let retrieved = tweeter.retrieve_tweet(&TweetId::Bare(1)); + assert_eq!(retrieved.is_some(), true); + + let retrieved = tweeter.retrieve_tweet(&TweetId::Dated(format!("{:04}{:02}{:02}", today.year(), today.month(), today.day()), 0)); + assert_eq!(retrieved.is_some(), true); + + let display_id = tweeter.display_id_for_tweet(&retrieved.unwrap()); + + assert_eq!(display_id, TweetId::Today(0)); + } + + #[test] + fn test_tweet_retrieval() { + let today = Utc::now(); let yesterday = today - chrono::Duration::days(1); let tweets = vec![ Tweet { @@ -274,8 +314,12 @@ mod tests { tweeter.retrieve_tweet(&TweetId::Today(0)).map(|x| x.id.to_owned()), Some(tweets[1].clone()).map(|x| x.id) ); + + let local_yesterday = yesterday.with_timezone(&Local); + let date = format!("{:04}{:02}{:02}", local_yesterday.year(), local_yesterday.month(), local_yesterday.day()); + assert_eq!( - tweeter.retrieve_tweet(&TweetId::Dated(format!("{:04}{:02}{:02}", yesterday.year(), yesterday.month(), yesterday.day()), 0)).map(|x| x.id.to_owned()), + tweeter.retrieve_tweet(&TweetId::Dated(date, 0)).map(|x| x.id.to_owned()), Some(tweets[0].clone()).map(|x| x.id) ); } -- cgit v1.1 From 3e322317ad9547ae76f887d0156dc8c813228fca Mon Sep 17 00:00:00 2001 From: Andy Wortman Date: Tue, 12 Dec 2017 00:36:15 -0800 Subject: fix incorrect preferred id selection references to UTC date rather than adjusted-to-local-TZ date meant that calendar mismatch happens when UTC calendar day moves forward, rather than local TZ calendar day in reality, this means if you're PST, and it's 4:01pm, all tweet ids turn into Bare :1235567 form, rather than changing over at midnight. Additionally, found a similar issue with id presentation for reply ids --- src/tw/mod.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/tw') diff --git a/src/tw/mod.rs b/src/tw/mod.rs index 0e5ec37..a8db62f 100644 --- a/src/tw/mod.rs +++ b/src/tw/mod.rs @@ -386,26 +386,26 @@ impl IdConversions { id @ &TweetId::Bare(_) => { tweeter.retrieve_tweet(id).and_then(|tweet| { let now = Local::now(); - let tweet_date = tweet.recieved_at.with_timezone(&now.timezone()); + let tweet_date = tweet.recieved_at.with_timezone(&Local); if now.year() == tweet_date.year() && now.month() == tweet_date.month() && now.day() == tweet_date.day() { - let date_string = format!("{:04}{:02}{:02}", tweet.recieved_at.year(), tweet.recieved_at.month(), tweet.recieved_at.day()); + let date_string = format!("{:04}{:02}{:02}", tweet_date.year(), tweet_date.month(), tweet_date.day()); let today_id = self.tweets_by_date_and_tweet_id.get(&date_string).and_then(|m| m.get(&tweet.internal_id)); today_id.map(|x| TweetId::Today(*x)) } else { - None + Some(TweetId::Bare(tweet.internal_id)) } }).unwrap_or(id.to_owned()) }, id @ &TweetId::Twitter(_) => { tweeter.retrieve_tweet(id).and_then(|tweet| { let now = Local::now(); - let tweet_date = tweet.recieved_at.with_timezone(&now.timezone()); + let tweet_date = tweet.recieved_at.with_timezone(&Local); if now.year() == tweet_date.year() && now.month() == tweet_date.month() && now.day() == tweet_date.day() { - let date_string = format!("{:04}{:02}{:02}", tweet.recieved_at.year(), tweet.recieved_at.month(), tweet.recieved_at.day()); + let date_string = format!("{:04}{:02}{:02}", tweet_date.year(), tweet_date.month(), tweet_date.day()); let today_id = self.tweets_by_date_and_tweet_id.get(&date_string).and_then(|m| m.get(&tweet.internal_id)); today_id.map(|x| TweetId::Today(*x)) } else { - None + Some(TweetId::Bare(tweet.internal_id)) } }).unwrap_or(id.to_owned()) } @@ -747,7 +747,7 @@ impl TwitterCache { if tw.internal_id == 0 { tw.internal_id = (self.tweets.len() as u64) + 1; self.id_conversions.id_to_tweet_id.insert(tw.internal_id, tw.id.to_owned()); - let local_recv_time = tw.recieved_at.with_timezone(&Local::now().timezone()); + let local_recv_time = tw.recieved_at.with_timezone(&Local); let tweet_date = format!("{:04}{:02}{:02}", local_recv_time.year(), local_recv_time.month(), local_recv_time.day()); if !self.id_conversions.tweets_by_date.contains_key(&tweet_date) { self.id_conversions.tweets_by_date.insert(tweet_date.clone(), HashMap::new()); @@ -770,10 +770,10 @@ impl TwitterCache { } pub fn display_id_for_tweet(&self, tweet: &Tweet) -> TweetId { let now = Local::now(); - let tweet_date = tweet.recieved_at.with_timezone(&now.timezone()); + let tweet_date = tweet.recieved_at.with_timezone(&Local); let bare_id = TweetId::Bare(tweet.internal_id); let maybe_dated_id = if now.year() == tweet_date.year() && now.month() == tweet_date.month() && now.day() == tweet_date.day() { - let date_string = format!("{:04}{:02}{:02}", tweet.recieved_at.year(), tweet.recieved_at.month(), tweet.recieved_at.day()); + let date_string = format!("{:04}{:02}{:02}", tweet_date.year(), tweet_date.month(), tweet_date.day()); let today_id = self.id_conversions.tweets_by_date_and_tweet_id.get(&date_string).and_then(|m| m.get(&tweet.internal_id)); today_id.map(|x| TweetId::Today(*x)) } else { -- cgit v1.1