From 7db981398cc554f310d870cbfd2042d0533a600b Mon Sep 17 00:00:00 2001 From: Alessio Date: Sat, 24 Jul 2021 21:48:01 -0700 Subject: [PATCH] Create test utils file for fixtures and helper functions --- persistence/user_queries_test.go | 57 ++--------------------- persistence/utils_test.go | 78 ++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 52 deletions(-) create mode 100644 persistence/utils_test.go diff --git a/persistence/user_queries_test.go b/persistence/user_queries_test.go index ea52c07..3778b2d 100644 --- a/persistence/user_queries_test.go +++ b/persistence/user_queries_test.go @@ -1,35 +1,13 @@ package persistence_test import ( - "fmt" "testing" - "time" - "math/rand" "github.com/go-test/deep" "offline_twitter/scraper" - "offline_twitter/persistence" ) -/** - * Helper function - */ -func create_or_load_profile(profile_path string) persistence.Profile { - var profile persistence.Profile - var err error - - if !file_exists(profile_path) { - profile, err = persistence.NewProfile(profile_path) - } else { - profile, err = persistence.LoadProfile(profile_path) - } - if err != nil { - panic(err) - } - return profile -} - /** * Create a user, save it, reload it, and make sure it comes back the same @@ -38,33 +16,14 @@ func TestSaveAndLoadUser(t *testing.T) { profile_path := "test_profiles/TestUserQueries" profile := create_or_load_profile(profile_path) - // Generate a new random user ID - rand.Seed(time.Now().UnixNano()) - userID := fmt.Sprint(rand.Int()) - - fake_user := scraper.User{ - ID: scraper.UserID(userID), - DisplayName: "display name", - Handle: scraper.UserHandle("handle" + userID), - Bio: "bio", - FollowersCount: 0, - FollowingCount: 1000, - Location: "location", - Website:"website", - JoinDate: time.Now().Truncate(1e9), // Round to nearest second - IsVerified: false, - IsPrivate: true, - ProfileImageUrl: "profile image url", - BannerImageUrl: "banner image url", - PinnedTweetID: scraper.TweetID("234"), - } + fake_user := create_dummy_user() // Save the user, then reload it and ensure it's the same err := profile.SaveUser(fake_user) if err != nil { panic(err) } - new_fake_user, err := profile.GetUserByID(scraper.UserID(userID)) + new_fake_user, err := profile.GetUserByID(fake_user.ID) if err != nil { panic(err) } @@ -92,17 +51,11 @@ func TestUserExists(t *testing.T) { profile_path := "test_profiles/TestUserQueries" profile := create_or_load_profile(profile_path) - // Generate a new random user ID - rand.Seed(time.Now().UnixNano()) - userID := fmt.Sprint(rand.Int()) - - user := scraper.User{} - user.ID = scraper.UserID(userID) - user.Handle = scraper.UserHandle("handle" + userID) + user := create_dummy_user() exists := profile.UserExists(scraper.UserHandle(user.Handle)) if exists { - t.Errorf("It shouldn't exist, but it does: %s", userID) + t.Errorf("It shouldn't exist, but it does: %s", user.ID) } err := profile.SaveUser(user) if err != nil { @@ -110,6 +63,6 @@ func TestUserExists(t *testing.T) { } exists = profile.UserExists(scraper.UserHandle(user.Handle)) if !exists { - t.Errorf("It should exist, but it doesn't: %s", userID) + t.Errorf("It should exist, but it doesn't: %s", user.ID) } } diff --git a/persistence/utils_test.go b/persistence/utils_test.go new file mode 100644 index 0000000..1585f4f --- /dev/null +++ b/persistence/utils_test.go @@ -0,0 +1,78 @@ +package persistence_test + +import ( + "time" + "fmt" + "math/rand" + + "offline_twitter/scraper" + "offline_twitter/persistence" +) + +/** + * Load a test profile, or create it if it doesn't exist + */ +func create_or_load_profile(profile_path string) persistence.Profile { + var profile persistence.Profile + var err error + + if !file_exists(profile_path) { + profile, err = persistence.NewProfile(profile_path) + } else { + profile, err = persistence.LoadProfile(profile_path) + } + if err != nil { + panic(err) + } + return profile +} + +/** + * Create a new user with a random ID and handle + */ +func create_dummy_user() scraper.User { + rand.Seed(time.Now().UnixNano()) + userID := fmt.Sprint(rand.Int()) + + return scraper.User{ + ID: scraper.UserID(userID), + DisplayName: "display name", + Handle: scraper.UserHandle("handle" + userID), + Bio: "bio", + FollowersCount: 0, + FollowingCount: 1000, + Location: "location", + Website:"website", + JoinDate: time.Now().Truncate(1e9), // Round to nearest second + IsVerified: false, + IsPrivate: true, + ProfileImageUrl: "profile image url", + BannerImageUrl: "banner image url", + PinnedTweetID: scraper.TweetID("234"), + } +} + + +/** + * Create a new tweet with a random ID and content + */ +func create_dummy_tweet() scraper.Tweet { + rand.Seed(time.Now().UnixNano()) + tweet_id := fmt.Sprint(rand.Int()) + + return scraper.Tweet{ + ID: scraper.TweetID(tweet_id), + UserID: "user", + Text: "text", + PostedAt: time.Now().Truncate(1e9), // Round to nearest second + NumLikes: 1, + NumRetweets: 2, + NumReplies: 3, + NumQuoteTweets: 4, + Video: "video", + Urls: []string{"url1", "url2"}, + Images: []string{"image1", "image2"}, + Mentions: []scraper.UserHandle{"mention1", "mention2"}, + Hashtags: []string{"hash1", "hash2"}, + } +}