From 9ccd9545e8a6c379f613157d159e407456c094ef Mon Sep 17 00:00:00 2001 From: Deluan Date: Fri, 10 Jan 2020 19:20:29 -0500 Subject: [PATCH] Refactored purgeInactive, better test suite setup --- persistence/storm/album_repository.go | 9 ++----- persistence/storm/album_repository_test.go | 12 +-------- persistence/storm/artist_repository.go | 9 ++----- persistence/storm/artist_repository_test.go | 15 +---------- persistence/storm/mediafile_repository.go | 9 ++----- persistence/storm/storm_repository.go | 12 +++++++-- persistence/storm/storm_suite_test.go | 29 +++++++++++++++++++++ 7 files changed, 47 insertions(+), 48 deletions(-) diff --git a/persistence/storm/album_repository.go b/persistence/storm/album_repository.go index 4465577c..255d85da 100644 --- a/persistence/storm/album_repository.go +++ b/persistence/storm/album_repository.go @@ -93,13 +93,8 @@ func (r *albumRepository) GetAllIds() ([]string, error) { return result, nil } -func (r *albumRepository) PurgeInactive(active domain.Albums) ([]string, error) { - activeIDs := make([]string, len(active)) - for i, album := range active { - activeIDs[i] = album.ID - } - - return r.purgeInactive(activeIDs) +func (r *albumRepository) PurgeInactive(activeList domain.Albums) ([]string, error) { + return r.purgeInactive(activeList) } func (r *albumRepository) GetStarred(options domain.QueryOptions) (domain.Albums, error) { diff --git a/persistence/storm/album_repository_test.go b/persistence/storm/album_repository_test.go index f4c1937d..10cd6087 100644 --- a/persistence/storm/album_repository_test.go +++ b/persistence/storm/album_repository_test.go @@ -8,24 +8,14 @@ import ( var _ = Describe("AlbumRepository", func() { var repo domain.AlbumRepository - var data domain.Albums BeforeEach(func() { - Db().Drop(&_Album{}) repo = NewAlbumRepository() - data = domain.Albums{ - {ID: "1", Name: "Sgt Peppers", Artist: "The Beatles", ArtistID: "1"}, - {ID: "2", Name: "Abbey Road", Artist: "The Beatles", ArtistID: "1"}, - {ID: "3", Name: "Radioactivity", Artist: "Kraftwerk", ArtistID: "2", Starred: true}, - } - for _, a := range data { - repo.Put(&a) - } }) Describe("GetAll", func() { It("returns all records", func() { - Expect(repo.GetAll(domain.QueryOptions{})).To(Equal(data)) + Expect(repo.GetAll(domain.QueryOptions{})).To(Equal(testAlbums)) }) It("returns all records sorted", func() { diff --git a/persistence/storm/artist_repository.go b/persistence/storm/artist_repository.go index 9dfb8628..fbed2ad8 100644 --- a/persistence/storm/artist_repository.go +++ b/persistence/storm/artist_repository.go @@ -36,13 +36,8 @@ func (r *artistRepository) Get(id string) (*domain.Artist, error) { return &a, nil } -func (r *artistRepository) PurgeInactive(active domain.Artists) ([]string, error) { - activeIDs := make([]string, len(active)) - for i, artist := range active { - activeIDs[i] = artist.ID - } - - return r.purgeInactive(activeIDs) +func (r *artistRepository) PurgeInactive(activeList domain.Artists) ([]string, error) { + return r.purgeInactive(activeList) } var _ domain.ArtistRepository = (*artistRepository)(nil) diff --git a/persistence/storm/artist_repository_test.go b/persistence/storm/artist_repository_test.go index 8f9738af..cd1f1383 100644 --- a/persistence/storm/artist_repository_test.go +++ b/persistence/storm/artist_repository_test.go @@ -10,19 +10,11 @@ var _ = Describe("ArtistRepository", func() { var repo domain.ArtistRepository BeforeEach(func() { - Db().Drop(&_Artist{}) repo = NewArtistRepository() }) It("saves and retrieves data", func() { - artist := &domain.Artist{ - ID: "1", - Name: "Saara Saara", - AlbumCount: 2, - } - Expect(repo.Put(artist)).To(BeNil()) - - Expect(repo.Get("1")).To(Equal(artist)) + Expect(repo.Get("1")).To(Equal(&domain.Artist{ID: "1", Name: "Saara Saara"})) }) It("returns ErrNotFound when the ID does not exist", func() { @@ -34,11 +26,6 @@ var _ = Describe("ArtistRepository", func() { var data domain.Artists BeforeEach(func() { - data = domain.Artists{ - {ID: "1", Name: "Saara Saara"}, - {ID: "2", Name: "Kraftwerk"}, - {ID: "3", Name: "The Beatles"}, - } for _, a := range data { repo.Put(&a) } diff --git a/persistence/storm/mediafile_repository.go b/persistence/storm/mediafile_repository.go index caf9b8da..a7394310 100644 --- a/persistence/storm/mediafile_repository.go +++ b/persistence/storm/mediafile_repository.go @@ -99,13 +99,8 @@ func (r *mediaFileRepository) GetAllIds() ([]string, error) { return result, nil } -func (r *mediaFileRepository) PurgeInactive(active domain.MediaFiles) ([]string, error) { - activeIDs := make([]string, len(active)) - for i, mediaFile := range active { - activeIDs[i] = mediaFile.ID - } - - return r.purgeInactive(activeIDs) +func (r *mediaFileRepository) PurgeInactive(activeList domain.MediaFiles) ([]string, error) { + return r.purgeInactive(activeList) } var _ domain.MediaFileRepository = (*mediaFileRepository)(nil) diff --git a/persistence/storm/storm_repository.go b/persistence/storm/storm_repository.go index e9e1e895..accb54a8 100644 --- a/persistence/storm/storm_repository.go +++ b/persistence/storm/storm_repository.go @@ -50,8 +50,16 @@ func (r *stormRepository) getByID(id string, ta interface{}) error { return nil } -func (r *stormRepository) purgeInactive(ids []string) (deleted []string, err error) { - query := Db().Select(q.Not(q.In("ID", ids))) +func (r *stormRepository) purgeInactive(activeList interface{}) (deleted []string, err error) { + reflected := reflect.ValueOf(activeList) + totalActive := reflected.Len() + activeIDs := make([]string, totalActive) + for i := 0; i < totalActive; i++ { + item := reflected.Index(i) + activeIDs[i] = item.FieldByName("ID").String() + } + + query := Db().Select(q.Not(q.In("ID", activeIDs))) // Collect IDs that will be deleted err = query.Each(r.bucket, func(record interface{}) error { diff --git a/persistence/storm/storm_suite_test.go b/persistence/storm/storm_suite_test.go index 61c1f1e4..2ba0183c 100644 --- a/persistence/storm/storm_suite_test.go +++ b/persistence/storm/storm_suite_test.go @@ -3,6 +3,7 @@ package storm import ( "testing" + "github.com/cloudsonic/sonic-server/domain" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) @@ -12,3 +13,31 @@ func TestStormPersistence(t *testing.T) { RegisterFailHandler(Fail) RunSpecs(t, "Storm Persistence Suite") } + +var testAlbums = domain.Albums{ + {ID: "1", Name: "Sgt Peppers", Artist: "The Beatles", ArtistID: "1"}, + {ID: "2", Name: "Abbey Road", Artist: "The Beatles", ArtistID: "1"}, + {ID: "3", Name: "Radioactivity", Artist: "Kraftwerk", ArtistID: "2", Starred: true}, +} +var testArtists = domain.Artists{ + {ID: "1", Name: "Saara Saara"}, + {ID: "2", Name: "Kraftwerk"}, + {ID: "3", Name: "The Beatles"}, +} + +var _ = Describe("Initialize test DB", func() { + BeforeSuite(func() { + Db().Drop(&_Album{}) + albumRepo := NewAlbumRepository() + for _, a := range testAlbums { + albumRepo.Put(&a) + } + + Db().Drop(&_Artist{}) + artistRepo := NewArtistRepository() + for _, a := range testArtists { + artistRepo.Put(&a) + } + }) + +})