REFACTOR: update persistence tests to use assert and require

This commit is contained in:
Alessio 2022-03-06 13:30:17 -08:00
parent 3e6b8acdda
commit 5fb6a4aa9a
7 changed files with 199 additions and 386 deletions

View File

@ -3,6 +3,9 @@ package persistence_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"offline_twitter/scraper"
)
@ -15,12 +18,8 @@ func test_all_downloaded(tweet scraper.Tweet, yes_or_no bool, t *testing.T) {
false: "Expected not to be downloaded, but it was",
}[yes_or_no]
if len(tweet.Images) != 2 {
t.Errorf("Expected %d images, got %d", 2, len(tweet.Images))
}
if len(tweet.Videos) != 1 {
t.Errorf("Expected %d videos, got %d", 1, len(tweet.Videos))
}
assert.Len(t, tweet.Images, 2)
assert.Len(t, tweet.Videos, 1)
for _, img := range tweet.Images {
if img.IsDownloaded != yes_or_no {
t.Errorf("%s: ImageID %d", error_msg, img.ID)
@ -47,27 +46,21 @@ func TestDownloadTweetContent(t *testing.T) {
// Persist the tweet
err := profile.SaveTweet(tweet)
if err != nil {
t.Fatalf("Failed to save the tweet: %s", err.Error())
}
require.NoError(t, err)
// Make sure everything is marked "not downloaded"
test_all_downloaded(tweet, false, t)
// Do the (fake) downloading
err = profile.DownloadTweetContentWithInjector(&tweet, FakeDownloader{})
if err != nil {
t.Fatalf("Error running fake download: %s", err.Error())
}
require.NoError(t, err)
// It should all be marked "yes downloaded" now
test_all_downloaded(tweet, true, t)
// Reload the Tweet (check db); should also be "yes downloaded"
new_tweet, err := profile.GetTweetById(tweet.ID)
if err != nil {
t.Fatalf("Couldn't reload the Tweet: %s", err.Error())
}
require.NoError(t, err)
test_all_downloaded(new_tweet, true, t)
}
@ -75,6 +68,7 @@ func TestDownloadTweetContent(t *testing.T) {
* Downloading a User's contents should mark the User as downloaded
*/
func TestDownloadUserContent(t *testing.T) {
assert := assert.New(t)
profile_path := "test_profiles/TestMediaQueries"
profile := create_or_load_profile(profile_path)
@ -82,31 +76,20 @@ func TestDownloadUserContent(t *testing.T) {
// Persist the User
err := profile.SaveUser(&user)
if err != nil {
t.Fatalf("Failed to save the user: %s", err.Error())
}
require.NoError(t, err)
// Make sure the User is marked "not downloaded"
if user.IsContentDownloaded {
t.Errorf("User shouldn't be marked downloaded, but it was")
}
assert.False(user.IsContentDownloaded)
// Do the (fake) downloading
err = profile.DownloadUserContentWithInjector(&user, FakeDownloader{})
if err != nil {
t.Fatalf("Error running fake download: %s", err.Error())
}
require.NoError(t, err)
// The User should now be marked "yes downloaded"
if !user.IsContentDownloaded {
t.Errorf("User should be marked downloaded, but it wasn't")
}
assert.True(user.IsContentDownloaded)
// Reload the User (check db); should also be "yes downloaded"
new_user, err := profile.GetUserByID(user.ID)
if err != nil {
t.Fatalf("Couldn't reload the User: %s", err.Error())
}
if !new_user.IsContentDownloaded {
t.Errorf("User should be marked downloaded, but it wasn't")
}}
require.NoError(t, err)
assert.True(new_user.IsContentDownloaded)
}

View File

@ -6,6 +6,7 @@ import (
"time"
"github.com/go-test/deep"
"github.com/stretchr/testify/require"
"offline_twitter/scraper"
)
@ -15,6 +16,7 @@ import (
* Create an Image, save it, reload it, and make sure it comes back the same
*/
func TestSaveAndLoadImage(t *testing.T) {
require := require.New(t)
profile_path := "test_profiles/TestMediaQueries"
profile := create_or_load_profile(profile_path)
@ -27,15 +29,11 @@ func TestSaveAndLoadImage(t *testing.T) {
// Save the Image
err := profile.SaveImage(img)
if err != nil {
t.Fatalf("Failed to save the image: %s", err.Error())
}
require.NoError(err)
// Reload the Image
imgs, err := profile.GetImagesForTweet(tweet)
if err != nil {
t.Fatalf("Could not load images: %s", err.Error())
}
require.NoError(err)
var new_img scraper.Image
for index := range imgs {
@ -43,9 +41,7 @@ func TestSaveAndLoadImage(t *testing.T) {
new_img = imgs[index]
}
}
if new_img.ID != img.ID {
t.Fatalf("Could not find image for some reason: %d, %d; %+v", new_img.ID, img.ID, imgs)
}
require.Equal(img.ID, new_img.ID, "Could not find image for some reason")
if diff := deep.Equal(img, new_img); diff != nil {
t.Error(diff)
}
@ -55,33 +51,27 @@ func TestSaveAndLoadImage(t *testing.T) {
* Change an Image, save the changes, reload it, and check if it comes back the same
*/
func TestModifyImage(t *testing.T) {
require := require.New(t)
profile_path := "test_profiles/TestMediaQueries"
profile := create_or_load_profile(profile_path)
tweet := create_stable_tweet()
img := tweet.Images[0]
if img.ID != -1 {
t.Fatalf("Got the wrong image back: wanted ID %d, got %d", -1, img.ID)
}
require.Equal(scraper.ImageID(-1), img.ID, "Got the wrong image back")
img.IsDownloaded = true
// Save the changes
err := profile.SaveImage(img)
if err != nil {
t.Error(err)
}
require.NoError(err)
// Reload it
imgs, err := profile.GetImagesForTweet(tweet)
if err != nil {
t.Fatalf("Could not load images: %s", err.Error())
}
require.NoError(err)
new_img := imgs[0]
if new_img.ID != img.ID {
t.Fatalf("Got the wrong image back: wanted ID %d, got %d", -1, new_img.ID)
}
require.Equal(imgs[0], new_img, "Got the wrong image back")
if diff := deep.Equal(img, new_img); diff != nil {
t.Error(diff)
@ -93,6 +83,7 @@ func TestModifyImage(t *testing.T) {
* Create an Video, save it, reload it, and make sure it comes back the same
*/
func TestSaveAndLoadVideo(t *testing.T) {
require := require.New(t)
profile_path := "test_profiles/TestMediaQueries"
profile := create_or_load_profile(profile_path)
@ -106,15 +97,11 @@ func TestSaveAndLoadVideo(t *testing.T) {
// Save the Video
err := profile.SaveVideo(vid)
if err != nil {
t.Fatalf("Failed to save the video: %s", err.Error())
}
require.NoError(err)
// Reload the Video
vids, err := profile.GetVideosForTweet(tweet)
if err != nil {
t.Fatalf("Could not load videos: %s", err.Error())
}
require.NoError(err)
var new_vid scraper.Video
for index := range vids {
@ -122,9 +109,8 @@ func TestSaveAndLoadVideo(t *testing.T) {
new_vid = vids[index]
}
}
if new_vid.ID != vid.ID {
t.Fatalf("Could not find video for some reason: %d, %d; %+v", new_vid.ID, vid.ID, vids)
}
require.Equal(vid.ID, new_vid.ID, "Could not find video for some reason")
if diff := deep.Equal(vid, new_vid); diff != nil {
t.Error(diff)
}
@ -134,34 +120,27 @@ func TestSaveAndLoadVideo(t *testing.T) {
* Change an Video, save the changes, reload it, and check if it comes back the same
*/
func TestModifyVideo(t *testing.T) {
require := require.New(t)
profile_path := "test_profiles/TestMediaQueries"
profile := create_or_load_profile(profile_path)
tweet := create_stable_tweet()
vid := tweet.Videos[0]
if vid.ID != -1 {
t.Fatalf("Got the wrong video back: wanted ID %d, got %d", -1, vid.ID)
}
require.Equal(scraper.VideoID(-1), vid.ID, "Got the wrong video back")
vid.IsDownloaded = true
vid.ViewCount = 23000
// Save the changes
err := profile.SaveVideo(vid)
if err != nil {
t.Error(err)
}
require.NoError(err)
// Reload it
vids, err := profile.GetVideosForTweet(tweet)
if err != nil {
t.Fatalf("Could not load videos: %s", err.Error())
}
require.NoError(err)
new_vid := vids[0]
if new_vid.ID != vid.ID {
t.Fatalf("Got the wrong video back: wanted ID %d, got %d", -1, new_vid.ID)
}
require.Equal(vid.ID, new_vid.ID, "Got the wrong video back")
if diff := deep.Equal(vid, new_vid); diff != nil {
t.Error(diff)
@ -173,6 +152,7 @@ func TestModifyVideo(t *testing.T) {
* Create an Url, save it, reload it, and make sure it comes back the same
*/
func TestSaveAndLoadUrl(t *testing.T) {
require := require.New(t)
profile_path := "test_profiles/TestMediaQueries"
profile := create_or_load_profile(profile_path)
@ -185,15 +165,11 @@ func TestSaveAndLoadUrl(t *testing.T) {
// Save the Url
err := profile.SaveUrl(url)
if err != nil {
t.Fatalf("Failed to save the url: %s", err.Error())
}
require.NoError(err)
// Reload the Url
urls, err := profile.GetUrlsForTweet(tweet)
if err != nil {
t.Fatalf("Could not load urls: %s", err.Error())
}
require.NoError(err)
var new_url scraper.Url
for index := range urls {
@ -201,9 +177,8 @@ func TestSaveAndLoadUrl(t *testing.T) {
new_url = urls[index]
}
}
if new_url.Text != url.Text {
t.Fatalf("Could not find url for some reason: %s, %s; %+v", new_url.Text, url.Text, urls)
}
require.Equal(url.Text, new_url.Text, "Could not find the url for some reason")
if diff := deep.Equal(url, new_url); diff != nil {
t.Error(diff)
}
@ -213,33 +188,27 @@ func TestSaveAndLoadUrl(t *testing.T) {
* Change an Url, save the changes, reload it, and check if it comes back the same
*/
func TestModifyUrl(t *testing.T) {
require := require.New(t)
profile_path := "test_profiles/TestMediaQueries"
profile := create_or_load_profile(profile_path)
tweet := create_stable_tweet()
url := tweet.Urls[0]
if url.Text != "-1text" {
t.Fatalf("Got the wrong url back: wanted %s, got %s!", "-1text", url.Text)
}
require.Equal("-1text", url.Text, "Got the wrong url back")
url.IsContentDownloaded = true
// Save the changes
err := profile.SaveUrl(url)
if err != nil {
t.Error(err)
}
require.NoError(err)
// Reload it
urls, err := profile.GetUrlsForTweet(tweet)
if err != nil {
t.Fatalf("Could not load urls: %s", err.Error())
}
require.NoError(err)
new_url := urls[0]
if new_url.Text != "-1text" {
t.Fatalf("Got the wrong url back: wanted %s, got %s!", "-1text", new_url.Text)
}
require.Equal("-1text", url.Text, "Got the wrong url back")
if diff := deep.Equal(url, new_url); diff != nil {
t.Error(diff)
@ -251,6 +220,7 @@ func TestModifyUrl(t *testing.T) {
* Create a Poll, save it, reload it, and make sure it comes back the same
*/
func TestSaveAndLoadPoll(t *testing.T) {
require := require.New(t)
profile_path := "test_profiles/TestMediaQueries"
profile := create_or_load_profile(profile_path)
@ -261,15 +231,11 @@ func TestSaveAndLoadPoll(t *testing.T) {
// Save the Poll
err := profile.SavePoll(poll)
if err != nil {
t.Fatalf("Failed to save the poll: %s", err.Error())
}
require.NoError(err)
// Reload the Poll
polls, err := profile.GetPollsForTweet(tweet)
if err != nil {
t.Fatalf("Could not load poll: %s", err.Error())
}
require.NoError(err)
var new_poll scraper.Poll
for index := range polls {
@ -277,9 +243,8 @@ func TestSaveAndLoadPoll(t *testing.T) {
new_poll = polls[index]
}
}
if new_poll.ID != poll.ID {
t.Fatalf("Could not find poll for some reason: %d, %d; %+v", new_poll.ID, poll.ID, polls)
}
require.Equal(poll.ID, new_poll.ID, "Could not find poll for some reason")
if diff := deep.Equal(poll, new_poll); diff != nil {
t.Error(diff)
}
@ -289,33 +254,27 @@ func TestSaveAndLoadPoll(t *testing.T) {
* Change an Poll, save the changes, reload it, and check if it comes back the same
*/
func TestModifyPoll(t *testing.T) {
require := require.New(t)
profile_path := "test_profiles/TestMediaQueries"
profile := create_or_load_profile(profile_path)
tweet := create_stable_tweet()
poll := tweet.Polls[0]
if poll.Choice1 != "-1" {
t.Fatalf("Got the wrong Poll back: wanted %q, got %q", "-1", poll.Choice1)
}
require.Equal("-1", poll.Choice1, "Got the wrong Poll back")
poll.Choice1_Votes = 1200 // Increment it by 200 votes
// Save the changes
err := profile.SavePoll(poll)
if err != nil {
t.Error(err)
}
require.NoError(err)
// Reload it
polls, err := profile.GetPollsForTweet(tweet)
if err != nil {
t.Fatalf("Could not load polls: %s", err.Error())
}
require.NoError(err)
new_poll := polls[0]
if new_poll.Choice1 != "-1" {
t.Fatalf("Got the wrong poll back: wanted %s, got %s!", "-1", new_poll.Choice1)
}
require.Equal("-1", new_poll.Choice1, "Got the wrong poll back")
if diff := deep.Equal(poll, new_poll); diff != nil {
t.Error(diff)

View File

@ -6,6 +6,9 @@ import (
"path"
"errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"offline_twitter/persistence"
)
@ -33,24 +36,20 @@ func isdir_map(is_dir bool) string {
* Should refuse to create a Profile if the target already exists (i.e., is a file or directory).
*/
func TestNewProfileInvalidPath(t *testing.T) {
require := require.New(t)
gibberish_path := "test_profiles/fjlwrefuvaaw23efwm"
if file_exists(gibberish_path) {
err := os.RemoveAll(gibberish_path)
if err != nil {
panic(err)
}
require.NoError(err)
}
err := os.Mkdir(gibberish_path, 0755)
if err != nil {
panic(err)
}
require.NoError(err)
_, err = persistence.NewProfile(gibberish_path)
if err == nil {
t.Errorf("Should have failed to create a profile in an already existing directory!")
}
if _, is_right_type := err.(persistence.ErrTargetAlreadyExists); !is_right_type {
t.Errorf("Expected 'ErrTargetAlreadyExists' error, got %T instead", err)
}
require.Error(err, "Should have failed to create a profile in an already existing directory!")
_, is_right_type := err.(persistence.ErrTargetAlreadyExists)
assert.True(t, is_right_type, "Expected 'ErrTargetAlreadyExists' error, got %T instead", err)
}
@ -58,34 +57,27 @@ func TestNewProfileInvalidPath(t *testing.T) {
* Should correctly create a new Profile
*/
func TestNewProfile(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
profile_path := "test_profiles/TestNewProfile"
if file_exists(profile_path) {
err := os.RemoveAll(profile_path)
if err != nil {
panic(err)
}
require.NoError(err)
}
profile, err := persistence.NewProfile(profile_path)
if err != nil {
t.Fatalf(err.Error())
}
require.NoError(err)
if profile.ProfileDir != profile_path {
t.Errorf("ProfileDir should be %s, but it is %s", profile_path, profile.ProfileDir)
}
assert.Equal(profile_path,profile.ProfileDir)
if len(profile.UsersList) != 0 {
t.Errorf("Expected empty users list, got %v instead", profile.UsersList)
}
// Check files were created
contents, err := os.ReadDir(profile_path)
if err != nil {
panic(err)
}
if len(contents) != 8 {
t.Fatalf("Expected 8 contents, got %d instead", len(contents))
}
require.NoError(err)
assert.Len(contents, 8)
expected_files := []struct {
filename string
@ -102,19 +94,14 @@ func TestNewProfile(t *testing.T) {
}
for i, v := range expected_files {
if contents[i].Name() != v.filename || contents[i].IsDir() != v.isDir {
t.Fatalf("Expected `%s` to be a %s, but got %s [%s]", v.filename, isdir_map(v.isDir), contents[i].Name(), isdir_map(contents[i].IsDir()))
}
assert.Equal(v.filename, contents[i].Name())
assert.Equal(v.isDir, contents[i].IsDir())
}
// Check database version is initialized
version, err := profile.GetDatabaseVersion()
if err != nil {
panic(err)
}
if version != persistence.ENGINE_DATABASE_VERSION {
t.Errorf("Expected database version %d, but got %d", persistence.ENGINE_DATABASE_VERSION, version)
}
require.NoError(err)
assert.Equal(persistence.ENGINE_DATABASE_VERSION, version)
}
@ -122,38 +109,25 @@ func TestNewProfile(t *testing.T) {
* Should correctly load the Profile
*/
func TestLoadProfile(t *testing.T) {
require := require.New(t)
profile_path := "test_profiles/TestLoadProfile"
if file_exists(profile_path) {
err := os.RemoveAll(profile_path)
if err != nil {
panic(err)
}
require.NoError(err)
}
_, err := persistence.NewProfile(profile_path)
if err != nil {
t.Fatalf(err.Error())
}
require.NoError(err)
// Create some users
err = os.WriteFile(path.Join(profile_path, "users.yaml"), []byte("- user: user1\n- user: user2\n"), 0644)
if err != nil {
t.Fatalf(err.Error())
}
require.NoError(err)
profile, err := persistence.LoadProfile(profile_path)
if err != nil {
t.Fatalf(err.Error())
}
require.NoError(err)
if profile.ProfileDir != profile_path {
t.Errorf("Expected profile path to be %q, but got %q", profile_path, profile.ProfileDir)
}
if len(profile.UsersList) != 2 {
t.Errorf("Expected 2 users, got %v", profile.UsersList)
}
if profile.UsersList[0].Handle != "user1" {
t.Errorf("Expected first user to be %s, got %s", "user1", profile.UsersList[0].Handle)
}
assert.Equal(t, profile_path, profile.ProfileDir)
assert.Len(profile.UsersList, 2)
assert.Equal("user1", profile.UsersList[0].Handle)
}

View File

@ -3,35 +3,33 @@ package persistence_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/go-test/deep"
)
func TestSaveAndLoadRetweet(t *testing.T) {
require := require.New(t)
profile_path := "test_profiles/TestRetweetQueries"
profile := create_or_load_profile(profile_path)
tweet := create_dummy_tweet()
err := profile.SaveTweet(tweet)
if err != nil {
t.Fatalf("Failed to save the tweet: %s", err.Error())
}
err := profile.SaveTweet(tweet)
require.NoError(err)
rt := create_dummy_retweet(tweet.ID)
rt := create_dummy_retweet(tweet.ID)
// Save the Retweet
err = profile.SaveRetweet(rt)
if err != nil {
t.Fatalf("Failed to save the retweet: %s", err.Error())
}
// Save the Retweet
err = profile.SaveRetweet(rt)
require.NoError(err)
// Reload the Retweet
new_rt, err := profile.GetRetweetById(rt.RetweetID)
if err != nil {
t.Fatalf("Failed to load the retweet: %s", err.Error())
}
// Reload the Retweet
new_rt, err := profile.GetRetweetById(rt.RetweetID)
require.NoError(err)
if diff := deep.Equal(rt, new_rt); diff != nil {
t.Error(diff)
}
if diff := deep.Equal(rt, new_rt); diff != nil {
t.Error(diff)
}
}

View File

@ -4,6 +4,9 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/go-test/deep"
)
@ -20,15 +23,11 @@ func TestSaveAndLoadTweet(t *testing.T) {
// Save the tweet
err := profile.SaveTweet(tweet)
if err != nil {
t.Fatalf("Failed to save the tweet: %s", err.Error())
}
require.NoError(t, err)
// Reload the tweet
new_tweet, err := profile.GetTweetById(tweet.ID)
if err != nil {
t.Fatalf("Failed to load the tweet: %s", err.Error())
}
require.NoError(t, err)
if diff := deep.Equal(tweet, new_tweet); diff != nil {
t.Error(diff)
@ -46,15 +45,11 @@ func TestSaveAndLoadTombstone(t *testing.T) {
// Save the tweet
err := profile.SaveTweet(tweet)
if err != nil {
t.Fatalf("Failed to save the tweet: %s", err.Error())
}
require.NoError(t, err)
// Reload the tweet
new_tweet, err := profile.GetTweetById(tweet.ID)
if err != nil {
t.Fatalf("Failed to load the tweet: %s", err.Error())
}
require.NoError(t, err)
if diff := deep.Equal(tweet, new_tweet); diff != nil {
t.Error(diff)
@ -70,6 +65,9 @@ func TestSaveAndLoadTombstone(t *testing.T) {
* - is_content_downloaded should only go from "no" to "yes"
*/
func TestNoWorseningTweet(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
profile_path := "test_profiles/TestTweetQueries"
profile := create_or_load_profile(profile_path)
@ -81,9 +79,7 @@ func TestNoWorseningTweet(t *testing.T) {
// Save the tweet
err := profile.SaveTweet(tweet)
if err != nil {
t.Fatalf("Failed to save the tweet: %s", err.Error())
}
require.NoError(err)
// Worsen the tweet and re-save it
tweet.IsContentDownloaded = false
@ -91,31 +87,22 @@ func TestNoWorseningTweet(t *testing.T) {
tweet.IsConversationScraped = false
tweet.LastScrapedAt = time.Unix(500, 0)
err = profile.SaveTweet(tweet)
if err != nil {
t.Fatalf("Failed to save the tweet: %s", err.Error())
}
require.NoError(err)
// Reload the tweet
new_tweet, err := profile.GetTweetById(tweet.ID)
if err != nil {
t.Fatalf("Failed to load the tweet: %s", err.Error())
}
require.NoError(err)
if new_tweet.IsStub != false {
t.Errorf("Should have preserved non-stub status")
}
if new_tweet.IsContentDownloaded != true {
t.Errorf("Should have preserved is-content-downloaded status")
}
if new_tweet.IsConversationScraped == false {
t.Errorf("Should have preserved is-conversation-scraped status")
}
if new_tweet.LastScrapedAt.Unix() != 1000 {
t.Errorf("Should have preserved last-scraped-at time")
}
assert.False(new_tweet.IsStub, "Should have preserved non-stub status")
assert.True(new_tweet.IsContentDownloaded, "Should have preserved is-content-downloaded status")
assert.True(new_tweet.IsConversationScraped, "Should have preserved is-conversation-scraped status")
assert.Equal(int64(1000), new_tweet.LastScrapedAt.Unix(), "Should have preserved last-scraped-at time")
}
func TestModifyTweet(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
profile_path := "test_profiles/TestTweetQueries"
profile := create_or_load_profile(profile_path)
@ -130,9 +117,7 @@ func TestModifyTweet(t *testing.T) {
tweet.LastScrapedAt = time.Unix(1000, 0)
err := profile.SaveTweet(tweet)
if err != nil {
t.Fatalf("Failed to save the tweet: %s", err.Error())
}
require.NoError(err)
tweet.NumLikes = 1500
tweet.NumRetweets = 2500
@ -144,69 +129,47 @@ func TestModifyTweet(t *testing.T) {
tweet.LastScrapedAt = time.Unix(2000, 0)
err = profile.SaveTweet(tweet)
if err != nil {
t.Fatalf("Failed to re-save the tweet: %s", err.Error())
}
require.NoError(err)
// Reload the tweet
new_tweet, err := profile.GetTweetById(tweet.ID)
if err != nil {
t.Fatalf("Failed to load the tweet: %s", err.Error())
}
require.NoError(err)
if new_tweet.NumLikes != 1500 {
t.Errorf("Expected %d likes, got %d", 1500, new_tweet.NumLikes)
}
if new_tweet.NumRetweets != 2500 {
t.Errorf("Expected %d retweets, got %d", 2500, new_tweet.NumRetweets)
}
if new_tweet.NumReplies != 3500 {
t.Errorf("Expected %d replies, got %d", 3500, new_tweet.NumReplies)
}
if new_tweet.NumQuoteTweets != 4500 {
t.Errorf("Expected %d quote tweets, got %d", 4500, new_tweet.NumQuoteTweets)
}
if new_tweet.IsStub != false {
t.Errorf("Expected tweet to not be a stub, but it was")
}
if new_tweet.IsContentDownloaded != true {
t.Errorf("Expected tweet content to be downloaded, but it wasn't")
}
if new_tweet.IsConversationScraped != true {
t.Errorf("Expected conversation to be scraped, but it wasn't")
}
if new_tweet.LastScrapedAt.Unix() != 2000 {
t.Errorf("Expected tweet to be scraped at %d (unix timestamp), but got %d", 2000, new_tweet.LastScrapedAt.Unix())
}
assert.Equal(1500, new_tweet.NumLikes)
assert.Equal(2500, new_tweet.NumRetweets)
assert.Equal(3500, new_tweet.NumReplies)
assert.Equal(4500, new_tweet.NumQuoteTweets)
assert.False(new_tweet.IsStub)
assert.True(new_tweet.IsContentDownloaded)
assert.True(new_tweet.IsConversationScraped)
assert.Equal(int64(2000), new_tweet.LastScrapedAt.Unix())
}
/**
* Should correctly report whether the User exists in the database
*/
func TestIsTweetInDatabase(t *testing.T) {
require := require.New(t)
profile_path := "test_profiles/TestTweetQueries"
profile := create_or_load_profile(profile_path)
tweet := create_dummy_tweet()
exists := profile.IsTweetInDatabase(tweet.ID)
if exists {
t.Errorf("It shouldn't exist, but it does: %d", tweet.ID)
}
require.False(exists)
err := profile.SaveTweet(tweet)
if err != nil {
panic(err)
}
require.NoError(err)
exists = profile.IsTweetInDatabase(tweet.ID)
if !exists {
t.Errorf("It should exist, but it doesn't: %d", tweet.ID)
}
assert.True(t, exists)
}
/**
* Should correctly populate the `User` field on a Tweet
*/
func TestLoadUserForTweet(t *testing.T) {
require := require.New(t)
profile_path := "test_profiles/TestTweetQueries"
profile := create_or_load_profile(profile_path)
@ -214,29 +177,19 @@ func TestLoadUserForTweet(t *testing.T) {
// Save the tweet
err := profile.SaveTweet(tweet)
if err != nil {
t.Errorf("Failed to save the tweet: %s", err.Error())
}
if tweet.User != nil {
t.Errorf("`User` field is already there for some reason: %v", tweet.User)
}
require.NoError(err)
require.Nil(tweet.User, "`User` field is already there for some reason")
err = profile.LoadUserFor(&tweet)
if err != nil {
t.Errorf("Failed to load user: %s", err.Error())
}
if tweet.User == nil {
t.Errorf("Did not load a user. It is still nil.")
}
require.NoError(err)
require.NotNil(tweet.User, "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) {
assert := assert.New(t)
profile_path := "test_profiles/TestTweetQueries"
profile := create_or_load_profile(profile_path)
@ -244,30 +197,20 @@ func TestCheckTweetContentDownloadNeeded(t *testing.T) {
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")
}
assert.True(profile.CheckTweetContentDownloadNeeded(tweet))
// Save the tweet
err := profile.SaveTweet(tweet)
if err != nil {
t.Errorf("Failed to save the tweet: %s", err.Error())
}
require.NoError(t, err)
// 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")
}
assert.True(profile.CheckTweetContentDownloadNeeded(tweet))
// 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())
}
require.NoError(t, err)
// Should no longer need a download
if profile.CheckTweetContentDownloadNeeded(tweet) != false {
t.Errorf("Downloaded tweets shouldn't need content downloaded")
}
assert.False(profile.CheckTweetContentDownloadNeeded(tweet))
}

View File

@ -19,6 +19,7 @@ import (
* Create a user, save it, reload it, and make sure it comes back the same
*/
func TestSaveAndLoadUser(t *testing.T) {
require := require.New(t)
profile_path := "test_profiles/TestUserQueries"
profile := create_or_load_profile(profile_path)
@ -26,13 +27,10 @@ func TestSaveAndLoadUser(t *testing.T) {
// Save the user, then reload it and ensure it's the same
err := profile.SaveUser(&fake_user)
if err != nil {
panic(err)
}
require.NoError(err)
new_fake_user, err := profile.GetUserByID(fake_user.ID)
if err != nil {
panic(err)
}
require.NoError(err)
if diff := deep.Equal(new_fake_user, fake_user); diff != nil {
t.Error(diff)
@ -40,9 +38,7 @@ func TestSaveAndLoadUser(t *testing.T) {
// Same thing, but get by handle
new_fake_user2, err := profile.GetUserByHandle(fake_user.Handle)
if err != nil {
panic(err)
}
require.NoError(err)
if diff := deep.Equal(new_fake_user2, fake_user); diff != nil {
t.Error(diff)
@ -53,6 +49,9 @@ func TestSaveAndLoadUser(t *testing.T) {
*
*/
func TestModifyUser(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
profile_path := "test_profiles/TestUserQueries"
profile := create_or_load_profile(profile_path)
@ -69,9 +68,7 @@ func TestModifyUser(t *testing.T) {
// Save the user so it can be modified
err := profile.SaveUser(&fake_user)
if err != nil {
panic(err)
}
require.NoError(err)
fake_user.DisplayName = "Display Name 2"
@ -86,42 +83,21 @@ func TestModifyUser(t *testing.T) {
// Save the modified user
err = profile.SaveUser(&fake_user)
if err != nil {
panic(err)
}
require.NoError(err)
// Reload the modified user
new_fake_user, err := profile.GetUserByID(fake_user.ID)
if err != nil {
panic(err)
}
require.NoError(err)
if new_fake_user.DisplayName != "Display Name 2" {
t.Errorf("Expected display name %q, got %q", "Display Name 2", new_fake_user.DisplayName)
}
if new_fake_user.Location != "location2" {
t.Errorf("Expected location %q, got %q", "location2", new_fake_user.Location)
}
if new_fake_user.IsPrivate != true {
t.Errorf("Should be private")
}
if new_fake_user.IsVerified != true {
t.Errorf("Should be verified")
}
if new_fake_user.IsBanned != true {
t.Errorf("Should be banned")
}
if new_fake_user.FollowersCount != 2000 {
t.Errorf("Expected %d followers, got %d", 2000, new_fake_user.FollowersCount)
}
if new_fake_user.JoinDate.Unix() != 1000 {
t.Errorf("Expected unchanged join date (%d), got %d", 1000, new_fake_user.JoinDate.Unix())
}
if new_fake_user.ProfileImageUrl != "asdf2" {
t.Errorf("Expected profile image url to be %q, got %q", "asdf2", new_fake_user.ProfileImageUrl)
}
if new_fake_user.IsContentDownloaded != true {
t.Errorf("Expected content to be downloaded (no-worsening)")
}
assert.Equal("Display Name 2", new_fake_user.DisplayName)
assert.Equal("location2", new_fake_user.Location)
assert.True(new_fake_user.IsPrivate)
assert.True(new_fake_user.IsVerified)
assert.True(new_fake_user.IsBanned)
assert.Equal(2000, new_fake_user.FollowersCount)
assert.Equal(int64(1000), new_fake_user.JoinDate.Unix())
assert.Equal("asdf2", new_fake_user.ProfileImageUrl)
assert.True(new_fake_user.IsContentDownloaded)
}
func TestHandleIsCaseInsensitive(t *testing.T) {
@ -131,9 +107,7 @@ func TestHandleIsCaseInsensitive(t *testing.T) {
user := create_stable_user()
new_user, err := profile.GetUserByHandle("hANdle StaBlE")
if err != nil {
t.Fatalf("Couldn't find the user: %s", err.Error())
}
require.NoError(t, err, "Couldn't find the user")
if diff := deep.Equal(user, new_user); diff != nil {
t.Error(diff)
@ -145,72 +119,58 @@ func TestHandleIsCaseInsensitive(t *testing.T) {
* Should correctly report whether the user exists in the database
*/
func TestUserExists(t *testing.T) {
require := require.New(t)
profile_path := "test_profiles/TestUserQueries"
profile := create_or_load_profile(profile_path)
user := create_dummy_user()
exists := profile.UserExists(user.Handle)
if exists {
t.Errorf("It shouldn't exist, but it does: %d", user.ID)
}
require.False(exists)
err := profile.SaveUser(&user)
if err != nil {
panic(err)
}
require.NoError(err)
exists = profile.UserExists(user.Handle)
if !exists {
t.Errorf("It should exist, but it doesn't: %d", user.ID)
}
require.True(exists)
}
/**
* Test scenarios relating to user content downloading
*/
func TestCheckUserContentDownloadNeeded(t *testing.T) {
assert := assert.New(t)
profile_path := "test_profiles/TestUserQueries"
profile := create_or_load_profile(profile_path)
user := create_dummy_user()
// If user is not in database, should be "yes" automatically
if profile.CheckUserContentDownloadNeeded(user) != true {
t.Errorf("Non-saved user should always require content download")
}
assert.True(profile.CheckUserContentDownloadNeeded(user))
// Save the user, but `is_content_downloaded` is still "false"
user.BannerImageUrl = "banner url1"
user.ProfileImageUrl = "profile url1"
user.IsContentDownloaded = false
err := profile.SaveUser(&user)
if err != nil {
panic(err)
}
require.NoError(t, err)
// If is_content_downloaded is false, then it needs download
if profile.CheckUserContentDownloadNeeded(user) != true {
t.Errorf("Non-downloaded user should require download")
}
assert.True(profile.CheckUserContentDownloadNeeded(user))
// Mark `is_content_downloaded` as "true" again
user.IsContentDownloaded = true
err = profile.SaveUser(&user)
if err != nil {
panic(err)
}
require.NoError(t, err)
// If everything is up to date, no download should be required
if profile.CheckUserContentDownloadNeeded(user) != false {
t.Errorf("Up-to-date user shouldn't need a download")
}
assert.False(profile.CheckUserContentDownloadNeeded(user))
// Change an URL, but don't save it-- needs to be different from what's in the DB
user.BannerImageUrl = "banner url2"
// Download needed for new banner image
if profile.CheckUserContentDownloadNeeded(user) != true {
t.Errorf("If banner image changed, user should require another download")
}
assert.True(profile.CheckUserContentDownloadNeeded(user))
}
/**

View File

@ -3,34 +3,30 @@ package persistence_test
import (
"testing"
"os"
"github.com/stretchr/testify/require"
"offline_twitter/scraper"
"offline_twitter/persistence"
)
func TestVersionUpgrade(t *testing.T) {
require := require.New(t)
profile_path := "test_profiles/TestVersions"
if file_exists(profile_path) {
err := os.RemoveAll(profile_path)
if err != nil {
panic(err)
}
require.NoError(err)
}
profile := create_or_load_profile(profile_path)
test_migration := "insert into tweets (id, user_id, text) values (21250554358298342, -1, 'awefjk')"
test_tweet_id := scraper.TweetID(21250554358298342)
if profile.IsTweetInDatabase(test_tweet_id) {
t.Fatalf("Test tweet shouldn't be in the database yet")
}
require.False(profile.IsTweetInDatabase(test_tweet_id), "Test tweet shouldn't be in db yet")
persistence.MIGRATIONS = append(persistence.MIGRATIONS, test_migration)
err := profile.UpgradeFromXToY(persistence.ENGINE_DATABASE_VERSION, persistence.ENGINE_DATABASE_VERSION + 1)
if err != nil {
panic(err)
}
require.NoError(err)
if !profile.IsTweetInDatabase(test_tweet_id) {
t.Errorf("Migration should have created the tweet, but it didn't")
}
require.True(profile.IsTweetInDatabase(test_tweet_id), "Migration should have created the tweet, but it didn't")
}