Create test utils file for fixtures and helper functions
This commit is contained in:
parent
bac66d767c
commit
7db981398c
@ -1,35 +1,13 @@
|
|||||||
package persistence_test
|
package persistence_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
"math/rand"
|
|
||||||
|
|
||||||
"github.com/go-test/deep"
|
"github.com/go-test/deep"
|
||||||
|
|
||||||
"offline_twitter/scraper"
|
"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
|
* 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_path := "test_profiles/TestUserQueries"
|
||||||
profile := create_or_load_profile(profile_path)
|
profile := create_or_load_profile(profile_path)
|
||||||
|
|
||||||
// Generate a new random user ID
|
fake_user := create_dummy_user()
|
||||||
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"),
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
new_fake_user, err := profile.GetUserByID(scraper.UserID(userID))
|
new_fake_user, err := profile.GetUserByID(fake_user.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -92,17 +51,11 @@ func TestUserExists(t *testing.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)
|
||||||
|
|
||||||
// Generate a new random user ID
|
user := create_dummy_user()
|
||||||
rand.Seed(time.Now().UnixNano())
|
|
||||||
userID := fmt.Sprint(rand.Int())
|
|
||||||
|
|
||||||
user := scraper.User{}
|
|
||||||
user.ID = scraper.UserID(userID)
|
|
||||||
user.Handle = scraper.UserHandle("handle" + userID)
|
|
||||||
|
|
||||||
exists := profile.UserExists(scraper.UserHandle(user.Handle))
|
exists := profile.UserExists(scraper.UserHandle(user.Handle))
|
||||||
if exists {
|
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)
|
err := profile.SaveUser(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -110,6 +63,6 @@ func TestUserExists(t *testing.T) {
|
|||||||
}
|
}
|
||||||
exists = profile.UserExists(scraper.UserHandle(user.Handle))
|
exists = profile.UserExists(scraper.UserHandle(user.Handle))
|
||||||
if !exists {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
78
persistence/utils_test.go
Normal file
78
persistence/utils_test.go
Normal file
@ -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"},
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user