Add test to ensure proper user images are being downloaded

This commit is contained in:
Alessio 2022-05-07 18:44:03 -07:00
parent 3b4b3fceb9
commit 8775f5337d
3 changed files with 81 additions and 9 deletions

View File

@ -153,14 +153,12 @@ func (p Profile) DownloadUserContentWithInjector(u *scraper.User, downloader Med
return nil return nil
} }
var outfile string outfile := p.get_profile_image_output_path(*u)
var target_url string
var target_url string
if u.ProfileImageUrl == "" { if u.ProfileImageUrl == "" {
outfile = path.Join(p.ProfileDir, "profile_images", path.Base(scraper.DEFAULT_PROFILE_IMAGE_URL))
target_url = scraper.DEFAULT_PROFILE_IMAGE_URL target_url = scraper.DEFAULT_PROFILE_IMAGE_URL
} else { } else {
outfile = path.Join(p.ProfileDir, "profile_images", u.ProfileImageLocalPath)
target_url = u.ProfileImageUrl target_url = u.ProfileImageUrl
} }
@ -171,7 +169,7 @@ func (p Profile) DownloadUserContentWithInjector(u *scraper.User, downloader Med
// Skip it if there's no banner image // Skip it if there's no banner image
if u.BannerImageLocalPath != "" { if u.BannerImageLocalPath != "" {
outfile = path.Join(p.ProfileDir, "profile_images", u.BannerImageLocalPath) outfile = p.get_banner_image_output_path(*u)
err = downloader.Curl(u.BannerImageUrl, outfile) err = downloader.Curl(u.BannerImageUrl, outfile)
if err != nil && strings.Contains(err.Error(), "404 Not Found") { if err != nil && strings.Contains(err.Error(), "404 Not Found") {

View File

@ -9,9 +9,36 @@ import (
"offline_twitter/scraper" "offline_twitter/scraper"
) )
type FakeDownloader struct{} /**
* Some types to spy on a MediaDownloader
*/
type SpyResult struct {
url string
outpath string
}
// TODO: doesn't need to be a struct, can just be the spy; also, the pointer might be unnecessary
type FakeDownloader struct {
Spy *[]SpyResult
}
func NewFakeDownloader() FakeDownloader {
ret := FakeDownloader{}
ret.Spy = &[]SpyResult{}
return ret
}
func (d FakeDownloader) Curl(url string, outpath string) error {
*d.Spy = append(*d.Spy, SpyResult{url, outpath})
return nil
}
func (d FakeDownloader) Contains(result SpyResult) bool {
for _, r := range *d.Spy {
if r == result {
return true
}
}
return false
}
func (d FakeDownloader) Curl(url string, outpath string) error { return nil }
func test_all_downloaded(tweet scraper.Tweet, yes_or_no bool, t *testing.T) { func test_all_downloaded(tweet scraper.Tweet, yes_or_no bool, t *testing.T) {
error_msg := map[bool]string{ error_msg := map[bool]string{
@ -53,7 +80,7 @@ func TestDownloadTweetContent(t *testing.T) {
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, NewFakeDownloader())
require.NoError(t, err) require.NoError(t, err)
// It should all be marked "yes downloaded" now // It should all be marked "yes downloaded" now
@ -83,9 +110,15 @@ func TestDownloadUserContent(t *testing.T) {
assert.False(user.IsContentDownloaded) assert.False(user.IsContentDownloaded)
// Do the (fake) downloading // Do the (fake) downloading
err = profile.DownloadUserContentWithInjector(&user, FakeDownloader{}) fake_downloader := NewFakeDownloader()
err = profile.DownloadUserContentWithInjector(&user, fake_downloader)
require.NoError(t, err) require.NoError(t, err)
// Check that the downloader was called with the appropriate stuff
assert.Len(*fake_downloader.Spy, 2)
assert.True(fake_downloader.Contains(SpyResult{"profile image url", "test_profiles/TestMediaQueries/profile_images/profile image local path"}))
assert.True(fake_downloader.Contains(SpyResult{"banner image url", "test_profiles/TestMediaQueries/profile_images/banner image local path"}))
// The User should now be marked "yes downloaded" // The User should now be marked "yes downloaded"
assert.True(user.IsContentDownloaded) assert.True(user.IsContentDownloaded)
@ -94,3 +127,26 @@ func TestDownloadUserContent(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
assert.True(new_user.IsContentDownloaded) assert.True(new_user.IsContentDownloaded)
} }
/**
* Should download the right stuff if User has no banner image and default profile image
*/
func TestDownloadDefaultUserContent(t *testing.T) {
assert := assert.New(t)
profile_path := "test_profiles/TestMediaQueries"
profile := create_or_load_profile(profile_path)
user := create_dummy_user()
user.BannerImageUrl = ""
user.BannerImageLocalPath = ""
user.ProfileImageUrl = ""
// Do the (fake) downloading
fake_downloader := NewFakeDownloader()
err := profile.DownloadUserContentWithInjector(&user, fake_downloader)
require.NoError(t, err)
// Check that the downloader was called with the appropriate stuff
assert.Len(*fake_downloader.Spy, 1)
assert.True(fake_downloader.Contains(SpyResult{scraper.DEFAULT_PROFILE_IMAGE_URL, "test_profiles/TestMediaQueries/profile_images/default_profile.png"}))
}

View File

@ -4,6 +4,7 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"path"
"offline_twitter/scraper" "offline_twitter/scraper"
) )
@ -253,3 +254,20 @@ func (p Profile) IsFollowing(user scraper.User) bool {
} }
return ret return ret
} }
/**
* Utility function to compute the path to save banner image to
*/
func (p Profile) get_banner_image_output_path(u scraper.User) string {
return path.Join(p.ProfileDir, "profile_images", u.BannerImageLocalPath)
}
/**
* Utility function to compute the path to save profile image to
*/
func (p Profile) get_profile_image_output_path(u scraper.User) string {
if u.ProfileImageUrl == "" {
return path.Join(p.ProfileDir, "profile_images", path.Base(scraper.DEFAULT_PROFILE_IMAGE_URL))
}
return path.Join(p.ProfileDir, "profile_images", u.ProfileImageLocalPath)
}