Get() methods from all repositories now return a ErrNotFound when the id is nonexistent
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"time"
|
||||
|
||||
"github.com/deluan/gosonic/domain"
|
||||
@@ -228,6 +227,9 @@ func (r *ledisRepository) readEntity(id string) (interface{}, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(res[0]) == 0 {
|
||||
return nil, domain.ErrNotFound
|
||||
}
|
||||
err = r.toEntity(res, entity)
|
||||
return entity, err
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"time"
|
||||
|
||||
"github.com/deluan/gosonic/domain"
|
||||
"github.com/deluan/gosonic/tests"
|
||||
"github.com/deluan/gosonic/utils"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
@@ -27,7 +27,8 @@ func shouldBeEqual(actualStruct interface{}, expectedStruct ...interface{}) stri
|
||||
return ShouldEqual(actual, expected)
|
||||
}
|
||||
|
||||
func createRepo() *ledisRepository {
|
||||
func createEmptyRepo() *ledisRepository {
|
||||
dropDb()
|
||||
repo := &ledisRepository{}
|
||||
repo.init("test", &TestEntity{})
|
||||
return repo
|
||||
@@ -37,7 +38,7 @@ func TestBaseRepository(t *testing.T) {
|
||||
tests.Init(t, false)
|
||||
|
||||
Convey("Subject: Annotations", t, func() {
|
||||
repo := createRepo()
|
||||
repo := createEmptyRepo()
|
||||
Convey("It should parse the parent table definition", func() {
|
||||
So(repo.parentTable, ShouldEqual, "parent")
|
||||
So(repo.parentIdField, ShouldEqual, "ParentId")
|
||||
@@ -51,7 +52,7 @@ func TestBaseRepository(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("Subject: calcScore", t, func() {
|
||||
repo := createRepo()
|
||||
repo := createEmptyRepo()
|
||||
|
||||
Convey("It should create an int score", func() {
|
||||
def := repo.indexes["ByCount"]
|
||||
@@ -86,7 +87,7 @@ func TestBaseRepository(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("Subject: NewId", t, func() {
|
||||
repo := createRepo()
|
||||
repo := createEmptyRepo()
|
||||
|
||||
Convey("When I call NewId with a name", func() {
|
||||
Id := repo.NewId("a name")
|
||||
@@ -120,7 +121,14 @@ func TestBaseRepository(t *testing.T) {
|
||||
Convey("Subject: saveOrUpdate/loadEntity/CountAll", t, func() {
|
||||
|
||||
Convey("Given an empty DB", func() {
|
||||
repo := createRepo()
|
||||
repo := createEmptyRepo()
|
||||
|
||||
Convey("When I try to retrieve an nonexistent ID", func() {
|
||||
_, err := repo.readEntity("NOT_FOUND")
|
||||
Convey("Then I should get a NotFound error", func() {
|
||||
So(err, ShouldEqual, domain.ErrNotFound)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("When I save a new entity and a parent", func() {
|
||||
entity := &TestEntity{Id: "123", Name: "My Name", ParentId: "ABC", Year: time.Now()}
|
||||
@@ -151,7 +159,7 @@ func TestBaseRepository(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("Given a table with one entity", func() {
|
||||
repo := createRepo()
|
||||
repo := createEmptyRepo()
|
||||
entity := &TestEntity{Id: "111", Name: "One Name", ParentId: "AAA"}
|
||||
repo.saveOrUpdate(entity.Id, entity)
|
||||
|
||||
@@ -186,7 +194,7 @@ func TestBaseRepository(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("Given a table with 3 entities", func() {
|
||||
repo := createRepo()
|
||||
repo := createEmptyRepo()
|
||||
for i := 1; i <= 3; i++ {
|
||||
e := &TestEntity{Id: strconv.Itoa(i), Name: fmt.Sprintf("Name %d", i), ParentId: "AAA"}
|
||||
repo.saveOrUpdate(e.Id, e)
|
||||
@@ -216,7 +224,7 @@ func TestBaseRepository(t *testing.T) {
|
||||
})
|
||||
Convey("And I get all saved ids", func() {
|
||||
So(len(ids), ShouldEqual, 3)
|
||||
for k, _ := range ids {
|
||||
for k := range ids {
|
||||
So(k, ShouldBeIn, []string{"1", "2", "3"})
|
||||
}
|
||||
})
|
||||
@@ -245,10 +253,5 @@ func TestBaseRepository(t *testing.T) {
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
Reset(func() {
|
||||
dropDb()
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@@ -45,7 +45,10 @@ func (m *MockAlbum) Get(id string) (*domain.Album, error) {
|
||||
if m.err {
|
||||
return nil, errors.New("Error!")
|
||||
}
|
||||
return m.data[id], nil
|
||||
if d, ok := m.data[id]; ok {
|
||||
return d, nil
|
||||
}
|
||||
return nil, domain.ErrNotFound
|
||||
}
|
||||
|
||||
func (m *MockAlbum) GetAll(qo domain.QueryOptions) (*domain.Albums, error) {
|
||||
|
||||
@@ -43,5 +43,8 @@ func (m *MockArtist) Get(id string) (*domain.Artist, error) {
|
||||
if m.err {
|
||||
return nil, errors.New("Error!")
|
||||
}
|
||||
return m.data[id], nil
|
||||
if d, ok := m.data[id]; ok {
|
||||
return d, nil
|
||||
}
|
||||
return nil, domain.ErrNotFound
|
||||
}
|
||||
|
||||
@@ -46,8 +46,10 @@ func (m *MockMediaFile) Get(id string) (*domain.MediaFile, error) {
|
||||
if m.err {
|
||||
return nil, errors.New("Error!")
|
||||
}
|
||||
mf := m.data[id]
|
||||
return mf, nil
|
||||
if d, ok := m.data[id]; ok {
|
||||
return d, nil
|
||||
}
|
||||
return nil, domain.ErrNotFound
|
||||
}
|
||||
|
||||
func (m *MockMediaFile) FindByAlbum(artistId string) (*domain.MediaFiles, error) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package persistence
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/deluan/gosonic/domain"
|
||||
"github.com/deluan/gosonic/engine"
|
||||
)
|
||||
|
||||
@@ -33,8 +34,8 @@ func (r *propertyRepository) Get(id string) (string, error) {
|
||||
func (r *propertyRepository) DefaultGet(id string, defaultValue string) (string, error) {
|
||||
v, err := r.Get(id)
|
||||
|
||||
if v == "" {
|
||||
v = defaultValue
|
||||
if err == domain.ErrNotFound {
|
||||
return defaultValue, nil
|
||||
}
|
||||
|
||||
return v, err
|
||||
|
||||
Reference in New Issue
Block a user