Create test fixtures (stable test objects)

This commit is contained in:
Alessio 2021-07-30 19:38:25 -07:00
parent 7f286d0596
commit 35cf1f1296
2 changed files with 66 additions and 31 deletions

View File

@ -4,9 +4,9 @@ import (
"testing" "testing"
"github.com/go-test/deep" "github.com/go-test/deep"
) )
/** /**
* Create a Tweet, save it, reload it, and make sure it comes back the same * Create a Tweet, save it, reload it, and make sure it comes back the same
*/ */
@ -15,18 +15,11 @@ func TestSaveAndLoadTweet(t *testing.T) {
profile := create_or_load_profile(profile_path) profile := create_or_load_profile(profile_path)
tweet := create_dummy_tweet() tweet := create_dummy_tweet()
user := create_dummy_user() user := create_stable_user()
tweet.UserID = user.ID tweet.UserID = user.ID
// Save the user
err := profile.SaveUser(user)
if err != nil {
t.Fatalf("Failed to save the user, so no point in continuing the test: %s", err.Error())
}
// Save the tweet // Save the tweet
err = profile.SaveTweet(tweet) err := profile.SaveTweet(tweet)
if err != nil { if err != nil {
t.Fatalf("Failed to save the tweet: %s", err.Error()) t.Fatalf("Failed to save the tweet: %s", err.Error())
} }
@ -50,20 +43,14 @@ func TestIsTweetInDatabase(t *testing.T) {
profile := create_or_load_profile(profile_path) profile := create_or_load_profile(profile_path)
tweet := create_dummy_tweet() tweet := create_dummy_tweet()
user := create_dummy_user() user := create_stable_user()
tweet.UserID = user.ID tweet.UserID = user.ID
// Save the user
err := profile.SaveUser(user)
if err != nil {
t.Fatalf("Failed to save the user, so no point in continuing the test: %s", err.Error())
}
exists := profile.IsTweetInDatabase(tweet.ID) exists := profile.IsTweetInDatabase(tweet.ID)
if exists { if exists {
t.Errorf("It shouldn't exist, but it does: %s", tweet.ID) t.Errorf("It shouldn't exist, but it does: %s", tweet.ID)
} }
err = profile.SaveTweet(tweet) err := profile.SaveTweet(tweet)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -81,18 +68,11 @@ func TestLoadUserForTweet(t *testing.T) {
profile := create_or_load_profile(profile_path) profile := create_or_load_profile(profile_path)
tweet := create_dummy_tweet() tweet := create_dummy_tweet()
user := create_dummy_user() user := create_stable_user()
tweet.UserID = user.ID tweet.UserID = user.ID
// Save the user
err := profile.SaveUser(user)
if err != nil {
t.Fatalf("Failed to save the user, so no point in continuing the test: %s", err.Error())
}
// Save the tweet // Save the tweet
err = profile.SaveTweet(tweet) err := profile.SaveTweet(tweet)
if err != nil { if err != nil {
t.Errorf("Failed to save the tweet: %s", err.Error()) t.Errorf("Failed to save the tweet: %s", err.Error())
} }

View File

@ -10,7 +10,7 @@ import (
) )
/** /**
* Load a test profile, or create it if it doesn't exist * Load a test profile, or create it if it doesn't exist.
*/ */
func create_or_load_profile(profile_path string) persistence.Profile { func create_or_load_profile(profile_path string) persistence.Profile {
var profile persistence.Profile var profile persistence.Profile
@ -18,6 +18,14 @@ func create_or_load_profile(profile_path string) persistence.Profile {
if !file_exists(profile_path) { if !file_exists(profile_path) {
profile, err = persistence.NewProfile(profile_path) profile, err = persistence.NewProfile(profile_path)
if err != nil {
panic(err)
}
err = profile.SaveUser(create_stable_user())
if err != nil {
panic(err)
}
err = profile.SaveTweet(create_stable_tweet())
} else { } else {
profile, err = persistence.LoadProfile(profile_path) profile, err = persistence.LoadProfile(profile_path)
} }
@ -27,6 +35,53 @@ func create_or_load_profile(profile_path string) persistence.Profile {
return profile return profile
} }
/**
* Create a stable user with a fixed ID and handle
*/
func create_stable_user() scraper.User {
return scraper.User{
ID: scraper.UserID("-1"),
DisplayName: "stable display name",
Handle: scraper.UserHandle("handle stable"),
Bio: "stable bio",
FollowersCount: 10,
FollowingCount: 2000,
Location: "stable location",
Website:"stable website",
JoinDate: time.Unix(10000000, 0),
IsVerified: true,
IsPrivate: false,
ProfileImageUrl: "stable profile image url",
BannerImageUrl: "stable banner image url",
PinnedTweetID: scraper.TweetID("345"),
}
}
/**
* Create a stable tweet with a fixed ID and content
*/
func create_stable_tweet() scraper.Tweet {
tweet_id := scraper.TweetID("-1")
return scraper.Tweet{
ID: tweet_id,
UserID: "-1",
Text: "stable text",
PostedAt: time.Unix(10000000, 0),
NumLikes: 10,
NumRetweets: 10,
NumReplies: 10,
NumQuoteTweets: 10,
Videos: []scraper.Video{},
Urls: []string{},
Images: []scraper.Image{},
Mentions: []scraper.UserHandle{},
Hashtags: []string{},
}
}
/** /**
* Create a new user with a random ID and handle * Create a new user with a random ID and handle
*/ */
@ -69,11 +124,11 @@ func create_dummy_tweet() scraper.Tweet {
NumRetweets: 2, NumRetweets: 2,
NumReplies: 3, NumReplies: 3,
NumQuoteTweets: 4, NumQuoteTweets: 4,
Videos: []scraper.Video{scraper.Video{TweetID: tweet_id, Filename: "video", IsDownloaded: false}}, Videos: []scraper.Video{scraper.Video{TweetID: tweet_id, Filename: "video" + string(tweet_id), IsDownloaded: false}},
Urls: []string{"url1", "url2"}, Urls: []string{"url1", "url2"},
Images: []scraper.Image{ Images: []scraper.Image{
scraper.Image{TweetID: tweet_id, Filename: "image1", IsDownloaded: false}, scraper.Image{TweetID: tweet_id, Filename: "image1" + string(tweet_id), IsDownloaded: false},
scraper.Image{TweetID: tweet_id, Filename: "image2", IsDownloaded: false}, scraper.Image{TweetID: tweet_id, Filename: "image2" + string(tweet_id), IsDownloaded: false},
}, },
Mentions: []scraper.UserHandle{"mention1", "mention2"}, Mentions: []scraper.UserHandle{"mention1", "mention2"},
Hashtags: []string{"hash1", "hash2"}, Hashtags: []string{"hash1", "hash2"},