From 7d2f35be68669d0972fbc360ad5a78c3fa765657 Mon Sep 17 00:00:00 2001 From: Alessio Date: Wed, 9 Aug 2023 15:32:50 -0300 Subject: [PATCH] Make 'tweet not in database' a named error that can be checked for --- pkg/persistence/tweet_queries.go | 6 +++++- pkg/persistence/tweet_queries_test.go | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/persistence/tweet_queries.go b/pkg/persistence/tweet_queries.go index 84f716c..81c78ed 100644 --- a/pkg/persistence/tweet_queries.go +++ b/pkg/persistence/tweet_queries.go @@ -132,7 +132,11 @@ func (p Profile) GetTweetById(id TweetID) (Tweet, error) { `, id) if err != nil { - return Tweet{}, fmt.Errorf("Error executing GetTweetByID(%d):\n %w", id, err) + if err == sql.ErrNoRows { + return Tweet{}, fmt.Errorf("GetTweetById %d: %w", id, ErrNotInDB) + } else { + panic(err) + } } t.Spaces = []Space{} diff --git a/pkg/persistence/tweet_queries_test.go b/pkg/persistence/tweet_queries_test.go index 0c4c440..6234b57 100644 --- a/pkg/persistence/tweet_queries_test.go +++ b/pkg/persistence/tweet_queries_test.go @@ -8,6 +8,7 @@ import ( "github.com/go-test/deep" + "gitlab.com/offline-twitter/twitter_offline_engine/pkg/persistence" "gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper" ) @@ -326,3 +327,12 @@ func TestCheckTweetContentDownloadNeeded(t *testing.T) { // Should no longer need a download assert.False(profile.CheckTweetContentDownloadNeeded(tweet)) } + +func TestLoadMissingTweet(t *testing.T) { + profile_path := "test_profiles/TestTweetQueries" + profile := create_or_load_profile(profile_path) + + _, err := profile.GetTweetById(scraper.TweetID(6234234)) // Random number + require.Error(t, err) + assert.ErrorIs(t, err, persistence.ErrNotInDB) +}