Add conditional content downloading for tweets

This commit is contained in:
Alessio 2021-12-21 21:49:42 -05:00
parent 469d846db4
commit 5d44186f91
2 changed files with 57 additions and 0 deletions

View File

@ -178,3 +178,21 @@ func (p Profile) LoadUserFor(t *scraper.Tweet) error {
t.User = &user t.User = &user
return nil 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
}

View File

@ -232,3 +232,42 @@ func TestLoadUserForTweet(t *testing.T) {
t.Errorf("Did not load a user. It is still nil.") 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")
}
}