From fb174df363cdea8a32dcb00afcf93911505284e1 Mon Sep 17 00:00:00 2001 From: Alessio Date: Fri, 7 Jan 2022 12:38:56 -0500 Subject: [PATCH] Add persistence for is_banned field --- persistence/schema.sql | 1 + persistence/user_queries.go | 40 +++++++++-------- persistence/user_queries_test.go | 75 ++++++++++++++++++++++++++++++++ persistence/versions.go | 3 +- 4 files changed, 99 insertions(+), 20 deletions(-) diff --git a/persistence/schema.sql b/persistence/schema.sql index 69cf178..3c6b5e9 100644 --- a/persistence/schema.sql +++ b/persistence/schema.sql @@ -12,6 +12,7 @@ create table users (rowid integer primary key, join_date integer, is_private boolean default 0, is_verified boolean default 0, + is_banned boolean default 0, profile_image_url text, profile_image_local_path text, banner_image_url text, diff --git a/persistence/user_queries.go b/persistence/user_queries.go index 0f1f30e..31be56a 100644 --- a/persistence/user_queries.go +++ b/persistence/user_queries.go @@ -17,25 +17,27 @@ func (p Profile) SaveUser(u scraper.User) error { db := p.DB _, err := db.Exec(` - insert into users (id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path, pinned_tweet_id, is_content_downloaded) - values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + insert into users (id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified, is_banned, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path, pinned_tweet_id, is_content_downloaded) + values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) on conflict do update set bio=?, - following_count=?, - followers_count=?, - location=?, - website=?, - is_private=?, - is_verified=?, - profile_image_url=?, - profile_image_local_path=?, - banner_image_url=?, - banner_image_local_path=?, - pinned_tweet_id=?, - is_content_downloaded=(is_content_downloaded or ?) + display_name=?, + following_count=?, + followers_count=?, + location=?, + website=?, + is_private=?, + is_verified=?, + is_banned=?, + profile_image_url=?, + profile_image_local_path=?, + banner_image_url=?, + banner_image_local_path=?, + pinned_tweet_id=?, + is_content_downloaded=(is_content_downloaded or ?) `, - u.ID, u.DisplayName, u.Handle, u.Bio, u.FollowingCount, u.FollowersCount, u.Location, u.Website, u.JoinDate.Unix(), u.IsPrivate, u.IsVerified, u.ProfileImageUrl, u.ProfileImageLocalPath, u.BannerImageUrl, u.BannerImageLocalPath, u.PinnedTweetID, u.IsContentDownloaded, - u.Bio, u.FollowingCount, u.FollowersCount, u.Location, u.Website, u.IsPrivate, u.IsVerified, u.ProfileImageUrl, u.ProfileImageLocalPath, u.BannerImageUrl, u.BannerImageLocalPath, u.PinnedTweetID, u.IsContentDownloaded, + u.ID, u.DisplayName, u.Handle, u.Bio, u.FollowingCount, u.FollowersCount, u.Location, u.Website, u.JoinDate.Unix(), u.IsPrivate, u.IsVerified, u.IsBanned, u.ProfileImageUrl, u.ProfileImageLocalPath, u.BannerImageUrl, u.BannerImageLocalPath, u.PinnedTweetID, u.IsContentDownloaded, + u.Bio, u.DisplayName, u.FollowingCount, u.FollowersCount, u.Location, u.Website, u.IsPrivate, u.IsVerified, u.IsBanned, u.ProfileImageUrl, u.ProfileImageLocalPath, u.BannerImageUrl, u.BannerImageLocalPath, u.PinnedTweetID, u.IsContentDownloaded, ) if err != nil { return err @@ -76,7 +78,7 @@ func parse_user_from_row(row *sql.Row) (scraper.User, error) { var u scraper.User var joinDate int64 - err := row.Scan(&u.ID, &u.DisplayName, &u.Handle, &u.Bio, &u.FollowingCount, &u.FollowersCount, &u.Location, &u.Website, &joinDate, &u.IsPrivate, &u.IsVerified, &u.ProfileImageUrl, &u.ProfileImageLocalPath, &u.BannerImageUrl, &u.BannerImageLocalPath, &u.PinnedTweetID, &u.IsContentDownloaded) + err := row.Scan(&u.ID, &u.DisplayName, &u.Handle, &u.Bio, &u.FollowingCount, &u.FollowersCount, &u.Location, &u.Website, &joinDate, &u.IsPrivate, &u.IsVerified, &u.IsBanned, &u.ProfileImageUrl, &u.ProfileImageLocalPath, &u.BannerImageUrl, &u.BannerImageLocalPath, &u.PinnedTweetID, &u.IsContentDownloaded) if err != nil { return u, err } @@ -99,7 +101,7 @@ func (p Profile) GetUserByHandle(handle scraper.UserHandle) (scraper.User, error db := p.DB stmt, err := db.Prepare(` - select id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path, pinned_tweet_id, is_content_downloaded + select id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified, is_banned, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path, pinned_tweet_id, is_content_downloaded from users where lower(handle) = lower(?) `) @@ -130,7 +132,7 @@ func (p Profile) GetUserByID(id scraper.UserID) (scraper.User, error) { db := p.DB stmt, err := db.Prepare(` - select id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path, pinned_tweet_id, is_content_downloaded + select id, display_name, handle, bio, following_count, followers_count, location, website, join_date, is_private, is_verified, is_banned, profile_image_url, profile_image_local_path, banner_image_url, banner_image_local_path, pinned_tweet_id, is_content_downloaded from users where id = ? `) diff --git a/persistence/user_queries_test.go b/persistence/user_queries_test.go index ce8a387..411016b 100644 --- a/persistence/user_queries_test.go +++ b/persistence/user_queries_test.go @@ -2,6 +2,7 @@ package persistence_test import ( "testing" + "time" "github.com/go-test/deep" ) @@ -41,6 +42,80 @@ func TestSaveAndLoadUser(t *testing.T) { } } +/** + * + */ +func TestModifyUser(t *testing.T) { + profile_path := "test_profiles/TestUserQueries" + profile := create_or_load_profile(profile_path) + + fake_user := create_dummy_user() + fake_user.DisplayName = "Display Name 1" + fake_user.Location = "location1" + fake_user.IsPrivate = false + fake_user.IsVerified = false + fake_user.IsBanned = false + fake_user.FollowersCount = 1000 + fake_user.JoinDate = time.Unix(1000, 0) + fake_user.ProfileImageUrl = "asdf" + fake_user.IsContentDownloaded = true + + // Save the user so it can be modified + err := profile.SaveUser(fake_user) + if err != nil { + panic(err) + } + + + fake_user.DisplayName = "Display Name 2" + fake_user.Location = "location2" + fake_user.IsPrivate = true + fake_user.IsVerified = true + fake_user.IsBanned = true + fake_user.FollowersCount = 2000 + fake_user.JoinDate = time.Unix(2000, 0) + fake_user.ProfileImageUrl = "asdf2" + fake_user.IsContentDownloaded = false // test No Worsening + + // Save the modified user + err = profile.SaveUser(fake_user) + if err != nil { + panic(err) + } + // Reload the modified user + new_fake_user, err := profile.GetUserByID(fake_user.ID) + if err != nil { + panic(err) + } + + if new_fake_user.DisplayName != "Display Name 2" { + t.Errorf("Expected display name %q, got %q", "Display Name 2", new_fake_user.DisplayName) + } + if new_fake_user.Location != "location2" { + t.Errorf("Expected location %q, got %q", "location2", new_fake_user.Location) + } + if new_fake_user.IsPrivate != true { + t.Errorf("Should be private") + } + if new_fake_user.IsVerified != true { + t.Errorf("Should be verified") + } + if new_fake_user.IsBanned != true { + t.Errorf("Should be banned") + } + if new_fake_user.FollowersCount != 2000 { + t.Errorf("Expected %d followers, got %d", 2000, new_fake_user.FollowersCount) + } + if new_fake_user.JoinDate.Unix() != 1000 { + t.Errorf("Expected unchanged join date (%d), got %d", 1000, new_fake_user.JoinDate.Unix()) + } + if new_fake_user.ProfileImageUrl != "asdf2" { + t.Errorf("Expected profile image url to be %q, got %q", "asdf2", new_fake_user.ProfileImageUrl) + } + if new_fake_user.IsContentDownloaded != true { + t.Errorf("Expected content to be downloaded (no-worsening)") + } +} func TestHandleIsCaseInsensitive(t *testing.T) { profile_path := "test_profiles/TestUserQueries" diff --git a/persistence/versions.go b/persistence/versions.go index 5fa7e7a..808130f 100644 --- a/persistence/versions.go +++ b/persistence/versions.go @@ -8,7 +8,7 @@ import ( ) -const ENGINE_DATABASE_VERSION = 5 +const ENGINE_DATABASE_VERSION = 6 type VersionMismatchError struct { @@ -58,6 +58,7 @@ var MIGRATIONS = []string{ alter table videos add column thumbnail_local_filename text not null default "missing"`, `alter table videos add column duration integer not null default 0; alter table videos add column view_count integer not null default 0`, +`alter table users add column is_banned boolean default 0`, } /**