REFACTOR: update persistence tests to use assert and require
This commit is contained in:
parent
3e6b8acdda
commit
5fb6a4aa9a
@ -3,6 +3,9 @@ package persistence_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"offline_twitter/scraper"
|
"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",
|
false: "Expected not to be downloaded, but it was",
|
||||||
}[yes_or_no]
|
}[yes_or_no]
|
||||||
|
|
||||||
if len(tweet.Images) != 2 {
|
assert.Len(t, tweet.Images, 2)
|
||||||
t.Errorf("Expected %d images, got %d", 2, len(tweet.Images))
|
assert.Len(t, tweet.Videos, 1)
|
||||||
}
|
|
||||||
if len(tweet.Videos) != 1 {
|
|
||||||
t.Errorf("Expected %d videos, got %d", 1, len(tweet.Videos))
|
|
||||||
}
|
|
||||||
for _, img := range tweet.Images {
|
for _, img := range tweet.Images {
|
||||||
if img.IsDownloaded != yes_or_no {
|
if img.IsDownloaded != yes_or_no {
|
||||||
t.Errorf("%s: ImageID %d", error_msg, img.ID)
|
t.Errorf("%s: ImageID %d", error_msg, img.ID)
|
||||||
@ -47,27 +46,21 @@ func TestDownloadTweetContent(t *testing.T) {
|
|||||||
|
|
||||||
// Persist the tweet
|
// Persist the tweet
|
||||||
err := profile.SaveTweet(tweet)
|
err := profile.SaveTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Failed to save the tweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure everything is marked "not downloaded"
|
// Make sure everything is marked "not downloaded"
|
||||||
test_all_downloaded(tweet, false, t)
|
test_all_downloaded(tweet, false, t)
|
||||||
|
|
||||||
// Do the (fake) downloading
|
// Do the (fake) downloading
|
||||||
err = profile.DownloadTweetContentWithInjector(&tweet, FakeDownloader{})
|
err = profile.DownloadTweetContentWithInjector(&tweet, FakeDownloader{})
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error running fake download: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// It should all be marked "yes downloaded" now
|
// It should all be marked "yes downloaded" now
|
||||||
test_all_downloaded(tweet, true, t)
|
test_all_downloaded(tweet, true, t)
|
||||||
|
|
||||||
// Reload the Tweet (check db); should also be "yes downloaded"
|
// Reload the Tweet (check db); should also be "yes downloaded"
|
||||||
new_tweet, err := profile.GetTweetById(tweet.ID)
|
new_tweet, err := profile.GetTweetById(tweet.ID)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Couldn't reload the Tweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
test_all_downloaded(new_tweet, true, t)
|
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
|
* Downloading a User's contents should mark the User as downloaded
|
||||||
*/
|
*/
|
||||||
func TestDownloadUserContent(t *testing.T) {
|
func TestDownloadUserContent(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
profile_path := "test_profiles/TestMediaQueries"
|
profile_path := "test_profiles/TestMediaQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
@ -82,31 +76,20 @@ func TestDownloadUserContent(t *testing.T) {
|
|||||||
|
|
||||||
// Persist the User
|
// Persist the User
|
||||||
err := profile.SaveUser(&user)
|
err := profile.SaveUser(&user)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Failed to save the user: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure the User is marked "not downloaded"
|
// Make sure the User is marked "not downloaded"
|
||||||
if user.IsContentDownloaded {
|
assert.False(user.IsContentDownloaded)
|
||||||
t.Errorf("User shouldn't be marked downloaded, but it was")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do the (fake) downloading
|
// Do the (fake) downloading
|
||||||
err = profile.DownloadUserContentWithInjector(&user, FakeDownloader{})
|
err = profile.DownloadUserContentWithInjector(&user, FakeDownloader{})
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error running fake download: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// The User should now be marked "yes downloaded"
|
// The User should now be marked "yes downloaded"
|
||||||
if !user.IsContentDownloaded {
|
assert.True(user.IsContentDownloaded)
|
||||||
t.Errorf("User should be marked downloaded, but it wasn't")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload the User (check db); should also be "yes downloaded"
|
// Reload the User (check db); should also be "yes downloaded"
|
||||||
new_user, err := profile.GetUserByID(user.ID)
|
new_user, err := profile.GetUserByID(user.ID)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Couldn't reload the User: %s", err.Error())
|
assert.True(new_user.IsContentDownloaded)
|
||||||
}
|
}
|
||||||
if !new_user.IsContentDownloaded {
|
|
||||||
t.Errorf("User should be marked downloaded, but it wasn't")
|
|
||||||
}}
|
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-test/deep"
|
"github.com/go-test/deep"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"offline_twitter/scraper"
|
"offline_twitter/scraper"
|
||||||
)
|
)
|
||||||
@ -15,6 +16,7 @@ import (
|
|||||||
* Create an Image, save it, reload it, and make sure it comes back the same
|
* Create an Image, save it, reload it, and make sure it comes back the same
|
||||||
*/
|
*/
|
||||||
func TestSaveAndLoadImage(t *testing.T) {
|
func TestSaveAndLoadImage(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
profile_path := "test_profiles/TestMediaQueries"
|
profile_path := "test_profiles/TestMediaQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
@ -27,15 +29,11 @@ func TestSaveAndLoadImage(t *testing.T) {
|
|||||||
|
|
||||||
// Save the Image
|
// Save the Image
|
||||||
err := profile.SaveImage(img)
|
err := profile.SaveImage(img)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Failed to save the image: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload the Image
|
// Reload the Image
|
||||||
imgs, err := profile.GetImagesForTweet(tweet)
|
imgs, err := profile.GetImagesForTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Could not load images: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
var new_img scraper.Image
|
var new_img scraper.Image
|
||||||
for index := range imgs {
|
for index := range imgs {
|
||||||
@ -43,9 +41,7 @@ func TestSaveAndLoadImage(t *testing.T) {
|
|||||||
new_img = imgs[index]
|
new_img = imgs[index]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if new_img.ID != img.ID {
|
require.Equal(img.ID, new_img.ID, "Could not find image for some reason")
|
||||||
t.Fatalf("Could not find image for some reason: %d, %d; %+v", new_img.ID, img.ID, imgs)
|
|
||||||
}
|
|
||||||
if diff := deep.Equal(img, new_img); diff != nil {
|
if diff := deep.Equal(img, new_img); diff != nil {
|
||||||
t.Error(diff)
|
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
|
* Change an Image, save the changes, reload it, and check if it comes back the same
|
||||||
*/
|
*/
|
||||||
func TestModifyImage(t *testing.T) {
|
func TestModifyImage(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
profile_path := "test_profiles/TestMediaQueries"
|
profile_path := "test_profiles/TestMediaQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
tweet := create_stable_tweet()
|
tweet := create_stable_tweet()
|
||||||
img := tweet.Images[0]
|
img := tweet.Images[0]
|
||||||
|
|
||||||
if img.ID != -1 {
|
require.Equal(scraper.ImageID(-1), img.ID, "Got the wrong image back")
|
||||||
t.Fatalf("Got the wrong image back: wanted ID %d, got %d", -1, img.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
img.IsDownloaded = true
|
img.IsDownloaded = true
|
||||||
|
|
||||||
// Save the changes
|
// Save the changes
|
||||||
err := profile.SaveImage(img)
|
err := profile.SaveImage(img)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload it
|
// Reload it
|
||||||
imgs, err := profile.GetImagesForTweet(tweet)
|
imgs, err := profile.GetImagesForTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Could not load images: %s", err.Error())
|
|
||||||
}
|
|
||||||
new_img := imgs[0]
|
new_img := imgs[0]
|
||||||
if new_img.ID != img.ID {
|
require.Equal(imgs[0], new_img, "Got the wrong image back")
|
||||||
t.Fatalf("Got the wrong image back: wanted ID %d, got %d", -1, new_img.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
if diff := deep.Equal(img, new_img); diff != nil {
|
if diff := deep.Equal(img, new_img); diff != nil {
|
||||||
t.Error(diff)
|
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
|
* Create an Video, save it, reload it, and make sure it comes back the same
|
||||||
*/
|
*/
|
||||||
func TestSaveAndLoadVideo(t *testing.T) {
|
func TestSaveAndLoadVideo(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
profile_path := "test_profiles/TestMediaQueries"
|
profile_path := "test_profiles/TestMediaQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
@ -106,15 +97,11 @@ func TestSaveAndLoadVideo(t *testing.T) {
|
|||||||
|
|
||||||
// Save the Video
|
// Save the Video
|
||||||
err := profile.SaveVideo(vid)
|
err := profile.SaveVideo(vid)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Failed to save the video: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload the Video
|
// Reload the Video
|
||||||
vids, err := profile.GetVideosForTweet(tweet)
|
vids, err := profile.GetVideosForTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Could not load videos: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
var new_vid scraper.Video
|
var new_vid scraper.Video
|
||||||
for index := range vids {
|
for index := range vids {
|
||||||
@ -122,9 +109,8 @@ func TestSaveAndLoadVideo(t *testing.T) {
|
|||||||
new_vid = vids[index]
|
new_vid = vids[index]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if new_vid.ID != vid.ID {
|
require.Equal(vid.ID, new_vid.ID, "Could not find video for some reason")
|
||||||
t.Fatalf("Could not find video for some reason: %d, %d; %+v", new_vid.ID, vid.ID, vids)
|
|
||||||
}
|
|
||||||
if diff := deep.Equal(vid, new_vid); diff != nil {
|
if diff := deep.Equal(vid, new_vid); diff != nil {
|
||||||
t.Error(diff)
|
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
|
* Change an Video, save the changes, reload it, and check if it comes back the same
|
||||||
*/
|
*/
|
||||||
func TestModifyVideo(t *testing.T) {
|
func TestModifyVideo(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
profile_path := "test_profiles/TestMediaQueries"
|
profile_path := "test_profiles/TestMediaQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
tweet := create_stable_tweet()
|
tweet := create_stable_tweet()
|
||||||
vid := tweet.Videos[0]
|
vid := tweet.Videos[0]
|
||||||
|
require.Equal(scraper.VideoID(-1), vid.ID, "Got the wrong video back")
|
||||||
if vid.ID != -1 {
|
|
||||||
t.Fatalf("Got the wrong video back: wanted ID %d, got %d", -1, vid.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
vid.IsDownloaded = true
|
vid.IsDownloaded = true
|
||||||
vid.ViewCount = 23000
|
vid.ViewCount = 23000
|
||||||
|
|
||||||
// Save the changes
|
// Save the changes
|
||||||
err := profile.SaveVideo(vid)
|
err := profile.SaveVideo(vid)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload it
|
// Reload it
|
||||||
vids, err := profile.GetVideosForTweet(tweet)
|
vids, err := profile.GetVideosForTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Could not load videos: %s", err.Error())
|
|
||||||
}
|
|
||||||
new_vid := vids[0]
|
new_vid := vids[0]
|
||||||
if new_vid.ID != vid.ID {
|
require.Equal(vid.ID, new_vid.ID, "Got the wrong video back")
|
||||||
t.Fatalf("Got the wrong video back: wanted ID %d, got %d", -1, new_vid.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
if diff := deep.Equal(vid, new_vid); diff != nil {
|
if diff := deep.Equal(vid, new_vid); diff != nil {
|
||||||
t.Error(diff)
|
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
|
* Create an Url, save it, reload it, and make sure it comes back the same
|
||||||
*/
|
*/
|
||||||
func TestSaveAndLoadUrl(t *testing.T) {
|
func TestSaveAndLoadUrl(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
profile_path := "test_profiles/TestMediaQueries"
|
profile_path := "test_profiles/TestMediaQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
@ -185,15 +165,11 @@ func TestSaveAndLoadUrl(t *testing.T) {
|
|||||||
|
|
||||||
// Save the Url
|
// Save the Url
|
||||||
err := profile.SaveUrl(url)
|
err := profile.SaveUrl(url)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Failed to save the url: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload the Url
|
// Reload the Url
|
||||||
urls, err := profile.GetUrlsForTweet(tweet)
|
urls, err := profile.GetUrlsForTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Could not load urls: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
var new_url scraper.Url
|
var new_url scraper.Url
|
||||||
for index := range urls {
|
for index := range urls {
|
||||||
@ -201,9 +177,8 @@ func TestSaveAndLoadUrl(t *testing.T) {
|
|||||||
new_url = urls[index]
|
new_url = urls[index]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if new_url.Text != url.Text {
|
require.Equal(url.Text, new_url.Text, "Could not find the url for some reason")
|
||||||
t.Fatalf("Could not find url for some reason: %s, %s; %+v", new_url.Text, url.Text, urls)
|
|
||||||
}
|
|
||||||
if diff := deep.Equal(url, new_url); diff != nil {
|
if diff := deep.Equal(url, new_url); diff != nil {
|
||||||
t.Error(diff)
|
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
|
* Change an Url, save the changes, reload it, and check if it comes back the same
|
||||||
*/
|
*/
|
||||||
func TestModifyUrl(t *testing.T) {
|
func TestModifyUrl(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
profile_path := "test_profiles/TestMediaQueries"
|
profile_path := "test_profiles/TestMediaQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
tweet := create_stable_tweet()
|
tweet := create_stable_tweet()
|
||||||
url := tweet.Urls[0]
|
url := tweet.Urls[0]
|
||||||
|
|
||||||
if url.Text != "-1text" {
|
require.Equal("-1text", url.Text, "Got the wrong url back")
|
||||||
t.Fatalf("Got the wrong url back: wanted %s, got %s!", "-1text", url.Text)
|
|
||||||
}
|
|
||||||
|
|
||||||
url.IsContentDownloaded = true
|
url.IsContentDownloaded = true
|
||||||
|
|
||||||
// Save the changes
|
// Save the changes
|
||||||
err := profile.SaveUrl(url)
|
err := profile.SaveUrl(url)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload it
|
// Reload it
|
||||||
urls, err := profile.GetUrlsForTweet(tweet)
|
urls, err := profile.GetUrlsForTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Could not load urls: %s", err.Error())
|
|
||||||
}
|
|
||||||
new_url := urls[0]
|
new_url := urls[0]
|
||||||
if new_url.Text != "-1text" {
|
require.Equal("-1text", url.Text, "Got the wrong url back")
|
||||||
t.Fatalf("Got the wrong url back: wanted %s, got %s!", "-1text", new_url.Text)
|
|
||||||
}
|
|
||||||
|
|
||||||
if diff := deep.Equal(url, new_url); diff != nil {
|
if diff := deep.Equal(url, new_url); diff != nil {
|
||||||
t.Error(diff)
|
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
|
* Create a Poll, save it, reload it, and make sure it comes back the same
|
||||||
*/
|
*/
|
||||||
func TestSaveAndLoadPoll(t *testing.T) {
|
func TestSaveAndLoadPoll(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
profile_path := "test_profiles/TestMediaQueries"
|
profile_path := "test_profiles/TestMediaQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
@ -261,15 +231,11 @@ func TestSaveAndLoadPoll(t *testing.T) {
|
|||||||
|
|
||||||
// Save the Poll
|
// Save the Poll
|
||||||
err := profile.SavePoll(poll)
|
err := profile.SavePoll(poll)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Failed to save the poll: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload the Poll
|
// Reload the Poll
|
||||||
polls, err := profile.GetPollsForTweet(tweet)
|
polls, err := profile.GetPollsForTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Could not load poll: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
var new_poll scraper.Poll
|
var new_poll scraper.Poll
|
||||||
for index := range polls {
|
for index := range polls {
|
||||||
@ -277,9 +243,8 @@ func TestSaveAndLoadPoll(t *testing.T) {
|
|||||||
new_poll = polls[index]
|
new_poll = polls[index]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if new_poll.ID != poll.ID {
|
require.Equal(poll.ID, new_poll.ID, "Could not find poll for some reason")
|
||||||
t.Fatalf("Could not find poll for some reason: %d, %d; %+v", new_poll.ID, poll.ID, polls)
|
|
||||||
}
|
|
||||||
if diff := deep.Equal(poll, new_poll); diff != nil {
|
if diff := deep.Equal(poll, new_poll); diff != nil {
|
||||||
t.Error(diff)
|
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
|
* Change an Poll, save the changes, reload it, and check if it comes back the same
|
||||||
*/
|
*/
|
||||||
func TestModifyPoll(t *testing.T) {
|
func TestModifyPoll(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
profile_path := "test_profiles/TestMediaQueries"
|
profile_path := "test_profiles/TestMediaQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
tweet := create_stable_tweet()
|
tweet := create_stable_tweet()
|
||||||
poll := tweet.Polls[0]
|
poll := tweet.Polls[0]
|
||||||
|
|
||||||
if poll.Choice1 != "-1" {
|
require.Equal("-1", poll.Choice1, "Got the wrong Poll back")
|
||||||
t.Fatalf("Got the wrong Poll back: wanted %q, got %q", "-1", poll.Choice1)
|
|
||||||
}
|
|
||||||
|
|
||||||
poll.Choice1_Votes = 1200 // Increment it by 200 votes
|
poll.Choice1_Votes = 1200 // Increment it by 200 votes
|
||||||
|
|
||||||
// Save the changes
|
// Save the changes
|
||||||
err := profile.SavePoll(poll)
|
err := profile.SavePoll(poll)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload it
|
// Reload it
|
||||||
polls, err := profile.GetPollsForTweet(tweet)
|
polls, err := profile.GetPollsForTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Could not load polls: %s", err.Error())
|
|
||||||
}
|
|
||||||
new_poll := polls[0]
|
new_poll := polls[0]
|
||||||
if new_poll.Choice1 != "-1" {
|
require.Equal("-1", new_poll.Choice1, "Got the wrong poll back")
|
||||||
t.Fatalf("Got the wrong poll back: wanted %s, got %s!", "-1", new_poll.Choice1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if diff := deep.Equal(poll, new_poll); diff != nil {
|
if diff := deep.Equal(poll, new_poll); diff != nil {
|
||||||
t.Error(diff)
|
t.Error(diff)
|
||||||
|
@ -6,6 +6,9 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"offline_twitter/persistence"
|
"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).
|
* Should refuse to create a Profile if the target already exists (i.e., is a file or directory).
|
||||||
*/
|
*/
|
||||||
func TestNewProfileInvalidPath(t *testing.T) {
|
func TestNewProfileInvalidPath(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
gibberish_path := "test_profiles/fjlwrefuvaaw23efwm"
|
gibberish_path := "test_profiles/fjlwrefuvaaw23efwm"
|
||||||
if file_exists(gibberish_path) {
|
if file_exists(gibberish_path) {
|
||||||
err := os.RemoveAll(gibberish_path)
|
err := os.RemoveAll(gibberish_path)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
err := os.Mkdir(gibberish_path, 0755)
|
err := os.Mkdir(gibberish_path, 0755)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
_, err = persistence.NewProfile(gibberish_path)
|
_, err = persistence.NewProfile(gibberish_path)
|
||||||
if err == nil {
|
require.Error(err, "Should have failed to create a profile in an already existing directory!")
|
||||||
t.Errorf("Should have failed to create a profile in an already existing directory!")
|
|
||||||
}
|
_, is_right_type := err.(persistence.ErrTargetAlreadyExists)
|
||||||
if _, is_right_type := err.(persistence.ErrTargetAlreadyExists); !is_right_type {
|
assert.True(t, is_right_type, "Expected 'ErrTargetAlreadyExists' error, got %T instead", err)
|
||||||
t.Errorf("Expected 'ErrTargetAlreadyExists' error, got %T instead", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -58,34 +57,27 @@ func TestNewProfileInvalidPath(t *testing.T) {
|
|||||||
* Should correctly create a new Profile
|
* Should correctly create a new Profile
|
||||||
*/
|
*/
|
||||||
func TestNewProfile(t *testing.T) {
|
func TestNewProfile(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
require := require.New(t)
|
||||||
|
|
||||||
profile_path := "test_profiles/TestNewProfile"
|
profile_path := "test_profiles/TestNewProfile"
|
||||||
if file_exists(profile_path) {
|
if file_exists(profile_path) {
|
||||||
err := os.RemoveAll(profile_path)
|
err := os.RemoveAll(profile_path)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
profile, err := persistence.NewProfile(profile_path)
|
profile, err := persistence.NewProfile(profile_path)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if profile.ProfileDir != profile_path {
|
assert.Equal(profile_path,profile.ProfileDir)
|
||||||
t.Errorf("ProfileDir should be %s, but it is %s", profile_path, profile.ProfileDir)
|
|
||||||
}
|
|
||||||
if len(profile.UsersList) != 0 {
|
if len(profile.UsersList) != 0 {
|
||||||
t.Errorf("Expected empty users list, got %v instead", profile.UsersList)
|
t.Errorf("Expected empty users list, got %v instead", profile.UsersList)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check files were created
|
// Check files were created
|
||||||
contents, err := os.ReadDir(profile_path)
|
contents, err := os.ReadDir(profile_path)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
panic(err)
|
assert.Len(contents, 8)
|
||||||
}
|
|
||||||
if len(contents) != 8 {
|
|
||||||
t.Fatalf("Expected 8 contents, got %d instead", len(contents))
|
|
||||||
}
|
|
||||||
|
|
||||||
expected_files := []struct {
|
expected_files := []struct {
|
||||||
filename string
|
filename string
|
||||||
@ -102,19 +94,14 @@ func TestNewProfile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i, v := range expected_files {
|
for i, v := range expected_files {
|
||||||
if contents[i].Name() != v.filename || contents[i].IsDir() != v.isDir {
|
assert.Equal(v.filename, contents[i].Name())
|
||||||
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.isDir, contents[i].IsDir())
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check database version is initialized
|
// Check database version is initialized
|
||||||
version, err := profile.GetDatabaseVersion()
|
version, err := profile.GetDatabaseVersion()
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
panic(err)
|
assert.Equal(persistence.ENGINE_DATABASE_VERSION, version)
|
||||||
}
|
|
||||||
if version != persistence.ENGINE_DATABASE_VERSION {
|
|
||||||
t.Errorf("Expected database version %d, but got %d", persistence.ENGINE_DATABASE_VERSION, version)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -122,38 +109,25 @@ func TestNewProfile(t *testing.T) {
|
|||||||
* Should correctly load the Profile
|
* Should correctly load the Profile
|
||||||
*/
|
*/
|
||||||
func TestLoadProfile(t *testing.T) {
|
func TestLoadProfile(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
|
|
||||||
profile_path := "test_profiles/TestLoadProfile"
|
profile_path := "test_profiles/TestLoadProfile"
|
||||||
if file_exists(profile_path) {
|
if file_exists(profile_path) {
|
||||||
err := os.RemoveAll(profile_path)
|
err := os.RemoveAll(profile_path)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := persistence.NewProfile(profile_path)
|
_, err := persistence.NewProfile(profile_path)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create some users
|
// Create some users
|
||||||
err = os.WriteFile(path.Join(profile_path, "users.yaml"), []byte("- user: user1\n- user: user2\n"), 0644)
|
err = os.WriteFile(path.Join(profile_path, "users.yaml"), []byte("- user: user1\n- user: user2\n"), 0644)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
profile, err := persistence.LoadProfile(profile_path)
|
profile, err := persistence.LoadProfile(profile_path)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if profile.ProfileDir != profile_path {
|
assert.Equal(t, profile_path, profile.ProfileDir)
|
||||||
t.Errorf("Expected profile path to be %q, but got %q", profile_path, profile.ProfileDir)
|
assert.Len(profile.UsersList, 2)
|
||||||
}
|
assert.Equal("user1", profile.UsersList[0].Handle)
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -3,35 +3,33 @@ package persistence_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/go-test/deep"
|
"github.com/go-test/deep"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func TestSaveAndLoadRetweet(t *testing.T) {
|
func TestSaveAndLoadRetweet(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
|
|
||||||
profile_path := "test_profiles/TestRetweetQueries"
|
profile_path := "test_profiles/TestRetweetQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
tweet := create_dummy_tweet()
|
tweet := create_dummy_tweet()
|
||||||
err := profile.SaveTweet(tweet)
|
err := profile.SaveTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Failed to save the tweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
rt := create_dummy_retweet(tweet.ID)
|
rt := create_dummy_retweet(tweet.ID)
|
||||||
|
|
||||||
// Save the Retweet
|
// Save the Retweet
|
||||||
err = profile.SaveRetweet(rt)
|
err = profile.SaveRetweet(rt)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Failed to save the retweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload the Retweet
|
// Reload the Retweet
|
||||||
new_rt, err := profile.GetRetweetById(rt.RetweetID)
|
new_rt, err := profile.GetRetweetById(rt.RetweetID)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Failed to load the retweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if diff := deep.Equal(rt, new_rt); diff != nil {
|
if diff := deep.Equal(rt, new_rt); diff != nil {
|
||||||
t.Error(diff)
|
t.Error(diff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/go-test/deep"
|
"github.com/go-test/deep"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -20,15 +23,11 @@ func TestSaveAndLoadTweet(t *testing.T) {
|
|||||||
|
|
||||||
// Save the tweet
|
// Save the tweet
|
||||||
err := profile.SaveTweet(tweet)
|
err := profile.SaveTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Failed to save the tweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload the tweet
|
// Reload the tweet
|
||||||
new_tweet, err := profile.GetTweetById(tweet.ID)
|
new_tweet, err := profile.GetTweetById(tweet.ID)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Failed to load the tweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if diff := deep.Equal(tweet, new_tweet); diff != nil {
|
if diff := deep.Equal(tweet, new_tweet); diff != nil {
|
||||||
t.Error(diff)
|
t.Error(diff)
|
||||||
@ -46,15 +45,11 @@ func TestSaveAndLoadTombstone(t *testing.T) {
|
|||||||
|
|
||||||
// Save the tweet
|
// Save the tweet
|
||||||
err := profile.SaveTweet(tweet)
|
err := profile.SaveTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Failed to save the tweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload the tweet
|
// Reload the tweet
|
||||||
new_tweet, err := profile.GetTweetById(tweet.ID)
|
new_tweet, err := profile.GetTweetById(tweet.ID)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Failed to load the tweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if diff := deep.Equal(tweet, new_tweet); diff != nil {
|
if diff := deep.Equal(tweet, new_tweet); diff != nil {
|
||||||
t.Error(diff)
|
t.Error(diff)
|
||||||
@ -70,6 +65,9 @@ func TestSaveAndLoadTombstone(t *testing.T) {
|
|||||||
* - is_content_downloaded should only go from "no" to "yes"
|
* - is_content_downloaded should only go from "no" to "yes"
|
||||||
*/
|
*/
|
||||||
func TestNoWorseningTweet(t *testing.T) {
|
func TestNoWorseningTweet(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
require := require.New(t)
|
||||||
|
|
||||||
profile_path := "test_profiles/TestTweetQueries"
|
profile_path := "test_profiles/TestTweetQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
@ -81,9 +79,7 @@ func TestNoWorseningTweet(t *testing.T) {
|
|||||||
|
|
||||||
// Save the tweet
|
// Save the tweet
|
||||||
err := profile.SaveTweet(tweet)
|
err := profile.SaveTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Failed to save the tweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Worsen the tweet and re-save it
|
// Worsen the tweet and re-save it
|
||||||
tweet.IsContentDownloaded = false
|
tweet.IsContentDownloaded = false
|
||||||
@ -91,31 +87,22 @@ func TestNoWorseningTweet(t *testing.T) {
|
|||||||
tweet.IsConversationScraped = false
|
tweet.IsConversationScraped = false
|
||||||
tweet.LastScrapedAt = time.Unix(500, 0)
|
tweet.LastScrapedAt = time.Unix(500, 0)
|
||||||
err = profile.SaveTweet(tweet)
|
err = profile.SaveTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Failed to save the tweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload the tweet
|
// Reload the tweet
|
||||||
new_tweet, err := profile.GetTweetById(tweet.ID)
|
new_tweet, err := profile.GetTweetById(tweet.ID)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Failed to load the tweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if new_tweet.IsStub != false {
|
assert.False(new_tweet.IsStub, "Should have preserved non-stub status")
|
||||||
t.Errorf("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")
|
||||||
if new_tweet.IsContentDownloaded != true {
|
assert.Equal(int64(1000), new_tweet.LastScrapedAt.Unix(), "Should have preserved last-scraped-at time")
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestModifyTweet(t *testing.T) {
|
func TestModifyTweet(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
require := require.New(t)
|
||||||
|
|
||||||
profile_path := "test_profiles/TestTweetQueries"
|
profile_path := "test_profiles/TestTweetQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
@ -130,9 +117,7 @@ func TestModifyTweet(t *testing.T) {
|
|||||||
tweet.LastScrapedAt = time.Unix(1000, 0)
|
tweet.LastScrapedAt = time.Unix(1000, 0)
|
||||||
|
|
||||||
err := profile.SaveTweet(tweet)
|
err := profile.SaveTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Failed to save the tweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
tweet.NumLikes = 1500
|
tweet.NumLikes = 1500
|
||||||
tweet.NumRetweets = 2500
|
tweet.NumRetweets = 2500
|
||||||
@ -144,69 +129,47 @@ func TestModifyTweet(t *testing.T) {
|
|||||||
tweet.LastScrapedAt = time.Unix(2000, 0)
|
tweet.LastScrapedAt = time.Unix(2000, 0)
|
||||||
|
|
||||||
err = profile.SaveTweet(tweet)
|
err = profile.SaveTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Failed to re-save the tweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload the tweet
|
// Reload the tweet
|
||||||
new_tweet, err := profile.GetTweetById(tweet.ID)
|
new_tweet, err := profile.GetTweetById(tweet.ID)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Fatalf("Failed to load the tweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if new_tweet.NumLikes != 1500 {
|
assert.Equal(1500, new_tweet.NumLikes)
|
||||||
t.Errorf("Expected %d likes, got %d", 1500, new_tweet.NumLikes)
|
assert.Equal(2500, new_tweet.NumRetweets)
|
||||||
}
|
assert.Equal(3500, new_tweet.NumReplies)
|
||||||
if new_tweet.NumRetweets != 2500 {
|
assert.Equal(4500, new_tweet.NumQuoteTweets)
|
||||||
t.Errorf("Expected %d retweets, got %d", 2500, new_tweet.NumRetweets)
|
assert.False(new_tweet.IsStub)
|
||||||
}
|
assert.True(new_tweet.IsContentDownloaded)
|
||||||
if new_tweet.NumReplies != 3500 {
|
assert.True(new_tweet.IsConversationScraped)
|
||||||
t.Errorf("Expected %d replies, got %d", 3500, new_tweet.NumReplies)
|
assert.Equal(int64(2000), new_tweet.LastScrapedAt.Unix())
|
||||||
}
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should correctly report whether the User exists in the database
|
* Should correctly report whether the User exists in the database
|
||||||
*/
|
*/
|
||||||
func TestIsTweetInDatabase(t *testing.T) {
|
func TestIsTweetInDatabase(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
profile_path := "test_profiles/TestTweetQueries"
|
profile_path := "test_profiles/TestTweetQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
tweet := create_dummy_tweet()
|
tweet := create_dummy_tweet()
|
||||||
|
|
||||||
exists := profile.IsTweetInDatabase(tweet.ID)
|
exists := profile.IsTweetInDatabase(tweet.ID)
|
||||||
if exists {
|
require.False(exists)
|
||||||
t.Errorf("It shouldn't exist, but it does: %d", tweet.ID)
|
|
||||||
}
|
|
||||||
err := profile.SaveTweet(tweet)
|
err := profile.SaveTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
exists = profile.IsTweetInDatabase(tweet.ID)
|
exists = profile.IsTweetInDatabase(tweet.ID)
|
||||||
if !exists {
|
assert.True(t, exists)
|
||||||
t.Errorf("It should exist, but it doesn't: %d", tweet.ID)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should correctly populate the `User` field on a Tweet
|
* Should correctly populate the `User` field on a Tweet
|
||||||
*/
|
*/
|
||||||
func TestLoadUserForTweet(t *testing.T) {
|
func TestLoadUserForTweet(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
profile_path := "test_profiles/TestTweetQueries"
|
profile_path := "test_profiles/TestTweetQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
@ -214,29 +177,19 @@ func TestLoadUserForTweet(t *testing.T) {
|
|||||||
|
|
||||||
// Save the tweet
|
// Save the tweet
|
||||||
err := profile.SaveTweet(tweet)
|
err := profile.SaveTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Errorf("Failed to save the tweet: %s", err.Error())
|
require.Nil(tweet.User, "`User` field is already there for some reason")
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if tweet.User != nil {
|
|
||||||
t.Errorf("`User` field is already there for some reason: %v", tweet.User)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = profile.LoadUserFor(&tweet)
|
err = profile.LoadUserFor(&tweet)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
t.Errorf("Failed to load user: %s", err.Error())
|
require.NotNil(tweet.User, "Did not load a user. It is still nil.")
|
||||||
}
|
|
||||||
|
|
||||||
if tweet.User == nil {
|
|
||||||
t.Errorf("Did not load a user. It is still nil.")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test all the combinations for whether a tweet needs its content downloaded
|
* Test all the combinations for whether a tweet needs its content downloaded
|
||||||
*/
|
*/
|
||||||
func TestCheckTweetContentDownloadNeeded(t *testing.T) {
|
func TestCheckTweetContentDownloadNeeded(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
profile_path := "test_profiles/TestTweetQueries"
|
profile_path := "test_profiles/TestTweetQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
@ -244,30 +197,20 @@ func TestCheckTweetContentDownloadNeeded(t *testing.T) {
|
|||||||
tweet.IsContentDownloaded = false
|
tweet.IsContentDownloaded = false
|
||||||
|
|
||||||
// Non-saved tweets should need to be downloaded
|
// Non-saved tweets should need to be downloaded
|
||||||
if profile.CheckTweetContentDownloadNeeded(tweet) != true {
|
assert.True(profile.CheckTweetContentDownloadNeeded(tweet))
|
||||||
t.Errorf("Non-saved tweets should need a download")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save the tweet
|
// Save the tweet
|
||||||
err := profile.SaveTweet(tweet)
|
err := profile.SaveTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Errorf("Failed to save the tweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Should still need a download since `is_content_downloaded` is false
|
// Should still need a download since `is_content_downloaded` is false
|
||||||
if profile.CheckTweetContentDownloadNeeded(tweet) != true {
|
assert.True(profile.CheckTweetContentDownloadNeeded(tweet))
|
||||||
t.Errorf("Non-downloaded tweets should need a download")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try again but this time with `is_content_downloaded` = true
|
// Try again but this time with `is_content_downloaded` = true
|
||||||
tweet.IsContentDownloaded = true
|
tweet.IsContentDownloaded = true
|
||||||
err = profile.SaveTweet(tweet)
|
err = profile.SaveTweet(tweet)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Errorf("Failed to save the tweet: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Should no longer need a download
|
// Should no longer need a download
|
||||||
if profile.CheckTweetContentDownloadNeeded(tweet) != false {
|
assert.False(profile.CheckTweetContentDownloadNeeded(tweet))
|
||||||
t.Errorf("Downloaded tweets shouldn't need content downloaded")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
* Create a user, save it, reload it, and make sure it comes back the same
|
* Create a user, save it, reload it, and make sure it comes back the same
|
||||||
*/
|
*/
|
||||||
func TestSaveAndLoadUser(t *testing.T) {
|
func TestSaveAndLoadUser(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
profile_path := "test_profiles/TestUserQueries"
|
profile_path := "test_profiles/TestUserQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
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
|
// Save the user, then reload it and ensure it's the same
|
||||||
err := profile.SaveUser(&fake_user)
|
err := profile.SaveUser(&fake_user)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
new_fake_user, err := profile.GetUserByID(fake_user.ID)
|
new_fake_user, err := profile.GetUserByID(fake_user.ID)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if diff := deep.Equal(new_fake_user, fake_user); diff != nil {
|
if diff := deep.Equal(new_fake_user, fake_user); diff != nil {
|
||||||
t.Error(diff)
|
t.Error(diff)
|
||||||
@ -40,9 +38,7 @@ func TestSaveAndLoadUser(t *testing.T) {
|
|||||||
|
|
||||||
// Same thing, but get by handle
|
// Same thing, but get by handle
|
||||||
new_fake_user2, err := profile.GetUserByHandle(fake_user.Handle)
|
new_fake_user2, err := profile.GetUserByHandle(fake_user.Handle)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if diff := deep.Equal(new_fake_user2, fake_user); diff != nil {
|
if diff := deep.Equal(new_fake_user2, fake_user); diff != nil {
|
||||||
t.Error(diff)
|
t.Error(diff)
|
||||||
@ -53,6 +49,9 @@ func TestSaveAndLoadUser(t *testing.T) {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
func TestModifyUser(t *testing.T) {
|
func TestModifyUser(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
require := require.New(t)
|
||||||
|
|
||||||
profile_path := "test_profiles/TestUserQueries"
|
profile_path := "test_profiles/TestUserQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
@ -69,9 +68,7 @@ func TestModifyUser(t *testing.T) {
|
|||||||
|
|
||||||
// Save the user so it can be modified
|
// Save the user so it can be modified
|
||||||
err := profile.SaveUser(&fake_user)
|
err := profile.SaveUser(&fake_user)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fake_user.DisplayName = "Display Name 2"
|
fake_user.DisplayName = "Display Name 2"
|
||||||
@ -86,42 +83,21 @@ func TestModifyUser(t *testing.T) {
|
|||||||
|
|
||||||
// Save the modified user
|
// Save the modified user
|
||||||
err = profile.SaveUser(&fake_user)
|
err = profile.SaveUser(&fake_user)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
// Reload the modified user
|
// Reload the modified user
|
||||||
new_fake_user, err := profile.GetUserByID(fake_user.ID)
|
new_fake_user, err := profile.GetUserByID(fake_user.ID)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if new_fake_user.DisplayName != "Display Name 2" {
|
assert.Equal("Display Name 2", new_fake_user.DisplayName)
|
||||||
t.Errorf("Expected display name %q, got %q", "Display Name 2", new_fake_user.DisplayName)
|
assert.Equal("location2", new_fake_user.Location)
|
||||||
}
|
assert.True(new_fake_user.IsPrivate)
|
||||||
if new_fake_user.Location != "location2" {
|
assert.True(new_fake_user.IsVerified)
|
||||||
t.Errorf("Expected location %q, got %q", "location2", new_fake_user.Location)
|
assert.True(new_fake_user.IsBanned)
|
||||||
}
|
assert.Equal(2000, new_fake_user.FollowersCount)
|
||||||
if new_fake_user.IsPrivate != true {
|
assert.Equal(int64(1000), new_fake_user.JoinDate.Unix())
|
||||||
t.Errorf("Should be private")
|
assert.Equal("asdf2", new_fake_user.ProfileImageUrl)
|
||||||
}
|
assert.True(new_fake_user.IsContentDownloaded)
|
||||||
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)")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleIsCaseInsensitive(t *testing.T) {
|
func TestHandleIsCaseInsensitive(t *testing.T) {
|
||||||
@ -131,9 +107,7 @@ func TestHandleIsCaseInsensitive(t *testing.T) {
|
|||||||
user := create_stable_user()
|
user := create_stable_user()
|
||||||
|
|
||||||
new_user, err := profile.GetUserByHandle("hANdle StaBlE")
|
new_user, err := profile.GetUserByHandle("hANdle StaBlE")
|
||||||
if err != nil {
|
require.NoError(t, err, "Couldn't find the user")
|
||||||
t.Fatalf("Couldn't find the user: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if diff := deep.Equal(user, new_user); diff != nil {
|
if diff := deep.Equal(user, new_user); diff != nil {
|
||||||
t.Error(diff)
|
t.Error(diff)
|
||||||
@ -145,72 +119,58 @@ func TestHandleIsCaseInsensitive(t *testing.T) {
|
|||||||
* Should correctly report whether the user exists in the database
|
* Should correctly report whether the user exists in the database
|
||||||
*/
|
*/
|
||||||
func TestUserExists(t *testing.T) {
|
func TestUserExists(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
profile_path := "test_profiles/TestUserQueries"
|
profile_path := "test_profiles/TestUserQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
user := create_dummy_user()
|
user := create_dummy_user()
|
||||||
|
|
||||||
exists := profile.UserExists(user.Handle)
|
exists := profile.UserExists(user.Handle)
|
||||||
if exists {
|
require.False(exists)
|
||||||
t.Errorf("It shouldn't exist, but it does: %d", user.ID)
|
|
||||||
}
|
|
||||||
err := profile.SaveUser(&user)
|
err := profile.SaveUser(&user)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
exists = profile.UserExists(user.Handle)
|
exists = profile.UserExists(user.Handle)
|
||||||
if !exists {
|
require.True(exists)
|
||||||
t.Errorf("It should exist, but it doesn't: %d", user.ID)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test scenarios relating to user content downloading
|
* Test scenarios relating to user content downloading
|
||||||
*/
|
*/
|
||||||
func TestCheckUserContentDownloadNeeded(t *testing.T) {
|
func TestCheckUserContentDownloadNeeded(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
profile_path := "test_profiles/TestUserQueries"
|
profile_path := "test_profiles/TestUserQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
user := create_dummy_user()
|
user := create_dummy_user()
|
||||||
|
|
||||||
// If user is not in database, should be "yes" automatically
|
// If user is not in database, should be "yes" automatically
|
||||||
if profile.CheckUserContentDownloadNeeded(user) != true {
|
assert.True(profile.CheckUserContentDownloadNeeded(user))
|
||||||
t.Errorf("Non-saved user should always require content download")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save the user, but `is_content_downloaded` is still "false"
|
// Save the user, but `is_content_downloaded` is still "false"
|
||||||
user.BannerImageUrl = "banner url1"
|
user.BannerImageUrl = "banner url1"
|
||||||
user.ProfileImageUrl = "profile url1"
|
user.ProfileImageUrl = "profile url1"
|
||||||
user.IsContentDownloaded = false
|
user.IsContentDownloaded = false
|
||||||
err := profile.SaveUser(&user)
|
err := profile.SaveUser(&user)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// If is_content_downloaded is false, then it needs download
|
// If is_content_downloaded is false, then it needs download
|
||||||
if profile.CheckUserContentDownloadNeeded(user) != true {
|
assert.True(profile.CheckUserContentDownloadNeeded(user))
|
||||||
t.Errorf("Non-downloaded user should require download")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mark `is_content_downloaded` as "true" again
|
// Mark `is_content_downloaded` as "true" again
|
||||||
user.IsContentDownloaded = true
|
user.IsContentDownloaded = true
|
||||||
err = profile.SaveUser(&user)
|
err = profile.SaveUser(&user)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// If everything is up to date, no download should be required
|
// If everything is up to date, no download should be required
|
||||||
if profile.CheckUserContentDownloadNeeded(user) != false {
|
assert.False(profile.CheckUserContentDownloadNeeded(user))
|
||||||
t.Errorf("Up-to-date user shouldn't need a download")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Change an URL, but don't save it-- needs to be different from what's in the DB
|
// Change an URL, but don't save it-- needs to be different from what's in the DB
|
||||||
user.BannerImageUrl = "banner url2"
|
user.BannerImageUrl = "banner url2"
|
||||||
|
|
||||||
// Download needed for new banner image
|
// Download needed for new banner image
|
||||||
if profile.CheckUserContentDownloadNeeded(user) != true {
|
assert.True(profile.CheckUserContentDownloadNeeded(user))
|
||||||
t.Errorf("If banner image changed, user should require another download")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,34 +3,30 @@ package persistence_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"offline_twitter/scraper"
|
"offline_twitter/scraper"
|
||||||
"offline_twitter/persistence"
|
"offline_twitter/persistence"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestVersionUpgrade(t *testing.T) {
|
func TestVersionUpgrade(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
profile_path := "test_profiles/TestVersions"
|
profile_path := "test_profiles/TestVersions"
|
||||||
if file_exists(profile_path) {
|
if file_exists(profile_path) {
|
||||||
err := os.RemoveAll(profile_path)
|
err := os.RemoveAll(profile_path)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
test_migration := "insert into tweets (id, user_id, text) values (21250554358298342, -1, 'awefjk')"
|
test_migration := "insert into tweets (id, user_id, text) values (21250554358298342, -1, 'awefjk')"
|
||||||
test_tweet_id := scraper.TweetID(21250554358298342)
|
test_tweet_id := scraper.TweetID(21250554358298342)
|
||||||
|
|
||||||
if profile.IsTweetInDatabase(test_tweet_id) {
|
require.False(profile.IsTweetInDatabase(test_tweet_id), "Test tweet shouldn't be in db yet")
|
||||||
t.Fatalf("Test tweet shouldn't be in the database yet")
|
|
||||||
}
|
|
||||||
|
|
||||||
persistence.MIGRATIONS = append(persistence.MIGRATIONS, test_migration)
|
persistence.MIGRATIONS = append(persistence.MIGRATIONS, test_migration)
|
||||||
err := profile.UpgradeFromXToY(persistence.ENGINE_DATABASE_VERSION, persistence.ENGINE_DATABASE_VERSION + 1)
|
err := profile.UpgradeFromXToY(persistence.ENGINE_DATABASE_VERSION, persistence.ENGINE_DATABASE_VERSION + 1)
|
||||||
if err != nil {
|
require.NoError(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !profile.IsTweetInDatabase(test_tweet_id) {
|
require.True(profile.IsTweetInDatabase(test_tweet_id), "Migration should have created the tweet, but it didn't")
|
||||||
t.Errorf("Migration should have created the tweet, but it didn't")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user