From 5d44186f91f79b53fa38a2db456ea1b1402f18d4 Mon Sep 17 00:00:00 2001 From: Alessio Date: Tue, 21 Dec 2021 21:49:42 -0500 Subject: [PATCH] Add conditional content downloading for tweets --- persistence/tweet_queries.go | 18 ++++++++++++++ persistence/tweet_queries_test.go | 39 +++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/persistence/tweet_queries.go b/persistence/tweet_queries.go index e37401d..f6f33b0 100644 --- a/persistence/tweet_queries.go +++ b/persistence/tweet_queries.go @@ -178,3 +178,21 @@ func (p Profile) LoadUserFor(t *scraper.Tweet) error { t.User = &user return nil } + +/** + * Return `false` if the tweet is in the DB and has had its content downloaded, `false` otherwise + */ +func (p Profile) CheckTweetContentDownloadNeeded(tweet scraper.Tweet) bool { + row := p.DB.QueryRow(`select is_content_downloaded from tweets where id = ?`, tweet.ID) + + var is_content_downloaded bool + err := row.Scan(&is_content_downloaded) + if err != nil { + if err == sql.ErrNoRows { + return true + } else { + panic(err) + } + } + return !is_content_downloaded +} diff --git a/persistence/tweet_queries_test.go b/persistence/tweet_queries_test.go index 8c7e13c..cc355b8 100644 --- a/persistence/tweet_queries_test.go +++ b/persistence/tweet_queries_test.go @@ -232,3 +232,42 @@ func TestLoadUserForTweet(t *testing.T) { t.Errorf("Did not load a user. It is still nil.") } } + +/** + * Test all the combinations for whether a tweet needs its content downloaded + */ +func TestCheckTweetContentDownloadNeeded(t *testing.T) { + profile_path := "test_profiles/TestTweetQueries" + profile := create_or_load_profile(profile_path) + + tweet := create_dummy_tweet() + tweet.IsContentDownloaded = false + + // Non-saved tweets should need to be downloaded + if profile.CheckTweetContentDownloadNeeded(tweet) != true { + t.Errorf("Non-saved tweets should need a download") + } + + // Save the tweet + err := profile.SaveTweet(tweet) + if err != nil { + t.Errorf("Failed to save the tweet: %s", err.Error()) + } + + // Should still need a download since `is_content_downloaded` is false + if profile.CheckTweetContentDownloadNeeded(tweet) != true { + t.Errorf("Non-downloaded tweets should need a download") + } + + // Try again but this time with `is_content_downloaded` = true + tweet.IsContentDownloaded = true + err = profile.SaveTweet(tweet) + if err != nil { + t.Errorf("Failed to save the tweet: %s", err.Error()) + } + + // Should no longer need a download + if profile.CheckTweetContentDownloadNeeded(tweet) != false { + t.Errorf("Downloaded tweets shouldn't need content downloaded") + } +}