diff --git a/.build.yml b/.build.yml index 4fb3a6e..917f8e4 100644 --- a/.build.yml +++ b/.build.yml @@ -11,6 +11,7 @@ sources: packages: - wget - build-essential + - sqlite3 tasks: - install_go: | diff --git a/persistence/media_queries.go b/persistence/media_queries.go index 29a0d59..85e665b 100644 --- a/persistence/media_queries.go +++ b/persistence/media_queries.go @@ -18,7 +18,7 @@ import ( func (p Profile) SaveImage(img scraper.Image) (sql.Result, error) { if img.ID == 0 { // New image - return p.DB.Exec("insert into images (tweet_id, filename) values (?, ?)", img.TweetID, img.Filename) + return p.DB.Exec("insert into images (tweet_id, filename) values (?, ?) on conflict do nothing", img.TweetID, img.Filename) } else { // Updating an existing image return p.DB.Exec("update images set filename=?, is_downloaded=? where rowid=?", img.Filename, img.IsDownloaded, img.ID) @@ -37,7 +37,7 @@ func (p Profile) SaveImage(img scraper.Image) (sql.Result, error) { func (p Profile) SaveVideo(vid scraper.Video) (sql.Result, error) { if vid.ID == 0 { // New image - return p.DB.Exec("insert into videos (tweet_id, filename) values (?, ?)", vid.TweetID, vid.Filename) + return p.DB.Exec("insert into videos (tweet_id, filename) values (?, ?) on conflict do nothing", vid.TweetID, vid.Filename) } else { // Updating an existing image return p.DB.Exec("update videos set filename=?, is_downloaded=? where rowid=?", vid.Filename, vid.IsDownloaded, vid.ID) diff --git a/persistence/media_queries_test.go b/persistence/media_queries_test.go index 55592ab..fdf19f3 100644 --- a/persistence/media_queries_test.go +++ b/persistence/media_queries_test.go @@ -57,6 +57,51 @@ func TestSaveAndLoadImage(t *testing.T) { } } +/** + * Change an Image, save the changes, reload it, and check if it comes back the same + */ +func TestModifyImage(t *testing.T) { + profile_path := "test_profiles/TestMediaQueries" + profile := create_or_load_profile(profile_path) + + tweet := create_stable_tweet() + img := tweet.Images[0] + + if img.ID != 1 { + t.Fatalf("Got the wrong image back: wanted ID %d, got %d", 1, img.ID) + } + + img.Filename = "local/sdfjk.jpg" + img.IsDownloaded = true + + // Save the changes + result, err := profile.SaveImage(img) + if err != nil { + t.Error(err) + } + rows_affected, err := result.RowsAffected() + if err != nil { + t.Error(err) + } + if rows_affected != 1 { + t.Errorf("Expected 1 row changed, but got %d", rows_affected) + } + + // Reload it + imgs, err := profile.GetImagesForTweet(tweet) + if err != nil { + t.Fatalf("Could not load images: %s", err.Error()) + } + new_img := imgs[0] + if new_img.ID != img.ID { + t.Fatalf("Got the wrong image back: wanted ID %d, got %d", 1, new_img.ID) + } + + if diff := deep.Equal(img, new_img); diff != nil { + t.Error(diff) + } +} + /** * Create an Video, save it, reload it, and make sure it comes back the same @@ -102,3 +147,48 @@ func TestSaveAndLoadVideo(t *testing.T) { t.Error(diff) } } + +/** + * Change an Image, save the changes, reload it, and check if it comes back the same + */ +func TestModifyVideo(t *testing.T) { + profile_path := "test_profiles/TestMediaQueries" + profile := create_or_load_profile(profile_path) + + tweet := create_stable_tweet() + vid := tweet.Videos[0] + + if vid.ID != 1 { + t.Fatalf("Got the wrong video back: wanted ID %d, got %d", 1, vid.ID) + } + + vid.Filename = "local/sdfjk.jpg" + vid.IsDownloaded = true + + // Save the changes + result, err := profile.SaveVideo(vid) + if err != nil { + t.Error(err) + } + rows_affected, err := result.RowsAffected() + if err != nil { + t.Error(err) + } + if rows_affected != 1 { + t.Errorf("Expected 1 row changed, but got %d", rows_affected) + } + + // Reload it + vids, err := profile.GetVideosForTweet(tweet) + if err != nil { + t.Fatalf("Could not load videos: %s", err.Error()) + } + new_vid := vids[0] + if new_vid.ID != vid.ID { + t.Fatalf("Got the wrong video back: wanted ID %d, got %d", 1, new_vid.ID) + } + + if diff := deep.Equal(vid, new_vid); diff != nil { + t.Error(diff) + } +}