Refactored purgeInactive, better test suite setup
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user