From 8775f5337d7a9648b7f8c27ba934cf53bf2b8ccb Mon Sep 17 00:00:00 2001 From: Alessio Date: Sat, 7 May 2022 18:44:03 -0700 Subject: [PATCH] Add test to ensure proper user images are being downloaded --- persistence/media_download.go | 8 ++-- persistence/media_download_test.go | 64 ++++++++++++++++++++++++++++-- persistence/user_queries.go | 18 +++++++++ 3 files changed, 81 insertions(+), 9 deletions(-) diff --git a/persistence/media_download.go b/persistence/media_download.go index c4cf405..ddd7406 100644 --- a/persistence/media_download.go +++ b/persistence/media_download.go @@ -153,14 +153,12 @@ func (p Profile) DownloadUserContentWithInjector(u *scraper.User, downloader Med return nil } - var outfile string - var target_url string + outfile := p.get_profile_image_output_path(*u) + var target_url string if u.ProfileImageUrl == "" { - outfile = path.Join(p.ProfileDir, "profile_images", path.Base(scraper.DEFAULT_PROFILE_IMAGE_URL)) target_url = scraper.DEFAULT_PROFILE_IMAGE_URL } else { - outfile = path.Join(p.ProfileDir, "profile_images", u.ProfileImageLocalPath) 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 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) if err != nil && strings.Contains(err.Error(), "404 Not Found") { diff --git a/persistence/media_download_test.go b/persistence/media_download_test.go index 2a9ba2f..868448e 100644 --- a/persistence/media_download_test.go +++ b/persistence/media_download_test.go @@ -9,9 +9,36 @@ import ( "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) { error_msg := map[bool]string{ @@ -53,7 +80,7 @@ func TestDownloadTweetContent(t *testing.T) { test_all_downloaded(tweet, false, t) // Do the (fake) downloading - err = profile.DownloadTweetContentWithInjector(&tweet, FakeDownloader{}) + err = profile.DownloadTweetContentWithInjector(&tweet, NewFakeDownloader()) require.NoError(t, err) // It should all be marked "yes downloaded" now @@ -83,9 +110,15 @@ func TestDownloadUserContent(t *testing.T) { assert.False(user.IsContentDownloaded) // Do the (fake) downloading - err = profile.DownloadUserContentWithInjector(&user, FakeDownloader{}) + 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, 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" assert.True(user.IsContentDownloaded) @@ -94,3 +127,26 @@ func TestDownloadUserContent(t *testing.T) { require.NoError(t, err) 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"})) +} diff --git a/persistence/user_queries.go b/persistence/user_queries.go index 16b3187..e6282f7 100644 --- a/persistence/user_queries.go +++ b/persistence/user_queries.go @@ -4,6 +4,7 @@ import ( "database/sql" "errors" "fmt" + "path" "offline_twitter/scraper" ) @@ -253,3 +254,20 @@ func (p Profile) IsFollowing(user scraper.User) bool { } 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) +}